[go: up one dir, main page]

0% found this document useful (0 votes)
24 views19 pages

Pytho

The document contains a series of programming exercises that demonstrate various coding techniques in Python. Each program addresses a specific problem, such as finding the largest of three numbers, calculating factorials, checking for prime numbers, and manipulating lists and tuples. The document includes coding examples, expected outputs, and spaces for teacher's signatures.

Uploaded by

silanamia
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)
24 views19 pages

Pytho

The document contains a series of programming exercises that demonstrate various coding techniques in Python. Each program addresses a specific problem, such as finding the largest of three numbers, calculating factorials, checking for prime numbers, and manipulating lists and tuples. The document includes coding examples, expected outputs, and spaces for teacher's signatures.

Uploaded by

silanamia
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

Date:___________________

Prg.1: Program to find largest of three numbers.

Coding:
num1=int(input("Enter the first number:"))
num2=int(input("Enter the second number:"))
num3=int(input("Enter the third number:"))
if(num1>num2) and (num1>num3):
large=num1
elif (num2>num1) and (num2>num3):
large=num2
else:
large=num3
print(f"The largest of {num1},{num2},{num3} is {large}")

Output:

Teacher’s Sign:

Page 1 of 19
Date:___________________
Prg.2: Program that reads two numbers and an arithmetic operator and
displays the computed result.

Coding:
print("Arithmetic calculator")
num1=int(input("Enter the first number:"))
num2=int(input("Enter the second number:"))
ch=input("Enter your choice of arithmetic operator(+,-,*,/,**):")
print("Your choice is:",ch)
if ch=='+':
print("The sum of {} and {} is {}".format(num1,num2,num1+num2))
elif ch=='-':
print("The difference of {} and {} is {}".format(num1,num2,num1-
num2))
elif ch=='*':
print("The product of {} and {} is
{}".format(num1,num2,num1*num2))
elif ch=='/':
print("The quotient of {} and {} is
{}".format(num1,num2,num1/num2))
elif ch=='**':
print("The power of {} and {} is {}".format(num1,num2,num1**num2))
else:
print("Choose one of the operator specified in the list")

Page 2 of 19
Output:

Page 3 of 19
Teacher’s Sign:
Date:___________________
Prg.3: Program to find factorial of a given number using for and while
loops.

Coding:
#Factorial of a number using for loop.
n=int(input("Enter a number to find factorial:"))
fact=1
for i in range(1,n+1):
fact*=i
print("The factorial of",n,'is',fact)
#Factorial of a number using while loop.
n=int(input("Enter a number to find factorial:"))
i=1
fact=1
while(i<=n):
fact*=i
i+=1
print("the factorial of",n,"is",fact)

Output:

Page 4 of 19
Teacher’s Sign:

Date:___________________
Prg.4: Program to calculate and print the sum of even and odd integers of
the first n natural numbers.

Coding:
num=int(input("Enter a positive number:"))
count=1
even_sum=odd_sum=0
while count<=num:
if count%2==0:
even_sum+=count
else:
odd_sum+=count
count+=1
print(f"The sum of even integers upto {num} is {even_sum}")
print(f"The sum of odd integers upto {num} is {odd_sum}")

Output:

Page 5 of 19
Teacher’s Sign:

Date:___________________
Prg.5: Program to check whether a given number is a prime or composite
number.

Coding:
num=int(input("Enter a positive number:"))
mid=int(num/2)+1
for i in range(2,mid):
if num%i==0:
print(num,"is not a prime number")
break
else:
print(num,"is a prime number")

Output:

Page 6 of 19
Teacher’s Sign:

Date:___________________
Prg.6: Program to find the sum of individual digits of a given integer.

Coding:
num=int(input("Enter a number:"))
sum=0
while num>0:
d=num%10
sum+=d
num//=10
print(f"The sum of the digits of the given number is {sum}")

Output:

Page 7 of 19
Teacher’s Sign:
Date:___________________
Prg.7: Program to find whether a given integer is palindrome or not.

Coding:
n=int(input("Enter a number:"))
rev=0
num=n
while n>0:
d=n%10
rev=rev*10+d
n=n//10
if rev==num:
print(num, "is a palindrome")
else:
print(num,"is not a palindrome")

Output:

Page 8 of 19
Teacher’s Sign:
Date:___________________
Prg.8: Program to print Fibonacci series up to n terms.

Coding:
num=int(input("Enter a number:"))
first=0
second=1
print(first,second,end=" ")
for i in range(2,num):
third=first+second
first,second=second,third
print(third,end=" ")

Output:

Page 9 of 19
Teacher’s Sign:

Date:___________________
Prg.9: Program to print the following pattern using nested loops:
a. 1 b. 5 4 3 2 1
1 2 5 4 3 2
1 2 3 5 4 3
1 2 3 4 5 4
1 2 3 4 5 5

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

Output:

Page 10 of 19
Teacher’s Sign:

Date:___________________
Prg.10: Program to print the following pattern using nested loops:

e. * * * * * f. *
* * * * * *
* * * * * *
* * * *
* *

Coding:
rows = 4 rows = 3
k = 2 * rows – 2 for i in range(0, rows):
for i in range(rows, -1, -1): for j in range(0, i + 1):
for j in range(k, 0, -1): print("*", end=' ')
print(end=" ") print()
k=k+1 for i in range(rows, 0, -1):
for j in range(0, i + 1): for j in range(0, i - 1):
print("*", end=" ") print("*", end=' ')
print("") print()

Output:

Page 11 of 19
Teacher’s Sign:
e:___________________

Teacher’s Sign:
Date:___________________
Prg.11: Program to read a line of text as input and print the count of
vowels in it.

Coding:
s=input("Enter a line of text:")
c=0
for i in s:
if i in "aeiouAEIOU":
c+=1
print("The count of vowels in",s,"is",c)

Output:

Page 12 of 19
Teacher’s Sign:

Teacher’s Sign:

Date:___________________
Prg.12: Program to read a list of numbers and calculate the length,
maximum, minimum, sum, average and also display the given list in
ascending order.

Coding:
l=list(eval(input("Enter a list od numbers:")))
print("The given list is:",l)
print("The length of the given list is:",len(l))
print("The maximum value in the list is:",max(l))
print("The minimum value in the list is:",min(l))
print("The sum of the elements in the list is:",sum(l))
print("The average of the list is:",sum(l)/len(l))

Page 13 of 19
l.sort()
print("The list in ascending order:",l)
l.sort(reverse=True)
print("The list in descending order:",l)

Output:

Page 14 of 19
Teacher’s Sign:

Teacher’s Sign:

Date:___________________
Prg.13: Program to read a list of numbers and calculate and display the
sum of all the odd numbers and even numbers from the list separately.

Coding:
l=list(eval(input("Enter a list:")))
print("The given list is;",l)
odd_sum=even_sum=0
for i in l:
if i%2==0:
even_sum+=i
else:
odd_sum+=i
print("The sum of odd elements:",odd_sum)
print("The sum of even elements:",even_sum)

Page 15 of 19
Output:

Teacher’s Sign:
Date:___________________
Prg.14: Program that inputs two tuples and creates a third, that contains
all elements of the first followed by all elements of the second.

Coding:
tup1 = eval(input("Enter the elements of first tuple: "))
tup2 = eval(input("Enter the elements of second tuple: "))
tup3 = tup1 + tup2
print(tup3)

Output:

Page 16 of 19
Teacher’s Sign:
Date:___________________
Prg.15: Program to convert a number entered by the user into its
corresponding number in words. (Hint. use dictionary for keys 0-9 and
their values as equivalent words)

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

Page 17 of 19
print(str)

Output:

Page 18 of 19
Page 19 of 19

You might also like