0% found this document useful (0 votes)
13 views19 pages

Python Assignment 3

The document contains a series of Python programming exercises demonstrating various concepts such as loops, functions, and conditionals. Each example includes code snippets that perform tasks like calculating squares, checking divisibility, summing numbers, and generating multiplication tables. The exercises illustrate fundamental programming techniques and mathematical operations.

Uploaded by

shifa1795sahota
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views19 pages

Python Assignment 3

The document contains a series of Python programming exercises demonstrating various concepts such as loops, functions, and conditionals. Each example includes code snippets that perform tasks like calculating squares, checking divisibility, summing numbers, and generating multiplication tables. The exercises illustrate fundamental programming techniques and mathematical operations.

Uploaded by

shifa1795sahota
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 19

ASSIGNMENT -3

EX:1
>>> import math
>>> i=1
>>> while i<=10:
... print(i*i)
... i=i+1
...
1
4
9
16
25
36
49
64
81
100
>>>
EX:2
>>> def multi(n):
... if n%5==0:
... print(n,"is multiple of 5")
... else:
... print(n,"is not multiple of 5")
...
>>> multi(10)
10 is multiple of 5
>>> multi(7)
7 is not multiple of 5
>>>
EX:3
>>> def f(x):
... if x%2==0:
... if x%3==0:
... if x%5==0:
... print(x,"is divisible by 2,3 and 5")
...
>>> f(60)
60 is divisible by 2,3 and 5
>>>
EX:5
>>> for i in range(20,31):
... print(i*i)
...
400
441
484
529
576
625
676
729
784
841
900
>>>
EX:6
>>> def f(x):
... if x%2==0 or x%3==0 or x%5==0:
... print(x,"is divisible by 2 or 3 or 5")
... else:
... print(x,"is not divisible by 2 or 3 or 5")
...
>>> f(44)
44 is divisible by 2 or 3 or 5
>>> f(23)
23 is not divisible by 2 or 3 or 5
>>>
EX:7
>>> sum=0
>>> n=1
>>> while n<=30:
... sum=sum+n
... n=n+1
...
>>> print("sum is=",sum)
sum is= 465
>>> sum=0
>>> n=1
>>> while n<=30:
... sum=sum+n
... n=n+1
... print("sum is=",sum)
...
sum is= 1
sum is= 3
sum is= 6
sum is= 10
sum is= 15
sum is= 21
sum is= 28
sum is= 36
sum is= 45
sum is= 55
sum is= 66
sum is= 78
sum is= 91
sum is= 105
sum is= 120
sum is= 136
sum is= 153
sum is= 171
sum is= 190
sum is= 210
sum is= 231
sum is= 253
sum is= 276
sum is= 300
sum is= 325
sum is= 351
sum is= 378
sum is= 406
sum is= 435
sum is= 465
EX:8
>>> i=1
>>> while i<=20:
... if i%2!=0:
... print(i*i)
... i=i+1
...
1
9
25
49
81
121
169
225
289
361
>>>
EX:10
>>> for i in range(21,50):
... print(i*i)
...
441
484
529
576
625
676
729
784
841
900
961
1024
1089
1156
1225
1296
1369
1444
1521
1600
1681
1764
1849
1936
2025
2116
2209
2304
2401
EX:11
>>> for x in range(1,151):
... if math.gcd(x,111)==1:
... print(x)
...
1
2
4
5
7
8
10
11
13
14
16
17
19
20
22
23
25
26
28
29
31
32
34
35
38
40
41
43
44
46
47
49
50
52
53
55
56
58
59
61
62
64
65
67
68
70
71
73
76
77
79
80
82
83
85
86
88
89
91
92
94
95
97
98
100
101
103
104
106
107
109
110
112
113
115
116
118
119
121
122
124
125
127
128
130
131
133
134
136
137
139
140
142
143
145
146
149
>>>
EX:12
>>> for x in range(1,201):
... if x%7==0:
... print(x)
...
7
14
21
28
35
42
49
56
63
70
77
84
91
98
105
112
119
126
133
140
147
154
161
168
175
182
189
196
EX:13
>>> for i in range(1,51):
... print(i*i*i)
...
1
8
27
64
125
216
343
512
729
1000
1331
1728
2197
2744
3375
4096
4913
5832
6859
8000
9261
10648
12167
13824
15625
17576
19683
21952
24389
27000
29791
32768
35937
39304
42875
46656
50653
54872
59319
64000
68921
74088
79507
85184
91125
97336
103823
110592
117649
125000
EX:14
>>> a,b=1,1
>>> while a<=60:
... print(a,end=',')
... a,b=b,a+b
...
1,1,2,3,5,8,13,21,34,55,
EX:15
>>> for n in range(25,51):
... if n%2!=0:
... print(n)
...
25
27
29
31
33
35
37
39
41
43
45
47
49
EX:17
>>> sum=0
>>> for i in range(50,101):
... sum=sum+i
...
>>> average=sum/50
>>> print("average is",average)
average is 76.5
EX:18
n>>> for i in ['Ronak','Shifa','Vivek','Hazel','Ashish']:
... print('Hello',i,'Do You Learn Python')
...
Hello Ronak Do You Learn Python
Hello Shifa Do You Learn Python
Hello Vivek Do You Learn Python
Hello Hazel Do You Learn Python
Hello Ashish Do You Learn Python
Ex:19
>>> x=range(1,11)
>>> for n in x:
... print(n)
...
1
2
3
4
5
6
7
8
9
10
EX:20
>>> def multitable(n):
... i=1
... while i<=10:
... print(n,'x',i,'=',i*n)
... i=i+1
...
>>> multitable(2)
2x1=2
2x2=4
2x3=6
2x4=8
2 x 5 = 10
2 x 6 = 12
2 x 7 = 14
2 x 8 = 16
2 x 9 = 18
2 x 10 = 20
>>> multitable(6)
6x1=6
6 x 2 = 12
6 x 3 = 18
6 x 4 = 24
6 x 5 = 30
6 x 6 = 36
6 x 7 = 42
6 x 8 = 48
6 x 9 = 54
6 x 10 = 60
EX:21
>>> def f(x):
... if x==0:
... print('x is 0')
... elif(x%2==0):
... print('x is even')
... else:
... print('x is odd')
...
>>> f(0)
x is 0
>>> f(4)
x is even
>>> f(33)
x is odd
EX:22
>>> def f(n):
... i=1
... while i<=10:
... print(i)
... i=i+1
... print(math.sqrt(i))
...
>>> f(10)
1
1.4142135623730951
2
1.7320508075688772
3
2.0
4
2.23606797749979
5
2.449489742783178
6
2.6457513110645907
7
2.8284271247461903
8
3.0
9
3.1622776601683795
10
3.3166247903554

You might also like