Submissions disabled: This deployment is running in read-only mode for safety. Code execution is not available here.
← Python Fundamentals

Variables and Data Types

Variables and Data Types

Variables are containers for storing data. Think of them as labeled boxes where you can put information.

Creating Variables

In Python, you create a variable by giving it a name and assigning a value:

name = "Alice"
age = 25
height = 5.6
is_student = True

Notice that we didn't have to declare the type - Python figures it out automatically!

Data Types

Python has several built-in data types:

Strings (str)

Text data, enclosed in quotes:

greeting = "Hello"
name = 'Bob'
message = """This is a
multi-line string"""

Integers (int)

Whole numbers:

count = 42
temperature = -10
year = 2024

Floats (float)

Decimal numbers:

pi = 3.14159
price = 19.99
percentage = 0.75

Booleans (bool)

True or False values:

is_active = True
has_permission = False

Checking Types

Use type() to check a variable's type:

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

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

Variable Naming Rules

  1. Must start with a letter or underscore
  2. Can contain letters, numbers, and underscores
  3. Case-sensitive (name and Name are different)
  4. Cannot use Python keywords (if, for, while, etc.)
# Good names
user_name = "Alice"
totalCount = 100
_private = "secret"

# Bad names (will cause errors)
# 2nd_place = "silver"  # Can't start with number
# my-variable = 5       # Can't use hyphens
# class = "Math"        # Can't use keywords

Practice

Try creating variables of different types and printing them:

# Your turn!
my_name = "Your Name"
my_age = 20
favorite_number = 7.5

print(f"Hi, I'm {my_name}!")
print(f"I'm {my_age} years old.")
print(f"My favorite number is {favorite_number}")

Note: The f before the string creates an "f-string" which lets you embed variables directly in text!