Python – Variable and Global Variable

  • By
  • November 25, 2021
  • Python

Python – Variable and Global Variable –

In the python variable and data type are the basic concepts. Before we start using the python language must know how to define the variable, how to assign the values to variables, what are the rules to give the name to variable, what is mean by global function and how to define it etc. Also, we must know about the different data types in python language and how to convert the one data type into other data type. All this concepts we will discuss in this blog. 

For Free, Demo classes Call: 7507414653
Registration Link: 

Python Variables –

The python variables are used to store data values or in other words we can say that python variables are reserved memory position to store the value. When we create the variable, it will automatically reserve some space in memory. When we create the variable interpreter will allocate the memory for that variable and decide that what is to be stored in the reserved memory this is depends on the data type of variable. There is no any specific command to create the variable, when you assign the value to variable it will created and reserved the memory space. 

Example: x = ‘Ram’, y = 10, z = 9.5 

Sometimes variables are created by using the casting method in this method we can define the data type of a variable. 

Example:  x = str(‘Ram’), y = int (10), z = Float (9.5)

If we have not created the data type by using casting method then also, we can get the data type of variable by using the type () function. 

Variable Name –

The variable we can give in two ways either we can use short name (x, y) or descriptive name (name, age). When we define the variable name following rules must keep in mind

  • The name of variable must start with the underscore or letter character. 
  • The name of variable only contains the alpha numeric and underscore characters no any special character will be used in variable name.
  • The variable name is the case sensitive (name, Name and NAME is different variables).
  • Also, variable name cannot start with numbers.

 Example: name = ‘ram’ , Name_first = ‘ram’, _name = ‘ram’, name2 = ‘ram’ etc

We can give the variable name in multi words example: firstname. The variable name can be defined in camel case means except first word remaining words start with capital letters example: firstName.

The variable name also defined in pascal case in this case all words start with capital letters example: FirstName

Another method to define the variable name is snake case in this case each case separated with underscore example: first_Name

In python we can assign the values to multiple variables in one line of code. Also, we can assign the single value to multiple variables.

Example: x,y,z = ‘apple’, ‘mango’, ‘orange’  or x = y = z = ‘mango’

Suppose if we have a list then also we can extract the values from list and assign to variables.

Example: fruits = [ ‘apple’, ‘mango’, ‘orange’] so we can assign variables like x, y, z = fruits

To get the output variable in python we need to use the print statement. Also it is possible to get add the both text and variable, we can add variable into another variable just by using plus (+) operator.

Example: x= ‘programming language’ 

    Print( ‘Python is’ + x)

Here x is the variable and we are adding this in string given in print statement. So will get the output like this ‘Python is programming language’ 

Also, we can get the same output by another method 

X= ‘programming language’

Y = ‘Python is’

Z= X + Y

Here X and Y are the variables and we are adding this both variables using + operator in third variable. So, the output will be like this ‘Python is programming language’

If we are trying to add the two different data types like string and number then python will show the error. So, we cannot add two different data types using plus (+) operator.

For Free, Demo classes Call: 7507414653
Registration Link: 

Global and Local Variables –

If the variables are created outside the function, then variable is called as global variable. The global variables we can use inside and outside the function. 

Example: x = ‘programming language’

Def myfunc():

Print (‘Python is’ + x)

Myfunc()

If the variables are created inside the function, then variable is called as local variable. This local variable can be used inside the function only. 

Example: Def myfunc():

x = ‘programming language’

Print (‘Python is’ + x)

Myfunc()

If the variable created inside the function with same name of global variable, then that variable is also called as the local variable and can be used only inside the function only. In such case the global variable will remain same with same value. 

Example: x= ‘programming language’ 

Def myfunc():

x= ‘fantastic’ 

print (‘Python is’ + x)

myfunc()

print (‘Python is’ + x)

Global Keyword

In general case if we create variable inside the function then variable is called as local variable and only used within the function. If we want to create the global variable inside the function then we can use the global keyword inside the function. This type of variable we can use inside or outside the function. 

Example: Def myfunc():

Global x

x = ‘programming language’

Print (‘Python is’ + x)

Myfunc().

print (‘Python is’ + x)

The global keyword can also be used to change variable inside the function. In other words, we can say that if we want change the global variable value inside the function then will use global keyword. 

Example: x= ‘universally programming language’ 

Def myfunc():

Global x

x = ‘programming language’

Print (‘Python is’ + x)

Myfunc().

print (‘Python is’ + x)

For Free, Demo classes Call: 7507414653
Registration Link: 

In this article you learned about python variable, how to define variables, rules for variable name, global variables etc. All these basic thing required when use the python for programming. 

Author:-

Madhuri Diwan

Call the Trainer and Book your free demo Class  Call now!!!
| SevenMentor Pvt Ltd.

© Copyright 2021 | Sevenmentor Pvt Ltd.

Submit Comment

Your email address will not be published. Required fields are marked *

*
*