Exercices for Python Basics
Exercise Set 1: Comments and Basic Printing
-
First Steps with Comments Write a program that includes:
- A comment with your name
- A comment explaining what the program does
- A
print()
statement that displays "Hello, World!"
-
Comment Practice Look at the following code and add some comments explaining what each line does:
name = "Alice"
age = 25
print(name)
print(age)
Exercise Set 2: Basic Operations
-
Calculator Practice Create a program that:
- Creates two variables with different numbers
- Performs all basic arithmetic operations (+, -, *, /) between them
- Prints each result
- Includes comments explaining each operation
-
String Operations Write a program that:
- Creates two variables containing your first and last name
- Concatenates them with a space in between
- Prints the full name
- Prints the length of your full name
Exercise Set 3: Variables and Types
-
Type Explorer Create variables of different types and use
type()
to explore them:- Create an integer variable
- Create a float variable
- Create a string variable
- Create a boolean variable
- Print the type of each variable
-
Variable Transformation Write a program that:
- Creates a number variable with the value 42
- Converts it to a string and stores it in a new variable
- Prints both variables and their types
Exercise Set 4: Functions
-
Simple Function Creation Create a function that:
- Takes no parameters
- Prints "Good morning!" when called
- Call the function three times
-
Basic Calculator Function Write a function that:
- Takes two numbers as parameters
- Prints their sum
- Prints their difference
- Prints their product
- Call the function with different numbers
Exercise Set 5: Indentation Practice
-
Fix the Indentation Fix the indentation in the following code:
def greet(name):
print("Hello")
print(name)
print("How are you?") -
Create Nested Code Write a program that:
- Creates a function containing an if statement
- Uses proper indentation
- Has at least three levels of indentation
Challenge Exercises
-
Mini Calculator Program Create a program that:
- Defines separate functions for addition, subtraction, multiplication, and division
- Takes two numbers as input
- Performs all operations
- Includes appropriate comments
- Uses proper indentation
- Prints results in a formatted way
-
Type Converter Write a program that:
- Creates variables of different types (int, float, string)
- Converts between these types where possible
- Includes error handling comments for impossible conversions
- Prints the original and converted values with their types
Tips for Completing These Exercises:
- Try to solve each exercise without looking at solutions first
- Test your code with different inputs
- Pay attention to proper indentation
- Add comments to explain your code
- If you get stuck, break the problem into smaller parts
- Remember to use meaningful variable names