Python Exercise Examples: 100+ Code Samples for Beginners

Python, being a high-level, interpreted programming language known for its readability and simplicity, is an excellent choice for beginners and professional developers alike. It's no wonder that it's one of the most popular programming languages globally. Getting proficient in Python requires practice, and what better way to learn than through exercise examples?

Python Basic: Exercises, Practice, Solution - w3resource
Python Basic: Exercises, Practice, Solution - w3resource

Python exercises not only help reinforce what you've learned but also challenge you to apply your knowledge in new and innovative ways. They can range from simple, bite-sized tasks to complex projects that span hours of coding. Let's delve into some categories of Python exercises and explore practical examples.

| Pythonista Planet
| Pythonista Planet

Control Structures and Conditionals

Control structures and conditionals are the backbone of any programming language, enabling us to dictate the flow of our program. In Python, these are implemented using keywords like if, elif, else, for, while, etc.

Python Code Challenge: Elevate Your Skills with this Simple Exercise
Python Code Challenge: Elevate Your Skills with this Simple Exercise

Loops: For loop

The for loop is used to iterate over a sequence (like a list, tuple, string, etc.) and execute a block of code for every element in the sequence.

Python list exercises guide
Python list exercises guide

Example: Print each character of a string using a for loop.

str1 = 'Hello, World!'
for char in str1:
    print(char)

Conditionals: if-elif-else

The if-elif-else statements are used to execute different blocks of code based on whether a certain condition is true or false.

Python Code For Geometry, Python Coding Patterns, Computational Geometry Python Code, Python Code For Shape Calculations, Python Pattern Program, Python Standard Deviation Tutorial, Python Code For Alphabet Pattern, Python Code With Pytest Fixture, Python Code For Factorial Design
Python Code For Geometry, Python Coding Patterns, Computational Geometry Python Code, Python Code For Shape Calculations, Python Pattern Program, Python Standard Deviation Tutorial, Python Code For Alphabet Pattern, Python Code With Pytest Fixture, Python Code For Factorial Design

Example: Classify a given year as a leap year or not using if-elif-else.

year = 2024
if (year % 4 == 0) and ((year % 100 != 0) or (year % 400 == 0)):
    print('Leap year')
else:
    print('Not a leap year')

Functions and Modules

Functions allow us to package a set of instructions to perform a specific task, making our code more organized and reusable. Modules, on the other hand, are files that contain the Python code, and they allow us to import and use functions and variables defined in them.

Python Cheatsheet for Beginners | Learn Python Fast
Python Cheatsheet for Beginners | Learn Python Fast

Defining and using Functions

Functions in Python are defined using the def keyword, followed by the function name and parentheses. The function body is then indented and enclosed in a block.

| Pythonista Planet
| Pythonista Planet
Python list exercises guide
Python list exercises guide
a poster with different types of functions in python
a poster with different types of functions in python
Python Programming Practice Questions Cheat Sheet 🐍
Python Programming Practice Questions Cheat Sheet 🐍
Python set exercises cheat sheet
Python set exercises cheat sheet
Python Practice Exercises for Beginners
Python Practice Exercises for Beginners
Master Python with these easy pattern programs
Master Python with these easy pattern programs
Python set exercises infographic
Python set exercises infographic
Python Exercise with Practice Questions and Solutions - GeeksforGeeks
Python Exercise with Practice Questions and Solutions - GeeksforGeeks
Learn Python in 10 days
Learn Python in 10 days
Python Practice Exercises for Beginners
Python Practice Exercises for Beginners
Python Cheat Sheet for Beginners
Python Cheat Sheet for Beginners
Python Notes for Beginners (Easy + Quick Guide)
Python Notes for Beginners (Easy + Quick Guide)
Shortcut to learn Python.[Cheatsheet]
Shortcut to learn Python.[Cheatsheet]
30-Day Python Learning Plan for Beginners | Complete Python Roadmap to Learn Coding Fast
30-Day Python Learning Plan for Beginners | Complete Python Roadmap to Learn Coding Fast
21 Python for Loop Examples and Exercises
21 Python for Loop Examples and Exercises
Day 3 – Operators | 10 Days Python Challenge
Day 3 – Operators | 10 Days Python Challenge
Python Cheat Sheet for Beginners 2026 | Python Basics, Syntax, Loops, Functions & Variables
Python Cheat Sheet for Beginners 2026 | Python Basics, Syntax, Loops, Functions & Variables
an image of a programming program with the words pyramids in it
an image of a programming program with the words pyramids in it
a poster with the words python master notes
a poster with the words python master notes

Example: Define a function to greet a person with their name.

def greet(name):
    print(f'Hello, {name}!')

greet('Alice')  # Outputs: Hello, Alice!

Importing Modules

We can import modules in Python using the import keyword, followed by the module name. We can also import specific functions or variables using the syntax module_name.function_name or module_name.variable_name.

Example: Import the math module and calculate the square root of a number.

import math
num = 25
sqrt = math.sqrt(num)
print(f'The square root of {num} is {sqrt}')

Python exercises are a fantastic way to improve your coding skills, understand Python's intricacies, and build creative projects. So, dive right in and start coding!

Related Articles

Web Python Games When Did Python 3 Get Released Python Exercise Examples Python 3 Keyboard Module Python3 Numpy Install Heap Explained Can Html And Python Work Together W3 Python Numpy Python3 Numpy Download Python Weblib