20 Simple Python Programs with Output
20 Simple Python Programs with Output
The code calculates the circumference of a circle using the formula circumference = 2 * pi * radius, where pi is approximated as 3.14. The radius is used as another input, representing half the diameter of the circle. This formula follows the standard mathematical definition of circumference for a circle .
The Python program determines the smallest among three numbers using a series of conditional statements. It first checks if num1 is less than or equal to both num2 and num3, if true, num1 is the smallest. If not, it checks if num2 is less than or equal to num1 and num3, assigning num2 as smallest if true. If neither condition is satisfied, num3 is considered the smallest. This logical sequence ensures comprehensive comparison .
The Python code checks for leap years by verifying if the year is divisible by 4, but with exceptions: if divisible by 100, it must also be divisible by 400. These conditions reflect real-world calendar adjustments, where leap years correct the calendar year to synchronize with the astronomical year, accounting for the Earth's orbit and seasonal drift .
The Python code checks if a number is positive, negative, or zero using conditional statements. First, it compares the number to zero. If greater, it prints that the number is positive. If equal to zero, it prints that the number is zero. Otherwise, it assumes the number is negative and prints that result. This straightforward decision-making flow ensures all three possible conditions are evaluated .
To determine whether a year is a leap year using Python, the code checks that the year must be divisible by 4. If it is divisible by 100, it must also be divisible by 400 to be considered a leap year. Otherwise, if it is divisible by 4 and not by 100, it is still a leap year .
The Python formula used to calculate the area of a triangle is area = 0.5 * base * height. This requires the input of the triangle's base and height measurements. The formula applies the basic geometric principle that the area of a triangle is half the product of its base and height .
The logic used to find the largest among three numbers involves a series of conditional checks. The code first checks if num1 is greater than or equal to num2 and num3. If true, num1 is assigned as the largest. Otherwise, it checks if num2 is greater than or equal to num1 and num3, assigning num2 as the largest. If neither condition is met, num3 is deemed the largest. This order ensures all possible comparisons are covered .
The Python logic used to convert Fahrenheit to Celsius employs the formula: celsius = (fahrenheit - 32) / 1.8. This formula reverses the conversion process by first removing the 32-degree offset in Fahrenheit's absolute zero compared to Celsius, then dividing by 1.8, the ratio due to different scaling increments between the two units .
The Python program converts Celsius to Fahrenheit using the formula: fahrenheit = (celsius * 1.8) + 32. This formula relies on the principle that each degree Celsius is equivalent to an increment of 1.8 degrees Fahrenheit, and the 32-degree adjustment accounts for the different starting points of the two scales at freezing point .
The method used in the Python program to determine whether a number is odd or even involves the modulo operation. The code checks if the number modulo 2 equals zero. If true, the number is even; otherwise, it is odd. The modulo operation is crucial as it calculates the remainder of division, directly indicating whether a number has a remainder when divided by 2 .



