Variables
Variables are containers for storing data values. In Python, variables are created the moment you assign a value to them using the equal sign (=). Python variables are case-sensitive and should be descriptive.
Example:
age = 30
first_name = "Alice"
Data Types
Python has several built-in data types. Some common data types are:
- String (str): A sequence of characters enclosed in single or double quotes.
- Integer (int): A whole number without a decimal point.
- Float (float): A number with a decimal point.
- Boolean (bool): A value that is either True or False.
Example:
text = "Hello, World!" # String
age = 25 # Integer
height = 1.75 # Float
is_student = True # Boolean
Type Conversion
You can convert between data types using built-in functions like int(), float(), and str().
Example:
number = 5
float_number = float(number) # Convert integer to float
Operators
Python has several types of operators, including:
- Arithmetic Operators: +, -, *, /, %, **, //
- Comparison Operators: ==, !=, >, <, >=, <=
- Logical Operators: and, or, not
Example:
x = 10
y = 3
add = x + y
subtract = x - y
multiply = x * y
divide = x / y
modulo = x % y
exponent = x ** y
floor_division = x // y
Hands-on Exercises
Exercise 1: Create a program that calculates the area of a rectangle.
length = 10
width = 5
area = length * width
print("The area of the rectangle is", area)
Exercise 2: Write a program that calculates the average of three numbers.
num1 = 10
num2 = 20
num3 = 30
average = (num1 + num2 + num3) / 3
print("The average of the three numbers is", average)
Exercise 3: Write a program to calculate the simple interest.
principal = 1000
rate = 5
time = 3
simple_interest = (principal * rate * time) / 100
print("The simple interest is", simple_interest)
Real-World Example
Suppose you want to calculate your monthly loan payment. You can use Python to create a program that takes the loan amount, interest rate, and loan term as inputs and calculates the monthly payment using the following formula:
Monthly Payment = P × r × (1 + r)^n / ((1 + r)^n – 1)
Where: P = Loan amount r = Monthly interest rate (annual interest rate / 12 / 100) n = Loan term in months
Additional Resources
- Python documentation: https://docs.python.org/3/tutorial/introduction.html
- W3Schools Python tutorial: https://www.w3schools.com/python/python_operators.asp
- Real Python: https://realpython.com/python-data-types/
Summary and Next Steps
In this lesson, you have learned about:
- Variables and data types in Python
- Type conversion
- Python operators
- Hands-on exercises
In the next lesson, you will learn about control structures in Python, including conditionals and loops. These concepts will allow you to create more complex and dynamic programs.
Remember to review the material periodically and practice your skills to reinforce your learning. Good luck on your journey to becoming a Python developer!
Review Questions
- What is the difference between a float and an integer in Python?
- How do you convert a string to an integer in Python?
- What is the purpose of the modulo operator (%) in Python?
- Write a Python program that takes the user’s input for their name and age, and then prints a message with their name and age in five years.
- Create a Python program that calculates the Body Mass Index (BMI) using the following formula: BMI = weight (kg) / height (m)^2. Take the user’s input for their weight and height, and then print the calculated BMI.
In Lesson 2, we have covered variables, data types, operators, and type conversion. We also provided hands-on exercises and real-world examples to help you practice what you’ve learned. As you move forward in your Python learning journey, you’ll build on these foundational concepts to develop more advanced programming skills. Remember to continually review the material, practice your skills, and seek out additional resources to strengthen your understanding of Python.



