Python Variables and Types

Variables are the containers which can hold some value. Variables are nothing but reserved memory locations to store values. It means that when you create a variable, you reserve some space in the memory.
Python is dynamically Typed Language that means In Python, we don't need to specify the type of variable because Python is a type infer language and smart enough to get variable type.

  Variable names can be a group of both letters and digits, but they have to begin with a letter or an underscore.


Declaring and Assigning Values to Variables

In Python we don't need to provide the type of variable , Python is smart enough to know the type at run time .
We don't need to declare explicitly variable in Python. When we assign any value to the variable that variable is declared automatically.
The equal (=) operator is used to assign value to a variable.

Example


name='durgesh' #string type variable
number=568 #integer type variable
salary =566556.775 # float type  variable

print(name)
print(number)
print(salary)
durgesh
568
566556.775

Multiple Assignment

Python allows you to assign a single value to several variables simultaneously. For example ??

a = b = c = 1


# assigning the single value to multiple variable
a=b=c=d=565
print(a)
print(b)
print(c)
print(d)
565
565
565
565

Example : Assigning multiple values to multiple variables:

# assigning the multiple values to multiple variable
a,b,c,d=565,343,545,349
print(a)
print(b)
print(c)
print(d)
565
343
545
349

how to find the type of variable at run time

type(variable) - function is used to find the type of variable at Run time.


#Program to print the type of any varible at run time
n=45
v='testing'
f=4545.545
c=4+6j
#type of n
print(type(n))

#type of v
print(type(v))

#type of f
print(type(f))

#type of c
print(type(c))
 < class 'int' >
< class 'str' >
< class 'float' >
< class 'complex' >