0% found this document useful (0 votes)
5 views2 pages

Python Practice Exercises

Uploaded by

heymanish30
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views2 pages

Python Practice Exercises

Uploaded by

heymanish30
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

Python Practice Exercises for Beginners

1. Print Hello World


print("Hello, World!")

The simplest Python program prints text to the screen. This helps verify your setup.

2. Variables and Data Types


x = 10
name = "Alice"
pi = 3.14
print(x, name, pi)

Variables store data. Python automatically detects types like int, str, and float.

3. If-Else Logic
num = 7
if num % 2 == 0:
print("Even")
else:
print("Odd")

Conditional statements let you branch logic. Here we check if a number is even or odd.

4. Loops - For Loop


for i in range(5):
print("Iteration", i)

Loops help repeat tasks. `range(5)` generates numbers from 0 to 4.

5. While Loop
count = 1
while count <= 5:
print("Count:", count)
count += 1

While loops run until a condition is false. Always ensure the loop ends to avoid infinite loops.

6. Functions
def greet(name):
return "Hello " + name

print(greet("Alice"))

Functions group reusable code. They can take inputs and return results.

7. Lists
fruits = ["apple", "banana", "cherry"]
for fruit in fruits:
print(fruit)
Lists store multiple values. You can loop through them easily.

8. Dictionaries
student = {"name": "Alice", "age": 21}
print(student["name"], student["age"])

Dictionaries store key-value pairs. Useful for structured data.

9. File Handling
with open("example.txt", "w") as f:
f.write("Hello File!")

File handling lets you read/write data. Always close files, or use `with` which does it automatically.

10. Simple Class


class Dog:
def __init__(self, name):
self.name = name
def bark(self):
print(self.name, "says Woof!")

d = Dog("Buddy")
d.bark()

Classes define objects with attributes and methods. This is the basics of Object-Oriented
Programming.

You might also like