Introduction To Python
The following article provides an outline for Python Variables. Python is a high-level, object-oriented, and interpreted programming language. Python supports dynamic semantics as well. It combines dynamic binding and dynamic typing, which makes it very lucrative for the developers for rapid development and scripting. Python has founded by Guido Van Rossum in 1991 (It recently completed 30 years).
Variables in programming sense are nothing but the assignment of names to a particular data type and allocate memory space to it. In the case of python, the variables are names assigned to objects. In python, each data type is considered as an object. The basic need of using the variable is to give identification or a named location to a particular value and then you can use it in your entire code that value by just referring to the variable. Two variables can have the same value as well, there is no hard and fast rule.
Key Takeaways
- Python is a high-level programming language.
- Variables are identifiers and memory allocators for any data type.
- How to name a variable and how not to?
- How to assign values to a variable and what different types of variables are available in python?
Variable In Python
In most programming languages, the variable is defined according to data types and then only that specific data type value can be assigned to a variable. But in the case of python it is really simple as all data types are considered as objects, one can just directly assign a variable to any data type say “int” and then can reuse it to assign it to another data type say “char”.
There are some rules though that shall be followed when assigning the variable name.
- The variable name could be in lower-case (a_z).
- The variable name could be in upper-case (A_Z).
- You can use a combination of digits(0_9) but not to start with a number.
- You can use special character underscore (‘_’), but not others like @,$,%,#,&,*, etc.
For example my_var, My_Var, My_Var_1 are valid but 1my_var, #_my_var are invalid.
How To Assign Values To Variables?
- Variable assignment is super easy in Python.
- There is no need to specify explicit memory space, it happens automatically when a value is assigned to a variable.
- For assigning the value to a variable you just need to put an equal sign (“=”).
- You can assign values to multiple variables at once also. You can assign a different value to the same variable by reassigning it using an equal sign.
Print A Variable In Python
Given below are the examples mentioned:
We will see 3 types of data types integer, float, and string.
Code:
print(4)
Output:
Code:
print(4.5)
Output:
Code:
print(“I love learning python”)
Output:
Variable Assignment: Single and Multiple Value Assignment.
Code:
a = 4
b = 4.5
c = “I love learning python”
print(a)
Output:
Code:
print(b)
Output:
Code:
print(c)
Output:
Types Of Variables In Python
Python has mainly five types of variables:
- Strings
- Numbers
- Lists
- Tuples
- Dictionary
1. Strings: Strings are nothing but a set of characters which in python are represented with quotes. You can print a slice of the string or assign it to another variable.
2. Numbers: Numbers could have 3 types of data types like int, float, complex numbers.
3. Lists: Lists are like compound data types, where you can have separate numbers, floats, or strings. Lists can also be sliced. It can also be understood as arrays in C. lists are enclosed with ‘[ ]’
4. Tuples: Tuples are more or less lists but tuples are immutable. Tuples are enclosed with ‘ ( )’
5. Dictionaries: Dictionaries in python are more or less like hash tables and contain keys and values.
Recommended Articles
This is a guide to Python Variables. Here we discuss the introduction, variable in python, how to assign values, print a variable & types of variables. You can also go through our other suggested articles to learn more –
Are you preparing for the entrance exam ?
Join our Python test series to get more practice in your preparation
View More