data types in python

What are the data types in Python?

A variable is a place holder for user data with a set of possible operations. A variable is associated with a data type. The type defined the possible values a variable can have and possible operations. In python, every variable is an Object. So each data type is a class.

For example when we say that variable x is of type int. Means x can hold integer values and it is the object of int.

In Python, there are various data types. In this tutorial, we will discuss all inbuild data types in python.

How to know data type of a variable? This is very easy there is an inbuild function , type(variable).

x = 688;
y = "I am a developer";

print("Type of x = ", type(x), "type of y = ", type(y));

Numeric(int, float and complex) data types :

As the name suggests the numeric data types can store the integer values. Unlike other programming languages C or C++, where the size is fixed for a data type. Each of the data types in Python is a class. Theoretically, it can store any possible long value.
int- It can store an integer signed or unsigned value. The example is -20, -30, 89 etc.

float – It can store a floating-point (with decimal) numbers. The maximum accuracy is 15 digits. Eg. 3.5, -6.8888, etc.

complex – The complex number has imaginary and real parts.


String (str) type :

A variable of type can hold a string. The variable of type str is an object, that can have methods for string operations such as strip(), lower(), upper(), etc.

var = "  Hello String   ";
print("String after strip =",var.strip());
print(var.lower(), var.upper());
Output -
String after strip = Hello String
hello string HELLO STRING

Collections (List, Tuple, Set, Dictionary) types:

List – An ordered sequence of heterogeneous elements. The value of items can be changed. You can access elements of the list by index. The index of the first element is zero (0). The list is created with [ ].

a = [100, 10.5, 'list example']
print("List = ",a);
print("First Element = ",a[0]);
a[1] = 30.6;
print("List after alteration = ",a);

Output-
List = [100, 10.5, 'list example']
First Element = 100
List after alteration = [100, 30.6, 'list example']

Tuple – These are similar to list. But a tuple can not modified once initialized. A tuple is created with ( ).

a = (100, 10.5, 'tuple example');
print("Tuple = ",a);
print("First Element = ",a[0]);
a[1] = 30.6;
print("Tuple after alteration = ",a);
Output -
a = (100, 10.5, 'tuple example');
print("Tuple = ",a);
print("First Element = ",a[0]);
a[1] = 30.6;
print("Tuple after alteration = ",a);

Set – Unordered collections of different types of items. A set can not have duplicate values. Can create a set with { } . Being unordered, so can not access elements by index.

a = {100, 10.5, 'tuple example',100};
print("Set = ",a);
print("First Element = ",a[0]);
Output- 
a = {100, 10.5, 'tuple example',100};
print("Set = ",a);
print("First Element = ",a[0]);

Dictionary – It is an unordered collection of elements. Each element is a key-value pair. It is similar to a hashmap or hashtable. You can declare a dictionary using { }.

a = {100:'century','age':34};
print("Dictionary = ",a);
print("Element with key 100 = ",a[100]);
print("Element with key age = ",a["age"]);
Output-
Dictionary = {100: 'century', 'age': 34}
Element with key 100 = century
Element with key age = 34

Other Data Types –

bool – Variable of bool data type can have either a True or False values. Please note that the first letter should be Upper.