Variables In Python
Last updated
Last updated
What are Variables?
Variables are a container that store temporary values or information in computer memory.To use a variable in program, you must declare a variable.
What do we mean by declaring a Variable in python?
In Python, the moment we store any information or value in a variable, variable is declared.We can use variables in program once we declare them or store a value in them. There are two most important entity to declare a variable: a. Variable name b. Value
For Example : age = 10, Here 10 is a value stored in variable named age. This is how we declare a Variable in Python.
In [1]:
Naming Convention in Variable:
There are some rules and best practices to follow while naming a variable:
Best Practice: While naming a variable, Keep in mind to give variables a meaningful name. a = 10 does not give clear message than age = 10
Rules: 1.Variables are Case-Sensitive. It means a variable with lowercase is different than the variable in upper case.
In [2]:
2.We cannot name a variable starting with a number . 1age = 10
will give error.
In [3]:
3.We cannot use whitespaces or any symbol while naming a variable , though underscore is allowed (_). For Example -
a ge = 10 , @%age=10 will give error
_age = 10 , voting_age = 18 is good.
In [1]:
How to assign or store multiple values in multiple variables:
To assign multiple values in multiple variables , we can simply write them in a single line of code using commas. For Example:
In [2]:
How to assign a single Value in multiple Variable:
To assign a single value in multiple variables , we can simply write them in a single line of code using = sign. For Example:
In [3]:
How to change a value of a variable:
Suppose there is a variable named age with value 10 , we will write it like this (age = 10) to change the value of variable we will again store another value to the same variable. for Example:
In [4]:
Create a variable and store any numerical value.
Change the value stored in a variable.
Store 10,20,30 in three different variables in a single line.
Store 10 in three different variables in a single line.