Basic Python Programs

 


Table of Contents

1) Program to Print "Hello World!"

2) Program to Add two numbers with user input

3) Program to swap two numbers without using temporary variable

4) Program to reverse a number.

5) Program to Check Whether a Number is Positive or Negative

6) Program to Take in the Marks of 5 Subjects and Display the Grade

7) Program to Take the Temperature in Celsius and Convert it into Fahrenheit

8) Program to Read Height in Centimeters and then Convert the Height to Feet and Inches



Python Programs

Program 1 : Print "Hello World!"

Code:
print("Hello World!")
Output:

Program 2 : Add two numbers with user input
Code:
# Store input numbers
num1 = int(input('Enter first number: '))
num2 = int(input('Enter second number: '))
# Add two numbers
sum = num1 + num2
# Display the sum
print('The sum of {0} and {1} is {2}'.format(num1, num2, sum))
Output:


Program 3 : Program to swap two numbers without using temporary variable

Code:
a=int(input("Enter value of first variable: "))
b=int(input("Enter value of second variable: "))
a=a+b
b=a-b
a=a-b
print("a is:",a," b is:",b)
Output:


Program 4 : Program to reverse a number.

n=int(input("Enter number: "))
rev=0
while(n>0):
 dig=n%10
 rev=rev*10+dig
 n=n//10
print("Reverse of the number:",rev)
Output:


Program 5 : Program to Check Whether a Number is Positive or Negative

Code:
n=int(input("Enter number: "))
if(n>0):
 print("Number is positive")
else:
 print("Number is negative")
Output:


Program 6 :  Program to Take in the Marks of 5 Subjects and Display the Grade

Code:
sub1=int(input("Enter marks of the first subject: "))
sub2=int(input("Enter marks of the second subject: "))
sub3=int(input("Enter marks of the third subject: "))
sub4=int(input("Enter marks of the fourth subject: "))
sub5=int(input("Enter marks of the fifth subject: "))
avg=(sub1+sub2+sub3+sub4+sub4)/5
if(avg>=90):
 print("Grade: A")
elif(avg>=80 and avg<90):
 print("Grade: B")
elif(avg>=70 and avg<80):
 print("Grade: C")
elif(avg>=60 and avg<70):
 print("Grade: D")
else:
 print("Grade: F")
Output:


Program 7 :  Program to Take the Temperature in Celsius and Convert it into Fahrenheit

Code:
celsius=int(input("Enter the temperature in celsius:"))
f=(celsius*1.8)+32
print("Temperature in farenheit is:",f)
Output:


Program 8 : Program to Read Height in Centimeters and then Convert the Height to Feet and Inches

Code:
cm=int(input("Enter the height in centimeters:"))
inches=0.394*cm
feet=0.0328*cm
print("The length in inches",round(inches,2))
print("The length in feet",round(feet,2))
Output:


More Questions will be added soon...
AJ Blogs

Hello everyone, My name Arth and I like to write about what I learn. Follow My Website - https://sites.google.com/view/aj-blogs/home

Post a Comment

Previous Post Next Post
Best Programming Books

Facebook

AJ Facebook
Checkout Our Facebook Page
AJ Blogs
Checkout Our Instagram Page