0% found this document useful (0 votes)
29 views23 pages

Class 11 Practical Term 1 AND 2

The document contains a series of Python programs that demonstrate various programming concepts such as divisibility checks, odd/even number identification, arithmetic operations, sorting, and string manipulation. It includes examples of loops, conditionals, functions, and data structures like lists and dictionaries. Each program is designed to perform a specific task, showcasing fundamental programming skills and techniques.
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)
29 views23 pages

Class 11 Practical Term 1 AND 2

The document contains a series of Python programs that demonstrate various programming concepts such as divisibility checks, odd/even number identification, arithmetic operations, sorting, and string manipulation. It includes examples of loops, conditionals, functions, and data structures like lists and dictionaries. Each program is designed to perform a specific task, showcasing fundamental programming skills and techniques.
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/ 23

1. Program to test the divisibility of a number with another number (i.e.

, if a
number is divisible by another number).
number1 = int( input( "Enter first number : ") )
number2 = int( input ( "Enter second number : ") )
remainder = number1% number2
if remainder ==0:
print (number1, "is divisible by" , number2)
else :
print (number1, "is not divisible by" , number2)
2. Program that takes a number and checks whether the given number is odd or
even.
Program:
num = int (input( "Enter an integer:"))
if num %2 ==0 :
print ( num, "is even number . " )
else:
print (num, "is odd number ." )

Enter an integer : 8
8 is EVEN number.

3. Program that reads to two numbers and an arithmetic operator and displays
the computed result.

num1 = float ( input ( "Enter first number : " ) )

num2 = float (input ("Enter second number : " ) )

op = input ( " Enter operator [ + - * / % ] : ")

result = 0

if op == "+":

result = num1 + num2

elif op == "-" :

result = num1 - num2

elif op =="*" :
result = num1 * num2

elif op == "/" :

result = num1 / num2

elif op == "%" :

result = num1 % num2

else :

print ( "invalid operator ! !" )

print ( num1, op, num2, "=", result)

4 . Program that reads three numbers ( integers) and prints them in an ascending
order.

x = int (input ( “Enter first number : “) )

y = int (input ( "Enter second number :" ) )

z = int( input ( "Enter third number :" ) )

min = mid = max = none

if x < y and x< z :

if y < z :

min, mid, max = x, y, z

else :

min , mid , max , = x, z ,y

elif y < x and y < z :

if x < z :

min, mid, max = y, x, z

else :

min, mid, max = y, z, x

else :
if x < y :

min, mid, max = z, x, y

else :

min, mid, max = z, y,x

print ( “Number in ascending order : “ , min, mid, max )

5.Program to calculate and print the sums of even and odd integers of the first n
natural numbers.
n = int( input ( “ Up to which natural number ? “ ) )
ctr = 1
sum _ even = sum _ odd = 0
while ctr< = n :
if ctr % 2 == 0 :
sum _even + = ctr #number is even
else :
sum _ odd + = ctr
ctr + = 1
print ( “ The sum of even integers is” , sum _ even )
print ( “ The sum of odd integers is “ , sum – odd )

6.Program to print whether a given character is an uppercase or a lowercase


character or a digit or any other character.

ch = input ( “ Enter a single character : “ )

if ch > =’ A ‘ and ch<=‘Z’ :

print ( “ You entered an uppercase character . “ )

elif ch> = “a” and ch< = “z”:

print (“You entered a lowercase character”)

elif ch> = ‘0’ and ch<= ‘9’ :

print ( “ you entered a digit. “ )

else :

print ( “ You entered a special character . “)


7.Program to print a table of a number , say 5 .
num = int(input(“enter any number”))
for a in range ( 1, 11 ) :
print ( num, ‘x’ , a , ‘=’ , num * a)
8.Program to print sum of natural numbers between 1 to 7 . Print the sum
progressively, i.e ., after adding each natural number, print sum so far .
sum = 0
for n in range ( 1,8) :
sum += n
print ( " sum of natural numbers < =" , n, "is" ,sum )

9. Program to calculate the factorial of a number.

num= int ( input ( "Enter a number : ") )

fact = 1

a=1

while a <=num :

fact *=a #fact = fact * a

a+=1 #a = a+ 1

print ( " The factorial of" , num, " is" , fact)

10. Program to illustrate the difference between break and continue statements.

print ( “ The loop with ‘ break ‘ produces output as : “ )


for i in range ( 1 , 11 ) :
if i% 3 == 0 :
break
else :
print (i)
print ( “ The loop with ‘ continue ‘ produces output as : “ )
for i in range ( 1, 11 ) :
if i % 3 ==0 :
continue
else :
print (i)
11. Program to input a number and test if it is a prime number or not.
num = int ( input ( "Enter number : ") )
lim = int (( num / 2 ) + 1)
for i in range (2,lim+1) :
rem = num % i
if rem == 0:
print ( num," is not a prime number")
break
else :
print ( num, "is a prime number" )

12.Write a python script to print this pattern


*
**
***
****
for i in range (1 , 6) :
for j in range ( 1 , i ) :
print ( “*” , end = ‘ ‘ )
print ( ) # to cause printing from next line.

13.Write a python script to print this pattern.

***
***
***

for a in range (3) :

for b in range (5 , 8 ) :

print ( “*” , end = ‘ ‘ )

print ( )

14. Write a Python script to print Fibonacci series of first 20 elements. Some initial
elements of a Fibonacci series are : 0, 1, 1, 2, 3, 5, 8….

first=0
second=1

print(first)

print(second)

for a in range (1,19):

third=first+second

print(third)

first,second=second,third

15. Write a Python script to read an integer > 1000 and reverse the number.

num=int(input("enter a 4 digit number:"))

tnum=num

reverse=0

whiletnum !=0:

digit=tnum%10

tnum=int(tnum/10)

reverse=reverse*10+digit

print("Reverse of",num,"is",reverse)

output: enter a 3 digit number:1234

Reverse of 1234 is 4321


16. Write a program to check if a given number is an Armstrong number or not.

num=int(input("enter a 3 digit number:"))

summ=0

temp=num

while(temp>0):

digit=temp%10

summ+=digit**3

temp//=10

if(num==summ):

print(num,"is an armstrog number")

else:

print(num,"is not an armstrog number")

output:

enter a 3 digit number:407

407 is an armstrong number

17.Write a program that reads a string and checks whether it is a palindrome string
or not using string slice.

Solutions :

18. Write a program to input two integers x and n, compute xn using a loop.

x=int(input("enter a positive number(x):"))


n=int(input("enter the power(n):"))

power=1

for a in range(n):

power=power*x

print(x,"to the power",n,"is",power)

19. Write a program to input a number and calculate its double factorial.

num=int(input("enter a positive number:"))

fac=1

for i in range(num,0,-2):

fac*=i

print(num,"!! is:",fac)

( for an even integer n , the double factorial is the product of all even positive
integers less than or equal to n. For an odd integers p, the double factorial is the
product of all odd positive integers less than or equal to p. )

Output:

enter a positive number:6

48

=== RESTART:
C:/Users/GOD/AppData/Local/Programs/Python/Python312/pattern.py ===

enter a positive number:7

105

20. Write Python script to print following pattern :

13
135

1357

for a in range(3,10,2):

for b in range(1,a,2):

print(b,end=” ”)

print()

21: Write a Python script that traverses through an input string and prints its
characters in different lines — two characters per line.

str = input("Enter the string: ")

length = len(str)

for a in range(0, length, 2):

print(str[a:a+2])

output:

Enter the string: computer

co

mp

ut

er

Q22: Write a program to count the number of times a character occurs in the given
string.

Solution

str = input("Enter the string: ")

ch = input("Enter the character to count: ");

c = str.count(ch)

print(ch, "occurs", c, "times")

output:
Enter the string: python

Enter the character to count: o

o occurs 1 times

Q23:write a program that replaces all the vowels in the string with “*”.

str = input("Enter the string: ")

newStr = ""

forch in str :

lch = ch.lower()

if lch == 'a' \

or lch == 'e' \

or lch == 'i' \

or lch == 'o' \

or lch == 'u' :

newStr += '*'

else :

newStr += ch
print(newStr)

output:Enter the string: computerscience

c*mp*t*rsc**nc*

24:Write a program which reverses a string and stores the


reversed string in a new string.
Solution:

str = input("Enter the string: ")

newStr = ""

for ch in str :

newStr= ch+newStr

print(newStr)

output:Enter the string: pythonprogram

margorpnohtyp

25:WAP to increment the element of a list with a number .

lst = eval(input("Enter a list: "))

print("Existing list is:", lst)

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

for i in range(len(lst)):

lst[i] += n

print("List after increment:", lst)

Enter a list: [10,20,30,40]

Existing list is: [10, 20, 30, 40]

Enter a number: 10

List after increment: [20, 30, 40, 50]


26.Wap that ask the user to enter a list of strings. Create a new
list that consists of those string with their first character
removed .

l1 = eval(input("Enter a list of strings: "))

l2 = []

for i in range(len(l1)):

l2.append(l1[i][1:])

print("List after removing first characters:")

print(l2)

Enter a list of strings: ["apple","orange","bananna"]

List after removing first characters:

['pple', 'range', 'ananna']

27.write a program that receives a Fibonacci term and returns


a number telling which term it is.

term=int(input("enter the fibonacci term"))

fib=(0,1)

while (fib(len(fib)-1)<term:

fib_len=len(fib)
fib=fib+(fib(fib_len-2)+fib(fib_len-1))

fib_len=len(fib)

if term==0:

print("0 is fibonacci term number 1")

elif term ==1:

print("1 is Fibonacci term number 2 ")

elif fib[fib_len-1]==term:

print(term,"is Fibonacci term number,fib_len)

else:

print("The term ",term,"does not exist in Fibonacci series")

Output:

enter the fibonacci term 8

8 is a fibonacci number

28.WAP to check the mode of a tuple is actually an element with


maximum occurrences.

tup=eval(input ("Enter a tuple:"))

maxcount=0

mode=0
for i in tup:

count=tup.count(i)

if maxcount<count:

maxcount=count

mode=i

print("mode:",mode)

29.WAP to calculate the avg. of a tuple's elements by calculating


it's sum and dividing it with the count of the elements.Then
compare it with the mean obtaineduosmean() of statistics module.

import statistics

tup =eval (input("Enter tuple"))

tup_sum=sum(tup)

tup_len=len(tup)

print("Average of tuple element is:",tup_sum/tup_len)

print("Mean of the tuple element is:",statistics.mean(tup))

30: Can you store the details of 10 students in a dictionary at the same time ? Details include - rollno,
name, marks, grade etc. Give example to support your answer.

n = 10

details = {}

for i in range(n):
name = input("Enter the name of student: ")

roll_num = int(input("Enter the roll number of student: "))

marks = int(input("Enter the marks of student: "))

grade = input("Enter the grade of student: ")

details[roll_num] = [name, marks, grade]

print()

print(details)

Enter the name of student: 3

Enter the roll number of student: 1

Enter the marks of student: 98

Enter the grade of student: A

Enter the name of student: ashis

Enter the roll number of student: 2

Enter the marks of student: 78

Enter the grade of student: A

Enter the name of student: bipin

Enter the roll number of student: 3

Enter the marks of student: 90

Enter the grade of student: A


Enter the name of student: anjana

Enter the roll number of student: 4

Enter the marks of student: 87

Enter the grade of student: C

Enter the name of student: rohit

Enter the roll number of student: 5

Enter the marks of student: 89

Enter the grade of student: A

Enter the name of student: renu

Enter the roll number of student: 6

Enter the marks of student: 96

Enter the grade of student: D

Enter the name of student: asni

Enter the roll number of student: 7

Enter the marks of student: 76

Enter the grade of student: C


Enter the name of student: bhinu

Enter the roll number of student: 8

Enter the marks of student: 56

Enter the grade of student: B

Enter the name of student: ranjan

Enter the roll number of student: 9

Enter the marks of student: 90

Enter the grade of student: A

Enter the name of student: bibhn

Enter the roll number of student: 10

Enter the marks of student: 94

Enter the grade of student: A

{1: ['3', 98, 'A'], 2: ['ashis', 78, 'A'], 3: ['bipin', 90, 'A'], 4:
['anjana', 87, 'C'], 5: ['rohit', 89, 'A'], 6: ['renu', 96, 'D'], 7: ['asni',
76, 'C'], 8: ['bhinu', 56, 'B'], 9: ['ranjan', 90, 'A'], 10: ['bibhn', 94,
'A']}

31.Program to count the frequency of list of elements using dictionary?


import json

sentence="This is a super idea This \it

idea will change the idea of learning"

words=sentence.split()

d={}

for one in words:

key=one

if key not in d:

count=words.count(key)

d[key]=count

print("counting frequencies in list \n",words)

print(json.dumps(d,indent=1))

output:

counting frequencies in list

['This', 'is', 'a', 'super', 'idea', 'This', 'idea', 'will', 'change', 'the',
'idea', 'of', 'learning']

"This": 2,

"is": 1,
"a": 1,

"super": 1,

"idea": 3,

"will": 1,

"change": 1,

"the": 1,

"of": 1,

"learning": 1

Q32: Write a program to convert a number entered by the user into its corresponding number in
words. For example, if the input is 876 then the output should be 'Eight Seven Six'.
(Hint. use dictionary for keys 0-9 and their values as equivalent words.)

num = int(input("Enter a number: "))

d = {0 : "Zero" , 1 : "One" , 2 : "Two" , 3 : "Three" , 4 : "Four" , 5 :


"Five" , 6 : "Six" , 7 : "Seven" , 8 : "Eight" , 9 : "Nine"}

digit = 0

str = ""

while num > 0:

digit = num % 10

num = num // 10

str = d[digit] + " " + str


print(str)

output:

Enter a number: 56

Five Six

Enter a number: 89

Eight Nine

Q33. Write a program to create a nested tuple to store roll number, name and marks of students.
tup = ()

ans = "y"

while ans == "y" or ans == "Y" :

roll_num = int(input("Enter roll number of student: "))

name = input("Enter name of student: ")

marks = int(input("Enter marks of student: "))

tup += ((roll_num, name, marks),)

ans = input("Do you want to enter more marks? (y/n): ")

print(tup)

Enter roll number of student: 1

Enter name of student: ashok

Enter marks of student: 90

Do you want to enter more marks? (y/n): y

Enter roll number of student: 2


Enter name of student: asish

Enter marks of student: 7

Do you want to enter more marks? (y/n): n

((1, 'ashok', 90), (2, 'asish', 7))

Q34. Write a program to check if a number is present in the list or not. If the number is present, print
the position of the number. Print an appropriate message if the number is not present in the list.

l = eval(input("Enter list: "))

n = int(input("Enter number to search: "))

if n in l:

print(n, "found at index", l.index(n))

else :

print(n, "not found in list")

Enter list: [10,20,30,40]

Enter number to search: 40

--------------------------

40 found at index 3

Enter list: [10,20,30,40]

Enter number to search: 50

50 not found in list


Q35: Write a program that inputs a line of text and prints out the count of vowels in it.

str = input("Enter a string: ")

count = 0

for ch in str :

lch = ch.lower()

if lch == 'a' \

or lch == 'e' \

or lch == 'i' \

or lch == 'o' \

or lch == 'u' :

count += 1

print("Vowel Count =", count)

Enter a string: PYTHON programming is fun

Vowel Count = 6

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy