Python Tutorials

Python Variables and Naming Rules

In programming, a variable is a named location in the computer’s memory used to store data. You can think of a variable as a container that holds a value, which can be accessed or modified later in the program.

Unlike languages like C, C++, or Java, Python is dynamically typed. This means you do not need to declare the data type of a variable before using it. The type is automatically determined based on the assigned value.

Creating and Assigning Variables

A variable is created the moment you first assign a value to it using the equal sign (=).

# Assigning a string value
name = "Amit"

# Assigning an integer value
age = 25

# Printing the variables
print(name)
print(age)
Code language: PHP (php)

Output:

Amit
25

Changing Variable Values

You can change the value of an existing variable at any time. You can even change its data type by assigning a different kind of value.

x = 10      # x is an integer
print(x)

x = "Hello" # x is now a string
print(x)
Code language: PHP (php)

Output:

10
Hello

Multiple Assignments in Python

Python allows you to assign values to multiple variables in a single line. This makes the code shorter and cleaner.

1. Assigning different values to different variables:

a, b, c = 10, 20, "Python"
print(a)
print(b)
print(c)
Code language: PHP (php)

2. Assigning the same value to multiple variables:

x = y = z = 50
print(x)
print(y)
print(z)
Code language: PHP (php)

Python Variable Naming Rules

While naming a variable, you cannot use any random text. Python has a strict set of rules that every programmer must follow. If you break these rules, Python will throw a SyntaxError.

Here are the mandatory rules for naming Python variables:

  1. Start with a Letter or Underscore: A variable name must start with a letter (a-z, A-Z) or the underscore character (_).
  2. Cannot Start with a Number: A variable name cannot begin with a digit (0-9).
  3. Allowed Characters: A variable name can only contain alpha-numeric characters and underscores (A-Z, a-z, 0-9, and _). Special characters like @, #, $, %, or spaces are strictly prohibited.
  4. Case-Sensitive: Variable names are case-sensitive. This means age, Age, and AGE are treated as three completely different variables.
  5. No Reserved Keywords: You cannot use Python built-in keywords (like if, for, while, class, import) as variable names.

Examples of Valid and Invalid Variable Names

# VALID Variable Names
my_name = "Rahul"
_age = 30
student1 = "Neha"
total_marks = 95

# INVALID Variable Names (Will cause SyntaxError)
2nd_student = "Karan"   # Cannot start with a number
my-name = "Rahul"       # Hyphens are not allowed
total marks = 100       # Spaces are not allowed
for = 50                # 'for' is a reserved keyword
Code language: PHP (php)

Naming Conventions (Best Practices)

When writing professional code, variables often consist of multiple words. To make these names readable, developers use specific naming conventions.

1. Snake Case (Recommended in Python) In this style, each word is separated by an underscore character. According to the official Python PEP 8 style guide, this is the standard way to write variable names.

  • Example: first_name, total_employee_count

2. Camel Case In this style, each word except the first one starts with a capital letter.

  • Example: firstName, totalEmployeeCount

3. Pascal Case In this style, every single word starts with a capital letter. This is generally used for naming Classes in Python, not variables.

  • Example: FirstName, TotalEmployeeCount
Tags: #Learn Python #PEP 8 #Python Basics #Python coding standards #Python dynamic typing #Python naming rules #Python syntax #Python variables #snake case Python #variable assignment

Leave a Comment