You are on page 1of 21

Python Data Types

Data Types

 Variables hold values, and every value has a data-type.


 Python is a dynamically typed language
Ex : var = 25
 Python interpreter automatically interprets the value of var as an
integer type
 Python provides  type() function, which is used to check the type
of the variable
 Since everything is an object in Python programming, data types
are actually classes and variables are instance (object) of these
classes.
Data Types Cont…
Consider the following example to define the values of
different data types and checking its type
> a=10  
> b="Python"  
> c = 10.5   Output:
> print(type(a))   <class 'int'>
> print(type(b))   < class 'str'>
< class 'float'>
> print(type(c)) 
Data Types Cont…
 Python provides various
standard data types
 The data types defined in
Python are given below.
1. Number
2. Sequence Type
3. Boolean
4. Set
5. Dictionary
Number
Number
Integers, floating point numbers and complex numbers fall under Python
numbers category. They are defined as int, float  and complex classes in
Python.
> a = 5  
> print("The type of a", type(a))  
>  b = 40.5   Output:
> print("The type of b", type(b))  
The type of a <class 'int'>
>  c = 1+3j   The type of b <class 'float'>
> print("The type of c", type(c))   The type of c <class 'complex'>
Sequences
String
 String is a sequence of characters represented in the quotation
marks. In Python, we can use single, double, or triple quotes to
define a string.
 Python provides built-in functions and operators to perform various
operations in the string.
 The operator + is used to concatenate two strings as the
operation "hello"+" python" returns "hello python"
 The operator * is known as a repetition operator as the operation
"Python" *2 returns 'PythonPython'
String
Example:
str = "string using double quotes"  
print(str)  
s = ''''A multiline 
Output:
string'''   string using double quotes
print(s)   A multiline
string
List
 List is an ordered sequence of items.
 All the items in a list do not need to be of the same type
Example: a = [1, 2.2, 'python']
 Lists are mutable, meaning, the value of elements of a
list can be altered.
Example: a = [1, 2, 3]
a[2] = 4
print(a) Output
[1, 2, 4]
List
 We can use the slicing operator [ ] to extract an item
or a range of items from a list. The index starts from
‘0’ in Python.
Example:
a = [5,10,15,20,25,30,35,40]
Output
print("a[2] = ", a[2])
a[2] = 15
print("a[0:3] = ", a[0:3])
a[0:3] = [5, 10, 15]
print("a[5:] = ", a[5:])
a[5:] = [30, 35, 40]
Tuple
 Tuple is an ordered sequence of items same as a list. The
only difference is that tuples are immutable. Tuples once
created cannot be modified.
 Tuples are used to write-protect data and are usually faster
than lists as they cannot change dynamically
Example: t = (5,'program', 1+3j)
 We can use the slicing operator [] to extract items  but we
cannot change its value.
Tuple
Example:
t = (5,'program', 1+3j)
print("t[1] = ", t[1])
print("t[0:3] = ", t[0:3])

Output
t[1] = program
t[0:3] = (5, 'program', (1+3j))
Traceback (most recent call last):
t[0] = 10 TypeError: 'tuple' object does not support
item assignment
Set
Set
 Set is the unordered collection of the data type. It is iterable,
mutable, and has unique elements.
 In set, the order of the elements is undefined; it may return
the changed sequence of the element.
 Set is defined by values separated by comma inside braces
 { }. It can contain various types of values.
Example:
a = {5,2,3,1,4}
Output
print("a = ", a)
a = {1, 2, 3, 4, 5}
print(type(a)) <class 'set'>
Set
 We can perform set operations like union, intersection on two
sets. Sets have unique values. Sets eliminate duplicates.
Example:
a = {1,2,2,3,3,3}
print(a)

Output
{1, 2, 3}

 Since, set are unordered collection, indexing has no meaning.


Hence, the slicing operator [] does not work.
Dictionary
Dictionary
 Dictionary is an unordered set of a key-value pair of items.
It is like an associative array or a hash table where each key
stores a specific value.
 Key can hold any primitive data type
 Dictionaries are defined within braces {} with each item
being a pair in the form key : value and multiples items in
the dictionary are separated with the comma (,)
Example:
d = {1:'Jimmy', 2:'Alex', 3:'john', 4:'mike'}     
Dictionary
Example: d = {1:'Jimmy', 2:'Alex', 3:'john', 4:'mike'}     
print (d)  
print("1st name is "+d[1])   
print("2nd name is "+ d[4])    
print (d.keys())    
Output:

print (d.values())  1st name is Jimmy


2nd name is mike
{1: 'Jimmy', 2: 'Alex', 3: 'john', 4: 'mike'}
dict_keys([1, 2, 3, 4])
dict_values(['Jimmy', 'Alex', 'john', 'mike'])
Boolean
 Boolean type provides two built-in values, True and
False. These values are used to determine the given
statement true or false.
 It denotes by the class bool.
Example:
print(type(True))   Output:
print(type(False))   <class 'bool'>
  <class 'bool'>
Thank You

You might also like