Skip to content

Commit 37625f9

Browse files
committed
dictionary, tuples, list and sets with basic codes
1 parent da70619 commit 37625f9

13 files changed

Lines changed: 390 additions & 0 deletions

DataStructure/Dictionary.py

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
tel={'jac':1,'sym':2}
2+
tel['u']=3
3+
tel['c']=4
4+
print(tel)
5+
print(tel['jac'])
6+
7+
del tel['c']
8+
print(tel)
9+
tel['x']=5
10+
tel['b']=7
11+
12+
sorted(tel)
13+
print(tel)
14+
15+
w=dict([('hello',1),('yes',2),('you',3)])
16+
print(w)
17+
18+
#dict compreshensions
19+
20+
print(x: x**3 for x in (3,4,5))
21+
22+
23+
24+
25+
for k in knights.items():
26+
print(k)
27+
28+
29+
for w,e in enumerate(knights):
30+
print(w,e)
31+
32+
for r,y in enumerate([1,2,34,45]):
33+
print(r,y)
34+
35+
x=list(knights)
36+
37+
print(x)
38+
39+
print(sorted(knights))
40+
41+
print('gm' in knights)
42+
print('ritu' in knights)
43+
44+
question=['a','b','c','d']
45+
print(type(question))
46+
answer=['one','two','three']
47+
48+
for a,b in zip(question,answer):
49+
print(a,b)
50+
51+
for x in zip(question,answer):
52+
for c in x:
53+
print(c,x)
54+
print(type(x),type(c))
55+
#traversed through both the values
56+
57+
b="my name is z"
58+
59+
knights={'hello':'ritu','gm':'rishabh sir'}
60+
i=[9,4,6,2,3]
61+
#problem occured
62+
p=set(i)
63+
print(p)
64+
for i in knights.items():
65+
print(i)
66+
67+
print(i)
68+
#cz python is a interpreter language
69+
for j in i:
70+
print(j)
71+
72+
print(j)
73+
#problem till here
74+
75+
76+
te=1,3,5,7,9
77+
for c in reversed(range(1,10,2)):
78+
print(c)
79+
basket=['set','pen','bat','hello','table','pen']
80+
81+
n={x : x*2 for x in basket }
82+
83+
for q,w in n.items():
84+
print(q,w)
85+
86+
87+
88+

DataStructure/Sets.py

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
#a set is unordered collection with no duplicate elements
2+
3+
k={'a','b','c'}
4+
x={'a','v','w','b','c','q'}
5+
6+
print(k<x)
7+
print(x<k)
8+
9+
print(x | k)
10+
print(x & k)
11+
print(x ^ k)
12+
print(x-k)
13+
14+
#with update()
15+
#can be used with set but cant be used withj frozenset
16+
x |= k
17+
print(x)
18+
x &= k
19+
print(x)
20+
21+
#print(x ^=k)
22+
#print(x -=k)
23+
24+
x.add('d')
25+
print(x)
26+
x.remove('a')
27+
x.discard('c')
28+
x.discard('h')
29+
x.pop()
30+
x.clear()
31+
32+
#set comprehensions
33+
34+
a={x for x in 'abracadabra' if x not in 'abc'}
35+
print(a)
36+
37+
vowels={'a','e','i','o','u'}
38+
39+
fset=frozenset(vowels)
40+
print(fset)
41+
42+
print(vowels | k)

DataStructure/Tuples.py

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
t= 1234,235,'hello'
2+
3+
print(t[0])
4+
5+
print(t)
6+
7+
u= t, 1,2,3,4
8+
print(u)
9+
10+
v=t, (12,23,4,56,7)
11+
print(v)
12+
#an empty tuple
13+
e=()
14+
#a tuple and it require trailing comma
15+
s='hello','group',
16+
17+
print(len(e))
18+
len(s)
19+
20+
#tuple is immutable
21+
22+
#sequesce or tuple packing
23+
t=1,2,3,45,6
24+
25+
#sequence unpacking
26+
v,b,n,m,j=t
27+
#now these v=1 whhere v will have type int, and so on
28+
29+
t=(1,2,3),(1,2,3),(3,4,56)
30+
31+
q,w,r=t
32+
#in this case q will have type tuple
33+
34+
#initializing tuple with one value
35+
o=1,
36+
37+
38+
39+
40+
41+
42+
43+
44+
45+

DataStructure/list.py

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
#methods of list
2+
3+
a=['hello','I','am','ritu','soni','I','am','happy']
4+
#append
5+
a.append(3)
6+
7+
print(a)
8+
a.append(34)
9+
a.append(4.45)
10+
a.append(len(a))
11+
a.append(True)
12+
print(a)
13+
x=a.append(56) #extend and append do not return anything x is none
14+
print(x)
15+
16+
#extend(iterable)
17+
18+
b=[1,2]
19+
x=a.extend(b)
20+
print(x)
21+
22+
a.extend(b)
23+
print(a)
24+
25+
#insert
26+
a.insert(1,0)
27+
print(a)
28+
29+
#remove parameter = value that we want to remove
30+
a.remove('hello')
31+
a.remove(1)
32+
a.remove(2)
33+
34+
#pop parameter = index that we want to pop element from.
35+
x=a.pop(8)
36+
print(x)
37+
#it removes the element from the list and return it
38+
print(a)
39+
40+
#a.clear() clears whole list
41+
42+
#index
43+
44+
v=a.index('ritu',2)
45+
print(v)
46+
47+
#count
48+
49+
c=a.count(1)
50+
print(c)
51+
52+
# cant sort the list with mixed values of str and int by a.sort()
53+
a.reverse()
54+
print(a)
55+
56+
c=a.copy()
57+
print(c)
58+
z=[1,2,3,4,5,6]
59+
print(z[-6:-3])
60+
print(z[:])
61+
62+
63+
64+
65+
66+
67+

session2/WhileElse.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
i=int(input("Enter int: "))
2+
count =0
3+
while i<10:
4+
count=count+1
5+
print(count)
6+
i=i+2
7+
else:
8+
print("end while")

session2/break cont pass.py

Whitespace-only changes.

session2/check prime no.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
""" if the for loop is breaked by break than the else statement
2+
of for will not be executed"""
3+
"""for n in range (2,10):
4+
print('n=',n)
5+
for x in range(2,n):
6+
print('x=',x)
7+
if n%x==0:
8+
print(n,'equals',x,'*',n//x)
9+
break
10+
else:
11+
print(n,'is prime no')
12+
"""
13+
14+
x=int(input("enter inetger"))
15+
16+
if(x==2 or x==3):
17+
print("prime no.")
18+
else:
19+
for i in range(x):
20+
if(6*i+1 == x or 6*i-1 ==x):
21+
print("prime")
22+
break
23+
else :
24+
print("not prime")

session2/decision making.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
y=int(input("please enter int "))
2+
3+
if y<0 :
4+
y=0
5+
print("negative value")
6+
elif y==0 :
7+
print("zero")
8+
elif y==1 :
9+
print("single")
10+
else :
11+
print("more")
12+
13+
words=['cat','window','def']
14+
inte=[1,2,3]
15+
for w in words :
16+
for x in inte:
17+
print(w,len(w),x)
18+

session2/for practice.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
words=['cat','window','def']
2+
for w in words:
3+
print(w, len(w))
4+
5+
for w in words[:]:
6+
if len(w)>4:
7+
words.insert(0,w)
8+
9+
print(words)
10+
11+
12+
print(range(0,5))
13+
#this will print only range(0,5)
14+
# to print values we write
15+
16+
print(list(range(0,6)))
17+
18+
"""the else keyword in for loop specifies
19+
a block of code to be executed
20+
after the loop is finished"""
21+
for i in range(4):
22+
print(i)
23+
else:
24+
print("finally executed")
25+
26+

session2/leap year.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
2+
yr=int(input("enter year to be checked"))
3+
4+
if(yr%400==0 & yr%100==0 & yr %4==0):
5+
print("leap")
6+
7+
else :
8+
print("not leap")

0 commit comments

Comments
 (0)