What exactly are Python Variables?
Variables, Data Types, Type Casting
The very first thing you have to learn when starting with python programming is python variables. If you want to get a basic understanding of python read:
Start with this If you are a Python Beginner
Python Variables are those we used in Mathematics, like Find X.
Well in mathematics the value of X vary that's why we call them variables. In Python, their value varies that's why we call them variables.
to declare a variable in python you can use this statement:
a = 10
Here we use =
because it is an assignment operator, it will give the value in right to the variable a
in left. You can say that value of variable a
is 10
How exactly Python will know the type of the variable?
Here python using Dynamic Data Types to identify the type of variable. That means the name a
is just referring to the value 10
Python treats the value 10 as an object. An object has the property of holding a memory slot of any size. We don't have to specify the type of it.
How do we know the type of the variable?
Python has a built-in function type()
which returns the type of the variable like this.
a = 10
print(type(a))
Output:
class < ' int ' >
The type of the variable a
is an integer.
a = 10.5
print(type(a))
Output:
class < ' float ' >
The type of the variable a
is decimal we call it float.
a = 'India'
print(type(a))
Output:
class < ' str ' >
The type of the variable a
is a set of characters we call string.
Type Casting
Can we convert the type of one variable into another type?
Yes we can use python built-in functions:
int()
float()
str()
Let's convert one type into another
a = 10.5
print(a)
print(type(a))
b = int(a)
print(b)
print(type(b)
Output:
10.5
class < ' float ' >
10
class < ' int ' >
These were some examples of python variables.