In Python, values of different types are stored inside variables such as numeric value is stored inside a variable, alphabetic value is stored inside another variable, it is called 'Data Types'.
There are three types of Number Data Types.
Integer Number Data Type
Floating-point Number Data Type
Complex Number Data Type
Important:-The type () function is used to check the data type.
Source Code:
a = 5
b = 1234456987
c = 12345678998745632117211
print (a)
print (b)
print (c)
print (type (a))
print (type (b))
print (type (c))
Output:
5
123456987
12345678998745632117211
<class 'int'>
<class 'int'>
<class 'int'>
Source Code:
a=0.5
b=12545885.568444444444444445
c=412154998644651351388678541351865465.4547878541516546845
Output:
5.0
12545885.568444444
4.121549864465135e + 35
<class 'float'>
<class 'float'>
<class 'float'>
Complex data type 'a + bj' is in this form, in which 'a' is the real part and 'b' is the imaginary part.
Source Code:
a = 1 + 4j
b = 5 + 4758499j
print (a)
print (b)
print (type (a))
print (type (b))
Output:
(1 + 4j)
(5 + 4758499j)
<class 'complex'>
<class 'complex'>
Source Code:
str1 = "Hello World"
str2 = "Hello Friends"
print (str1)
print (str2)
Output:
Hello World
Hello Friends
In most programming languages, the string is printed in the index, similarly in Python string is also printed with indexes. The first character of the string starts with index '0' and the last index of the string is '-1'.
Source Code:
str = "Hello World"
print ("First letter in string:", str [0])
print ("Last letter in string:", str [-1])
print ("Second last letter in string:", str [-2])
Output:
The first letter in a string: H
Last letter in a string: d
Second last letter in a string: l
Click here to know more about string.
List This is a compound data type in which items of any data types can be accessed. list This is a mutable data type. The values of items of these data types can be changed.
Source Code:
list = [1, "Hello", 5.6, (1, "Hii")]
for i in list:
print (i)
Output:
1
Hello
5.6
(1, 'Hii')
Click here to know more about Python
Tuple is a compound data type in which items of any data types can be accessed. list This is an immutable data type. The values of items of these data types cannot be changed.
Source Code:
tuple = (1, "Hello", 5.6, (1, "Hii"))
for i in tuple:
print (i)
tuple [0] = 3 #trying to changing 0th index
print (tuple [0])
Output:
1
Hello
5.6
(1, 'Hii')
Traceback (most recent call last):
tuple [0] = 3 #trying to changing 0th index
TypeError: 'tuple' object does not support item assignment
Click here
Source Code:
dict = {1: "H", 5: "e", 7: "l", 8: "l", 9: "o"}
print (dict [5])
print (dict [9])
Output:
e
o
Click here to know about Python
Set data type in python
Set Data Type This is an unordered collection of items. Every item given in the set is new. If a duplicate item is found, it is removed. Items of the set data type are written under curly braces ({}}).
Source Code:
set1 = {"Ramesh", "Suresh", "Kamlesh"}
for i in set1:
print (i)
set2 = {3, 5, 8, 7, 1, 3}
for j in set2:
print (j)
Output:
Suresh
Kamlesh
Ramesh
1
3
5
7
8
Types of Data in Python
There are six types of data types in Python.Data type |
- Number
- String
- List
- Tuple
- Dictionary
- Set
1. Number Data Types in Python
There are three types of Number Data Types.
Integer Number Data Type
Floating-point Number Data Type
Complex Number Data Type
Important:-The type () function is used to check the data type.
1 Integer Number Data Type
The integer data type is the normal numeric values. integer numbers do not have an undefined part.Source Code:
a = 5
b = 1234456987
c = 12345678998745632117211
print (a)
print (b)
print (c)
print (type (a))
print (type (b))
print (type (c))
Integer number data type |
Output:
5
123456987
12345678998745632117211
<class 'int'>
<class 'int'>
<class 'int'>
2 Floating-point Number Data Type
Floating-point numbers are the undefined part. There is no limit to the floating-point number in Python.Source Code:
a=0.5
b=12545885.568444444444444445
c=412154998644651351388678541351865465.4547878541516546845
float number data type |
Output:
5.0
12545885.568444444
4.121549864465135e + 35
<class 'float'>
<class 'float'>
<class 'float'>
3 Complex Number Data Type
Complex data type 'a + bj' is in this form, in which 'a' is the real part and 'b' is the imaginary part.
Source Code:
a = 1 + 4j
b = 5 + 4758499j
print (a)
print (b)
print (type (a))
print (type (b))
complex number data type |
Output:
(1 + 4j)
(5 + 4758499j)
<class 'complex'>
<class 'complex'>
String data type in python
The string is a set of characters. characters; There can be letters, numbers or special symbols. In Python, single ('') or double ("") are written inside quotes. String This is the immutable data type.Source Code:
str1 = "Hello World"
str2 = "Hello Friends"
print (str1)
print (str2)
String data type |
Output:
Hello World
Hello Friends
In most programming languages, the string is printed in the index, similarly in Python string is also printed with indexes. The first character of the string starts with index '0' and the last index of the string is '-1'.
Source Code:
str = "Hello World"
print ("First letter in string:", str [0])
print ("Last letter in string:", str [-1])
print ("Second last letter in string:", str [-2])
String data type |
Output:
The first letter in a string: H
Last letter in a string: d
Second last letter in a string: l
Click here to know more about string.
List Data Type in Python
Python's list data type has more than one item. Each item is separated by a comma (,). All the items in the list are closed under square bracket ([]).List This is a compound data type in which items of any data types can be accessed. list This is a mutable data type. The values of items of these data types can be changed.
Source Code:
list = [1, "Hello", 5.6, (1, "Hii")]
for i in list:
print (i)
List data type |
Output:
1
Hello
5.6
(1, 'Hii')
Click here to know more about Python
Tuple Data Type in Python
Python's list data type has more than one item. This is similar to the list data type. Each item is separated by comma (,). All items of Tuple are enclosed within parenthesis (()).Tuple is a compound data type in which items of any data types can be accessed. list This is an immutable data type. The values of items of these data types cannot be changed.
Source Code:
tuple = (1, "Hello", 5.6, (1, "Hii"))
for i in tuple:
print (i)
tuple [0] = 3 #trying to changing 0th index
print (tuple [0])
Tuple data type |
Output:
1
Hello
5.6
(1, 'Hii')
Traceback (most recent call last):
tuple [0] = 3 #trying to changing 0th index
TypeError: 'tuple' object does not support item assignment
Click here
Dictionary Data Type in Python
Dictionary Data Type has pairs of keys and values. The pair of each key value is separated by comma (,) and the key and value by a colon (:). All the keys and values of the dictionary are written in curly braces ({}). This is an immutable data type.Source Code:
dict = {1: "H", 5: "e", 7: "l", 8: "l", 9: "o"}
print (dict [5])
print (dict [9])
Dictionary data type |
Output:
e
o
Click here to know about Python
Set data type in python
Set Data Type This is an unordered collection of items. Every item given in the set is new. If a duplicate item is found, it is removed. Items of the set data type are written under curly braces ({}}).
Source Code:
set1 = {"Ramesh", "Suresh", "Kamlesh"}
for i in set1:
print (i)
set2 = {3, 5, 8, 7, 1, 3}
for j in set2:
print (j)
Set data type |
Output:
Suresh
Kamlesh
Ramesh
1
3
5
7
8
0 Comments