0% found this document useful (0 votes)
2 views

Python Programs

The document contains ten Python programs that demonstrate various basic functionalities, including calculating sums, swapping variables, converting units, and checking conditions like leap years. Each program includes user input and outputs relevant results such as total sums, areas, and whether numbers are odd or even. These examples serve as practical applications of Python programming concepts for beginners.

Uploaded by

garvitschauhan
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Python Programs

The document contains ten Python programs that demonstrate various basic functionalities, including calculating sums, swapping variables, converting units, and checking conditions like leap years. Each program includes user input and outputs relevant results such as total sums, areas, and whether numbers are odd or even. These examples serve as practical applications of Python programming concepts for beginners.

Uploaded by

garvitschauhan
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

Python Programs

1. Python Program to calculate sum of numbers until user enters zero

total = 0
while True:
num = int(input("Enter a number (0 to stop): "))
if num == 0:
break
total += num
print("Total sum:", total)

2. Python Program to input two numbers and add them

num1 = float(input("Enter first number: "))


num2 = float(input("Enter second number: "))
sum = num1 + num2
print("Sum:", sum)

3. Python Program to Swap Two Variables

a = input("Enter first variable: ")


b = input("Enter second variable: ")
a, b = b, a
print("After swapping:")
print("First variable:", a)
print("Second variable:", b)

4. Python program to convert kilometers to miles

km = float(input("Enter distance in kilometers: "))


miles = km * 0.621371
print(f"{km} kilometers is equal to {miles:.2f} miles")

5. Python program to convert Celsius to Fahrenheit

celsius = float(input("Enter temperature in Celsius: "))


fahrenheit = (celsius * 9/5) + 32
print(f"{celsius}°C is equal to {fahrenheit:.2f}°F")

6. Python program to find area and circumference of a circle

radius = float(input("Enter the radius of the circle: "))


area = 3.14159 * radius ** 2
circumference = 2 * 3.14159 * radius
print(f"Area: {area:.2f}")
print(f"Circumference: {circumference:.2f}")

7. Python program to input principal, rate of interest, and time, and find simple
interest

principal = float(input("Enter principal amount: "))


rate = float(input("Enter rate of interest (in %): "))
time = float(input("Enter time (in years): "))
simple_interest = (principal * rate * time) / 100
print(f"Simple Interest: {simple_interest:.2f}")

8. Python program to input marks of a student in 5 subjects, find total and


percentage

marks = []
for i in range(5):
marks.append(float(input(f"Enter marks for subject {i+1}: ")))
total = sum(marks)
percentage = total / 5
print(f"Total Marks: {total}")
print(f"Percentage: {percentage:.2f}%")

9. Python Program to Check if a Number is Odd or Even

num = int(input("Enter a number: "))


if num % 2 == 0:
print(f"{num} is Even")
else:
print(f"{num} is Odd")
10. Python Program to input any year and check if it is a Leap Year or not

year = int(input("Enter a year: "))


if (year % 4 == 0 and year % 100 != 0) or (year % 400 == 0):
print(f"{year} is a Leap Year")
else:
print(f"{year} is not a Leap Year")

You might also like