Flat-style educational banner titled Python Variables Explained showing variable examples like x = 10, name = 'Alice', and is_valid = True, with Python logo, checklist icon, and best practices tips on a blue background.

Python Variables Explained: Rules, Examples and Best Practices

Estimated reading time: 3.4 mins read

Variables are the building blocks of every program. They allow you to store, update, and manipulate data, making your programs dynamic and interactive.

What is a Variable?

A variable is a name that refers to a value stored in memory. Think of it as a label for a container that holds data.

Example:

# Storing an integer value
age = 25

# Storing a string value
name = "Nishtha"

# Printing values
print("Name:", name)
print("Age:", age)

Here, age and name are variables that store values.


Rules for Naming Variables

Python has rules and best practices for naming variables:

  1. Names must start with a letter or underscore (_)_username = "admin"
  2. Cannot start with a number : 1age = 25 ❌ Invalid
  3. Names are case-sensitive age = 25 Age = 30 print(age, Age) # Output: 25 30
  4. Avoid using Python keywords like if, for, print
  5. Use meaningful names (user_age instead of ua)

Snake Case Naming Convention

What is Snake Case?

Snake case is a naming convention where all letters are lowercase and words are separated by underscores (_).

Why Use Snake Case?

  1. Readability: Easier to read user_age than userage
  2. Consistency: Follows Python’s PEP 8 style guide
  3. Avoids errors: Makes variable names clear, especially when multiple words are used

Examples:

# Good variable names (snake_case)
user_name = "Nishtha"
total_salary = 50000
is_logged_in = True

# Bad examples
username = "Nishtha"      # Harder to read for multiple words
UserName = "Nishtha"      # Camel case, less Pythonic
userName = "Nishtha"      # Camel case, inconsistent with PEP8

Real-World Example:

# Taking user input using snake_case variables
first_name = input("Enter your first name: ")
last_name = input("Enter your last name: ")

full_name = first_name + " " + last_name
print(f"Welcome, {full_name}!")

Assigning Values to Variables

Single Assignment

x = 10
name = "Python"

Multiple Assignments in One Line

a, b, c = 5, 10, 15
print(a, b, c)

Assigning Same Value to Multiple Variables

x = y = z = 100
print(x, y, z)

Reassigning Values

Variables in Python are mutable, meaning you can change their value anytime.

# Initial value
salary = 50000
print("Old Salary:", salary)

# Reassigning value
salary = 60000
print("Updated Salary:", salary)

Dynamic Typing in Python

Python is dynamically typed, which means you don’t need to declare the data type explicitly.
The same variable can store different types of values at different times.

var = 10        # Integer
print(var, type(var))

var = "Python"  # Now a string
print(var, type(var))

var = 99.99     # Now a float
print(var, type(var))

✅ Output:

10 <class 'int'>
Python <class 'str'>
99.99 <class 'float'>

Real-World Examples

Example 1: Storing User Age

age = int(input("Enter your age: "))
print(f"You are {age} years old")

Example 2: Storing Salary

salary = float(input("Enter your monthly salary: "))
print(f"Your salary is: {salary}")

Example 3: Login Status

is_logged_in = True
if is_logged_in:
    print("Welcome, you are logged in!")
else:
    print("Please log in to continue")

Best Practices for Variables

  1. Use meaningful names (user_name instead of u)
  2. Stick to snake_case for variable names
  3. Avoid using Python keywords
  4. Initialize variables before using them
  5. Use dynamic typing wisely

Interview Questions with Answers

1. What is a variable in Python?

A variable is a name that stores data in memory.

2.Is Python statically typed or dynamically typed?

Python is dynamically typed, meaning variable types can change at runtime.

3.Can we assign multiple variables in one line?

Yes, e.g., a, b = 5, 10

4.Are variable names case-sensitive?

Yes, age and Age are different variables.

5.Can a variable store different data types at different times?

Yes, Python allows changing the data type of a variable dynamically.

6.Is it okay to start a variable name with a number?

No, variable names must start with a letter or underscore.

7.What is a meaningful variable name?

A name that clearly describes its content, e.g., user_age, total_salary.

8.Can we use Python keywords as variable names?

No, keywords like if, for, print cannot be used as variable names.


Happy Learning !


Comments

Leave a Reply

Discover more from Learn With Nishtha

Subscribe now to keep reading and get access to the full archive.

Continue reading