Python programs for beginners
Classic beginner programs, each with an explanation of the approach, the complete code, a real sample run and the mistakes to avoid. Open any program in the Py-CRAFT editor to run it, change it and watch it work line by line.
Basics
- Python program to add two numbersAdding two numbers is the smallest complete program: read input, calculate, show the result.
- Python program to swap two numbersSwapping exchanges the values of two variables.
- Python program to check if a number is even or oddA number is even when it divides by 2 with nothing left over.
- Python program to find the largest of three numbersFinding the largest of three numbers practises comparisons and the word
and. - Python program to check a leap yearA leap year has 366 days.
Numbers
- Python program to check a prime numberA prime number has exactly two divisors: 1 and itself.
- Python program to find the factorial of a numberThe factorial of n, written n!, is 1 × 2 × 3 × … × n.
- Python program to print the Fibonacci seriesIn the Fibonacci series each number is the sum of the two before it: 0, 1, 1, 2, 3, 5, 8 and so on.
- Python program to find the sum of digits of a numberAdding the digits of a number is the classic way to learn the
%and//operators. - Python program to check an Armstrong numberAn Armstrong number equals the sum of its digits, each raised to the power of the number of digits.
- Python program to find the GCD and LCM of two numbersThe greatest common divisor (GCD) is the largest number that divides both; the least common multiple (LCM) is the smallest number both divide into.
- Python program to print a multiplication tableA multiplication table is a neat first use of a
forloop and of f-strings. - Python program to calculate simple and compound interestSimple interest grows by the same amount every year; compound interest grows on the total so far.
- Python program to convert Celsius to FahrenheitConverting temperatures is a short program that practises decimals and formatting.
Strings
- Python program to reverse a stringReversing a string is often the first string exercise.
- Python program to check a palindrome stringA palindrome reads the same forwards and backwards, like
madamorlevel. - Python program to count vowels and consonantsCounting letters by type combines a loop, a membership test and a couple of string methods.
- Python program to count the frequency of wordsCounting how often each word appears is the standard use for a dictionary.
Lists
- Python program to find the second largest number in a listFinding the second largest value shows why you must think about repeated numbers.
- Python program to remove duplicates from a listRemoving duplicates while keeping the original order is a common list exercise.
- Python program for bubble sortBubble sort repeatedly compares neighbouring numbers and swaps them when they are in the wrong order.
Patterns
NumPy
How to learn from these programs
Do not just copy the code. Read the approach first and try to write the program yourself. Then compare it with the one here, run it with your own input, and use Step through in the editor to watch each variable change. Finally, change it: every page ends with ideas to try.