Python Variables

Python Variables are used to store some data or values in the program. These variables are actually reserved memory locations whose values can be changed later during program execution.

Unlike other programming languages, there is no need of extra keyword or command to declare the Python variables. The Syntax of the Python variable is x = 123 . Here, x is a variable name and it stores a number 123.

Example of Python Variables:

We can store different types of value in the variables such as integer, float, string, complex etc.

Let’s see a simple example to store some values. After that, we will display those variables using the print function.

student_name = "Robin"        # variable of type str
student_age = 15              # variable of type int
user_height = 5.4             # variable of type float


Key points about Python Variables:

Here is the list of important points to remember while giving the name to the variables.

  1. We can’t use special characters anywhere in the name of variables, except underscore _.
  2. Space or Indents are not allowed in the variable naming such as var 1 is invalid variable.
  3. We can use digits (numbers) at the middle or at the end of the variable’s names. But digits are not allowed at the starting of the variable naming such as 12sum is invalid variable.
  4. We recommend to use the meaning name for the variables so that it is easy to understand.

Example of valid numbers:

variable1 = 12
user_15 = "abc"
EmployeeSalary = "10345"

Example of invalid numbers:

22131 = 334
$variable = 'xyz'
int 23 = '345'
user-name = ""

Variable names in Python is case sensitive. So, using Salary, salary will be treated as two different variables.

Assigning multiple values to variables in a single line

We can assign multiple values in a single line to the different variables kike other programming languages. Let’s see a simple example of it.

var_1, var_2, var_3 = "value_1", "value_2", "value_3"


Assigning a same value to the multiple variables

We can also assign the same value to multiple variables. Let’s see a simple example now.

var_1 = var_2 = var_3 = "value"


Printing multiple variables using a single print statement

We can print multiple Python Variables using a single print statement. Here is a simple example.

var_1, var_2, var_3 = 12, 23, 43
print(var_1, var_2, var_3)
12 23 43

Congratulations! Chapter Finished. Learn more about the similar topics:
Exercises & Assignments
No Content Found.
Interview Questions & Answers
No Content Found.