In this tutorial, we are going to learn about Python variables, and also we will learn types of data types in Python programming. In the previous tutorial, we have seen how to add Python to the Windows path to run the Python programmer on the terminal. If you don’t know, then you can click here to learn.
If you don’t know about variables in Python, don’t worry at the end of this article you can deal with variables in Python
Before going through this tutorial, Let’s see What are Variables in Python.
Headings of Contents
What are Variables in Python?
In Python programming, A variable is just like a container that can store value to be used in the future. If you have any value and you want to use that value in your program multiple times, then the variable is the best option to store.
In other words, You can say. Variable in Python is a reserved location in the memory that is used to store values to be processed.
Python is a dynamic type programming language which means you don’t need to specify the data type during the creation of variables. Python is very smart it can automatically detect the data type of a variable when you assign the value to the variable.
Important!
When you create a variable in Python, it occupies memory in RAM (Random Access Memory) to store its value. In Python, variables are essentially names that refer to objects in memory. When you assign a value to a variable, Python allocates memory to store that value, and the variable then refers to that memory location.
In the above picture, The name is a variable that refers to the string object “Programming Funda” stored in memory. So yes, every variable you create in Python consumes some memory in RAM.
Let’s see types of data types in Python. Python supports many types of standard data types.
Types of Data Type in Python :
Let’s see types of data types in Python. Python supports many types of standard data types.
- List
- String
- Tuple
- Set
- Int
- Float
- Complex
- Dictionary
Note:- If you want to check the data type of any variable, you can use the type() type built-in Python function.
Note:- Click Here to Learn all about the Python common Data Types
Creating Python Variables:
In Python programming, you don’t need to specify the data types during the creation of variables like C, C++, etc. Python automatically detects data types of variables, when you assign the values.
Example
x = 12 y = 12.23 name = 'Programming Funda' print(x) print(y) print(name) print(type(x)) print(type(y)) print(type(name))
You can change the data type of any variable.
Example
x = 23 print(type(x)) x = 'programming Funda' print(type(x))
Variables Names:
A Python variable can have short names ( like x and y ) or more descriptive names like the name. To create Python variables, you should have followed some rules.
- A variable name always starts with a letter and an underscore character.
- A variable can not start with a number
- A variable name can only contain alphanumeric characters and an underscore ( A-z, 0-9, and _ ).
- A Python variable is case sensitive which means name, Name, NAME, and naMe will be treated as different variables.
Valid Python Variable
name = 'Programming Funda' roll_no = 120 Score = 12300
Unvalid Python Variable
12age = 12 roll-no = 120 my score = 12300
Assign Value to the Multiple Variables:
You can also provide or assign values to multiple variables in a single line. As you can see below.
Example
x, y, z = 12, 'Programming Funda', 1200.89
You can also assign the same value to multiple variables into the same line.
x = y = z = 'Programming Funda' print(x) print(y) print(z)
Output Variables:
To get the value or output of a variable, use a Python print statement. Python print is a built-in function.
x = 'Programming Funda' print(x)
You can use the +
operator in the print function to add variable values.
Example
x = 'Programmig' y = 'Funda' print(x + y)
If you try to add a string with numbers, python raises an error.
Example
x = 'Programmig' y = 12 print(x + y)
Python Global Variables:
Variables that are created outside of a function are known as Python global variables.
Example
x = 'Programming Funda' def printNow(): print(x) printNow()
Python Local Variables:
The variables that are created inside a function are known as Python local variables:
Example
def printNow(): x = 'Programming Funda' print(x) printNow()
If you create a variable with the same name inside the function, then it will be treated as a local variable and can be only used inside the function.
Example
x = 'Python Tutorial' def printNow(): x = 'Programming Funda' print(x) printNow() print(x)
The global Keyword:
Normally, you can create a variable name inside a function, That variable is local. It is only used inside the function. To create a global variable name inside a function, use the global keyword.
Example
def printNow(): global x x = 'Programming Funda' print(x) printNow() print(x)
Conclusion
So, in this tutorial, We have seen all about Python variables like creating variables, and assigning value to variables, rules of creating variables, etc. I hope you don’t have any confusion regarding the Python variable and its uses.
Variables are the most important part of any programming language. It plays a major role in any Programming language Python itself. If you want to use a value in multiple places in Your program then you can store that value in a variable and after that, you can use that variable name in multiple places.
I hope this Python variables tutorial will help you to use variables in the Python program. If you like this tutorial pls share it with someone who wants to learn Python easily.