Lesson 2: Variables and Data Types
Welcome back, inquisitive minds. In this lesson, we shall dissect the very building blocks of Python programs—variables and data types.
What Are Variables?
Variables are akin to empty vessels, ready to hold a multitude of values for us. They are labels, identifiers that we bestow upon data.
x = 5
name = "Blaise"
Data Types in Python
Just as there are many types of vessels—be it a goblet for wine or a cauldron for potions—Python has different data types to hold various forms of data. Here’s a deeper look:
- Integers (int): These are whole numbers, without a decimal point. Example: 5, -3, 0
- Floating-point numbers (float): Numbers that have a decimal point. Example: 3.14, -0.01, 2.0
- Strings (str): A sequence of characters enclosed in quotes. Example: “Hello”, ‘Python’
- Booleans (bool): Represents true or false values. Example: True, False
Practice Exercises
- Define a variable to hold your age and print it.
Solution
age = 25 print(age)
- Define a variable to hold your name and another for your favorite color. Then print them.
Solution
name = "Blaise" color = "blue" print(name, color)
- Calculate the perimeter of a rectangle using variables for length and width.
Solution
length = 10 width = 5 perimeter = 2 * (length + width) print(perimeter)
Upcoming Lessons
- Lesson 3: Conditional Statements
- Lesson 4: Loops
- Lesson 5: Functions