“Mastering Python Variables: Essential Tips and Tricks”

Jeeshan
“Grokking Python Fundamentals”
4 min readJul 12, 2024

Python — Variables

Variables are essential in any programming language. They are used to store data values. In Python, variables are created when you assign a value to them, and they don’t require explicit declaration to reserve memory space. The variable is created the moment you first assign a value to it.

Creating Python Variables

Creating variables in Python is straightforward; you simply assign a value to a variable name.

Example

# Creating variables

first_name = “John” # Defining a first_name variable

age = 30 # Defining a age variable

height = 5.11 # Defining a height variable

Explanation:

  • first_name holds the string "John".
  • age holds the integer 30.
  • height holds the floating point number 5.11.

Printing Python Variables

To output the value of a variable in Python, you can use the print() function. This function sends the data you specify to the standard output, which is typically the console.

Example

print(height) # Outputs: 5.11

Explanation:

  • first_name, age, and height are variables holding the values "John", 30, and 5.11, respectively.
  • The print() function is used to display the values of these variables. Each call to print() outputs the value of the variable specified within the parentheses.

Deleting Python Variables

In Python, you can delete variables from the memory using the del statement. This can be useful when you want to free up memory or ensure that the variable is no longer accessible in later parts of your program.

Example

# Define variables

player_name = “Alice”

player_score = 120

# Printing variables before deletion

print(player_name) # Outputs: Alice

print(player_score) # Outputs: 120

# Deleting variables

del player_name

del player_score

# Trying to print deleted variables (this will cause an error)

print(player_name)

print(player_score)

Explanation:

  • Initially, player_name and player_score are defined and printed.
  • After using the del statement, these variables are removed from memory.
  • Uncommenting the last two print statements would result in an error because the variables no longer exist.

Case-Sensitivity of Python Variables

Python variables are case-sensitive. This means that variables such as Age, age, and AGE are treated as distinct.

Example

# Case sensitivity in Python variables

Age = 25

age = 30

AGE = 35

# Printing variables to show different values

print(Age) # Outputs: 25

print(age) # Outputs: 30

print(AGE) # Outputs: 35

Explanation:

  • Each variable (Age, age, AGE) is different due to Python's case sensitivity. They can hold different values without interfering with each other.

Python Variables — Multiple Assignment

Python allows you to assign values to multiple variables in a single line, which can make your code cleaner and faster to write.

Example

# Multiple assignment

x, y, z = 10, 20, 30

# Printing variables

print(x) # Outputs: 10

print(y) # Outputs: 20

print(z) # Outputs: 30

Explanation:

  • x, y, and z are simultaneously assigned the values 10, 20, and 30, respectively. This method of multiple assignment is useful for initializing several variables at once.

Python Variables — Naming Convention

When naming variables in Python, it’s important to follow certain conventions and rules to ensure that your code is readable and understandable. These conventions also help avoid conflicts with Python’s keywords and built-in function names.

Rules for Naming Python Variables:

  1. Variable names should start with a letter or an underscore (_).
  2. Variable names cannot begin with a number.
  3. Variable names can only contain alphanumeric characters and underscores (A-z, 0–9, and _).
  4. Variable names are case-sensitive (age, Age, and AGE are different variables).
  5. Avoid using Python keywords as variable names (e.g., if, else, class, etc.).

Example

# Correct variable names

username = “admin”

_user_id = 42

user2name = “guest”

# Incorrect variable names

# 2user = “guest” # SyntaxError: invalid syntax

# user-name = “admin” # SyntaxError: invalid syntax

# class = “data” # SyntaxError: invalid syntax

Explanation:

  • username, _user_id, and user2name are examples of valid variable names.
  • The commented-out variable names (2user, user-name, and class) illustrate common mistakes that will result in syntax errors.

Python Best Practices for Variable Names:

  • Use Descriptive Names: Variable names should be descriptive to indicate the kind of data they hold. For example, age is better than a, and username is better than usrnm.
  • Use Lowercase for Variables: It is a common practice to use all lowercase letters for variable names, with words separated by underscores if necessary (e.g., user_age).
  • Use CamelCase for Classes: Class names in Python are usually written using CamelCase, where each word starts with a capital letter, without underscores (e.g., UserProfile).

By adhering to these naming conventions and best practices, you can make your Python code more organized and easier for others (and yourself) to read and maintain. This is especially important in collaborative environments or when writing publicly shared code.

--

--

Jeeshan
“Grokking Python Fundamentals”

Data Analyst Enthusiast | Unveiling Insights Through Numbers | Helping You Navigate the World of Data Science | Exploring the Frontiers of ML and Generative AI