Introduction to Python Variables
A Variable is nothing but a programming element that is used for defining, storing and performing operations on the input data. Python variables are of four different types, and they are Integer, Long Integer, Float and String. Integers are used for defining numeric values, Long Integers are used for defining integers with bigger lengths than a normal Integer, Floats are used for defining decimal values and Strings are used for defining characters.
The process flow of variables can be defined as below,
- Creation of variable with suitable names
- Storage of values into the variables
- retrieve and process the variables as needed
Like every other programming language variables play a critical role in python too. let us discuss detailed on python programming language variables.
Variables Declaration
Like other programming, languages python does not expect a static variable declaration along with its type of the variable being handled. python has the ability to determine the type of the variable just based on the type of value being stored in it.
Example:
ten = 10
twenty = 20
thirty = 30
forty = 40
fifty = 50
sixty = 60
seventy = 70
eighty = 80
ninety = 90
Total = ten + twenty + thirty + forty + fifty + sixty + seventy + eighty + ninety
print("Print Total numeric value : " , Total )
ten = " 10 "
twenty = " 20 "
thirty = " 30 "
forty = " 40 "
fifty = " 50 "
sixty = " 60 "
seventy = " 70 "
eighty = " 80 "
ninety = " 90 "
Total = ten + " " + twenty + " " + thirty + " " + forty + " " + fifty + " " + sixty + " " + seventy + " " + eighty + " " + ninety
print("Print Total text value : " , Total)
Output:
Explanation
The above program shows the addition of values with a difference of ten up to ninety. each value is stored in a different variable. the significance is we can notice the process of operator overloading coming into play, in the first set the variables are stored with static numeric values whereas in the second set the numeric values are stored within double quotes which make them an as string value, this leads the value to be an addition in the first set whereas in the second set it turns out to be a concatenation of the strings involved.
4.8 (7,864 ratings)
View Course
Top 4 Types of Variables in Python
Some among the key variable types in python are as below,
1. Integer: Numeric values.
2. Long Integer: A integer whose length is greater than a usual integer type variable.
3. Float: Variables that are intended to hold floating precession values.
4. String: Variables that are intended to hold a string of letters.
Example:
Variable2 = int("111111111111111111111")
Variable3 = 3.14
Variable4 = "Numbers"
print("Print variable type of value ' 10 [variable1]' " , type(Variable1) )
print("Print variable type of value '[variable2]' " , type(Variable2) )
print("Print variable type of value ' 3.14 [variable3] ' " , type(Variable3) )
print("Print variable type of string ' 'Numbers' [variable4] ' " , type(Variable4) )
Output:
Explanation
The above program consists of four variables, one holds an integer value, the other holds a long integer value the next one holds a float value and the last one holds a string value. the print statements print the type of each of the variables used in the program.
Variable Casting
Variable casting is the process of converting a variable from one type to the other. for achieving this in python the casting functions are in place. the casting functions take the responsibility for conversion of the variables from their actual type to the other format,
- Type int(x) to convert x to a plain integer.
- Type long(x) to convert x to a long integer.
- Type float(x) to convert x to a floating-point number.
Example:
ten = 10
twenty = 20
thirty = 30
forty = 40
fifty = 50
sixty = 60
seventy = 70
eighty = 80
ninety = 90
Total = ten + twenty + thirty + forty + fifty + sixty + seventy + eighty + ninety
print("Print Total numeric value : " , Total ,"Variable Type :", type(Total) )
ten = str(ten)
twenty = str(twenty)
thirty = str(thirty)
forty = str(forty)
fifty = str(fifty)
sixty = str(sixty)
seventy = str(seventy)
eighty = str(eighty)
ninety = str(ninety)
Total = ten + twenty + thirty + forty + fifty + sixty + seventy + eighty + ninety
print("Print Total text value : " , Total ,"Variable Type :", type(Total) )
ten = float(ten)
twenty = float(twenty)
thirty = float(thirty)
forty = float(forty)
fifty = float(fifty)
sixty = float(sixty)
seventy = float(seventy)
eighty = float(eighty)
ninety = float(ninety)
Total = ten + twenty + thirty + forty + fifty + sixty + seventy + eighty + ninety
print("Print Total numeric value : " , Total ,"Variable Type :", type(Total) )
Output:
Explanation
The above program shows the addition of values with a difference of ten up to ninety. each value is stored in a different variable. here unlike the first program, the subsequent variables are typecast and the results of the casted values are printed along with their type. we can clearly notice how the process of typecasting converts a variable of integer type to string and then to float.
Conclusion
Like any other programming languages, the concept of variables plays a very significant role in python too, the classified number of functionalities and flexibility in coding them make variables more precise entities for access in the python programming language.
Recommended Articles
This is a guide to the Python Variables. Here we discuss the introduction, variables declaration with top 4 types of python respectively. You can also go through our other suggested articles to learn more –