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

Practic 05.py

The document contains Python code snippets demonstrating various programming concepts like loops, conditional statements, functions, lists and strings. Some examples include printing patterns using loops, calculating sum of series, finding greatest of two numbers, palindrome checker, uppercase to lowercase conversion and vice versa, finding max/min of lists, swapping list elements and searching lists.

Uploaded by

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

Practic 05.py

The document contains Python code snippets demonstrating various programming concepts like loops, conditional statements, functions, lists and strings. Some examples include printing patterns using loops, calculating sum of series, finding greatest of two numbers, palindrome checker, uppercase to lowercase conversion and vice versa, finding max/min of lists, swapping list elements and searching lists.

Uploaded by

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

1 # 1

2 # 23
3 # 456
4 # 78910  
5 k = 1
6 for i in range(1,5):
7    for j in range(1,i+1):
8        print(k,end="")
9        k = k +1
10    print()
11
12
13 # 1
14 # 13
15 # 135
16 # 1357
17 for i in range(1,9,2):
18    for j in range(1,i+1,2):
19        print(j,end="")
20    print()
21
22
23 # A
24 # AC
25 # ACE
26 # ACEG
27 for i in range(65,72,2):
28    for j in range(65,i+1,2):
29        print(chr(j),end="")
30    
31    print()
32
33
34 # 1111
35 # 222
36 # 33
37 # 4
38 for i in range(1,5):
39    for j in range (4,i-1,-1):
40        print(i,end="")
41    print()
42
43
44 # 1234
45 # 123
46 # 12
47 # 1
48 for i in range(5,1,-1):
49    for j in range (1,i):
50        print(j,end="")
51    print()
52
53
54 # ****
55 # ***
56 # **
57 # *
58 for g in range(4,0,-1):
59    for h in range(1,g+1):
60        
61        print('*',end="")
62    print()
63
64
65
66 msg = input("Enter welcome message: ")
67 print(msg)
68
69
70 # find gratest of two number.
71 n1 = int(input("Enter num1: "))
72 n2 = int(input("Enter num2: "))
73 if n1>n2:
74    print("num1=",n1,"is gratest")
75 elif n2>n1:
76    print("num2=",n2,"is gratest")
77 else:
78    print("num1 & num2 are equal",n1,'=',n2)
79
80
81
82
83
84 #pattern 1
85 for i in range(4):
86    print("*"* i)
87
88 #pattern 2
89 for i in range(1,5):
90    for j in range (1,i+1):
91        print(j,end="")
92    print()
93
94
95
96    
97 # 1+ x^1 + x^2 + x^3.....
98 import math
99 x = int(input('Enter value of x : '))
100 n = int (input('Enter value of n : '))
101 sum = 0
102 for i in range(n+1):
103    sum += pow(x,i)
104 print(sum)
105
106 # sum of seris 2
107 import math
108 x = int(input('Enter value of x : '))
109 n = int (input('Enter value of n : '))
110 sum = 0
111 for i in range(n+1):
112    if i%2 == 0:
113        sum += pow(x,i)
114    if i%2 != 0:
115        sum -= pow(x,i)
116 print('Sum of series= ',sum)
117
118
119 #serese 3
120 import math
121 x = int(input('Enter value of x : '))
122 n = int (input('Enter value of n : '))
123 sum = 0
124 for i in range(1,n+1):
125    if i%2 == 0:
126        sum -= pow(x,i)/i
127    if i%2 != 0:
128        sum += pow(x,i)/i
129 print('Sum of series= ',sum)
130
131
132 # fabonici
133 a,b,c = 1,0,1
134 n = int (input('Enter value of n : '))
135 print(b,c,end=" ")
136 for i in range(n-2):
137    a = b
138    b = c
139    c = a+b
140    print(c ,end=' ')
141
142 # lcm and hcf
143 n1 = int(input('Enter num1: '))
144 n2 = int(input('Enter num2: '))
145 a= []
146 if n1>n2:
147    n = n1
148 elif n2>n1:
149    n = n2
150 else :
151    n = n1
152 for i in range(1,n):
153    if n2%i ==0 and n1%i == 0:
154        a.append(i)
155 hcf = max(a)
156 print('highest common divisor of ',n1,'and',n2 ,'is', hcf)
157 lcm = (n1*n2)//hcf
158 print('lowest common multiple of ',n1,'and',n2 ,'is', lcm)
159
160
161 l=list(eval (input("Enter a list:")))
162
163
164 str = input()
165 vov = ''
166 con =''
167 Lc = ''
168 Uc = ''
169 for i in range(len(str)):
170    if str[i] in 'aeiouAEIOU':
171        vov += str[i]
172    else:
173        con += str[i]
174    if str[i].isupper() :
175        Uc+= str[i]
176    if str[i].islower() :
177        Lc+= str[i]
178 print('Vovels :',len(vov))
179 print('Consunants',len(con))
180 print('Upper case',len(Uc))
181 print('Lower case',len(Lc))
182
183 str = input("Enter string: ")
184 rev = str[::-1]
185 if str.lower()==rev.lower():
186    print("string is pallindrome")
187 else:
188    print("string is not a pallinndrome")
189
190 c = ''
191 for i in range(len(str)):
192    if str[i].isupper():
193        c+= str[i].lower()
194    else :
195        c += str[i].upper()
196 print(c)
197
198 #Find max & min number in a list
199 l = [5,4,36,58,98]
200 print('largest number of list=',l,'is',max(l))
201 print('smallest number of list=',l,'is',min(l))
202
203
204 #swap position of elements in a list
205 l = list(eval(input("Enter a list:")))
206 print("Old list :",l)
207 if len(l)%2==0:
208    x=len(l)
209 else:
210    x=len(l)-1
211 for i in range(0,x,2):
212    l[i],l[i+1]=l[i+1],l[i]
213 print("The new list is: ",l)
214 #OR
215
216 l=list(eval (input("Enter a list of no. :")))
217 print("Old list :",l)
218
219 for i in range(len(l)-1):
220    if i%2 == 0 :
221        l[i],l[i+1]=l[i+1],l[i]
222 print('New list: ',l)
223
224    
225
226 #To find positon of element in a list.
227 l = list(eval(input("Enter a list:")))
228 s= int(input("Enter element to search for: "))
229 dx = []
230 for i in range(len(l)):
231    if s == l[i]:
232        dx.append(i)
233 if len(dx)==0:
234    print("Element not found in list")
235 print('Element found at the following index: ',end="")
236 for j in dx:
237    print(j,end=",")
238    
239
240 l = list(eval(input("Enter a list of number:")))
241 print('largest number of list=',l,'is',max(l))
242 print('smallest number of list=',l,'is',min(l))
243
244
245
246

You might also like