Opens in a new tab
Lesson 2

Variables, Data Types, and Operators in Python

Python Mastery Program, Stage 1 Python, Variables & Data Types

Written by

Muhammad Atif

Last Updated

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:

Python
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:

Python
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:

Python
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:

Python
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.

Python
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.

Python
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.

Python
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

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

  1. What is the difference between a float and an integer in Python?
  2. How do you convert a string to an integer in Python?
  3. What is the purpose of the modulo operator (%) in Python?
  4. 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.
  5. 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.

Muhammad Atif

Data Analyst & Web Development Professional | Focused on Machine Learning, SQL and Python

Transitioned from a background in finance to IT, I now focus on data science and web development, with specialties in Machine Learning, SQL, and Python. My articles aim to clarify complex IT concepts, informed by a rich history in analytical thinking and strategy.

You Might Also Like

Browse more topics and insights handpicked just for you. Stay updated and informed with our latest articles and news.