You are on page 1of 19

Python Programming

Class – XI (Computer Science Practical File (083) )

Q1. #Python program to input a welcome message and print it

message=input( " Enter welcome message: " )

print("Hello, ",message)

Output : Enter welcome message: hii

Hello, hii
Q2. # Python Program to input 2 numbers and display larger number

num1=int(input("Enter First Number"))

num2=int(input("Enter Second Number"))

if (num1>num2):

print("The Larger number is", num1)

else:

print ("The Larger number is", num2)

Output :

Enter First Number 45

Enter Second Number88

The Larger number is 88


Q3. Python Program to input 2 numbers and display Smaller number

num1=int(input("Enter First Number"))

num2=int(input("Enter Second Number"))

if (num1<num2):

print("The Smaller number is", num1)

else:

print ("The Smaller number is", num2)

Output :

Enter First Number 66

Enter Second Number99

The Smaller number is 66


Q4. Patterns using nested For Loop in Python

1. for i in range(1,10):

for j in range(i):

print('*',end='')

print('')

Output :

**

***

****

*****

******

*******

********

*********

2. #Number Pattern

for i in range(5,0,-1):
for j in range(1,i+1):
print(j,end="")
print()
Output :

12345

1234

123

12

3. Character pattern

for i in range(1,6):

A=65
for j in range(0,i):
print(chr(A),end="")
A=A+1
print()

Output :

AB

ABC

ABCD

ABCDE
Q5. Determine whether a number is a perfect number, an Armstrong number
or a palindrome.

n = int(input("Enter any number to check whether it is perfect ,armstrong or palindrome


: "))

sum = 0

# Check for perfect number

for i in range(1,n):

if n%i==0:

sum = sum + i

if sum == n :

print( n,"is perfect number")

else :

print( n, "is not perfect number")

#check for armstrong number

temp = n

total = 0

while temp > 0 :

digit = temp %10

total = total + (digit**3)

temp = temp//10

if n == total:

print( n,"is an armstrong number")


else :

print( n, "is not armstrong number")

#check for palindrome number

temp = n

rev = 0

while n > 0:

d = n % 10

rev = rev *10 + d

n = n//10

if temp == rev :

print( temp,"is palindrome number")

else :

print( temp, "is not palindrome number")

Output :

Enter any number to check whether it is perfect ,armstrong or palindrome : 28

28 is perfect number

28 is not armstrong number

28 is not palindrome number


Q 6. Input a number and check if the number is prime or composite number

n= int(input("Enter any number:"))

if(n ==0 or n == 1):

print(n,"Number is neither prime nor composite")

elif n>1 :

for i in range(2,n):

if(n%i == 0):

print(n,"is not prime but composite number")

break

else:

print(n,"number is prime but not composite number")

else :

print("Please enter positive number only ")

Output :

Enter any number:12

12 is not prime but composite number


Q7. Display the terms of a Fibonacci series

def fib(n):

a=0

b=1

if n == 1:

print(a)

else:

print(a)

print(b)

for i in range(2,n):

c=a+b

a=b

b=c

print(c)

fib(10)

Output :

0 1 1 2 3 5 8 13 21 34
Q8. (a) Write a program to input the value of x and n and print the sum of
the following series: ⮚

1+𝑥+𝑥2+𝑥3+𝑥4+⋯𝑥𝑛

print("write a program to input the value of xand n and print the sum of the \n")

print("following series:1+x+x^2+x^3+x^4+.....x^n")

x = int(input("Enter the value of x: "))

n = int(input("Enter the value of n: "))

s=0

for i in range(n+1):

k=x**i

s=s+k

print("The sum of the series is 1+x+x^2+x^3+x^4+....x^n\n")

print(" with x:",x,"and n:",n,"is=",s)

Output :

Enter the value of x: 2

Enter the value of n: 4

The sum of the series is 1+x+x^2+x^3+x^4+....x^n

with x: 2 and n: 4 is= 31


(b) 𝑥 + 𝑥 2 2! + 𝑥 3 3! + 𝑥 4 4! + ⋯ 𝑥 𝑛 𝑛!

𝑥 +𝑥2!+𝑥3!+𝑥4!+ ⋯𝑥n

x = int(input("Enter the value of x: "))

n = int(input("Enter the value of n: "))

sum = x

for i in range(2, n + 1) :

fact=1

for j in range(i,1,-1):

fact=fact*j

nextterm= (x**i)/fact

if i%2==0:

sum=sum+nextterm

else:

sum=sum-nextterm

print("the sum of given series is :",sum)

Output :

Enter the value of x: 4

Enter the value of n: 2

the sum of given series is : 12.0


Q9. Compute the greatest common divisor and least common multiple of two
integers.

n1 = int(input("Enter First number :"))

n2 = int(input("Enter Second number :"))

x = n1

y = n2

while(n2!=0):

t = n2

n2 = n1 % n2

n1 = t

gcd = n1

print("GCD of {0} and {1} = {2}".format(x,y,gcd))

lcm = (x*y)/gcd

print("LCM of {0} and {1} = {2}".format(x,y,lcm))

Output :

Enter First number :22

Enter Second number :55

GCD of 22 and 55 = 11

LCM of 22 and 55 = 110.0


Q10. Count and display the number of vowels, consonants, uppercase,
lowercase characters in string.

Str="GeeksForGeeks"

lower=0

upper=0

for i in Str:

if(i.islower()):

lower+=1

else:

upper+=1

print("The number of lowercase characters is:",lower)

print("The number of uppercase characters is:",upper)

Output :

The number of lowercase characters is: 10

The number of uppercase characters is: 3


Q11. Input a string and determine whether it is a palindrome or not; convert the case
of characters in a string

def isPalindrome(s):

return s == s[::-1]

# Driver code

s = "malayalam"

ans = isPalindrome(s)

if ans:

print("Yes")

else:

print("No")

Output : Yes
Q 12. Python program to find the largest/ smallest number in a list/tuple

# creating empty list

listn = []

# Input number of elements to put in list

num = int(input("Enter number of elements in list: "))

# iterating till num to append elements in list

for i in range(1, num + 1):

element= int(input("Enter elements: "))

listn.append(element)

# print Smallest element

print("Smallest element in List is:", min(listn))


print("Largest element in List is:", max(listn))

Output

Enter number of elements in list: 5

Enter elements: 56

Enter elements: 44

Enter elements: 33

Enter elements: 77
Q13. Input a list of numbers and swap elements at the even location with the
elements at the odd location.

def swapPositions(list, pos1, pos2):

list[pos1], list[pos2] = list[pos2], list[pos1]


return list

# Driver function
List = [23, 65, 19, 90]
pos1, pos2 = 1, 3

print(swapPositions(List, pos1-1, pos2-1))

Output : [19, 65, 23, 90]


Q14. Input a list/tuple of elements, search for a given element in the
list/tuple
mylist = []
print("Enter 5 elements for the list: ")
for i in range(5):
value = int(input())
mylist.append(value)
print("Enter an element to be search: ")
element = int(input())
for i in range(5):
if element == mylist[i]:
print("\nElement found at Index:", i)
print("Element found at Position:", i+1)

Output:

Enter 5 elements for the list:


33
45
67
78
55
Enter an element to be search:
67

Element found at Index: 2


Element found at Position: 3
Q 15. Create a dictionary with the roll number, name and marks of n
students in a class and display the names of students who have marks above
75.
no_of_std = int(input("Enter number of students: "))
result = {}
for i in range(no_of_std):
print("Enter Details of student No.", i+1)
roll_no = int(input("Roll No: "))
std_name = input("Student Name: ")
marks = int(input("Marks: "))
result[roll_no] = [std_name, marks]
print(result)
# Display names of students who have got marks more than 75
for student in result:
if result[student][1] > 75:
print("Student's name who get more than 75 marks
is/are",(result[student][0]))

Output :
Enter number of students: 5
Enter Details of student No. 1
Roll No: 1
Student Name: riya
Marks: 55
Enter Details of student No. 2
Roll No: 2
Student Name: rahul
Marks: 88
Enter Details of student No. 3
Roll No: 3
Student Name: mohit
Marks: 76
Enter Details of student No. 4
Roll No: 4
Student Name: sneha
Marks: 65
Enter Details of student No. 5
Roll No: 5
Student Name: neha
Marks: 90
{1: ['riya', 55], 2: ['rahul', 88], 3: ['mohit', 76], 4: ['sneha', 65], 5: ['neha', 90]}
Student's name who get more than 75 marks is/are rahul
Student's name who get more than 75 marks is/are mohit
Student's name who get more than 75 marks is/are neha

You might also like