-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path2nd assignment.txt
182 lines (111 loc) · 3.75 KB
/
2nd assignment.txt
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
NAME :: Tharun Kumar T
1. Verify if the given string is palindrome
12.check for palindrome
s= input('enter the string\n')
l = list(s)
l.reverse()
n1 = ''
for i in l:
n1 = n1 + i
if s == n1:
print('given value is palindrome\n')
else:
print('not a palindrome\n')
2. Find the highest ascii value in a given string
st = input()
n = max(s)
print(ord(n))
3. Print true if the given sentence ends with a "holiday"
sentence = input('enter the sentence\n')
if sentence.endswith('holiday'):
print('True')
4. Unicode value of a given string
name = input()
for i in name:
print(ord(i))
5. Given input string, add 4 spaces in the end and use full stop in the end.
s = input('enter the input string\n')
l = list(s)
l.extend([' ',' ',' ',' ','.'])
st = ''
for i in l:
st+=i
print(st)
6. Convert given string into list in one line: e.g., "hello world" output = ['h', 'e', 'l', 'l', 'o', ' ', 'w', 'o', 'r', 'l', 'd']
lis = list("hello world")
7. Find the sum of elements in the list e.g., [10,20,30] output = 60
l =[10,20,30]
print(sum(l))
8. Duplicate the elements in the list e.g', list1 = [1,2,3] output = [1,2,3,1,2,3]
list1 = [1,2,3]
print(list1*2)
9. Find the greater of two lists list1 = [1,2,3] and list2 = [1,2,3,4]
list1 = [1,2,3]
list2 = [1,2,3,4]
if list1>list2:
print('list1 is greater\n')
else:
print('list2 is greater\n')
10. What are help() and dir() used for?
11. Write a python program to print prime numbers between 100-300
def prime(num):
for i in range(2,(num//2)+1):
if num % i == 0:
return False
return True
def primenos(n1,n2):
for i in range(n1,n2+1):
if(prime(i)):
print(i)
primenos(100,300)
12. 1. Find the common elements between two lists without duplicates.
a = [1,2,3,2,4,5,6,7,8]
b=[1,2,4,10,11,12]
output: [1,2,4]
l=[]
for i in a:
if i in b:
l.append(i)
print(list(set(l)))
13. Find the word reverse of a given sentence.
input = "my name is virat kohli"
output = "kohli virat is name my"
lis = input.split()
lis.reverse()
for i in lis:
print(i,end=' ')
14. Join list a (as keys) and list b (as values) to make a dictionary in one line.
a = ["key1", "key2", "key3"]
b = [1,2,3]
15. Implement greatest of three numbers a=10,b=20,c=30 using ternary operator or regular if-else
a = int(input())
b = int(input())
c = int(input())
if a>b and a>c:
print('a is greatest\n')
elif b>c:
print('b is greatest\n')
else:
print('c is greatest\n')
16. Find int, bin, oct, hex of a number = 16
17. What is the difference between .py and .pyc file?
18. What is the difference between append and extend method?
19. Is the memory de-allocated once you exit Python console? How garbage collector works?
20. How is memory managed in Python?
21. Read capital letters and punctuations from a sentence.
22. join() and split() What is the difference
explain with a sentence example.
23. What does pass statement do in Python?
24. Write a program to check if a given year is leap year of not.
25. Write a program to check if the given pair of numbers form a right angled triangle.
(Hint: check a2 + b2 = c2)
26. Write a python program to count the number of bits set in an integer
e.g., a = 20 (bits: 0001 0100) answer 2 bits set.s
27. Write a program to check if the given number is a power of 2
28. Write a program to find the highest number in the given loop.
e.g., l1 = [10,33,45,56,78,98] output should be 98
print(max(l1))
29. Write a program to perform matrix multiplication
a = [[1,2,3],[4,5,6]] b = [[1,2,3],[4,5,6],[7,8,9]]
30. Write a program to reverse the bits in an integer
e.g., a = 0x12 (0001 0010) output = (1110 1101)