Variables in Python

Variables are fundamental to any programming language, allowing you to store and manipulate data. In Python, variables are dynamically typed, which means you don’t have to specify the type when declaring them.


What is a Variable?

A variable is a name that refers to a value stored in the memory of your program. It acts as a label for the data you want to work with.

Example:

x = 10  # x is a variable holding the integer value 10
y = "Hello, World!"  # y is a variable holding a string value

Naming Variables

Variable names in Python must follow certain rules and conventions to be valid and readable.

Rules for Naming Variables

  1. Must start with a letter or an underscore (_):

    • Valid: name, _name
    • Invalid: 1name, @name
  2. Can contain letters, digits, and underscores:

    • Valid: name1, user_name
    • Invalid: user-name, user name
  3. Cannot be a Python keyword:

    • Invalid: if, for, while
  4. Case-sensitive:

    • Name and name are treated as two different variables.

Best Practices for Naming Variables

  • Use meaningful names that describe the data or purpose.
    age = 25  # Good
    a = 25   # Bad
    
  • Use snake_case for multi-word variable names.
    first_name = "Alice"  # Recommended
    firstName = "Alice"   # Avoid in Python
    

Declaring Variables

In Python, you don’t need to declare the type of a variable explicitly. The type is inferred from the assigned value.

Example:

# Integer
x = 10

# String
y = "Hello"

# Float
z = 3.14

# Boolean
is_active = True

Reassigning Variables

Python allows variables to be reassigned to a value of a different type during the program execution. This is because Python is dynamically typed.

Example:

x = 10
x = "Now I am a string!"
print(x)  # Output: Now I am a string!

Constants in Python

Although Python does not have built-in support for constants, developers use naming conventions to indicate variables that should not change.

Example:

PI = 3.14159  # A constant (by convention, use uppercase names)
GRAVITY = 9.8

Note: Python does not enforce immutability for constants.


Task

Practice: Working with Variables

Objective: Write a Python script to practice declaring and using variables with proper naming conventions.

  1. Create a Python File: Create a file named variables_practice.py.

  2. Declare Variables: Declare variables to store your name, age, and a floating-point number representing your height.

    Example:

    name = "Alice"
    age = 30
    height = 5.5
    
  3. Print Variables: Print the values of these variables using meaningful messages.

    Example:

    print("Name:", name)
    print("Age:", age)
    print("Height:", height)
    
  4. Reassign Values: Reassign one of the variables with a new value of a different type and print the updated value.

    Example:

    age = "Thirty"
    print("Updated Age:", age)
    
  5. Define Constants: Define two constants using uppercase variable names and print their values.

    Example:

    SPEED_OF_LIGHT = 299792458  # meters per second
    PLANCK_CONSTANT = 6.62607015e-34  # m^2 kg / s
    
    print("Speed of Light:", SPEED_OF_LIGHT)
    print("Planck Constant:", PLANCK_CONSTANT)
    

Understanding variables and their naming conventions is the first step toward writing clean, maintainable Python code. Next, we’ll explore data types to understand the kind of values variables can store.

Copyright © 2025 Devship. All rights reserved.

Made by imParth