practice.py
4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
#swap the elements in list, string, tuple, set and dictionary
# l1 = [2,3,4,5,6,7]
# l1[0] = l1[3]
# # print(l1)
# s = "this is great"
# # s = s.split()
# for i in range(3):
# for j in range(3):
# s[i] = s[j]
# print(s)
# s = {3,4,5,6}
# s = (3,4,5) #in tiple indexing is possible but not in sets
# print(s[1])
#string rotation
# s = "this"
# n = int(len(s)/2)
# print(s[n:]+s[:n])
# removing digits from string
# s = "alpha23"
# s1 = ""
# l = []
# for i in s:
# if i.isnumeric():
# l.append(i)
# else:
# s1+=i
# print(s1)
# example of picking
# import pickle
# with open("piko.txt",'bw') as file:
# k = "this is my brain!"
# pickle.dump(k,file)
# l = [34,56,12,21,23]
# for i in range(len(l)):
# for j in range(len(l)):
# if l[i]>l[j]:
# l[i],l[j] = l[j],l[i]
# print(l)
# l1 = [4,5,6,7]
# l2 = [8,9,2,1]
# l4 = list(zip(l1,l2))
# print(l4)
# l3 = [i+j for i,j in zip(l1,l2)]
# print(l3)
#star pattern
# for i in range(5):
# print(" "*(4-i),"*"*(i*2+1),end = " ")
# print()
# remove duplicacy from string
# s = "thisismypen"
# s1 = ""
# l = ""
# for i in s:
# if i not in l:
# l+=i
# else:
# s1+=i
# print(s1,l)
# vowel detection
# v = "aeiou"
# w = ""
# n_w = ""
# s = "thisisgreat"
# for i in s:
# if i in v:
# n_w+=i
# else:
# w+=i
# print(w,n_w)
# odd even characters in a string
# s = "this is my car"
# for i in s:
# if i == "i":
# s=s.replace(i,"new")
# print(s)
# replacement in a list
# l = [3,4,5,6,7]
# for i in range(len(l)):
# if l[i] == 3:
# l[i] = 67
# print(l)
# detecting the index number of a element
# l = [5,6,7,8]
# for i in l:
# if i == 7:
# print(l.index(i))
# d = {"saroj":1,"saroj":2}
# print(d)
# count particular element in a list
# l = [3,4,5,6,3,4,5,3,3,33,3]
# count = 0
# for i in range(len(l)):
# if l[i] == 3:
# l[i] = 6789
# print(l)
#reverse a list
# l = [4,5,6,7,8,9]
# for i in range(len(l)):
# for j in range(len(l)):
# if l[i]>l[j]:
# l[i],l[j]=l[j],l[i]
# print(l)
# year = int(input("enter the year: "))
# if (year%4==0 and year%400==0) or year%100 !=0:
# print("yes year is leap year")
# else:
# print("year is not leap year")
# find the index number in a list
# l = [5,6,7,3,2]
# def finding_index_number(l,n):
# for i in l:
# if i == n:
# print(l.index(i))
# finding_index_number(l,7)
#check the number is square or not
# n = 4
# import math
# print(math.isqrt(n))
#decorators
# def decorators(function):
# def inside_function(string):
# function(string.upper())
# return inside_function
# @decorators
# def apply_dec(string):
# print(string[::-1])
# apply_dec("saroj")
# raising exception
# try:
# a = 3
# b = 0
# if b == 0:
# raise Exception("not divisible")
# except Exception as e:
# print(e)
# finally:
# print("code is fully execited")
# # odd and even numbers
# even = [i for i in range(10) if i%2==0]
# odd = [i for i in range(10) if i%2!=0]
# print(even)
# print(odd)
# x = lambda w : w**2
# print(x(3))
# l = [5,6,7,3,1,423,1]
# min = l[0]
# max = l[0]
# for i in range(len(l)):
# if l[i]> max:
# max = l[i]
# if l[i]<min:
# min = l[i]
# print(min,max)
# k = 10
# l = [6,4,6,4,8,2,10,0,7,3,11,-1]
# for i in l:
# for j in l:
# if l[i]+l[j]==k:
# print(l[i],l[j])
# make a dictionary of two lists
# l1 = [4,5,6,7]
# l2 = [8,9,7,6]
# d = dict(zip(l1,l2))
# print(d)
# def factorial(n):
# if n<=1:
# return 1
# else:
# return n*factorial(n-1)
# print(factorial(5))
#printing fibbonaci series
# def febbonaci_series(n):
# if n<=1:
# return 1
# else:
# return febbonaci_series(n-1) + febbonaci_series(n-2)
# for i in range(10):
# print(febbonaci_series(i))
# # genrator
# def genrator():
# for i in range(1,10):
# yield i
# g = genrator()
# print(g.__next__())
# import os
# l = ["sar","sa"]
# s = os.path.commonprefix(l)
# print(s)