Author: SUNNY BHAVEEN CHANDRA
For more information -
[1] Python notes- https://c17hawke.github.io/Python/
[2] Python YouTube Playlist- https://youtube.com/playlist?list=PLrdaCCBhU_hnxIzB7EJlY-pfYOMGRycAK
Data-types 📊¶
- In programming, the concept of data type is crucial. A data type defines the kind of value a variable can hold and the operations that can be performed on it.
- Python, like many other programming languages, has several built-in data types that are used to handle common data such as
- numbers,
- text, and
- collections of items.
- These data types are the building blocks of any program and understanding them is essential for writing efficient and effective code.
Python has several built-in data types, which fall into the following categories:
Category | Common Data Types | Description |
---|---|---|
Text | str |
text |
Numeric | int , float , complex |
numbers |
Sequence | list , tuple , range |
collection of items |
Mapping | dict |
collection of items |
Set | set |
collection of items |
Boolean | bool |
boolean |
None | NoneType |
NA/NULL type |
Let's Identify the type of different variables 🔍-¶
by using inbuilt type()
function:
In [1]:
Copied!
name = "Sunny"
print(name)
print(type(name))
name = "Sunny"
print(name)
print(type(name))
Sunny <class 'str'>
In [2]:
Copied!
sentence = "Sunny is a life long student"
print(sentence)
print(type(sentence))
sentence = "Sunny is a life long student"
print(sentence)
print(type(sentence))
Sunny is a life long student <class 'str'>
In [3]:
Copied!
sentence = 'single quote example'
print(sentence)
print(type(sentence))
sentence = 'single quote example'
print(sentence)
print(type(sentence))
single quote example <class 'str'>
In [4]:
Copied!
character = "S"
print(character)
print(type(character))
character = "S"
print(character)
print(type(character))
S <class 'str'>
In [5]:
Copied!
x = "1234"
print(x)
print(type(x))
x = "1234"
print(x)
print(type(x))
1234 <class 'str'>
In [6]:
Copied!
x = "12.34"
print(x)
print(type(x))
x = "12.34"
print(x)
print(type(x))
12.34 <class 'str'>
In [7]:
Copied!
x = 1234
print(x)
print(type(x))
x = 1234
print(x)
print(type(x))
1234 <class 'int'>
In [8]:
Copied!
x = -1234
print(x)
print(type(x))
x = -1234
print(x)
print(type(x))
-1234 <class 'int'>
In [9]:
Copied!
x = 0
print(x)
print(type(x))
x = 0
print(x)
print(type(x))
0 <class 'int'>
float
data type¶
float
represents a pure decimal numbers it can be positive or negative decimal numbers
In [10]:
Copied!
x = 12.34
print(x)
print(type(x))
x = 12.34
print(x)
print(type(x))
12.34 <class 'float'>
In [11]:
Copied!
x = -12.34
print(x)
print(type(x))
x = -12.34
print(x)
print(type(x))
-12.34 <class 'float'>
In [12]:
Copied!
x = 0.0
print(x)
print(type(x))
x = 0.0
print(x)
print(type(x))
0.0 <class 'float'>
In [13]:
Copied!
z = 1 + 2j
print(z)
print(type(z))
print(f"real part of {z} => {z.real}")
print(f"imaginary part of {z} => {z.imag}")
z = 1 + 2j
print(z)
print(type(z))
print(f"real part of {z} => {z.real}")
print(f"imaginary part of {z} => {z.imag}")
(1+2j) <class 'complex'> real part of (1+2j) => 1.0 imaginary part of (1+2j) => 2.0
In [14]:
Copied!
x = [1,2,3,4,5,6,7,8,9,10]
print(x)
print(type(x))
x = [1,2,3,4,5,6,7,8,9,10]
print(x)
print(type(x))
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10] <class 'list'>
In [15]:
Copied!
x = ["item1", "item2", "item3"]
print(type(x))
x = ["item1", "item2", "item3"]
print(type(x))
<class 'list'>
tuple
data-type¶
In [16]:
Copied!
x = (1,2,3,4,5,6,7,8,9,10)
print(x)
print(type(x))
x = (1,2,3,4,5,6,7,8,9,10)
print(x)
print(type(x))
In [17]:
Copied!
x = ("item1", "item2", "item3")
print(type(x))
x = ("item1", "item2", "item3")
print(type(x))
<class 'tuple'>
In [18]:
Copied!
x = {
"key": "value",
"key_1": "value_1",
}
print(type(x))
x = {
"key": "value",
"key_1": "value_1",
}
print(type(x))
<class 'dict'>
In [19]:
Copied!
x = {1,2,3,4,5,6,7,8,9,10}
print(x)
print(type(x))
x = {1,2,3,4,5,6,7,8,9,9,9,9,9,9,10}
print(x)
print(type(x))
x = {1,2,3,4,5,6,7,8,9,10}
print(x)
print(type(x))
x = {1,2,3,4,5,6,7,8,9,9,9,9,9,9,10}
print(x)
print(type(x))
{1, 2, 3, 4, 5, 6, 7, 8, 9, 10} <class 'set'> {1, 2, 3, 4, 5, 6, 7, 8, 9, 10} <class 'set'>
In [20]:
Copied!
x = True
print(x)
print(type(x))
x = True
print(x)
print(type(x))
True <class 'bool'>
In [21]:
Copied!
x = False
print(x)
print(type(x))
x = False
print(x)
print(type(x))
False <class 'bool'>
In [22]:
Copied!
x = None
print(x)
print(type(x))
x = None
print(x)
print(type(x))
None <class 'NoneType'>
Example from School records or marksheets 🏫¶
In [23]:
Copied!
name_of_student = "Sunny"
print(name_of_student)
print(type(name_of_student))
name_of_student = "Sunny"
print(name_of_student)
print(type(name_of_student))
Sunny <class 'str'>
In [24]:
Copied!
roll_no = 12
print(roll_no)
print(type(roll_no))
roll_no = 12
print(roll_no)
print(type(roll_no))
12 <class 'int'>
In [25]:
Copied!
percentage_of_marks = 75.2
print(percentage_of_marks)
print(type(percentage_of_marks))
percentage_of_marks = 75.2
print(percentage_of_marks)
print(type(percentage_of_marks))
75.2 <class 'float'>
In [26]:
Copied!
student_1_data = {
"name_of_student": "Sunny",
"roll_no": 12,
"percentage_of_marks": 75.2
}
print(student_1_data)
print(type(student_1_data))
student_1_data = {
"name_of_student": "Sunny",
"roll_no": 12,
"percentage_of_marks": 75.2
}
print(student_1_data)
print(type(student_1_data))
{'name_of_student': 'Sunny', 'roll_no': 12, 'percentage_of_marks': 75.2} <class 'dict'>
In [27]:
Copied!
list_of_subjects = [
"English",
"Maths",
"Science",
"Social Science",
"Hindi"
]
print(list_of_subjects)
print(type(list_of_subjects))
list_of_subjects = [
"English",
"Maths",
"Science",
"Social Science",
"Hindi"
]
print(list_of_subjects)
print(type(list_of_subjects))
['English', 'Maths', 'Science', 'Social Science', 'Hindi'] <class 'list'>
In [28]:
Copied!
passing_status = False
print(passing_status)
print(type(passing_status))
passing_status = False
print(passing_status)
print(type(passing_status))
False <class 'bool'>
In [29]:
Copied!
practical_marks_in_English = None
print(practical_marks_in_English)
print(type(practical_marks_in_English))
practical_marks_in_English = None
print(practical_marks_in_English)
print(type(practical_marks_in_English))
None <class 'NoneType'>
Example from Amazon Ecommerce website 🛒-¶
In [30]:
Copied!
name_of_product = "Hands on Machine Learning"
cost_of_product = 3000
one_day_delivery = False
rating_of_product = 4.7
reviews_of_product = [
"Book is good",
"Book is average",
"Book is excellent"
]
product_info = {
"name_of_product": "Hands on Machine Learning",
"cost_of_product": 3000,
"one_day_delivery": False,
"rating_of_product": 4.7,
"reviews_of_product": [
"Book is good",
"Book is average",
"Book is excellent"
]
}
name_of_product = "Hands on Machine Learning"
cost_of_product = 3000
one_day_delivery = False
rating_of_product = 4.7
reviews_of_product = [
"Book is good",
"Book is average",
"Book is excellent"
]
product_info = {
"name_of_product": "Hands on Machine Learning",
"cost_of_product": 3000,
"one_day_delivery": False,
"rating_of_product": 4.7,
"reviews_of_product": [
"Book is good",
"Book is average",
"Book is excellent"
]
}