What are Variables?

Python Variables: Complete Beginner's Guide

Python Variables: Complete Beginner's Guide

What are Variables?

Imagine you have a glass of water. The glass acts as a container, and the water inside is the item/quantity it holds. Similarly in programming, variables are containers that temporarily store data while your program executes.

Variables can change their values during program execution - that's why they're called "variables".

Variable Naming Conventions

Three Standard Naming Methods:

Method Example Description
Snake Case user_name All lowercase with underscores between words
Camel Case userName First word lowercase, subsequent words capitalized
Pascal Case UserName First letter of each word capitalized

Important: Be consistent! If a project uses Pascal Case, stick with it throughout.

Python Variable Rules

  1. Must start with a letter or underscore _first_name
  2. Cannot start with a number 1st_name ← Invalid
  3. Can only contain letters, numbers and underscores
  4. Case sensitive: firstnamefirstName
  5. Cannot use Python reserved words like print, if

Creating Variables

Three components needed:

1. Variable name
2. Assignment operator (=)
3. Value to store

Examples:

fruit = "apple" # String
a = 10 # Integer
price = 99.99 # Float
is_valid = True # Boolean

Data Types in Python

1. Integers

Whole numbers (positive or negative) without decimals

age = 25
temperature = -10

2. Floats

Numbers with decimal points

pi = 3.14
temperature = 98.6

3. Strings

Sequence of characters enclosed in single/double quotes

name = "Pakistan"
message = 'Hello World'

Note: 123 is integer, "123" is string

4. Booleans

Only two possible values: True or False

is_active = True
has_permission = False

Key Takeaways

  • Variables are temporary storage containers
  • Follow naming conventions consistently
  • Python has strict variable naming rules
  • Main four data types: integers, floats, strings, booleans
  • Meaningful variable names improve code readability

Comments