We will start with Variable in Python before starting this blog you must be clear in basic concepts of Python https://ainewgeneration.com/introduction-to-python/. So what is variable? The word variable we used in mathematics concepts for Algebra. Where we are using x & y as variables, let x be 5 and y be 6 in Algebra. The same concepts arise in a programming language, in which a variable is used to store some value. Let’s get started with more details about the python variable.
Table of Contents
- What is Variable ?
- Declaring variable
- Rules for Variable Naming
- Assigning Values
- Single Assignment
- Multiple Assignment
- Python Variable Types
- Local Variable
- Global Variable
What is Variable ?
A variable is a container where you can put your values for example in below image 2 is value and where 2 is store in container name as x.
We don’t need to declare the type of variable. python automatic detects what type of value you are assigning. for example :
- x= 2 (python automatic declare it is an integer)
- x = 2.3 (python automatic declare it is an float)
- x = 2i+3j (python automatic declare it is an complex)
- x = “AI Next Generation” (python automatic declare it is an string)
Rules for Variable Naming
There are some rules given below when you want to declare a variable name :
- The first character of the variable should be alphabetical or underscore (_).
- All characters except the first character can be alphabetical alphabet (a-z), uppercase (A-Z), underscore, or digit (0-9).
- The variable name must not contain a white space, or a special character (!, @, #,%, ^, &, *).
- The variable name must not be the same as any keyword defined in the language.
- The variable names are sensitive; for example, ainextgeneration, and AINextGeneration is not the same.
The equivalent operator (=) is used for variable conversion i.e equal operator “=” just assign value to a variable.
We have already seen above about a single assignment if you want to store or assign any value in a single variable. for example : a = 50 , b =100, z = “AI Next Generation” etc.
We can store or assign any single value in multiple variables or multiple values in multiple variables which are known as multiple assignments. let’s understand with example with single and multiple assignments.
- Assigning single value to multiple variables :
a = b = c = 100 print("a:" ,a) print("b:" ,b) print("c:" ,c) output: a : 100 b : 100 c : 100
- Assigning multiple values to multiples variables :
a,b,c = 10 ,20 ,30 print("a:" ,a) print("b:" ,b) print("c:" ,c) output: a : 10 b : 20 c : 30
Python Variable Types
There are 2 types of variables: Local Variable and Global Variable we will understand the basic difference between them with an example using function.
Local variables are declared within the function and are wide within the function. let’s understand with an example in the below code using function.
# Declaring a function def my_name(): #defining a local variable inside function age = 22 n = "my name is AI Next Generation" print(n," ,my age is :" ,age) #calling my_name function my_name() output:my name is AI Next Generation,my age is:22
In the above, we build a function my_name() where age and n are the local variables we have used so they are known as local variables as they can’t be used outside of function as it will display an error i.e variable name is not defined.
Global Variable is declared anywhere as whenever you are defining a variable name outside of function the variable automatic becomes a global variable and you can use a variable that within function too but if you want to declare any variable name inside the function python provides a specific keyword i.e Global let’s understand with code given below :
#Declare a variable x = 100 def my_name(): #Declare a global variable global x print(x) #modify a global variable x = "AI Next Generation" print(x) #call function my_name() my_name() print(x) output: 100 AI Next Generation AI Next Generation
In the above code, we declared x is a global variable and function my_name() we create global variable x by using keyword i.e global now we can modify the variable x and assigned other value to it.
I hope this blog variable in python is much clear from basic to advanced In the next blog, we will focus on the list in python.