Menu

Variables and Operators

Introduction

Variables in Python are created the moment you assign a value. There's no need for type declarations — Python figures it out for you.

Variables

# Variable assignment
age = 25
name = "Alice"
is_student = True

# Multiple assignment
x, y, z = 1, 2, 3
a = b = c = 0

# Variable naming conventions (snake_case)
my_variable = "hello"  # ✓ Correct
myVariable = "hello"   # ✗ Not Pythonic

Python uses snake_case for variable and function names by convention (PEP 8).

Operators

# Comparison operators
5 == 5   # True
5 != 3   # True
5 > 3    # True
5 >= 5   # True

# Logical operators
True and False  # False
True or False   # True
not True        # False

# Identity operators
x = [1, 2]
y = [1, 2]
x == y   # True  (same value)
x is y   # False (different objects)

# Membership operators
3 in [1, 2, 3]      # True
"a" in "apple"       # True
4 not in [1, 2, 3]   # True

Assignment

  1. Read about Python variables in the official docs.
  2. Practice using comparison, logical, identity, and membership operators in the REPL.

Knowledge check

Support me!

I am a software engineer giving back to the community - my name is Musila Peter. Join me in empowering learners around the globe by supporting SaneGenius!