Python Data Types and Data Structures
Python data types and data structures
Python Data Types: Variable can hold values of different data types. Python is a dynamically typed language hence we need not define the type of the variable while declaring it. The interpreter implicitly binds the value with its type.
Python enables us to check the type of variable used in the program. python provides us the type() function which returns the type of the variable passed.
Consider the following example to define the values of different data types and check their type.
How To Use Data Types in Python?
a=100
b="Hello Python"
c= 10.6
print("value of a:",a)
print("value of b:",b)
print("value of c:",b)
print("Data type:",type(a))
print("Data type:",type(b))
print("Data type:",type(c))
*****OUTPUT*****
value of a: 100
value of b: Hello Python
value of c: 10.6
Data types: <class 'int'>
Data types: <class 'str'>
Data types: <class 'float'>
a=100
b="Hello Python"
c= 10.6
print("value of a:",a)
print("value of b:",b)
print("value of c:",b)
print("Data type:",type(a))
print("Data type:",type(b))
print("Data type:",type(c))
*****OUTPUT*****
value of a: 100
value of b: Hello Python
value of c: 10.6
Data types: <class 'int'>
Data types: <class 'str'>
Data types: <class 'float'>
Python Standard Data Types
A variable can hold different types of values. For Example, a person's name must be stored as a string whereas its id must be stored as an integer.
Python provides various standard data types that define the storage method on each of them. the data types defined in python are given below.
1. Numbers
2. String
3. List
4. Tuple
5. Dictionary
In this section of the Python course, we will give a brief introduction to the above data types. We will discuss each one of them in detail in this course.
Numbers: Number stores numeric values. Python creates number objects when a number is assigned to a variable.
How To Use number Data Types in Python?
a=100
b=200
print(a)
print(b)
*****OUTPUT*****
100
200
Here a and b are number objects
a=100
b=200
print(a)
print(b)
*****OUTPUT*****
100
200
Here a and b are number objects
Python Support 4 Types of Numeric data
1. int (signed integer like 1,2,3,4 etc)
2. long(long integer used for a higher range of values like 85674477L, -64754736734L, etc).
3. float(float is used to store a floating-point number like 1.4, 1.5, 5.7, etc).
4. Complex( complex numbers like 2.2j, 3.5+2j, etc).
Python allows us to use a lower-case L to used with long integers. However, we must always use an upper-case l to avoid confusion.
A complex number contains an ordered pair l.e, x+iy where x and y denote the real and imaginary parts respectively).
How To Use Integer Data Types in Python?
int a=100
int b=200
print("value of a:",a)
print("value of b:",b)
print("Data type:",type(a))
print("Data type:",type(b))
*****OUTPUT*****
value of a: 100
value of b: 200
Data types: <class 'int'>
Data types: <class 'int'>
int a=100
int b=200
print("value of a:",a)
print("value of b:",b)
print("Data type:",type(a))
print("Data type:",type(b))
*****OUTPUT*****
value of a: 100
value of b: 200
Data types: <class 'int'>
Data types: <class 'int'>
How To Use Float Data Types in Python?
a=25.65
b=28.45
print("value of a:",a)
print("value of b:",b)
print("Data type:",type(a))
print("Data type:",type(b))
*****OUTPUT*****
value of a: 25.65
value of b: 28.45
Data types: <class 'float'>
Data types: <class 'float'>
a=25.65
b=28.45
print("value of a:",a)
print("value of b:",b)
print("Data type:",type(a))
print("Data type:",type(b))
*****OUTPUT*****
value of a: 25.65
value of b: 28.45
Data types: <class 'float'>
Data types: <class 'float'>
How To Use Complex Data Types in Python?
a=5+2j
b=3+4j
print("complex value of a:",a)
print("complex value of b:",b)
print("Data type:",type(a))
print("Data type:",type(b))
*****OUTPUT*****
complex value of a: 5+2j
complex value of b: 3+4j
Data types: <class 'complex'>
Data types: <class 'complex'>
a=5+2j
b=3+4j
print("complex value of a:",a)
print("complex value of b:",b)
print("Data type:",type(a))
print("Data type:",type(b))
*****OUTPUT*****
complex value of a: 5+2j
complex value of b: 3+4j
Data types: <class 'complex'>
Data types: <class 'complex'>
More Related Languages
C Programming Language
C++ Programming Language
Java Programming Language
PHP Programming Language
Projects Programming Language
String: The string can be defined as the sequence of the characters represented in the quotation marks. In python, we can use single, double, or triple quotes to define a string.
String handling in python is a straightforward task since there are various in-built functions and operators provides.
In the case of string handling the operator + is used to concatenate two strings as the operation "hello"+"python" returns hello python.
The operator * is known as repetition operator as the operations "python" *2 returns "python python".
How To Use String Data Types in Python?
a="Welcome to icoderweb"
b="Welcome Python Language"
print("String value of a:",a)
print("String value of b:",b)
print("Data type:",type(a))
print("Data type:",type(b))
*****OUTPUT*****
The string value of a: Welcome to icoderweb
The string value of b: Welcome Python Language
Data types: <class 'str'>
Data types: <class 'str'>
a="Welcome to icoderweb"
b="Welcome Python Language"
print("String value of a:",a)
print("String value of b:",b)
print("Data type:",type(a))
print("Data type:",type(b))
*****OUTPUT*****
The string value of a: Welcome to icoderweb
The string value of b: Welcome Python Language
Data types: <class 'str'>
Data types: <class 'str'>
List: List is similar to arrays in C. However the list can contain data of different types. The items stored in the list are separated with a comma(,) and enclosed within square brackets[].
We can use slice[:] operators to access the data of the list the concatenation operator(+) and repetition operator(*) works with the list in the same ways as they were working with the strings.
How to use list data type in python?
int_list=[11,12,13,14,15 16,17,18,19]
float_list=[4.6,2.6,3.7,8.4,6.3]
Str_list=["java","Python","icoderweb","Google"]
Mix_list=[1234,23.54,"list program"]
print(int_list)
print(float_list)
print(Str_list)
print(Mix_list)
*****OUTPUT*****
[11,12,13,14,15,16,17,18,19]
[4.6, 2.6, 3.7, 8.4, 6.3]
['java', 'Python', 'icoderweb', 'Google']
[1234, 23.54, 'list program']
int_list=[11,12,13,14,15 16,17,18,19]
float_list=[4.6,2.6,3.7,8.4,6.3]
Str_list=["java","Python","icoderweb","Google"]
Mix_list=[1234,23.54,"list program"]
print(int_list)
print(float_list)
print(Str_list)
print(Mix_list)
*****OUTPUT*****
[11,12,13,14,15,16,17,18,19]
[4.6, 2.6, 3.7, 8.4, 6.3]
['java', 'Python', 'icoderweb', 'Google']
[1234, 23.54, 'list program']
Tuple: A tuple is similar to the list in many ways. like lists, tuples also contain the collection of the items of different data types. The items of the tuple are separated with a comma(,) and enclosed in parentheses().
A tuple is a read-only data structure as we can not modify the size and value of the items of a tuple.
How to use tuple data type in python?
str_tup=("Python","Java","Programming")
int_tup=(80,77,14,78,96)
float_tup=(4.3,8.6,7.4,5.6)
mix_tup=("Icoderweb",8077,35.3)
print("String Tuple:",str_tup)
print("Integer Tuple:",int_tup)
print("Floating Tuple:",float_tup)
print("Mixed Tuple:",mix_tup)
print("Type:",type(str_tup))
*****OUTPUT*****
String Tuple: ('Python', 'Java', 'Programming')
Integer Tuple: (80, 77, 14, 78, 96)
Floating Tuple: (4.3, 8.6, 7.4, 5.6)
Mixed Tuple: ('Icoderweb', 8077, 35.3)
Type: <class 'tuple'>
str_tup=("Python","Java","Programming")
int_tup=(80,77,14,78,96)
float_tup=(4.3,8.6,7.4,5.6)
mix_tup=("Icoderweb",8077,35.3)
print("String Tuple:",str_tup)
print("Integer Tuple:",int_tup)
print("Floating Tuple:",float_tup)
print("Mixed Tuple:",mix_tup)
print("Type:",type(str_tup))
*****OUTPUT*****
String Tuple: ('Python', 'Java', 'Programming')
Integer Tuple: (80, 77, 14, 78, 96)
Floating Tuple: (4.3, 8.6, 7.4, 5.6)
Mixed Tuple: ('Icoderweb', 8077, 35.3)
Type: <class 'tuple'>
Dictionary: Dictionary is an ordered set of key-value pairs of items.
It is like an associative array of the hash tables where each key stores a specific value. the key can hold any primitive data type whereas value is an arbitrary python object.
The items in the dictionary are separated with the comma and enclosed in the curly braces{}.
How to use dictionary data type in python?
student={
"name":"icoderweb",
"rollno":807714,
"email":"icoderweb@gmail.com",
"address":"Delhi"
}
print("Student Name:",student["name"])
print("Student Roll No:",student["rollno"])
print("Student Email:",student["email"])
print("Student address:",student["address"])
*****OUTPUT*****
Student Name: icoderweb
Student Roll No: 807714
Student Email: icoderweb@gmail.com
Student address: Delhi
student={
"name":"icoderweb",
"rollno":807714,
"email":"icoderweb@gmail.com",
"address":"Delhi"
}
print("Student Name:",student["name"])
print("Student Roll No:",student["rollno"])
print("Student Email:",student["email"])
print("Student address:",student["address"])
*****OUTPUT*****
Student Name: icoderweb
Student Roll No: 807714
Student Email: icoderweb@gmail.com
Student address: Delhi
0 Comments