0% found this document useful (0 votes)
23 views

Python Data Types Part-2

This document summarizes key Python data types and structures. It discusses lists, which can contain different data types and are mutable; tuples, which are immutable lists; dictionaries, which contain key-value pairs; Boolean values of True and False; and sets, which do not contain duplicates. Operators like + and * can be used on lists, and dictionaries allow accessing values by key.

Uploaded by

Vadivu
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
23 views

Python Data Types Part-2

This document summarizes key Python data types and structures. It discusses lists, which can contain different data types and are mutable; tuples, which are immutable lists; dictionaries, which contain key-value pairs; Boolean values of True and False; and sets, which do not contain duplicates. Operators like + and * can be used on lists, and dictionaries allow accessing values by key.

Uploaded by

Vadivu
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 11

Python Programming

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:-

>>> first = [1,”two”,3.0,”four”,5]


>>> second = [“five”,6]
>>> first
[1, ‘two’,3.0,’four’,5]
>>> first+second
[1, ‘two’,3.0,’four’,5,”five”,6]
>>> second * 3
[‘five’,6,’five’,6,’five’,6]
>>> first[0:3]
[1,’two’,3.0]

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:

>>> dict1={1:”first”, 2:”second”}

>>> dict2={3:”three”, “four”:4}

>>>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])

• Symmetric Difference :Operation performed on


two sets. It returns elements present in either
set1 or set2 but not in both.^ operator is used
here
>>>sym_diff=set1 ^ set2

>>>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

You might also like