top of page

What are Variables?

Variables are also called as identifiers which is a container used to store the values.

Declare a Variable

a = 45

As we discussed earlier that python is not a static typed but a dynamic typed language. so we don't need to assign the type of a variable while creating it. we can create a variable which holds the string value similar to we declare the above variable which holds the integer value.

Declare a Variable which holds string value

a = "Hello"

Python variable or identifier could be anything like you can even have your name as a variable.

Create a varible as your name and store your name

nilesh = "He is the best python programmer"

As you can see we could even have our name also as a variable but there are certain rules we should be abide by while creating a variable names.

Variables Declaration Rules

  • Variables can contain only alphabets, numbers and underscore.

  • Special Characters are not allowed

  • Variables must start with a underscore or alphabets.

  • Variables can't start with a number.

  • Variables can't be a Reserved Keyword.

  • Variables are case sensitive.

  • Variables can't have space in between.

As variables can't have space in between so to create a multi word variable we could use underscore in between or toggle with uppercase and lowercase to improve the readability. 

Create a multi word variable

my_name = "Nilesh"

my_favorite_language = "Python"

myFavoriteLanguage = 'Python'

Variables Multiple Assignment

In python, we can assign multiple variables a single value or multiple variables can be assign multiple values in a single line.

Multiple Variable to have a single value

a = b = c = 100

Multiple Variable to have a multiple value

a,b,c = 10,20,30

Single Variable to have a multiple value

a = 10,20,30

Variables Addition

Try adding to variables:

  • integer to integer

  • String to String

  • Integer to String

Adding to two variables.

a = 10

b = 20

c = a + b

print(c)

Variables Modification

Try changing the values of a variables.

Changing the value of a vairable

a = 10

print(a)

a = 15

print(a)

Variables Deletion

When you are done with the use of a variable. you can even delete it using the del keyword.

Delete a Variable

a = 10

print(a)

del a

print(a)

bottom of page