Basic Python Programs
print, input, if, a bit of math. Week-one stuff. If this feels slow, skip ahead.
25 programs · open one, then the next
The matching chapter is Day 1: Introduction to Python in the Python course. For a short check, try the Python quiz (300 questions). Every topic is listed on Python exercises.
Start with Print "Hello, World!"- 01Print "Hello, World!"BeginnerOne print. If this runs, Python is actually installed.Open example →py# if this prints, Python is installed print("Hello, World!")
- 02Add Two NumbersBeginnerinput() gives strings. Forget float() and you glue 10 and 20 into 1020.Open example →py# Program to add two numbers provided by the user # taking input from the user num1 = float(input("Enter first number: "))
- 03Swap Two VariablesBeginnerThe textbook temp-variable swap, then the one-liner you will actually use.Open example →py# Swapping using a temporary variable a = 5 b = 10
- 04Find ASCII Value of a CharacterBeginnerord('A') is 65. Type two characters and this program refuses.Open example →py# Program to find the ASCII value of a character ch = input("Enter a character: ")
- 05Check Even or OddBeginnernum % 2. Remainder 0 means even. That is the whole trick.Open example →py# Program to check if a number is even or odd num = int(input("Enter an integer: "))
- 06Find Largest of Two NumbersBeginnerThree cases: a bigger, b bigger, or they match. The equal case is the one people skip.Open example →py# Program to find the largest of two numbers a = float(input("Enter first number: ")) b = float(input("Enter second number: "))
- 07Find Largest of Three NumbersBeginnera vs b vs c. Use >= so ties don't fall through into the wrong variable.Open example →py# Program to find the largest of three numbers a = float(input("Enter first number: ")) b = float(input("Enter second number: "))
- 08Check Positive, Negative, or ZeroBeginnerZero is not positive. Put it in its own branch or you will lie to the user.Open example →py# Program to check if a number is positive, negative, or zero num = float(input("Enter a number: "))
- 09Convert Kilometers to MilesBeginnerMultiply by 0.621371. Put the magic number in a named variable so you can find it later.Open example →py# Program to convert kilometers to miles kilometers = float(input("Enter distance in kilometers: "))
- 10Convert Celsius to FahrenheitBeginnerF = C * 9/5 + 32. The parentheses are not optional in your head even if Python's precedence saves you.Open example →py# Program to convert Celsius to Fahrenheit celsius = float(input("Enter temperature in Celsius: "))
- 11Calculate Area of a TriangleBeginner0.5 * base * height. Height means perpendicular height, not a slanty side.Open example →py# Program to calculate the area of a triangle base = float(input("Enter the base of the triangle: ")) height = float(input("Enter the height of the triangle: "))
- 12Calculate Simple InterestBeginnerSI = P*R*T / 100. Rate is percent, so 5 means 5%, not 0.05.Open example →py# Program to calculate simple interest principal = float(input("Enter principal amount: ")) rate = float(input("Enter annual interest rate (in %): "))
- 13Calculate Compound InterestBeginnerA = P(1 + R/100n)^(nT). Then interest is A minus P. The ** is the whole point.Open example →py# Program to calculate compound interest principal = float(input("Enter principal amount: ")) rate = float(input("Enter annual interest rate (in %): "))
- 14Calculate Square RootBeginnermath.sqrt. Negative input: don't call it. Python will throw, not politely refuse.Open example →py# Program to calculate the square root of a number import math
- 15Convert Seconds to Hours, Minutes, and SecondsBeginner// 3600 for hours, % for leftovers. Same trick as making change.Open example →py# Program to convert seconds to hours, minutes, and seconds total_seconds = int(input("Enter total seconds: "))
- 16Calculate Body Mass Index (BMI)Beginnerweight / height². Height in metres. 175 cm is 1.75, not 175 — that BMI looks fatal.Open example →py# Program to calculate Body Mass Index (BMI) weight = float(input("Enter your weight in kg: ")) height = float(input("Enter your height in meters: "))
- 17Check Leap YearBeginnerDivisible by 4, except centuries, except 400. 1900 is not a leap year. 2000 is.Open example →py# Program to check if a year is a leap year year = int(input("Enter a year: "))
- 18Find Absolute ValueBeginnerIf it's negative, flip the sign. abs() exists; this is the if version.Open example →py# Program to find the absolute value of a number num = float(input("Enter a number: "))
- 19Calculate Age from Birth YearBeginnerthis year minus birth year. Wrong if your birthday hasn't happened yet. Close enough for a first program.Open example →py# Program to calculate age from birth year from datetime import date
- 20Sum of First N Natural NumbersBeginnerN(N+1)/2. A loop also works. The formula is O(1) and easier to get wrong with / vs //.Open example →py# Program to find the sum of first N natural numbers n = int(input("Enter a positive integer N: "))
- 21Calculate Power Without Using pow()BeginnerMultiply in a loop. Negative exponents: do the positive power, then 1/result.Open example →py# Program to calculate a^b without using pow() or ** for the core logic base = float(input("Enter the base (a): ")) exponent = int(input("Enter the exponent (b, integer): "))
- 22Print Multiplication TableBeginnerfor i in range(1, 11): print n * i. The `or 10` is the sneaky bit.Open example →py# Program to print multiplication table of a number num = int(input("Enter a number: ")) limit = int(input("Enter limit (default 10): ") or 10)
- 23Convert Decimal to BinaryBeginnerKeep dividing by 2, save the remainders, reverse them. bin(10) is 0b1010 if you are in a hurry.Open example →py# Program to convert decimal to binary without using bin() num = int(input("Enter a non-negative integer: "))
- 24Convert Binary to DecimalBeginnerLeft to right: double, add the bit. 1010 → 10. Reject anything that isn't 0 or 1.Open example →py# Program to convert binary to decimal without using int(x, 2) binary_str = input("Enter a binary number: ")
- 25Calculate Electricity BillBeginnerSlabs: first 100 at 5, next 100 at 7, rest at 10. 250 units should be 1850, not 2500.Open example →py# Program to calculate electricity bill using slab rates units = float(input("Enter electricity units consumed: "))