ProgramsPython

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!"
  1. 01Print "Hello, World!"BeginnerOne print. If this runs, Python is actually installed.Open example →
  2. 02Add Two NumbersBeginnerinput() gives strings. Forget float() and you glue 10 and 20 into 1020.Open example →
  3. 03Swap Two VariablesBeginnerThe textbook temp-variable swap, then the one-liner you will actually use.Open example →
  4. 04Find ASCII Value of a CharacterBeginnerord('A') is 65. Type two characters and this program refuses.Open example →
  5. 05Check Even or OddBeginnernum % 2. Remainder 0 means even. That is the whole trick.Open example →
  6. 06Find Largest of Two NumbersBeginnerThree cases: a bigger, b bigger, or they match. The equal case is the one people skip.Open example →
  7. 07Find Largest of Three NumbersBeginnera vs b vs c. Use >= so ties don't fall through into the wrong variable.Open example →
  8. 08Check Positive, Negative, or ZeroBeginnerZero is not positive. Put it in its own branch or you will lie to the user.Open example →
  9. 09Convert Kilometers to MilesBeginnerMultiply by 0.621371. Put the magic number in a named variable so you can find it later.Open example →
  10. 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 →
  11. 11Calculate Area of a TriangleBeginner0.5 * base * height. Height means perpendicular height, not a slanty side.Open example →
  12. 12Calculate Simple InterestBeginnerSI = P*R*T / 100. Rate is percent, so 5 means 5%, not 0.05.Open example →
  13. 13Calculate Compound InterestBeginnerA = P(1 + R/100n)^(nT). Then interest is A minus P. The ** is the whole point.Open example →
  14. 14Calculate Square RootBeginnermath.sqrt. Negative input: don't call it. Python will throw, not politely refuse.Open example →
  15. 15Convert Seconds to Hours, Minutes, and SecondsBeginner// 3600 for hours, % for leftovers. Same trick as making change.Open example →
  16. 16Calculate Body Mass Index (BMI)Beginnerweight / height². Height in metres. 175 cm is 1.75, not 175 — that BMI looks fatal.Open example →
  17. 17Check Leap YearBeginnerDivisible by 4, except centuries, except 400. 1900 is not a leap year. 2000 is.Open example →
  18. 18Find Absolute ValueBeginnerIf it's negative, flip the sign. abs() exists; this is the if version.Open example →
  19. 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 →
  20. 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 →
  21. 21Calculate Power Without Using pow()BeginnerMultiply in a loop. Negative exponents: do the positive power, then 1/result.Open example →
  22. 22Print Multiplication TableBeginnerfor i in range(1, 11): print n * i. The `or 10` is the sneaky bit.Open example →
  23. 23Convert Decimal to BinaryBeginnerKeep dividing by 2, save the remainders, reverse them. bin(10) is 0b1010 if you are in a hurry.Open example →
  24. 24Convert Binary to DecimalBeginnerLeft to right: double, add the bit. 1010 → 10. Reject anything that isn't 0 or 1.Open example →
  25. 25Calculate Electricity BillBeginnerSlabs: first 100 at 5, next 100 at 7, rest at 10. 250 units should be 1850, not 2500.Open example →