100% found this document useful (1 vote)
2K views4 pages

20 Simple Python Programs with Output

The document lists 20 simple Python programs with output that perform basic calculations and conditional checks. The programs include calculating sums, square roots, areas of shapes, temperature conversions, checking number properties like positive/negative/even/odd, finding largest/smallest numbers, and checking for leap years. Each program is presented with the Python code and expected output.

Uploaded by

uplifter40
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
100% found this document useful (1 vote)
2K views4 pages

20 Simple Python Programs with Output

The document lists 20 simple Python programs with output that perform basic calculations and conditional checks. The programs include calculating sums, square roots, areas of shapes, temperature conversions, checking number properties like positive/negative/even/odd, finding largest/smallest numbers, and checking for leap years. Each program is presented with the Python code and expected output.

Uploaded by

uplifter40
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
  • Python Programming Exercises

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:

[Link] 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.

[Link] 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:

[Link] 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

[Link] 4/4

Common questions

Powered by AI

To convert Fahrenheit to Celsius, apply the formula celsius = (fahrenheit - 32) / 1.8. In a Python program, for fahrenheit = 99.5, it is implemented as: fahrenheit = 99.5; celsius = (fahrenheit - 32) / 1.8; print("{0} degree Fahrenheit is equal to {1} degree Celsius".format(fahrenheit, celsius)). This prints: 99.5 degree Fahrenheit is equal to 37.5 degree Celsius .

To determine if a year is a leap year, you can use this Python program: if a year is divisible by 4, then it is a leap year. However, if the year is divisible by 100, it must also be divisible by 400 to be a leap year. This check ensures that years like 1900 are not considered leap years while 2000 is. Here's the code: 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)). The output confirms that 2024 is a leap year .

To check if a number is positive, negative, or zero in Python, a simple conditional statement can be used: if num > 0 then the number is positive, if num == 0 then it is zero, and else it is negative. For example, 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)). For the input -5, the output is: -5 is a negative number .

The formula to calculate the circumference of a circle is circumference = 2 * pi * radius. In Python, you can set pi = 3.14 and radius = 5, then compute the circumference as follows: circumference = 2 * pi * radius. The implementation is: radius = 5; pi = 3.14; circumference = 2 * pi * radius; print("Circumference of the circle is {0}".format(circumference)). The result of this program is: Circumference of the circle is 31.400000000000002 .

To convert Celsius to Fahrenheit, use the formula fahrenheit = (celsius * 1.8) + 32. In Python, for example, with celsius = 37.5, apply the formula: celsius = 37.5; fahrenheit = (celsius * 1.8) + 32; print("{0} degree Celsius is equal to {1} degree Fahrenheit".format(celsius, fahrenheit)). The output is: 37.5 degree Celsius is equal to 99.5 degree Fahrenheit .

To determine if a number is odd or even in Python, use the modulus operator (%). If num % 2 == 0, the number is even; otherwise, it is odd. For example, for num = 7, the conditional check num % 2 == 0 is false, so the print statement in the else block is executed: print("{0} is odd".format(num)). The output for this check is: 7 is odd .

To find the largest among three numbers in Python, use conditional statements to compare the numbers. For the numbers num1 = 5, num2 = 7, num3 = 3, implement: if num1 >= num2 and num1 >= num3, the largest is num1; if num2 >= num1 and num2 >= num3, the largest is num2; otherwise, it is num3. Here's the code: num1 = 5; num2 = 7; num3 = 3; write if-elif blocks to determine the largest. This results in num2, which is 7, being the largest. The output is: The largest number is 7 .

To find the smallest among three numbers in Python, use conditional statements similarly to finding the largest. For the numbers num1 = 5, num2 = 7, num3 = 3, the code involves: if num1 <= num2 and num1 <= num3 then smallest = num1; else if num2 <= num1 and num2 <= num3 then smallest = num2; else smallest = num3. Implement this logic to determine that 3 is the smallest number: the smallest number is 3 .

The square root of a number can be found in Python using the exponentiation operator (**) by raising the number to the power of 0.5. Given num = 16, the program is: num = 16; sqrt = num ** 0.5; print("Square root of {0} is {1}".format(num, sqrt)). This outputs: Square root of 16 is 4.0 .

To calculate the area of a triangle in Python, use the formula: area = 0.5 * base * height. For example, with base = 10 and height = 5, you can implement it as: base = 10; height = 5; area = 0.5 * base * height. Use the print function to verify: print("Area of the triangle is {0}".format(area)), which outputs: Area of the triangle is 25.0 .

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

You might also like