Python If-Else Programs with Solutions

Python Practice › If-Else Programs

Python If-Else Programs with Solutions

64 programs59 easy5 medium0 hard

64 if-else programs that practise decisions in Python: comparisons, elif chains, nested conditions and combining tests with and / or. Each solution tests several inputs so you can see every branch run.

Before you start

Read the If…Else and Match Case lesson first if the topic is new to you.

All programs

Python Basic Programs with Solutions

Python Practice › Basic Programs

Python Basic Programs with Solutions

40 programs34 easy6 medium0 hard

Start here if you are new to Python. These 40 basic programs use only variables, arithmetic, a few built-in modules and print(), and each one comes with an explained solution you can run in the browser.

Before you start

Read the Variables and Numbers & Math and Operators & Booleans lesson first if the topic is new to you.

All programs

Read File In Python

Method read() reads at most size bytes from the file and read hits the end of the file before obtaining size bytes, then it reads only available bytes.

read_file.txt input file

input file.txt
In the below program example, we are opening a text file with read mode, and read mode has this read method that read the entire file content from beginning to end. The open method accepts two parameters file path and file mode to open.
def read_file_content(filename):
    file = open(filename, 'r+')
    file_data = file.read()
    print(file_data)

filename = "input_file.txt"
read_file_content(filename)

Output:

output data


Python Loop Programs with Solutions

Python Practice › Loop Programs

Python Loop Programs with Solutions

90 programs75 easy14 medium1 hard

90 loop programs from counting and digit tricks to star and letter patterns. They practise for, while, range(), break, continue and nested loops; every solution is explained and shows its real output.

Before you start

Read the Loops: for & while lesson first if the topic is new to you.

All programs

Python Function Programs with Solutions

Python Practice › Function Programs

Python Function Programs with Solutions

39 programs35 easy4 medium0 hard

39 function programs that practise defining functions, parameters, return values, *args, nested functions and small menu-driven systems. Each solution is written as a reusable function and tested with several inputs.

Before you start

Read the Functions and Lambda and Scope lesson first if the topic is new to you.

All programs

Python Dictionary Programs with Solutions

Python Practice › Dictionary Programs

Python Dictionary Programs with Solutions

94 programs91 easy3 medium0 hard

94 dictionary programs: adding, removing and renaming keys, merging, sorting by key or value, counting with dictionaries, grouping, inverting, and working with nested dictionaries and lists of dictionaries. Every solution is explained with its real output.

Before you start

Read the Dictionaries lesson first if the topic is new to you.

All programs

Python List Programs with Solutions

Python Practice › List Programs

Python List Programs with Solutions

100 programs93 easy7 medium0 hard

100 list programs, from sums and reversing to sublists, de-duplication and run-length decoding. Lists are Python’s most used data structure, and these exercises appear in most Python interviews. Every solution is explained and shows its real output.

Before you start

Read the Lists and List Comprehension lesson first if the topic is new to you.

All programs