Keyboard Shortcuts N Next post
P Previous post
S Save / unsave
R Read aloud
T Toggle theme
/ Focus search
Esc Close panels
🔥
Ready to read...
Module 1 - Python Fundamentals Python Python Basics Python Programming: From Zero to Real-World Applications

Variables and Assignment in Python

Reviewed & accurate
AI Summary

What You Will Learn

Beginner

  • How to create and use variables
  • Python dynamic typing
  • Variable naming rules and conventions

A variable is a named container for data. You assign a value with =:

Terminalpython
name = "Anita"       # String
age = 28             # Integer
height = 5.6         # Float
is_student = True     # Boolean

print(name)           # Anita
print(age)            # 28

Dynamic Typing

Python is dynamically typed - you do not declare the type. Python figures it out automatically. You can even change the type:

Terminalpython
x = 10          # x is an int
print(type(x))   # 

x = "hello"     # now x is a string
print(type(x))   # 

Multiple Assignment

Terminalpython
# Assign same value to multiple variables
a = b = c = 0

# Assign different values in one line
x, y, z = 1, 2, 3
print(x, y, z)   # 1 2 3

# Swap variables (no temp needed!)
a, b = b, a

Naming Rules

RuleExample
Letters, numbers, underscoresmy_var, age2, _count
Cannot start with a number2name is INVALID
Case-sensitivename != Name != NAME
Cannot use keywordsclass, if, for, def are reserved
Naming convention
Use snake_case for variables: first_name, total_count, is_active. Use ALL_CAPS for constants: MAX_SIZE, PI.

Practical Exercise

Create variables: name, age, city
Print each variable
Try: x, y = 10, 20 then swap them
Check types: print(type(name))

Key Takeaways

  • Variables store data. Assign with =.
  • Python is dynamically typed - no type declaration needed.
  • Multiple assignment: a, b, c = 1, 2, 3.
  • Naming: snake_case for variables, ALL_CAPS for constants.
  • Case-sensitive: name != Name.
Previously: Lesson 03 ran your first program.
Today: You learned: How to store and name data in Python.
Next: Lesson 05 covers data types.
Test Your Knowledge
How did you find this?

Comments

Join the discussion! Sign in with your Google or Blogger account, or comment as Anonymous - no account needed. For quick questions, also reach me on Telegram @cytestch.

Comments