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 line is enough for your first Python script. Print Hello, World!, check that Python runs, and learn what quotes and a terminal newline actually do.Open example →py# if this prints, Python is installed print("Hello, World!")
- 02Add Two NumbersBeginnerWhy does adding two Python inputs sometimes give 1020? This beginner addition program converts text with float before calculating and printing a numeric sum.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 VariablesBeginnerTrace two Python swaps side by side: a temporary variable and tuple unpacking. New programmers can see exactly when each original value is read and replaced.Open example →py# Swapping using a temporary variable a = 5 b = 10
- 04Find ASCII Value of a CharacterBeginnerWhat number represents A? Use Python ord to inspect a character, reject empty or longer responses, and understand the Unicode detail behind this ASCII exercise.Open example →py# Program to find the ASCII value of a character ch = input("Enter a character: ")
- 05Check Even or OddBeginnerDecide whether an integer is even or odd with one Python remainder test. This first conditional exercise includes zero, negatives, and readable comparisons.Open example →py# Program to check if a number is even or odd num = int(input("Enter an integer: "))
- 06Find Largest of Two NumbersBeginnerChoose the larger of two Python numbers without forgetting equal inputs. A beginner three-branch walkthrough explains comparisons and the 12.0 output format.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 NumbersBeginnerTwo equal maxima can break a careless Python comparison. Follow this beginner three-number program and see why >= and and belong in the winning conditions.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 ZeroBeginnerGive positive numbers, negative numbers, and zero their own Python branches. Practice strict comparisons with decimal input in this beginner sign classifier.Open example →py# Program to check if a number is positive, negative, or zero num = float(input("Enter a number: "))
- 09Convert Kilometers to MilesBeginnerTurn a kilometer measurement into miles with Python float input and a named factor. A first conversion exercise explains units, multiplication, and rounding.Open example →py# Program to convert kilometers to miles kilometers = float(input("Enter distance in kilometers: "))
- 10Convert Celsius to FahrenheitBeginnerCheck 25°C against 77°F while building a Python temperature converter. See why new programmers need both the 9/5 scale factor and the extra 32 in the formula.Open example →py# Program to convert Celsius to Fahrenheit celsius = float(input("Enter temperature in Celsius: "))
- 11Calculate Area of a TriangleBeginnerCalculate a triangle’s area from base and perpendicular height in Python. This beginner formula exercise keeps units straight and explains why 1//2 is wrong.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 InterestBeginnerEnter 5, not 0.05, for a 5% annual rate in this Python simple-interest exercise. Follow the formula as a beginner and separate earned interest from principal.Open example →py# Program to calculate simple interest principal = float(input("Enter principal amount: ")) rate = float(input("Enter annual interest rate (in %): "))
- 13Calculate Compound InterestBeginnerWatch quarterly interest grow a balance with Python **. A beginner formula walkthrough explains period rates, compounding counts, and total versus interest.Open example →py# Program to calculate compound interest principal = float(input("Enter principal amount: ")) rate = float(input("Enter annual interest rate (in %): "))
- 14Calculate Square RootBeginnerCall Python math.sqrt only after checking the real-number domain. A beginner lesson connects square roots, float results, negative input, and ValueError.Open example →py# Program to calculate the square root of a number import math
- 15Convert Seconds to Hours, Minutes, and SecondsBeginnerSplit 3665 seconds into one hour, one minute, and five seconds with Python // and %. A beginner dry run shows exactly where each quotient and remainder goes.Open example →py# Program to convert seconds to hours, minutes, and seconds total_seconds = int(input("Enter total seconds: "))
- 16Calculate Body Mass Index (BMI)BeginnerSquare the height, check the units, then calculate BMI in Python. First-year practice covers safe division, ordered cutoffs, display rounding, and model limits.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 YearBeginner1900 is not a leap year; 2000 is. Build the full Gregorian test in Python and practice the 400, 100, and 4 exceptions with beginner-friendly Boolean logic.Open example →py# Program to check if a year is a leap year year = int(input("Enter a year: "))
- 18Find Absolute ValueBeginnerSee what Python abs normally hides by writing the sign decision yourself. This beginner lesson keeps the original input and explains a signed-zero wrinkle.Open example →py# Program to find the absolute value of a number num = float(input("Enter a number: "))
- 19Calculate Age from Birth YearBeginnerEstimate an age with Python date.today rather than a stale year constant. For beginners, the main lesson is why a birth year alone cannot establish exact age.Open example →py# Program to calculate age from birth year from datetime import date
- 20Sum of First N Natural NumbersBeginnerReplace a long addition with N(N+1)/2 in Python. A first-year exercise explains the pairing idea, exact integer division, and the positive-input requirement.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()BeginnerBuild 2³ one multiplication at a time in Python, without pow or **. Beginner traces cover the accumulator, zero exponents, reciprocals, and a zero-base trap.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 TableBeginnerMake a Python times table that stops at the requested row. Learn range’s exclusive stop, a blank-input default, and readable f-strings in a beginner loop.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 BinaryBeginnerCollect remainders to turn decimal 10 into binary 1010 with Python. New programmers trace the shrinking quotient, reversed bits, and the separate zero case.Open example →py# Program to convert decimal to binary without using bin() num = int(input("Enter a non-negative integer: "))
- 24Convert Binary to DecimalBeginnerRead binary 1010 as repeated double-and-add steps in Python. This beginner conversion checks for actual digits, rejects blank input, and rebuilds place value.Open example →py# Program to convert binary to decimal without using int(x, 2) binary_str = input("Enter a binary number: ")
- 25Calculate Electricity BillBeginnerBill each block of electricity at its own rate in Python. A beginner slab calculation shows why 250 units cost 1700, not 2500, under the stated example rates.Open example →py# Program to calculate electricity bill using slab rates units = float(input("Enter electricity units consumed: "))