w3resource

What are variables in Python? Rules for naming variables in Python

Python variables: Definition, assignment, and naming rules

Python variables are names used to store and reference data in memory. They allow you to label a piece of data with a descriptive name. This makes it easier to work with and manipulate data. Variables can hold various types of data, such as numbers, strings, lists, and more.

Python variables are declared by assigning a value to a name using the assignment operator =. Variables are declared and assigned values as follows:

Code:

# Declare and assign values to variables
age = 25
name = "Izabela Gautfrid"
is_employee = True
salary = 1234.45

# Print the values of variables
print("Age:", age)
print("Name:", name)
print("Is Employee:", is_employee)
print("Salary:", salary)

Output:

Age: 25
Name: Izabela Gautfrid
Is Employee: True
Salary: 1234.45

In the code above:

  • age, name, is_employee and salary are variable names.
  • The values 25, " Izabela Gautfrid ", True, and 1234.45 are assigned to these variables, respectively.
  • We can use the print() function to display the values of the variables.

When declaring a variable in Python, you don't have to specify its data type explicitly. Based on the assigned value, the interpreter determines the data type.

Variables can be reassigned to new values:

Code:

age = 25  # Initial assignment
print("Age:", age)
age = 35  # Reassignment
print("Updated Age:", age)
is_employee = True # Initial assignment
print("Is Employee:", is_employee)
is_employee = False # Reassignment
print("Is Employee (Updated):", is_employee)

Output:

Age: 25
Updated Age: 35
Is Employee: True
Is Employee (Updated): False

Variables can also be used in expressions:

Code:

x = 6
y = 8
sum_result = x + y
product_result = x * y
print("Sum:", sum_result)
print("Product:", product_result)

Output:

Sum: 14
Product: 48

Python Variable Name Rules:

  • Must begin with a letter (a - z, A - B) or underscore (_)
  • Other characters can be letters, numbers or _
  • Case Sensitive
  • Can be any (reasonable) length
  • There are some reserved words which you cannot use as a variable name because Python uses them for other things.

Good Variable Name

  • Choose meaningful name instead of short name. roll_no is better than rn.
  • Maintain the length of a variable name. Roll_no_of_a-student is too long?
  • Be consistent; roll_no or RollNo
  • Begin a variable name with an underscore(_) character for a special case.
  • Convention: Python is flexible when it comes to naming, but following certain conventions can make your code more readable and consistent. The convention is to use lowercase letters for variable names, with underscores (_) to separate words in multi-word variable names (snake_case). For example, my_variable, student_age, total_amount, item_price.

Here are some examples of valid and invalid variable names:

Valid Variable Names:

age
price
_total
my_var
student_age
example_of_a_long_variable_name

Invalid Variable Names:

2age (starts with a digit)
student-age (contains a hyphen)
while (reserved keyword)
totalPrice$ (contains special character)


Follow us on Facebook and Twitter for latest update.