how to use variables in Python or any programming language. You should already know those programming variables are simply some containers, where we can sort everything. In Python, we can sort all numbers, text, files, and kinds of objects. It is effortless to create a variable in Python, pick a name for a variable and store something. I am creating a variable here and assigning it a number that says potato price equals three and another value variable amount equals to 2.5.
Potatoprice=3
Amount = 2.5
notice here that we Implicit declared the data types stored in the variable. You would have to write something like INT Potato Price to declare that the variable will be an integer in many other programming languages.
What
is the difference between Implicit and explicit variables in Python?
In Implicit
variable, we are not assigning the data type with them; the Python
automatically gets the data type as per the nature of the variable data.
In Explicit,
we have to define with the variable that what type of data we have to store in
the particular variable in programming like INT, double, string, etc.
So Int
stands for integer, and after that, you store the value, so you were variable.
In Python, However, you don't have to write that much; indeed, either number
you are assigning to your variable is a whole number. Python understands that
and makes a variable an integer type. But I'll tell you more about data types
in the future. For now, let's check the data type that our variables hold, and we
can do that using that type function, So we're going to print all the type of
data our variables hold, So to do that type, print and then all of the brackets
and inside the brackets will try to what to print out on the screen.
Example:
print(type(potatoPrice))
Output:
<class ‘int’>
In this
case, we want to print the int variable data type, the type function generates
the data type. So type, and then we pass the value in the type function, which
is a potato price variable.
So Python
tells us the value of the potato Price variable is an integer. Furthermore,
check the other variable as well. Print again type, and then we enter the other
variable. The value of the other variable is a floating-point number called
float for short in Python.
Now you can
assign any other values in the existing variables and even different data types
from what the variable called them currently hold. So here I'm going to assign
a string to my potato Price variable. Strings are like text, but I will call
that later. So potato price equals "free for today" The variable we
have is called. It is pretty evident by giving the new one.
Example:
potatoPrice = “Free for today”
Now I am
going to print the potato Price variable and let’s check out what exactly it
gets.
Print(PotatoPrice)
Output: Free
for today
That is all
about variables in python.
0 Comments