Python variables in Python Programming
During this lecture, we are going to discuss the Python variables with more details. Are you want to know about Variable then you can also read my previous lecture. We have discussed in the previous lecture about “Python print variable and Python data types in Python 3”. In this lecture, you are going to learn how to use the correct data type of variables to compute the correct result in python programming. If we will use the wrong Data type then computed result will be wrong.
How to use Correct Python variables in Python Programming?
Example of Data Types
Like we initialize a Python Variables variable in string Datatype.
speed_of_light= ("23465394") print (speed_of_light)Â Â Â Â Â #speed of light in meter per second OUTPUT: 23465394
We will convert it in meter per minute.
If we can multiply direct this variable by (speed_of_light * 60). It will be wrong because string keyword will repeat 60 times as given below.
light = (speed_of light*60)Â Â Â print (light) Output: 23465394234653942346539423465394234653942346539423465394234653942346539423465394... repeat 60 times that is wrong output because we used wrong Data Type. if we can multiply any number by string then string alphabets will be repeated.
Now we will use the correct Python Variables data type
we will convert it in meter per minute by multiplies 60 using correct Python variable’s Data Type.
speed_of_light =Â 23465394 print (speed_of_light)Â # also speed of light in meter per second light = (speed_of_light*60) # it is correct because it is possible to multiply integer with integer print (light) Output: 23465394 1407923640
Correct Output √ because we have to use an integer data type that is correct according to the program logic. That’s why it’s much important to know about it data types and it’s used in the program.
How to use Multiple Python Variables with the Same Name in Python Programming?
We can use multiple variables with the same name in a program.
Example:
minute = 6                # Declare a minute variable minute = (minute+1)      #Now minute’s variable value will be 7 minute =(minute*2)       #Now minute’s variable value will be 14 print (minute) Output: 14
Note: Compiler start computes the value from top to bottom and Print those value which is given at the end before print statements. As you can see in the above example. I hope you are understanding the concept of value at each given line. In this lecture, we learn about “Python variables in Python Programming â€.