20 Simple Python Programs with Output
20 Simple Python Programs with Output
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 .



