Category: Examples
-
Find the Factorial of a Number
Python Program to Find the Factorial of a Number To understand this example, you should have the knowledge of the following Python programming topics: The factorial of a number is the product of all the integers from 1 to that number. For example, the factorial of 6 is 1*2*3*4*5*6 = 720. Factorial is not defined for negative numbers,…
-
Check Prime Number
Python Program to Check Prime Number To understand this example, you should have the knowledge of the following Python programming topics: A positive integer greater than 1 which has no other factors except 1 and the number itself is called a prime number. 2, 3, 5, 7 etc. are prime numbers as they do not have any…
-
Check Leap Year
Python Program to Check Leap Year To understand this example, you should have the knowledge of the following Python programming topics: A leap year is exactly divisible by 4 except for century years (years ending with 00). The century year is a leap year only if it is perfectly divisible by 400. For example, 2017 is not…
-
Convert Celsius To Fahrenheit
Python Program to Convert Celsius To Fahrenheit To understand this example, you should have the knowledge of the following Python programming topics: In the program below, we take a temperature in degree Celsius and convert it into degree Fahrenheit. They are related by the formula: fahrenheit = celsius * 1.8 + 32 Source Code Output 37.5 degree…
-
Convert Kilometers to Miles
Python Program to Convert Kilometers to Miles To understand this example, you should have the knowledge of the following Python programming topics: Example: Kilometers to Miles Output Enter value in kilometers: 3.5 3.50 kilometers is equal to 2.17 miles Here, the user is asked to enter kilometers. This value is stored in the kilometers variable. Since 1 kilometer is…
-
Generate a Random Number
Python Program to Generate a Random Number To understand this example, you should have the knowledge of the following Python programming topics: To generate random number in Python, randint() function is used. This function is defined in random module. Source Code Output 5 Note that we may get different output because this program generates random number in range 0 and…
-
Swap Two Variables
Python Program to Swap Two Variables To understand this example, you should have the knowledge of the following Python programming topics: Source Code: Using a temporary variable Code Visualization: Want to see exactly how variable swapping works? Step through each line with our new code visualizer. Try it yourself! Output The value of x after swapping: 10 The…
-
Solve Quadratic Equation
Python Program to Solve Quadratic Equation To understand this example, you should have the knowledge of the following Python programming topics: The standard form of a quadratic equation is: ax2 + bx + c = 0, where a, b and c are real numbers and a ≠ 0 The solutions of this quadratic equation is given by:…
-
Calculate the Area of a Triangle
Python Program to Calculate the Area of a Triangle To understand this example, you should have the knowledge of the following Python programming topics: If a, b and c are three sides of a triangle. Then, s = (a+b+c)/2 area = √(s(s-a)*(s-b)*(s-c)) Source Code Run Code Output The area of the triangle is 14.70 In this program, area of the triangle is…
-
Find the Square Root
Python Program to Find the Square Root To understand this example, you should have the knowledge of the following Python programming topics: Example: For positive numbers Output The square root of 8.000 is 2.828 In this program, we store the number in num and find the square root using the ** exponent operator. This program works for all positive real numbers.…