Python Practice › Basic Programs
Python Basic Programs with Solutions
Start here if you are new to Python. These 40 basic programs use only variables, arithmetic, a few built-in modules and print(), and each one comes with an explained solution you can run in the browser.
Before you start
Read the Variables and Numbers & Math and Operators & Booleans lesson first if the topic is new to you.
All programs
- #1Add two integers
Write a Python program to add two integer values and print the result.Easy - #2Subtract two integers
Write a Python program to subtract one integer from another and print the result.Easy - #3Multiply two numbers
Write a Python program to multiply two numbers.Easy - #4Repeat a string 5 times
Write a Python program to repeat a given string 5 times.Easy - #5Average of given numbers
Write a Python program to find the average of given numbers. Average = sum of the numbers / how many numbers there are.Easy - #6Median of given numbers
Write a Python program to find the median of given numbers. The median is the middle value after sorting; for an even count it is the average of the two middle values.Medium - #7Square and cube of a number
Write a Python program to print the square and cube of a given number.Easy - #8Swap two variables
Write a Python program to interchange (swap) the values of two variables.Easy - #9Pythagoras theorem
Write a Python program to find the hypotenuse of a right-angled triangle using the Pythagoras theorem: a² + b² = c².Easy - #10Formula (a + b)² = a² + b² + 2ab
Write a Python program to solve the formula (a + b)² = a² + b² + 2ab and show both sides are equal.Easy - #11Formula (a – b)² = a² + b² – 2ab
Write a Python program to solve the formula (a – b)² = a² + b² – 2ab.Easy - #12Formula a² – b² = (a – b)(a + b)
Write a Python program to solve the formula a² – b² = (a – b)(a + b).Easy - #13Formula (a + b)³ = a³ + 3ab(a + b) + b³
Write a Python program to solve the formula (a + b)³ = a³ + 3ab(a + b) + b³.Medium - #14Formula (a – b)³ = a³ – 3a²b + 3ab² – b³
Write a Python program to solve the formula (a – b)³ = a³ – 3a²b + 3ab² – b³.Medium - #15Area of a square
Write a Python program to calculate the area of a square. Area = side × side.Easy - #16Area of a circle
Write a Python program to calculate the area of a circle. Area = π × r².Easy - #17Surface area of a cube
Write a Python program to calculate the surface area of a cube. Surface area = 6 × a².Easy - #18Surface area of a cylinder
Write a Python program to calculate the surface area of a cylinder. Area = 2πrh + 2πr².Easy - #19Check an Armstrong number
Write a Python program to check whether a number is an Armstrong number: the sum of its digits, each raised to the power of the number of digits, equals the number (153 = 1³ + 5³ + 3³).Medium - #20Simple interest
Write a Python program to calculate simple interest. Simple interest = P × R × T / 100, where P is the principal, R the yearly rate in percent and T the time in years.Easy - #21Print the current date in a format
Write a Python program to print the current date in the format “2023 Jan 05” (year, short month name, day) using the datetime module.Easy - #22Days between two dates
Write a Python program to calculate the number of days between two dates.Easy - #23Factorial of a number
Write a Python program to find the factorial of a number. n! = 1 × 2 × … × n, and 0! = 1.Easy - #24Reverse a number
Write a Python program to reverse the digits of a number.Easy - #25Fibonacci series up to 50
Write a Python program to print the Fibonacci series between 0 and 50. Each number is the sum of the two before it.Easy - #26Check a palindrome number
Write a Python program to check whether a number is a palindrome (reads the same backwards, like 12321).Easy - #27Compound interest
Write a Python program to calculate compound interest. Amount = P × (1 + R/100)ᵀ and interest = amount − P.Easy - #28Check a prime number
Write a Python program to check whether a number is prime (greater than 1 and divisible only by 1 and itself).Medium - #29Check a leap year
Write a Python program to check whether a year is a leap year: divisible by 4, except century years, which must be divisible by 400.Easy - #30Check for an anagram
Write a Python program to check whether two words are anagrams: one is a rearrangement of all the letters of the other.Medium - #31Generate random numbers
Write a Python program to generate random numbers.Easy - #32Random string of a given length
Write a Python program to generate a random string of a specific length.Easy - #33Get the current date and time
Write a Python program to get the current date (and time).Easy - #34Convert decimal to binary
Write a Python program to convert a decimal number to binary.Easy - #35Sum of natural numbers
Write a Python program to find the sum of the first n natural numbers (1 + 2 + … + n).Easy - #36HCF (GCD) of two numbers
Write a Python program to find the HCF (highest common factor, also called GCD) of two numbers.Easy - #37LCM of two numbers
Write a Python program to find the LCM (lowest common multiple) of two numbers.Easy - #38Square root of a number
Write a Python program to find the square root of a number using the math library.Easy - #39Volume of a sphere
Write a Python program to calculate the volume of a sphere. Volume = 4/3 × π × r³.Easy - #40Arithmetic operations on two numbers
Write a Python program to perform all the basic arithmetic operations on two numbers.Easy