Python Data Types Part-2
Python Data Types Part-2
1
List
❖ A list can contain the same type of items and
also contain different types of items
❖ It is an ordered and indexable sequence
❖ Separate the items using commas and enclose
within square brackets [ ]
❖ Lists are mutable. i.e., can add/delete items
❖ List – contain different types of items
❖[ : ] , + and * operators also used
2
• Eg:-
3
Tuple
❖ Used to store sequence of items, separated by commas
❖ Tuples enclosed within parentheses ( )
❖ Tuples are immutable. i.e., read only list.
❖ Once the items are stored, cannot be modified.
❖Eg:
>>> third = (7, “eight”, 9, 10.0)
>>>third
(7, ‘eight’, 9, 10.0)
>>> third[0]= “seven” # provide error bcz immutable
4
Dictionary
• It is an unordered collection of key-value pairs
• Hash table type
• Order of elements are undefined in dictionary
▪ Keys
▪ Values
▪ Items (Key-Value pairs)
• Used in where we have the large amount of data
• Used to store different type of data / values
• Colon (:) is used to separate key from value
• Can use the key to access the value with the help of
[]
•Items in dictionary enclosed with { } and separated by
comma(,)
5
• Eg:
>>>dict2[5]=“five”
>>>dict2.keys()
>>>dict2.values()
6
Boolean
• It store the data as true and false
• >>> a=true
>>> type(a)
<type ‘bool’>
• >>> a=false
>>> type(a)
<type ‘bool’>
7
Sets
• Unordered collection of data is called set
• Does not contain any duplicate values or
elements
• Operations in set
o Union
o Intersection
o Difference
o Symmetric Difference
• Union : Operation performed on two sets returns
all the elements from both sets
• Union operation performed using | operator
8
Eg: >>>set1= set([1,2,4,1,2,8,5,4])
>>>set2=set([1,9,3,2,5])
>>>union=set1 | set2
>>>print(union)
Set([1,2,3,4,5,8,9])
• Intersection : Operation performed on two
sets returns common elements from both sets.
& operator is used to perform intersection
>>> intersect=set1 & set2
>>> print(intersect)
Set([1,2,5])
• Difference : Operation performed on two sets.
It returns elements present on set1 but not in
set2. – operator is used for this operation. 9
>>>diff= set1 – set2
>>>print(diff)
Set([8,4])
>>>print(sym_diff)
Set([3,4,8,9])
10
syllabus
• UNIT I
Introduction:
Introduction to Python, Python overview, Getting started with
python, Comments, Identifiers, Reserved keywords, Variables,
Standard Data Types, Operators, Statements and Expressions, String
Operations, Boolean Expressions. Control Statements- for, while, if
elif else, while.
11