Spread the love

in this article, we discuss Data types in Python

Data types in Python

In Python programming language, everything is an object. Thus, data types are treated as classes and variables are an instance or object of these classes. There are various data types in Python to represent the types of value.

  • Python Data Types
  • Standard Data Types in Python
    • Numbers
    • List
    • String
    • Tuple
    • Dictionary

Python Data Types

Variables are used to hold values for different data types. As Python is a dynamically typed language, you don’t need to define the type of the variable while declaring it. The interpreter implicitly binds the value with its type. Python enables us to check the type of the variable used in the program. With the help of the type() function, you can find out the type of the variable passed.

Example:

x = 24
y = 14.7
z = "Welcome to Edureka"
print(type(x));
print(type(y));
print(type(z));

Output:

<type 'int'>
<type 'float'> 
<type 'str'>

Standard Data Types in Python

A variable is used to hold different types of values. For example, a person’s name must be stored as a string whereas an employee ID must be stored as an integer.

Python provides various standard data types that define the storage method on each of them. The standard data types in Python include:

  • Numbers
  • List
  • String
  • Tuple
  • Dictionary

Now that you know about the standard python data types, let’s move on and understand each of these in detail.

Numbers

The number is used to store numeric values. Python creates Number objects when a number is assigned to a variable. There are 4 types of numeric data:

  • int – It is used for signed integers like 12, 2, 7, etc.
  • long – This integer is used for a higher range of values like 908090800L, -0x1929292L, etc.
  • float – It is used to store floating-point numbers like 1.5, 701.89, 15.2, etc.
  • complex – This is used for complex numbers like 2.14j, 2.0 + 2.3j, etc.

In Python, you can use a lower-case L with long integers. However, it is more convenient to use an upper-case L.

Example:

a = 12
print(a, "is of type", type(a))
 
b = 5.05
print(b, "is of type", type(b))
 
c = 1+2j
print(c, "is complex number?", isinstance(1+2j,complex))

Output:

12 is of type <class 'int'>
5.05 is of type <class 'float'>
(1+2j) is complex number? True

String

A string is defined as a sequence of characters represented in the quotation marks. In python, you can use single, double, or triple quotes to define a string.

String handling in python can be done using various inbuilt functions and operators. In the case of string handling, the operator + is used to concatenate two strings.

Example

str1 = 'Welcome to Edureka' #string str1
str2 = 'Python Programming' #string str2
print (str1[0:3])
print (str1[4])
print (str1 + str2)

Output:

Wel
c
Welcome to Edureka Python Programming

List

Lists are similar to arrays in C but it can contain different types of data in Python. The items stored in the list are separated with a comma (,) and enclosed within square brackets [].

You can use slice [:] operators to access the data of the list. The concatenation operator (+) is similar to the one in strings.

Example

list = [20, "welcome", "edureka", 40]
print (list[3:]);
print (list);
print (list + list);

Output:

[40]
[20, 'welcome']
[20, "welcome", "edureka", 40]
[20, "welcome", "edureka", 40, 20, "welcome", "edureka", 40]

Tuple

A tuple is similar to lists in many ways. Like lists, tuples also contain the collection of the items of different data types. The items of the tuple are separated with a comma (,) and enclosed in parentheses ().

A tuple is a read-only data structure and you cannot modify the size and value of the items of a tuple.

Example:

tuple = ("welcome", "stackoverflow", 40)
print (tuple[1:]);
print (tuple);
print (tuple + tuple);

Output:

('edureka', 40)
('welcome', 'stackoverflow', 40)
('welcome', 'stackoverflow', 40, 'welcome', 'stackoverflow', 40)

Dictionary

Dictionary is an ordered set of a key-value pair of items. It is like an associative array or a hash table where each key stores a specific value. Key can hold any primitive data type whereas value is an arbitrary Python object.

The items in the dictionary are separated with the comma and enclosed in the curly braces {}.

Example:

dict = {1:'John', 2:'Rachel', 3:'Nancy', 4:'Daniel'};
print("1st name is "+dict[1]);
print (dict.keys());
print (dict.values());

Output:

1st name is John
[1, 2, 3, 4]
['John', 'Rachel', 'Nancy', 'Daniel']

Also Check out What is the use of self in Python

If you interested in the Entertainment genre please check this also thebiographys.com

Related Posts

One thought on “Data types in Python

  1. It was such a great article. i really acquire a lot of new knowledge about python through this article. I would remain greatful to you if you keep on updating new block about python in the future.
    thank you

Leave a Reply

Your email address will not be published. Required fields are marked *