Updated March 17, 2023
Introduction to Python Data Types
Data types are nothing but the different types of input data accepted by a programming language for defining, declaring, storing and performing mathematical & logical values/ operations. For example, in Python, many data types are used to deal with the general operations on the program developer’s input data. A few of the commonly used data types are numbers for numeric values, String for single or series of characters, Tuple for a combination of different data types, List for a collection of values, etc.
Example of Python Data Types
Let’s see example to store different data types of value into the variables and check their type.
Code
var1 = 20
var2 = 20.65
var3 = "Hello!, World "
print( type(var1) );
print( type(var2) );
print( type(var3) );
Output:
Top 6 Python Data Types
The standard data types of python are given below:
- Numbers: The Number data type is used to stores numeric values.
- String: The string data type is used to stores the sequence of characters.
- Tuple: Tuple data type is used to stores a collection of different data types of elements, and it is immutable.
- List: List data type is used to store the collection of different data types of elements, and it is mutable.
- Set: Set data type is used to store different data types of elements; it is mutable and stores unique elements.
- Dictionary: Dictionary data type is used to store a collection of different data types of elements in the form of key-value pairs; it is mutable and stores the unique key.
1. Numbers
When a number is assigned to a variable Number class object is created.
Consider an example: var a = 100, var b = 200 # var a and var b number are assigned and these are objects of number.
The Number can have 4 types of numeric data:
- int: int stores integers eg a=100, b=25, c=526, etc.
- long: long stores higher range of integers eg a=908090999L, b=-0x1990999L, etc.
- float: float stores floating-point numbers eg a=25.6, b=45.90, c=1.290, etc.
- complex: complex stores numbers eg a=3 + 4j, b=2 + 3j, c=complex(4,6), etc.
2. String
The string can be defined as the sequence of characters represented in the quotation marks. In python, the string can be quoted by single, double, or triple quotes. In python, various inbuilt operators and functions are available to work with the string data type easily.
The following example shows the string handling with inbuilt operators and functions:
Code:
s = 'hello! how are you' # s is string variable
print (s[1]) # index operator - printing second character, character start storing from index 0
print (s[2:6]) # slice operator - printing 3rd character to 5th character of the string, the syntax of slice operator str[ start: end-1: increment] print (s*3) # printing the string three times
print (s[2:12:2])
s1 = 'hello world'
print (s + s1) # printing the concatenation of s and s1
Output:
3. Tuple
Tuples also store the collection of the elements of different data types. A tuple is the same as the list, but a tuple is immutable (non-editable or cannot modify the size and elements value). To create a tuple uses the () simple parenthesis; within this brackets, store all the elements separated with the comma (,).
The following example shows the tuple handling:
Code:
tp = ("apple", "a", 100, 20.78)
print (tp[1])
print (tp[1:])
print (tp[:3])
print (tp)
print (tp + tp)
print (tp * 3)
print (type(tp))
tp[1] = "banana"
print (tp)
Output:
4. List
List stores a collection of different types of elements. The list is mutable (editable). It is the same as arrays in C, but the list stores elements of different data types. To create a list uses the [] square brackets; within these brackets, stores all the elements separated with the comma (,). We can use index[i], slice [:] operators, concatenation operator (+), repetition operator (*) etc., to works with the list the same as with the strings.
The following example shows the list handling:
Code:
ls = ["apple", "a", 100, 20.78]
print (ls[1])
print (ls[1:])
print (ls[:3])
print (ls)
print (ls + ls)
print (ls * 3)
print (type(ls))
ls[1] = "banana"
print (ls)
Output:
5. Set
Set also stores the collection of the elements of different data types. A Set is the same as the list and tuple, but the set is immutable (non-editable or cannot modify the size and elements value), un order and stores only the unique elements. To create a set uses the {} curly brackets, within this brackets stores all the elements separated with the comma (,).
The following example shows the set handling:
Code:
st = {"apple", "banana", 100, 20.78}
# set cannot support indexing st[1]
# set cannot support slicing st[1:]
print (st)
print (st + st)# set cannot support concatenation
print (st * 2) # set cannot support repetition
print (type(st))
# set is immutable st[2] = "hi"
Output:
6. Dictionary
Dictionary is also stored in a collection of different data types elements in the form of key-value pairs. It is ordered, mutable and stores unique keys as a set. To create a set uses the {} curly brackets same as a set, within this, brackets stores all the elements (key-value pair) separated with the comma (,).
The following example shows the set handling:
Code:
dc = {"fruits":["apple", "banana"],'qty':100}
print("Fruits: ",dc['fruits'])
print("Quantity: ", dc['qty'])
print ("Dictionary: ",dc)# print all elements of the dictionary
print ("Keys: ",dc.keys()) # print all the keys of the dictionary
print ("values: ",dc.values()) # print all the values of the dictionary
print ("key value pairs: ",dc.items()) # print all the key values pair elements of the dictionary
Output:
Recommended Articles
This is a guide to Python Data Types. Here we discuss the top 6 data types of python in detail and code implementation and output. You can also go through our other suggested articles to learn more –