πŸ”„ Quick Recap (Day 3)

  • You mastered print() and saw how comments work.

  • You created simple scripts like hello.py and favorite.py.

🎯 What You’ll Learn Today

  1. What variables are and why we use them.

  2. The basic data types in Python: whole numbers, decimal numbers, text, and true/false values.

  3. How to convert values from one type to another.

πŸ“– Overview: What Is a Variable?

A variable is like a labeled box in the computer’s memory. You give the box a name and put something inside it: a number, a word, or a true/false value.

age = 5         # stores the whole number 5 in a box labeled age
name = "Alice"  # stores the text β€œAlice” in a box labeled name
  • The name on the left (age, name) is the box’s label.

  • The = sign means β€œput what’s on the right into the labeled box on the left.”

Why Use Variables?

  • Readability: Labels make your code much easier to understand.

  • Reusability: Use the same value in multiple places without retyping it.

  • Flexibility: Change the value in one place and it updates everywhere.

πŸ“– Data Types in Python

Every value has a type. The most common types you’ll use first are:

Type

Example

Description

int

5, -3, 0

Whole numbers

float

3.14, 0.0

Decimal (floating-point)

str

"hello"

Text (strings)

bool

True, False

True/false values

You can check a variable’s type with the type() function:

x = 10
print(type(x))   # ➞ <class 'int'>

y = "hi"
print(type(y))   # ➞ <class 'str'>

z = True
print(type(z))   # ➞ <class 'bool'>

πŸ“– Converting Between Types

Sometimes you need to change a value’s type:

  • str(value): turns value into text (string).

  • int(value): turns value into a whole number (if it’s valid).

  • float(value): turns value into a decimal number.

num = 5
text_version = str(num)      # "5"
print(text_version, type(text_version))

pi = 3.14
whole_part = int(pi)         # 3
print(whole_part, type(whole_part))

num_str = "42"
num_int = int(num_str)       # 42
print(num_int * 2)           # 84

πŸ§™β€β™‚οΈ Take the Wand and Try Yourself

  1. Create a file called data_types.py.

  2. Inside, declare and print these variables (each on its own line):

    • An integer of your choice.

    • A float of your choice.

    • A string with your favorite quote.

    • A boolean showing whether you like Python (True or False).

  3. Use type() to print the type of each variable.

  4. Convert your float to an integer, then print that integer and its type.

Solution Example (data_types.py):

# data_types.py
num = 10
print(num)
print(type(num))

pi = 3.14
print(pi)
print(type(pi))

quote = "Code is like magic."
print(quote)
print(type(quote))

likes_python = True
print(likes_python)
print(type(likes_python))

# Convert float to int
pi_as_int = int(pi)
print(pi_as_int)
print(type(pi_as_int))

Run:

python data_types.py

Expected output:

10
<class 'int'>
3.14
<class 'float'>
Code is like magic.
<class 'str'>
True
<class 'bool'>
3
<class 'int'>

Once you see these correct values and types, you’ve mastered variables and data types!

Ready for Day 5: Code Style & Best Practices? Let’s continue!

Keep Reading