Python for AIPython for AI
Python Basics

Variables

Learn to store and name your data

What are variables?

A variable is like a labeled box where you can store information. You give it a name and put a value inside. Later, you can use that name to get the value back.

In real life:

  • Your name is a "variable" that stores what people call you
  • Your age is a "variable" that stores a number
  • Your favorite color is a "variable" that stores text

In Python, we create variables to store data our programs need to remember.

Creating your first variable

Here's how to create a variable:

python
name = "Alice"
age = 25
is_student = True

Let's break this down:

  • name is the variable name
  • = means "store the value on the right in the variable on the left"
  • "Alice" is the value we're storing
name
"Alice"
str
age
25
int
is_student
True
bool
Each variable is a labeled box in memory holding one value.

Naming rules

Python has rules for variable names:

Allowed:

python
user_name = "Dave"      # lowercase with underscores (Python style)
userName = "Dave"       # camelCase (works but not Python style)
age2 = 30              # numbers are OK (not at start)
_private = "secret"    # underscore at start is OK

Not allowed:

python
2age = 30              # Can't start with number
my-name = "Dave"       # No hyphens (Python thinks it's subtraction)
my name = "Dave"       # No spaces
class = "Python"       # Can't use Python keywords

Python naming convention

In Python, we use lowercase letters with underscores between words. This is called "snake_case" and it's the standard way to name variables in Python.

python
# Good Python style
first_name = "Alice"
user_age = 25
is_logged_in = True
shopping_cart_total = 49.99

# Avoid camelCase (this is for other languages)
firstName = "Alice"  # Works, but not Python style
userAge = 25
isLoggedIn = True
Use descriptive names! Instead of x or temp, use names like user_score or file_path.
Quiz
Not started
Which of these is a valid Python variable name?
Quiz
Not started
Which line follows Python's snake_case naming convention?
python
# Pick the one that matches Python style
A:  firstName = "Alice"
B:  first_name = "Alice"
C:  FirstName = "Alice"

Changing variables

Variables can change (that's why they're called variables!):

python
# Start with one value
score = 0
print(score)  # Shows: 0

# Change it
score = 10
print(score)  # Shows: 10

# Change it again
score = score + 5
score = score + 5
print(score)  # Shows: 15
Quiz
Not started
What is the output of the program?
python
username = "vp0fVibes"
password = ""
score = 0

password = "Hello"
score = 5

password = "HelloH@ck3rs!"
print(score)
score = 19

Data Types Basics

Every variable in Python holds a value — and every value has a type. Python automatically detects the type based on what you assign.

The 3 most common types you'll use right away:

  • str (text) → assigned with quotes: name = "Alice"
  • int (whole number) → assigned without quotes: age = 25
  • bool (True/False) → assigned as: is_student = True

You can check any variable's type using the built-in type() function.

python
name = "Alice"
age = 25
is_student = True

print(type(name))       # <class 'str'>
print(type(age))        # <class 'int'>
print(type(is_student)) # <class 'bool'>
Quiz
Not started
What does type("Hello") return?

Formatted Strings (f-strings)

An f-string lets you insert a variable's value directly inside a string. Just add f before the opening quote, then put the variable name inside curly braces .

python
name = "Alice"
age = 25

# Without f-string (old way)
print("My name is " + name + " and I am " + str(age))

# With f-string (Python way)
print(f"My name is {name} and I am {age}")
Quiz
Not started
What will this print?
python
city = "Delhi"
print(f"I live in {city}")

Common mistakes

Forgetting quotes around text

python
# Wrong
name = Alice  # Python looks for a variable called Alice

# Right
name = "Alice"  # This creates text

Using undefined variables

python
# Wrong
print(score)  # Error: score doesn't exist yet
score = 10

# Right
score = 10
print(score)  # Now it works

Confusing = and ==

python
# = means "store"
age = 25

# == means "compare" (we'll learn this later)
if age == 25:
    print("Quarter century!")

Practice

Try these exercises to lock in what you just learned.

Quiz
Not started
Which is a valid Python variable name?
Fill in the blanks
Not started
Assign a name and an age, then print both.
1
2
3print(name)
4print(age)
Code challenge
Not started
Given a=1, b=2, swap them and print b then a (so output is 1 then 2).

What's next?

Before we explore data types, let's learn about comments — a crucial tool for making your code readable.