Q1. Write a python program to find square of given number using list comprehension.
=[1,2,3,4,5,6,7]
data= [i*i for i in data]
result print(result)
Q2. Write a python program which will find all such numbers which are divisible by 3 and not by 7 within given range m to n.
= int(input("Enter The Start Number (m)"))
start_num = int(input("Enter The End Number (n)"))
end_num = start_num
cnt while cnt <= end_num:
# if number divisible by 3 and not by y
if cnt % 3 == 0 and cnt % 7 != 0:
print(cnt, " is divisible by 3 and not by 7.")
+= 1 cnt
Enter The Start Number (m)3
Enter The End Number (n)30
3 is divisible by 3 and not by 7.
6 is divisible by 3 and not by 7.
9 is divisible by 3 and not by 7.
12 is divisible by 3 and not by 7.
15 is divisible by 3 and not by 7.
18 is divisible by 3 and not by 7.
24 is divisible by 3 and not by 7.
27 is divisible by 3 and not by 7.
30 is divisible by 3 and not by 7.
Q2. Write a python program to reverse each word of file and also count total lines.
=0
num_lineswith open("test.txt", "r") as f:
for line in f:
+= 1
num_lines with open("test.txt", "r") as f:
= f.read()
data for line in f:
+= 1
num_lines = ""
new_data for word in data.split():
= word[::-1]
rev_word += rev_word
new_data += " "
new_data print(new_data)
print("Number of lines:")
print(num_lines)
luhaR enawanoS rebmevoN yluJ yaM hcraM nuJ laT ksN luhaR tsiD kihsaN luhaR
Number of lines:
5
0 Comments:
Post a Comment