Variables

A Python variable is simply a name (a label) you use to refer to a value (the information). It allows you to store, retrieve, and change data in your program in an organized way.

Creating Variables

Creating a variable is simple in Python. You just give it a name and use the equals sign (=) to put a value inside the “box”.
Syntax (The Rule): variable_name = value
Example What it does (Box Analogy)
age = 30 Creates a box labeled age and puts the number 30 inside.
name = "Sara" Creates a box labeled name and puts the text “Sara” inside.
is_sunny = True Creates a box labeled is_sunny and puts the value True inside.

Variable Names

A variable can have a short name (like a and b) or a more descriptive name (age, name, total_number).
Golden Rules for Naming Variables
  • A variable name must start with a letter or the underscore character.
  • A variable name cannot begin with a number.
  • A variable name can only contain alphanumeric characters and underscores (A-z, 0-9, and _).
  • Variable names are case-sensitive (e.g., age, Age, and AGE are three different variables).
  • Cannot be one of Python’s reserved words (like if, for, while, etc.).
Example
user_age = 30
high_score = 9875
daily_temp = 25.5
item_price = 49.99
first_name = "Jamie"
website_url = "www.estudy247.com"
status_message = "Order Shipped"
is_active = True
has_discount = False
total_amount_2 = 150
MAX_CAPACITY = 5000
_temp_var = "Internal data"

Key Concepts for Variables

1. Variables are Dynamic (They Can Change!)

A variable is not set in stone. You can change the value in the “box” whenever you need to.

  • Start: score = 100 (The score box holds 100)
  • Change: score = 150 (The score box now holds 150. The old value 100 is forgotten.)

You can even change the type of information a variable holds (though this is often avoided in professional code, Python allows it).

  • Change Type: data = 5 (Holds a number) –> data = "hello" (Now holds text)
2. Data Types (What's in the Box?)
The type of value a variable holds is called its Data Type. Python figures this out automatically.
The three main types you’ll use are:
TypeLayman’s TermPython NameExample
Whole NumbersIntegersint10, -5, 0
TextStrings (anything inside quotes)str"Hello", "30", "A"
True/FalseBooleansboolTrue, False
3. What is a Global Variable?

In Python, variables have different scopes, which simply means where they are visible or where they can be used.

  • A Global Variable is a variable defined in the main body of your Python script (outside of any specific function).
  • Think of it as a central bulletin board available to everyone in the office (your entire program). Any function or piece of code can read its value.
Example
MAX_PLAYERS = 50  # This is a Global Variable

def check_limit(new_players):
  # This function can READ MAX_PLAYERS without any special keywords.
  if new_players > MAX_PLAYERS:
      print("Limit reached!")
Post a comment

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top