HOME C C++ PYTHON JAVA HTML CSS JAVASCRIPT BOOTSTRAP JQUERY REACT PHP SQL AJAX JSON DATA SCIENCE AI

Python Variables

Variables are containers for storing data values.

Example:
x=5
gpa=3.38

Creating Variables

Python has no command for declaring a variable.

A variable is created the moment you first assign a value to it.

Example
x = 5
y = "Rohan"
print(x)
print(y)               
              

Casting


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

Example
a = str(4)    # a will be '4'
b = int(4)    # b will be 4
c = float(4)  # c will be 4.0               
              

Get the Type


You can get the data type of a variable with the type() function.

Example
a = 5
b = "Mohan"
print(type(a))
print(type(b))               
              

Single or Double Quotes?


String variables can be declared either by using single or double quotes:

Example
a = "Mohan"
# is the same as
a = 'Mohan'