Python Chap 3
Python Chap 3
Anand Komiripalem
Ex:
list=[]
print(list) #[]
print(type(list)) #<class ‘list’>
list = [10, 20, 30, 40] if we already know the elements of list
With Dynamic Input: Output:
list=eval(input("Enter List:"))
Enter List: [10,20,30,40]
print(list)
[10,20,30,40]
print(type(list)) <class ‘list’>
Anand Komiripalem
By using Index:
List follows zero based index.
List supports both +ve and -ve indexes.
Ex:
list = [10, 20, 30, 40]
print(list[0]) # 10
print(list[-1]) # 40
print(list[10]) # IndexError:
Anand Komiripalem
n=[1,2,3,4,5,6,7,8,9,10]
print(n[2:7:2]) #[3, 5, 7]
print(n[4::2]) #[5, 7, 9]
print(n[3:7]) #[4, 5, 6, 7]
print(n[8:2:-2]) #[9, 7, 5]
print(n[4:100]) #[5, 6, 7, 8, 9, 10]
Anand Komiripalem
n = [0,1,2,3,4,5]
i=0
while I < len(n): Output:
print(n[i])
i=i+1 1
2
3
By using for Loop: 4
n=[0,1,2,3,4,5] 5
for n1 in n:
print(n1)
Anand Komiripalem
n=[0,1,2,3,4,5,6,7,8,9,10]
Output:
for n1 in n: 0 2
if n1%2==0: 4 6
print(n1) 8 10
Output
A is available at +ve index 0 and at –ve index -1
l = ["A","B","C"] B is available at +ve index 1 and at –ve index -2
x = len(l) C is available at +ve index 2 and at –ve index -3
for i in range(x):
print(l[i],"is available at +ve index: ",i ,"and at -ve index: ", i-x)
Anand Komiripalem
Manipulating Elements:
list=[]
list.append("A")
list.append("B")
list.append("C")
print(list) #[‘A’, ‘B’, ‘C’]
Anand Komiripalem
Output: ['Chicken', 'Mutton', 'Fish', 'M', 'u', 's', 'h', 'r', 'o', 'o', 'm']
Anand Komiripalem
n=[10,20,30,40]
print(n.pop()) #40
print(n.pop()) #30
print(n) #[10,20]
s = ["Dog","Banana","Cat","Apple"]
s.sort()
print(s) # ['Apple','Banana','Cat','Dog']
Note: To use sort() function, compulsory list should contain only homogeneous
elements. Otherwise we will get TypeError
Anand Komiripalem
x = [10,20,30,40]
y = x[:]
y[1] = 777
print(x) #[10, 20, 30, 40]
print(y) # [10, 777, 30, 40]
Anand Komiripalem
x = [10,20,30,40]
y = x.copy()
y[1] = 200
print(x) # [10, 20, 30, 40]
print(y) # [10, 200, 30, 40]
Ex:
c = a+40 #TypeError:
c = a+[40] # Valid
Anand Komiripalem
When ever we are using relatational Operators (<, <=, >, >=) between List
Objects, only 1ST Element comparison will be performed.
Ex:
n=[10,20,30,40]
print (10 in n) #True
print (10 not in n) #False
print (50 in n) #False
print (50 not in n) #True
Anand Komiripalem
Output: [['the', 3], ['quick', 5], ['brown', 5], ['fox', 3], ['jumps', 5],
['over', 4], ['the', 3], ['lazy', 4], ['dog', 3]]
Anand Komiripalem
vowels=['a','e','i','o','u']
word=input("Enter the word to search for vowels: ")
found=[]
for letter in word:
if letter in vowels:
if letter not in found:
found.append(letter)
print(found)
print("different vowels present in",word,"are",found)
Anand Komiripalem
Tuple
Data Type
Anand Komiripalem
Tuple Creation:
t = (): Creating an empty tuple
t = (value,): Creation of Single valued Tuple, Parenthesis are Optional,
should ends with Comma
t = 10, 20, 30: Creation of multi values Tuples & Parenthesis are
Optional.
Anand Komiripalem
t=tuple(range(10,20,2))
print(t) #(10,12,14,16,18,20)
t=(10,20,30,40,50,60)
print(t[2:5]) #(30,40,50)
print(t[2:100]) #(30,40,50,60)
print(t[::2]) #(10,30,50)
Anand Komiripalem
min(), max():
returns min and max values according to default natural sorting order.
t = (40,10,30,20)
print(min(t)) # 10
print(max(t)) # 40
Anand Komiripalem
Tuple Comprehension:
Tuple Comprehension is not supported by Python.
t = ( x**2 for x in range(1,6))
Here we are not getting tuple object and we are getting generator
object.
t= ( x**2 for x in range(1,6))
print(type(t)) #<class ‘generator’>
for x in t:
print(x) #1,4,9,16,25
Anand Komiripalem
Set
Data Type
Anand Komiripalem
In Set:
Duplicates are not allowed.
Insertion order is not preserved. But we can sort the elements.
Indexing and slicing not allowed for the set.
Heterogeneous elements are allowed.
Set objects are mutable
We can represent set elements within curly braces and with comma
separation
Anand Komiripalem
l = [10,20,30,40,10,20,10]
s=set(l)
print(s) # {40, 10, 20, 30}
---------------------------------------------------
s=set(range(5))
print(s) #{0, 1, 2, 3, 4}
Anand Komiripalem
update(x,y,z):
To add multiple items to the set.
Arguments are not individual elements and these are Iterable
objects like List, Range etc.
s={10,20,30}
l=[40,50,60,10]
s.update(l,range(5))
print(s) #{0,1,2,3,4,40,10,50,20,60,30}
Anand Komiripalem
pop(): removes and returns some random elements from the set
s={40,10,30,20}
print(s.pop()) # 40
print(s) #{20,10,30}
x={20,40,50}
Y={50, 20, 10, 30}
x.intersection_update(y)
print(x) #{40}
isdisjoint():
Two sets are said to be disjoint when their intersection is null. In simple
words they do not have any common element in between them.
returns Trueif the two sets are disjoint.
returns Falseif the twos sets are not disjoint.
Anand Komiripalem
issubset(): returns True if all elements of a set are present in another set. If not, it
returns False.
Issuperset(): returns true if all the elements of another set are present in the set if
not false.
x={10,20,30}
y={10,20,30,40,50}
print(x.issubset(y)) #True
print(y.issubset(x)) #False
print(y.issubset(y)) #True
print(y.issuperset(x)) #True
Anand Komiripalem
x={10,20,30}
y={10,20,30,40,50}
x.symmetric_difference_update(y)
print(y)
s=set(“anand")
print(s) #{‘a’, ‘n’, ‘a’, ‘n’, ‘d’}
print('d' in s) #True
print('z' in s) #False
Anand Komiripalem
Dictionary
Data Type
Anand Komiripalem
Ex:
d= {1:”hai”, 2:”hello”, 3:”bye”}
Note: If the specified key is not available then we will get KeyError
Ex:
d = {1:anand',2:'ravi', 3:’raghu'}
print(d[1]) #anand
print(d[3]) #raghu
print(d[6]) #Error
Anand Komiripalem
If the key is not available then a new entry will be added to the
dictionary with the specified key-value pair
If the key is already available then old value will be replaced with
new value.
d={1:"anand",2:"ravi",3:"raghu"}
d[4]="pavan"
print(d) #{1: 'anand', 2: 'ravi', 3: 'raghu', 4: 'pavan'}
d[1]="sunny"
print(d) #{1: 'sunny', 2: 'ravi', 3: 'raghu', 4: 'pavan'}
Anand Komiripalem
del d[key]:
It deletes entry associated with the specified key.
If the key is not available then we will get KeyError.
d={1:"anand",2:"ravi",3:"raghu", 4:"pavan"}
del d[2]
print(d) #{1: 'anand', 3: 'raghu', 4: 'pavan'}
del d[6] #keyerror
d={1:"anand",2:"ravi",3:"raghu", 4:"pavan"}
del d
print(d) #NameError
Anand Komiripalem
d={1:"anand",2:"ravi",3:"raghu", 4:"pavan"}
print(d) #{1: 'anand', 2: 'ravi', 3: 'raghu', 4: 'pavan'}
print(d.get(2)) #ravi
print(d.get(23)) #None
print(d.get(3,"hai")) #raghu
print(d.get(23,"hai")) #hai
Anand Komiripalem
popitem(): it removes an key-value item from the dictionary and returns it.
d={1:"anand",2:"ravi",3:"raghu", 4:"pavan"}
print(d) #{1:"anand",2:"ravi",3:"raghu", 4:"pavan"}
print(d.popitem()) # 4:"pavan”
print(d) #{1:"anand",2:"ravi",3:"raghu"}
print(d.popitem()) #3:"raghu”
Anand Komiripalem
d={1:"anand",2:"ravi",3:"raghu", 4:"pavan"}
d1=d.copy()
print(d1) #{1: 'anand', 2: 'ravi', 3: 'raghu', 4: 'pavan'}
print(id(d)) #46602688
print(id(d1)) #47325376
Anand Komiripalem