0% found this document useful (0 votes)
14 views20 pages

VND - Openxmlformats Officedocument - Wordprocessingml.document&rendition 1

The document contains a series of Python programs that demonstrate various programming concepts, including number divisibility, odd/even checks, arithmetic operations, sorting, and more. Each program is accompanied by example inputs and outputs to illustrate its functionality. The programs cover a wide range of topics, from basic arithmetic to string manipulation and data structures.

Uploaded by

brokencrystal502
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)
14 views20 pages

VND - Openxmlformats Officedocument - Wordprocessingml.document&rendition 1

The document contains a series of Python programs that demonstrate various programming concepts, including number divisibility, odd/even checks, arithmetic operations, sorting, and more. Each program is accompanied by example inputs and outputs to illustrate its functionality. The programs cover a wide range of topics, from basic arithmetic to string manipulation and data structures.

Uploaded by

brokencrystal502
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/ 20

# 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)

OUTPUT:
Enter first number : 4
Enter second number : 2
4 is divisible by 2

# 2.Program that takes a number and checks whether the given


number is odd or even.
num = int (input( "Enter an integer:"))
if num %2 ==0 :
print ( num, "is even number . " )
else:
print (num, "is odd number ." )

Output
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)
OUTPUT:

Enter first number : 5

Enter second number : 6

Enter operator [ + - * / % ] : *

5.0 * 6.0 = 30.0

#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 = 0
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)
OUTPUT:-

Enter first number :12

Enter second number :55

Enter third number :98

Number in ascending order : 12 55 98


# 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)
OUTPUT:
Up to which natural number ? 6
The sum of even integers is 12
The sum of odd integers is 9

#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 .')
OUTPUT:-

Enter a single character :A

You entered an uppercase 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)
OUTPUT:-
Enter any number:5
5x1=5
5 x 2 = 10
5 x 3 = 15
5 x 4 = 20
5 x 5 = 25
5 x 6 = 30
5 x 7 = 35
5 x 8 = 40
5 x 9 = 45
5 x 10 = 50

# 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)
OUTPUT:
Sum of natural numbers: 7 is 28

#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)
OUTPUT:

Enter a number : 6

The factorial of 6 is 720

# 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)
OUTOUT:
The loop with 'break' produces output as :
1
2
The loop with 'continue' produces output as :
1
2
4
5
7
8
10

# 11.Program to input a number and test if it is a prime number


or not.
num=int(input('Enter a number to check:'))
a=0
for i in range(1,num+1):
if num % i==0:
a+=1
if a==2:
print("Number is prime")
else:
print("Number is NOT prime")
OUTPUT:

Enter a number to check:2


Number is prime

# 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.
OUTPUT:

**

***

****

#13.Write a python script to print this pattern.


# ***
# ***
# ***

for a in range (3) :


for b in range (5 , 8 ) :
print ( "*" , end = '' )
print ( )

OUTPUT:

***

***

***

# 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
OUTPUT:

1
1

13

21

34

55

89

144

233

377

610

987

1597

2584

4181

# 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
while tnum !=0:
digit=tnum%10
tnum=int(tnum/10)
reverse=reverse*10+digit
print("Reverse of",num,"is",reverse)
OUTPUT:

Enter a 4 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 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 number:407

407 is an armstrog 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)
OUTPUT:

Enter a positive number(x):2

Enter the power(n):3

2 to the power 3 is 8

# 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)
OUTPUT:

Enter a positive number:7

7 !! is: 105

# 20.Write Python script to print following pattern :


# 1
# 1 3
# 1 3 5
# 1 3 5 7
for a in range(3,10,2):
for b in range(1,a,2):
print(b,end=" ")
print()

OUTPUT:

13

135

1357
# 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

# 22. Write a program to count the number of times a character


occurs in the given string.
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

# 23.Write a program that replaces all the vowels in the string


with “*”.
str = input("Enter the string: ")
newStr = ""
for ch 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: Computer Science

C*mp*t*r Sc**nc*

# 24.Write a program which reverses a string and stores the


reversed string in a new string.
str = input("Enter the string: ")
newStr = ""
for ch in str :
newStr= ch+newStr
print(newStr)

OUTPUT:

Enter the string: PYTHONPROGAM

MAGORPNOHTYP

# 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)

OUTPUT:

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)

OUTPUT:

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[-1] < term:
fib.append(fib[-1] + fib[-2])
if term == 0:
print("0 is Fibonacci term number 1")
elif term == 1:
print("1 is Fibonacci term number 2 or 3")
elif term in fib:
print(term,"is Fibonacci term number", (fib.index(term)) +
1)
else:
print("The term", term, "does not exist in the Fibonacci
series")

OUTPUT:

Enter the Fibonacci term: 8

8 is Fibonacci term number 7

# 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)

OUTPUT:

Enter a tuple:(1,1,12,23,2,4,5,4,6,4,3,5,8)

Mode: 4

# 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))

OUTPUT:

Enter tuple:(12,23,55,14,89)

Average of tuple element is: 38.6

Mean of the tuple element is: 38.6

# 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)

OUTPUT:

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 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