Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
100% found this document useful (2 votes)
1K views

Write 20 Simple Basic Python Programs With Output - Search

The document lists 20 simple Python programs with output that perform basic calculations and conditional checks. The programs include addition, square roots, area calculations, conversions between Celsius and Fahrenheit, checking positive/negative numbers, odd/even, leap years, and finding maximum/minimum values. Each program contains a short code sample and expected output.

Uploaded by

uplifter40
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
100% found this document useful (2 votes)
1K views

Write 20 Simple Basic Python Programs With Output - Search

The document lists 20 simple Python programs with output that perform basic calculations and conditional checks. The programs include addition, square roots, area calculations, conversions between Celsius and Fahrenheit, checking positive/negative numbers, odd/even, leap years, and finding maximum/minimum values. Each program contains a short code sample and expected output.

Uploaded by

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

12/1/23, 8:07 PM write 20 simple basic python programs with output - Search

Sure, here are 20 simple Python programs with output:


1. Addition of two numbers:
Python
AI-generated code. Review and use carefully. More info on FAQ.

num1 = 5
num2 = 7
sum = num1 + num2
print("Sum of {0} and {1} is {2}".format(num1, num2, sum))

Output:
Sum of 5 and 7 is 12

2. Find the square root of a number:


Python
AI-generated code. Review and use carefully. More info on FAQ.

num = 16
sqrt = num ** 0.5
print("Square root of {0} is {1}".format(num, sqrt))

Output:
Square root of 16 is 4.0

3. Calculate the area of a triangle:


Python
AI-generated code. Review and use carefully. More info on FAQ.

base = 10
height = 5
area = 0.5 * base * height
print("Area of the triangle is {0}".format(area))

Output:
Area of the triangle is 25.0

4. Calculate the area of a rectangle:


Python
AI-generated code. Review and use carefully. More info on FAQ.

length = 10
breadth = 5
area = length * breadth
print("Area of the rectangle is {0}".format(area))

Output:
Area of the rectangle is 50

5. Calculate the circumference of a circle:

https://www.bing.com/search?pglt=673&q=write+20+simple+basic+python+programs+with+output&cvid=599dc6aafd8f4617b39c07a1df63a5bd&… 1/4
12/1/23, 8:07 PM write 20 simple basic python programs with output - Search

Python
AI-generated code. Review and use carefully. More info on FAQ.

radius = 5
pi = 3.14
circumference = 2 * pi * radius
print("Circumference of the circle is {0}".format(circumference))

Output:
Circumference of the circle is 31.400000000000002

6. Calculate the area of a circle:


Python
AI-generated code. Review and use carefully. More info on FAQ.

radius = 5
pi = 3.14
area = pi * radius ** 2
print("Area of the circle is {0}".format(area))

Output:
Area of the circle is 78.5

7. Convert Celsius to Fahrenheit:


Python
AI-generated code. Review and use carefully. More info on FAQ.

celsius = 37.5
fahrenheit = (celsius * 1.8) + 32
print("{0} degree Celsius is equal to {1} degree Fahrenheit".format(celsius,
fahrenheit))

Output:
37.5 degree Celsius is equal to 99.5 degree Fahrenheit

8. Convert Fahrenheit to Celsius:


Python
AI-generated code. Review and use carefully. More info on FAQ.

fahrenheit = 99.5
celsius = (fahrenheit - 32) / 1.8
print("{0} degree Fahrenheit is equal to {1} degree
Celsius".format(fahrenheit, celsius))

Output:
99.5 degree Fahrenheit is equal to 37.5 degree Celsius

9. Check if a number is positive, negative or zero:


Python
AI-generated code. Review and use carefully. More info on FAQ.

https://www.bing.com/search?pglt=673&q=write+20+simple+basic+python+programs+with+output&cvid=599dc6aafd8f4617b39c07a1df63a5bd&… 2/4
12/1/23, 8:07 PM write 20 simple basic python programs with output - Search

num = -5
if num > 0:
print("{0} is a positive number".format(num))
elif num == 0:
print("{0} is zero".format(num))
else:
print("{0} is a negative number".format(num))

Output:
-5 is a negative number

10. Check if a number is odd or even:


Python
AI-generated code. Review and use carefully. More info on FAQ.

num = 7
if num % 2 == 0:
print("{0} is even".format(num))
else:
print("{0} is odd".format(num))

Output:
7 is odd

11. Check if a year is a leap year:


Python
AI-generated code. Review and use carefully. More info on FAQ.

year = 2024
if year % 4 == 0:
if year % 100 == 0:
if year % 400 == 0:
print("{0} is a leap year".format(year))
else:
print("{0} is not a leap year".format(year))
else:
print("{0} is a leap year".format(year))
else:
print("{0} is not a leap year".format(year))

Output:
2024 is a leap year

12. Find the largest among three numbers:


Python
AI-generated code. Review and use carefully. More info on FAQ.

num1 = 5
num2 = 7
num3 = 3
if num1 >= num2 and num1 >= num3:

https://www.bing.com/search?pglt=673&q=write+20+simple+basic+python+programs+with+output&cvid=599dc6aafd8f4617b39c07a1df63a5bd&… 3/4
12/1/23, 8:07 PM write 20 simple basic python programs with output - Search

largest = num1
elif num2 >= num1 and num2 >= num3:
largest = num2
else:
largest = num3
print("The largest number is {0}".format(largest))

Output:
The largest number is 7

13. Find the smallest among three numbers:


Python
AI-generated code. Review and use carefully. More info on FAQ.

num1 = 5
num2 = 7
num3 = 3
if num1 <= num2 and num1 <= num3:
smallest

https://www.bing.com/search?pglt=673&q=write+20+simple+basic+python+programs+with+output&cvid=599dc6aafd8f4617b39c07a1df63a5bd&… 4/4

You might also like