Python Solved QB
Python Solved QB
#Program to swap
x=5
y = 10
Output
PYTHON CODE :
n=int(input("Enter number:"))
count=0
while(n>0):
count=count+1
n=n//10
OUTPUT:
Enter number:1234
PYTHON CODE:
SI = (P * N * R)/100
OUTPUT:
PYTHON CODE:
base=int(input("Enter base:"))
height=int(input("Enter height:"))
area=0.5*base*height
OUTPUT :
Enter base:3
Enter height:4
PYTHON CODE:
PI = 3.14
area = PI * r * r
OUTPUT:
Enter the radius of a circle:4
PYTHON CODE:
width=5
height=10
area=width*height
print("Area of rectangle=",area)
OUTPUT:
Area of rectangle= 50
PYTHON CODE:
n=int(input("Enter a number:"))
tot=0
while(n>0):
dig=n%10
tot=tot+dig
n=n//10
OUTPUT:
Enter a number:14
PYTHON CODE:
import cmath
a = float(input('Enter a: '))
b = float(input('Enter b: '))
c = float(input('Enter c: '))
d = (b**2) - (4*a*c)
sol1 = (-b-cmath.sqrt(d))/(2*a)
sol2 = (-b+cmath.sqrt(d))/(2*a)
OUTPUT:
Enter a: 2
Enter b: 4
Enter c: 6
PYTHON CODE:
NumList = []
Positive_count = 0
Negative_count = 0
j=0
NumList.append(value)
Positive_count = Positive_count + 1
else:
Negative_count = Negative_count + 1
j=j+1
OUTPUT:
Please enter the Total Number of List Elements: 5
7. Write a python program to find the Fibonacci series for a given length
PYTHON CODE :
n_terms = int(input ("How many terms the user wants to print? "))
num1 = 0
num2 = 1
count = 0
if n_terms <= 0:
print ("Please enter a positive integer, the given number is not valid")
print ("The Fibonacci sequence of the numbers up to", n_terms, ": ")
print(num1)
else:
print(num1)
num1 = num2
num2 = nth
count += 1
OUTPUT:
8
8. Write a python program to calculate the sum and average of the digits of a
number
#Python program to calculate the sum and average of the digits of a number
PYTHON CODE:
#python program to calculate the sum and average of the digits of a number
sumDigits =0
numberDigits=0
if (character.isdigit()):
# Calculate the sum of the digits of all numbers that appear in the string
sumDigits+=int(character)
numberDigits+=1
# Calculate the average of the digits of all numbers that appear in the string
average=sumDigits/numberDigits
OUTPUT:
9. Write a python program to calculate the sum and average of first n natural
number
PYTHON CODE:
sum = 0
sum = sum+num
average = sum / n
OUTPUT:
PYTHON CODE:
if(ch=='n'):
bonus=0.05*sal
else:
bonus=0.10*sal
amt_to_be_paid=sal+bonus
print("Salary = ",sal)
print("Bonus = ",bonus)
print("************************************")
OUTPUT:
Salary = 40000
Bonus = 4000.0
************************************
11.. Write a python program to find the greatest number from three numbers
#Program to find greatest of the numbers
PYTHON CODE:
if(num1>num2):
if(num2>num3):
else:
elif(num2>num3):
else:
OUTPUT:
12. Write a python program to calculate tax given the following conditions:
PYTHON CODE:
MIN1 = 150001
MAX1 = 300000
RATE1 = 0.10
MIN2 = 300001
MAX2 = 500000
RATE2 = 0.20
MIN3 = 500001
RATE3 = 0.30
if(taxable_income <=0):
print("No tax")
else:
print("TAX = ",tax)
OUTPUT:
13. . Write a python program to enter the marks of a student in four subjects. Then
calculate the total and aggregate, and display the grade obtained by the student. If the
student scores an aggregate >75%, then the grade is “Distinction”. If aggregate >=60%
and <75%, then the grade is “First Division”. If aggregate >=50% and <60%, then the
grade is “Second Division”. If aggregate >=40% and <50%, then the grade is “Third
Division” Else the grade is “Fail”.
PYTHON CODE:
total =marks1+marks2+marks3+marks4
avg =float(total)/4
if(avg>=75):
print("Distinction")
print("First division")
print("Second division")
print("Third division")
else:
print("Fail")
OUTPUT:
Enter the marks in mathematics:98
Distinction
PYTHON CODE:
D=(b*b)-(4*a*c)
deno=2*a
if(D>0):
print("REAL ROOTS")
root1=(-b + D**0.5)/deno
root2=(-b - D**0.5)/deno
elif(D==0):
print("REAL ROOTS")
root1 = -b/deno
else:
print("IMAGINARY ROOTS")
OUTPUT:
IMAGINARY ROOTS
15. Write a python program to read the numbers until -1 is encountered. Also
count the negatives, positives, and zeroes entered by the user.
PYTHON CODE:
print("Enter -1 to exit..")
while(1):
if(num==-1):
break
if(num==0):
zeros = zeroes+1
elif(num>0):
positives = positives+1
else:
negatives = negatives+1
OUTPUT:
-1 to exit..
16. Write a python program to find whether the given number is an Armstrong
number or not
PYTHON CODE:
n=int(input("Enter a number:"))
s=0
num=n
while (n>0):
r=n%10
s=s+(r**3)
n=n/10
if (s==num):
else:
OUTPUT:
PYTHON CODE:
def Count(str):
for i in range(len(str)):
if str[i].isupper():
upper += 1
elif str[i].islower():
lower += 1
elif str[i].isdigit():
number += 1
else:
special += 1
print('Number:', number)
Count(str)
OUTPUT:
Number: 5
Special characters: 2
18. Write a python program to generate a calendar month given the start
day(Sunday (1), Monday (2), etc.) and the number of days in that month
PYTHON CODE:
print("--------------------------------")
for i in range(startDay-1):
print(end=" ")
i=startDay-1
for j in range(1,num_of_days+1):
if(i>6):
print()
i=1
else:
i=i+1
OUTPUT:
------------------------------------------------
01 02 03
04 05 06 07 08 09 10
11 12 13 14 15 16 17
18 19 20 21 22 23 24
25 26 27 28 29 30 31
TEAM C
Program:
for i in range(0,rows):
print("*******")
Output:
*******
*******
*******
**
***
****
*****
Program:
for i in range(0,rows):
for j in range(0,i+1):
print("*",end="")
print("\n")
Output:
**
***
****
*****
12
123
1234
12345
Program:
for j in range(1,i+1):
print(j,end=" ")
print()
Output:
12
123
1234
12345
22
333
4444
55555
Program:
for i in range(1,rows+1):
for j in range(0,i):
print(i,end="")
print("\n")
Output:
22
333
4444
55555
12
345
6789
Program:
num=0
for i in range(0,rows):
for j in range(0,i):
print(num,end=" ")
num=num+1
print("\n")
Output:
12
345
6789
24. Write a python program to print the following pattern
12
123
1234
Program:
for j in range(1,(row-i)+1):
print(" ",end="")
for k in range(1,i+1):
print("{0}".format(k),end="")
print()
Output:
12
123
1234
121
12321
1234321
123454321
Program:
def pyramid():
odd = 1
space = row-2
k=0
if j <= i:
k=k+1
else:
k =k-1
print()
odd = odd + 2
space = space - 1
pyramid()
Output:
121
12321
1234321
123454321
12345654321
26. Write a python program to make a simple calculator by providing choice for
operations
#calculator program
Program:
print("********CALCULATOR*********")
print("Menu \n 1.Add \n 2.Subtract \n 3.Multiply \n 4.Division \n 5.Modulus")
choice = input("enter your choice:").lower()
num1 = int(input("enter number 1:"))
num2 = int(input("enter number 2:"))
if choice == '1' or choice == "add":
print(num1, "+", num2, "=", num1 + num2)
elif choice == '2' or choice == "subtract":
print(num1, "-", num2, "=", num1 - num2)
elif choice == '3' or choice == "multiply":
print(num1, "*", num2, "=", num1 * num2)
elif choice == '4' or choice == "division":
print(num1, "/", num2, "=", num1 / num2)
elif choice == '5' or choice == 'modulus':
print(num1, "%", num2, "=", num1 % num2)
else:
print("enter a valid operation!")
OUTPUT:
********CALCULATOR*********
Menu
1.Add
2.Subtract
3.Multiply
4.Division
5.Modulus
enter your choice:multiply
enter number 1:3
enter number 2:3
3*3=9
********CALCULATOR*********
Menu
1.Add
2.Subtract
3.Multiply
4.Division
5.Modulus
enter your choice:1
enter number 1:2
enter number 2:4
2+4=6
27. Write a python program to compute F(x,y) where F(x,y)=F(x-y,y)+1 if y<=x
Program:
1. Write a python program to display all leap years between 1900 to 2100
PROGRAM:
else:
return
OUTPUT:
PROGRAM:
OUTPUT:
29.. Write a python program to sum the series : 1/1 2 + 1/22 + 1/32 +…+ 1/n2
PROGRAM:
#Sum of the Series 1/1^+½^+⅓^+....+1/n^
n=int(input("Enter the number of terms: "))
sum1=0
for i in range(1,n+1):
sum1=sum1+(1/(i**2))
print("The sum of series is",round(sum1,4))
OUTPUT:
30. Write a python program to find the sum of first 10 natural numbers using lambda
function
#PROGRAM
#sum of first 10 natural numbers using lambda function
x=lambda: sum(range(1,11))
print(x())
#OUTPUT
55
31. .Write a program using functions in python to convert the hours:minutes format to
minutes. Eg. input: 2 hrs, 20 minutes output: 140 minutes
# PROGRAM
#To Convert Hours to Minutes
min= hrs*60+min
return min
h=int(input("Enter the hours:"))
m=int(input("Enter the minutes:"))
m=convert_time(h,m)
print("Total Minutes=",m)
#OUTPUT
#PROGRAM:
#To Find factorial of a number using recursive function
def recur_factorial(n):
if n == 1:
return n
else:
return n*recur_factorial(n-1)
num=int(input("Enter a number:"))
if num < 0:
print("Sorry, factorial does not exist for negative numbers")
elif num == 0:
print("The factorial of 0 is 1")
else:
print("The factorial of", num, "is", recur_factorial(num))
#OUTPUT
Enter a number:7
The factorial of 7 is 5040
33. Write a program using recursive functions in python to calculate GCD of two
numbers
#PROGRAM:
def GCD(x,y):
r=x%y
if(r==0):
return y
else:
return GCD(y,r)
n= int(input("Enter the First Number :"))
m= int(input("Enter the Second Number :"))
print("The GCD of Two Numbers is:", GCD(n,m))
#OUTPUT:
#PROGRAM:
#OUTPUT:
35. Write a Python program to create a lambda function that adds 15 to a given number
passed in as an argument, also create a lambda function that multiplies argument x with
argument y and print the result.
# PROGRAM
#To create a lambda function to add 15 to a given number as an argument
& multiplies argument x with argument y
l = lambda add : add + 15
print(l(20))
l = lambda x, y : x * y
print(l(10, 3))
# OUTPUT
35
30
PACKAGES:
USES OF PACKAGES:
BENEFITS