Variables In Python

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]:
age = 10
print(age)
10
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]:
age = 10
Age = 20
AGE = 30
print(age,Age,AGE)
10 20 30
2.We cannot name a variable starting with a number . 1age = 10
will give error.
In [3]:
1age = 20
File "<ipython-input-3-ef3a9e7a157c>", line 1
1age = 20
^
SyntaxError: invalid syntax
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]:
_company = "Console Flare"
company_name = "ConsoleFlare"
print(_company,company_name)
Console Flare ConsoleFlare
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]:
company , company_member = "Console Flare",5000
print(company,company_member)
Console Flare 5000
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]:
company = firm = startup = "Console flare"
print(company,firm,startup)
Console flare Console flare Console flare
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]:
age = 10
print(age)
age = 20
print(age)
10
20
Assignment:
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.

Last updated