Basic Python Programs
Fundamental Python programs to get started with syntax, I/O, and basic problem solving.
25 program solutions
Program Solutions
Print "Hello, World!"
Your very first Python program that prints "Hello, World!" to the screen.
Add Two Numbers
Read two numbers from the user, add them, and display the result.
Swap Two Variables
Swap the values of two variables using a temporary variable and Python’s tuple unpacking.
Find ASCII Value of a Character
Read a character from the user and print its ASCII (Unicode code point) value.
Check Even or Odd
Check whether a given integer number is even or odd.
Find Largest of Two Numbers
Compare two numbers and print the larger one (or that they are equal).
Find Largest of Three Numbers
Compare three numbers and print the largest one (or that some/all are equal).
Check Positive, Negative, or Zero
Determine whether a given number is positive, negative, or zero.
Convert Kilometers to Miles
Convert a distance given in kilometers to miles using a standard conversion factor.
Convert Celsius to Fahrenheit
Convert temperature from Celsius to Fahrenheit using the standard formula.
Calculate Area of a Triangle
Calculate the area of a triangle given its base and height.
Calculate Simple Interest
Calculate simple interest given principal, rate, and time.
Calculate Compound Interest
Calculate compound interest given principal, rate, time, and number of times interest is compounded per year.
Calculate Square Root
Calculate the square root of a number using the math module.
Convert Seconds to Hours, Minutes, and Seconds
Convert a time duration in total seconds to hours, minutes, and seconds.
Calculate Body Mass Index (BMI)
Calculate BMI from weight (kg) and height (meters) and classify the result.
Check Leap Year
Determine whether a given year is a leap year using the Gregorian calendar rules.
Find Absolute Value
Compute the absolute value of a number without using the built-in abs() directly.
Calculate Age from Birth Year
Calculate current age from a given birth year using the current year.
Sum of First N Natural Numbers
Compute the sum of the first N natural numbers using a formula.
Calculate Power Without Using pow()
Raise a number to an integer power using a loop instead of the built-in pow() or **.
Print Multiplication Table
Print the multiplication table for a given number up to 10 (or a user-defined limit).
Convert Decimal to Binary
Convert a non-negative integer from decimal to binary representation.
Convert Binary to Decimal
Convert a binary string (e.g. 1010) to its decimal integer value.
Calculate Electricity Bill
Calculate an electricity bill based on units consumed using a simple slab system.