What are the data types in Python?
A variable is a placeholder for user data/information with a set of allowed operations. The operation could be mathematical, logical, etc. Each variable in a program is associated with a data type.
A type defines the possible values a variable can hold 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. This means x can hold integer values and is the int class’s object.
In Python, there are various data types. In this tutorial, we will discuss all inbuild data types in Python.
How to know the data type of a variable? This is very easy. There is an inbuilt 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, numeric data types can store numbers, 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 floating-point (with decimal) numbers. The maximum accuracy is 15 digits. E.g., 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 the list. But a tuple can not be 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 True or False values. Please note that the first letter should be Upper.