Home > Automation Testing > Python sample programs – Part 1

Python sample programs – Part 1

This post was most recently updated on July 29th, 2024

1. Calculate the Average of Numbers in a Given List

count=int(input(“Average of how many numbers do you want to find?”))
a=[]
for i in range(0,count):
number=int(input(“Enter number: “))
a.append(number)
average=sum(a)/count
print(“Average of numbers is: “,average)

Output:
Average of how many numbers do you want to find?5
Enter number: 2
Enter number: 3
Enter number: 5
Enter number: 6
Enter number: 1
Average of numbers is: 3.4
2. Exchange the Values of Two Numbers Without Using a Temporary Variable

firstno=int(input(“Enter first number: “))
secondno=int(input(“Enter second number: “))
firstno=firstno+secondno
secondno=firstno-secondno
firstno=firstno-secondno
print(“After exchange first number is:”,firstno,” and second number is:”,secondno)

Output:
Enter first number: 10
Enter second number: 20
After exchange first number is: 20 and second number is: 10
3. Reverse a Given Number

number=int(input(“Enter number: “))
reverse=0
while(number>0):
digit=number%10
reverse=reverse*10+digit
number//=10
print(“Reverse of the number:”,reverse)

Output:
Enter number: 215
Reverse of the number: 512
4. Check Number is Positive or Negative

number = int(input(“Enter number to check: “))
if(number>=0):
print(“Number is positive”,number)
else:
print(“Number is negative”,number)

Output:
Enter number to check: 2
Number is positive 2
5. Take in the Marks of 5 Subjects and Display the Grade

English=int(input(“Enter marks in English: “))
Marathi=int(input(“Enter marks in Marathi: “))
Hindi=int(input(“Enter marks in Hindi: “))
Sanskrit=int(input(“Enter marks in Sanskrit: “))
Maths=int(input(“Enter marks in Maths: “))
avg= (English+Marathi+Hindi+Sanskrit+Maths) / 5
if(avg >= 80):
print(“Distinction”)
elif(avg >= 70 and avg < 80): print("First Class") elif(avg >= 60 and avg < 70): print("Second Class") elif(avg >= 50 and avg < 60): print("Third Class") else: print("Fail") Output: Enter marks in English: 70 Enter marks in Marathi: 60 Enter marks in Hindi: 80 Enter marks in Sanskrit: 99 Enter marks in Maths: 85 First Class 6. Print all Numbers in a Range Divisible by a Given Number bottom=int(input("Enter bottom limit:")) top=int(input("Enter top limit:")) number=int(input("Enter the number to be divided by:")) for i in range(bottom,top+1): if(i%number==0): print(i) Output: Enter bottom limit:5 Enter top limit:55 Enter the number to be divided by:11 11 22 33 44 55 7. Read Two Numbers and Print Their Quotient and Remainder number1 = int(input("Enter number 1: ")) number2 = int(input("Enter number 2: ")) quotient = number1//number2 remainder = number1%number2 print("Quotient is: ",quotient," and remainder is: ",remainder) Output: Enter number 1: 10 Enter number 2: 3 Quotient is: 3 and remainder is: 1 8. Accept Three Digits and Print all Possible Combinations from the Digits number1 = int(input("Enter number 1: ")) number2 = int(input("Enter number 2: ")) number3 = int(input("Enter number 3: ")) number=[] number.append(number1) number.append(number2) number.append(number3) for i in range(0,3): for j in range(0,3): for k in range(0,3): if(i!=j&j!=k&k!=i): print(number[i],number[j],number[k]) Output: Enter number 1: 10 Enter number 2: 20 Enter number 3: 30 10 20 30 10 30 20 20 10 30 20 30 10 30 10 20 30 20 10 9. Print Odd Numbers Within a Given Range bottom=int(input("Enter bottom limit:")) top=int(input("Enter top limit:")) for i in range(bottom,top+1): if(i%2!=0): print(i) Output: Enter bottom limit:1 Enter top limit:15 1 3 5 7 9 11 13 15 10. Print Even Numbers Within a Given Range: bottom=int(input("Enter bottom limit:")) top=int(input("Enter top limit:")) for i in range(bottom,top+1): if(i%2==0): print(i) Output: Enter bottom limit:1 Enter top limit:15 2 4 6 8 10 12 14

This Article is TAGGED in . BOOKMARK THE permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">