Introduction to Python Variables
A Variable is nothing but a programming element used to define, store, and perform operations on the input data. Python variables are of four different types: Integer, Long Integer, Float, and String. Integers are used to define 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 variables 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. So 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 the type of the variable being handled. Instead, 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 a 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.
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:
Variable1 = 10 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 responsibility for the 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 language, the concept of variables plays a significant role in python. 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 the top 4 types of python, respectively. You can also go through our other suggested articles to learn more –
- String Array in Python
- Python Sets
- Pointers in Python
- Python Features