Enter Your Name Nishigandha Enter Your Age 22 Nishigandha You Will Turn 100 Years Old in 2098
Enter Your Name Nishigandha Enter Your Age 22 Nishigandha You Will Turn 100 Years Old in 2098
Print out a
message addressed to them that tells them the year that they will turn 100 years old
Output :-
2. Write a program to check whether the number is even or odd, print out an
appropriate message to the user
Output:-
Enter Number 8
Number is even
Enter Number 5
Number is odd
3. Write a program which will find all such numbers which are divisible by 7.
n = []
for i in range(1,70):
if (i%7==0):
n.append(i)
print(i)
Output:-
7
14
21
28
35
42
49
56
63
4.Write a program which can compute the factorial of a given numbers.
Output:-
Enter number 5
factorial of 5 is 120
5. Write a program that prints out all the elements of the list that are less than 10.
a = [2,4,5,67,8,45,20,23,11,5,3]
b = []
for b in a:
if b<10:
print(b)
Output:-
2
4
5
8
5
3
6. Write a program that returns a list that contains only the elements that are
common between the lists (without duplicates). Make sure your program works on
two lists of different sizes.
a = [1,2,3,4,5,4]
b = [1,2,4,7,8,9,4]
mylist = []
for i in a:
if i in b:
mylist.append(i)
mylist = list(dict.fromkeys(mylist))
print(mylist)
Output:-
[1, 2, 4]
7. To determine whether the number is prime or not.
if num > 1:
for i in range(2,num):
if (num % i) == 0:
print(num,"is not a prime number")
break
else:
print(num,"is a prime number")
else:
print(num,"is not a prime number")
Output:-
Enter a number: 85
85 is not a prime number
Enter a number: 10
10 is not a prime number
Without recursion:-
n=int(input("Enter number:"))
temp=n
rev=0
while(n>0):
dig=n%10
rev=rev*10+dig
n=n//10
if(temp==rev):
print("The number is a palindrome!")
else:
print("The number isn't a palindrome!")
Output:-
Enter number:121
The number is a palindrome!
Enter number:123
The number is not a palindrome!
n = int(input("Enter number"))
temp = rev(n, 0);
if (temp != n):
print("yes its palindrome");
else:
print("its not palindrome");
output:-
Enter number101
yes its palindrome
9. Write a program that asks the user how many Fibonnaci numbers to generate and
then generates them
return a + b
fibonacci_list = []
for n in range(number):
if n in [0, 1]:
fibonacci_list += [1]
else:
fibonacci_list += [fib(fibonacci_list[n-1], fibonacci_list[n-2])]
Output:-
10.Write a program (using functions!) that asks the user for a long string containing
multiple words. Print back to the user the same string, except with the words in
backwards order. E.g “ I am Msc student” is :”student Msc am I”
def stringReverse(string):
list1 = string.split(' ')
list1.reverse()
list1 = ' '.join(list1)
return list1
11.Write a program to implement binary search to search the given element using
function.
12.Given a .txt file that has a list of a bunch of names, count how manyof each name
there are in the file, and print out the results to the screen.
%%writefile sample.txt
abc
xyz
abc
pqr
Uwx
d = dict()
for line in text:
line = line.strip()
line = line.lower()
words = line.split(" ")
if word in d:
# Increment count of word by 1
d[word] = d[word] + 1
else:
d[word] = 1
abc : 2
xyz : 1
pqr : 1
uwx : 1
13.Write a program that takes a list of numbers (for example, a = [5, 10, 15, 20,
25])and makes a new list of only the first and last elements of the given list.
a = [5,10,15,20,25]
b = [a[0],a[-1]]
print(b)
Output:-
[5, 25]
14.Write a program that accepts sequence of lines as input and prints the lines after
making all characters in the sentence capitalized.
lines = []
while True:
s = input("Enter Lines :")
if s:
lines.append(s.upper())
else:
break
for sentence in lines:
print(sentence)
Output:-
15.Write a program that accepts a sentence and calculate the number of letters and
digits
16.Write a program that accepts a sentence and calculate the number of upper case
letters and lower case letters.
ab = input("Enter string:")
upper= lower= 0
for u in ab:
if u.isupper():
upper=upper+1
elif u.islower():
lower=lower+1
else:
pass
print("Upper letters : ", upper)
print("Lower letters : ", lower)
Output:-
def fact(n):
if n==0:
return 1
else:
return n*fact(n-1)
n = int(input("Enter the number"))
print(fact(n))
Output:-
def list_sum(list):
total=0
for i in list:
if type(i) == type([]):
total = total + list_sum(i)
else:
total = total + i
return total
print(list_sum([1,3,5]))
Output:-
9
return a + b
fibonacci_list = []
for n in range(number):
if n in [0, 1]:
fibonacci_list += [1]
else:
fibonacci_list += [fib(fibonacci_list[n-1], fibonacci_list[n-2])]
Output:-
def sumDigits(n):
if n == 0:
return 0
else:
return n % 10 + sumDigits(int(n / 10))
n = int(input("Enter your number: "))
print("sum of number of digits {0} is ".format(n),sumDigits(n))
Output:-
21.Write a Python program to find the greatest common divisor (gcd) of two integers
22.Write a Python function that takes a list and returns a new list with unique
elements of the first list.
def unique_list(l):
x = []
for a in l:
if a not in x:
x.append(a)
return x
print(unique_list([1,2,3,3,3,3,4,5]))
Output:-
[1, 2, 3, 4, 5]
def perfect_number(n):
sum = 0
for x in range(1, n):
if n % x == 0:
sum += x
return sum == n
n = int(input("Enter your number "))
if perfect_number(n):
print("{0} is perfect number" .format(n))
else:
print("{0} is not perfect number".format(n))
Output:-
24.Write a Python program to read a file line by line store it into an array.
Output:-
['this is the first line \n', 'and this is second line']
file = open("test.txt","r")
count = 0
content = file.read()
count_list = content.split("\n")
for i in count_list:
if i:
count+=1
print("Number of lines in file")
print(count)
file.close()
Output:-
file.close()
Output:-
one : 3
two : 2
three : 2
four : 1
five : 1
six : 1
ten : 1
seven : 1
Output:
Copy content successfully..!
File2.txt: -
import random
file = open("test.txt", "r")
lines = file.readlines()
print(random.choice(lines))
file.close()
Output:-
class py_solution:
def reverse_words(self, s):
return ' '.join(reversed(s.split()))
print(py_solution().reverse_words('hello .py'))
Output:-
.py hello
30.Write a Python class named Rectangle constructed by a length and width and a
method which will compute the area and perimeter of a rectangle. –
class rectangle():
def __init__(self,l,w):
self.length=l
self.width=w
def rectangle_area(self):
return self.length*self.width
newRectangle = rectangle(5, 5)
print(newRectangle.rectangle_area())
Output:-
25
31.Write a Python class named Circle constructed by a radius and two methods
which will compute the area and the perimeter of a circle
class Circle():
def __init__(self, r):
self.radius = r
def area(self):
return self.radius**2*3.14
def perimeter(self):
return 2*self.radius*3.14
NewCircle = Circle(5)
print(NewCircle.area())
print(NewCircle.perimeter())
Output:-