Python Basic Exercises for Beginners. This tutorial is designed to help beginners get familiar with Python programming concepts through hands-on exercises. We will cover fundamental Python topics, including data types, control flow, functions, and more.
Before we begin, make sure you have Python installed on your computer. You can download Python from the official website: https://www.python.org/downloads/
Let’s get started with the exercises:
Exercise 1: Hello, World!
The classic “Hello, World!” program is the first step in learning any programming language. It prints a simple greeting to the screen.
pythonCopy code# Exercise 1: Hello, World!
print("Hello, World!")
Exercise 2: Variables and Data Types
In Python, you can use variables to store data. Python has various data types like int, float, str, bool, etc.
pythonCopy code# Exercise 2: Variables and Data Types
name = "Alice"
age = 25
height = 5.6
is_student = True
print("Name:", name)
print("Age:", age)
print("Height:", height)
print("Is Student:", is_student)
Exercise 3: Basic Arithmetic Operations
Perform basic arithmetic operations in Python.
pythonCopy code# Exercise 3: Basic Arithmetic Operations
a = 10
b = 5
print("Sum:", a + b)
print("Difference:", a - b)
print("Product:", a * b)
print("Quotient:", a / b)
print("Remainder:", a % b)
print("Exponent:", a ** b)
Exercise 4: Input from User
Take user input and perform operations on it.
pythonCopy code# Exercise 4: Input from User
name = input("Enter your name: ")
age = int(input("Enter your age: "))
print("Hello,", name)
print("You are", age, "years old.")
Exercise 5: If-Else Statements
Use if-else statements to control the flow of the program based on conditions.
pythonCopy code# Exercise 5: If-Else Statements
num = int(input("Enter a number: "))
if num % 2 == 0:
print(num, "is even.")
else:
print(num, "is odd.")
Exercise 6: Loops (For and While)
Learn how to use loops in Python.
pythonCopy code# Exercise 6: Loops (For and While)
# For loop
for i in range(1, 6):
print("Iteration", i)
# While loop
count = 0
while count < 5:
print("Count:", count)
count += 1
Exercise 7: Functions
Create and call functions in Python.
pythonCopy code# Exercise 7: Functions
def greet(name):
print("Hello,", name)
def add(a, b):
return a + b
greet("Bob")
result = add(3, 7)
print("Result:", result)
Exercise 8: Lists and List Comprehensions
Work with lists and list comprehensions.
pythonCopy code# Exercise 8: Lists and List Comprehensions
numbers = [1, 2, 3, 4, 5]
# Accessing list elements
print("First element:", numbers[0])
print("Last element:", numbers[-1])
# Slicing
print("Sliced list:", numbers[1:4])
# List Comprehension
squared_numbers = [num**2 for num in numbers]
print("Squared numbers:", squared_numbers)
Exercise 9: Dictionaries
Learn about dictionaries and how to use them.
pythonCopy code# Exercise 9: Dictionaries
person = {"name": "Alice", "age": 30, "is_student": False}
print("Name:", person["name"])
print("Age:", person["age"])
# Adding a new key-value pair
person["city"] = "New York"
# Looping through keys and values
for key, value in person.items():
print(key, ":", value)
Exercise 10: File Handling
Read and write files in Python.
pythonCopy code# Exercise 10: File Handling
# Writing to a file
with open("output.txt", "w") as file:
file.write("Hello, this is a text file.\n")
file.write("Writing to a file is easy in Python.")
# Reading from a file
with open("output.txt", "r") as file:
content = file.read()
print(content)
Congratulations! You’ve completed the basic Python exercises for beginners. By working through these exercises, you’ve learned essential concepts like variables, data types, control flow, loops, functions, and file handling in Python.
Keep practicing and exploring more Python resources to further enhance your skills! Happy coding!