Variables are containers for storing data values. Here we will discuss about Python Variables.

Name conventions for Variables –

  1. Variable name cannot start with a number
  2. Variable name must start with a letter or the underscore character
  3. Variable name can only contain alpha-numeric characters and underscores
  4. Variable names are case-sensitive. For example , Firstname and firstname are two different variables.
Python Variables

1. A Variable is a name that is used to refer to memory location.

Example :-

x = 10
y = "Hello Python"
print(x)
print(y)

Result -
10
Hello Python

2. Variables are declared without any type and can be changed.

Example :-

x = 10 #variable type is int
x = "Hello Python" #now variable type is string,written as str 
print(x) #It will result Hello Python
Note - String variable can be declared by using single quotes or double quotes.

3. If you want to specify the type of a variable, this can be done with casting.

Example :-

x = int(10) #here x is 10
x = str(10) #here x is '10'
x = float(10) #here x is 10.0

4. Check type of a variable

x = 10
y = "Hello Python"
print(type(x))  #here type is int
print(type(y))  #here type is str

5. One value can be assigned to multiple variables

Example :-

x = y = z = 10
print(x)
print(y)
print(z)

Result –

10

10

10

6. Multiple values can be assigned to multiple variables in one line

Example :-

x , y , z = 10 , 20 , 30
print(x)
print(y)
print(z)

Result –

10

20

30

Hash(#) is used for single line comment in Python.

"""
This is a comment
written in
more than just one line
"""

Leave a Reply

Your email address will not be published.