Python Variables

About Python Variables

In Python, variables are used to store and manipulate data.

Python is dynamically typed. You do not have to explicitly declare the data type of a variable.

Python variables serve as named containers that can hold values of different data types.

How to create variables in Python

Just choose a name for the variable and use the assignment operator = to assign a value to the variable.

Let us create three variables 'message', 'age' and 'length' to store a string, an integer and a float value respectively as shown in the following example.

  • Create a variable name 'message' and assign value 'Welcome to Python variables!' to the variable.
  • Create a variable name 'age' and assign value 30 to the variable.
  • Create a variable name 'length' and assign value 15.9 to the variable.
  • Add the print() function to display the values of the variables.
Example of Python Variables
Output of the above example
Welcome to Python variables!
30
15.9
                                

Assign same value to multiple variables in a single line

You can create variables by assigning the same value to multiple variables in a single line as shown in the following example.

In the following example, 'a = b = c = "Python"' refers that 'a = "Python"', 'b = "Python"' and 'c = "Python"'.

Example of multiple variables in a single line
Output of the above example
Python
Python
Python
                                

Assign different values to multiple variables in a single line

You can create variables by assigning different values to multiple variables in a single line.

In the following example, the code 'x, y, z = "Python", 15, 20.5' refers that 'x = "Python"', 'y = 15' and 'z = 20.5'.

Example of different values to multiple variables
Output of the above example
Python
15
20.5
                                

Python's Variable Naming Rules & Conventions

  • Variable names must start with a letter (a-z, A-Z) or an underscore (_). Example: my_variable or _myVariable
  • The rest of the variable name can contain letters, numbers, and underscores. Example: var1, data_2
  • Variable names are case-sensitive. myVar and myvar are considered different variables.
  • Python reserved keywords cannot be used as variable names. Example: if, for, while are invalid variable names. If you want to use keywords as variable names, add a trailing underscore. Example: if_, for_, while_ are valid variable names.
  • A variable name cannot start with a number. Example: 1st_variable is invalid.
  • Use lowercase with underscores for most variables. Example: my_variable
  • Use uppercase for constants. Example: PI = 3.14
  • Use descriptive names that convey the purpose of the variable. Example: total_price, user_age
  • Use camelCase for class names. Example: MyClass
  • Use leading underscores for private variables. Example: _private_var

Python Compiler For Live Practice

Practice the above Python code in the following Python Editor. Create more Python variables in the following code editor and click Execute button. Output will be displayed in Python Code Output section.

Python logo Python Editor
Python Code Output