Loading, please wait...

A to Z Full Forms and Acronyms

What are Variables in Python

This is for absolute Beginners. Here we will learn about Variables , important points abouts variables and types of variables.

Before starting programming in python, We must know the fundamentals of Python. So In this article, we will learn about the concepts of  Variables in Python Programming with example code.

The following are the topics covered in this article.

  • What are the Variables in Python?
  • Variable Definition and Declaration
  • Types of Variables
  • Local Variable 
  • Global Variable
  • Use of Global Keyword

What are the variables in Python?

In a programming language, a variable is a temporary memory location where we store value.

The value you store may change in the future according to the specifications. let us understand this by diagram for better understandability.

Fig: Concept of Variable

Some important points about variables in Python:

  • Python has no command for declaring a variable.
  • It is created when you assign a value to it & it is not necessary to declare the variable datatype.
x= 2020
y= "Tutorial Link"
print(x)
print(y)
                    
#output: 80
#Tutorial Link
  •   Type easily changeable after they have been set.   
x= 2020
print(x)
x= "Tutorial Link"
print(x)
                          
#output: 2020
#Tutorial Link
  • STRING variables can be declared either by single or double-quotes.
country = "INDIA"
print("I love my " +country)
country ='INDIA'
print("I love my " +country)

#output:
I love my INDIA
I love my INDIA
  • The variable must start with a letter or underscore. The variable name must not start with a number.
    varbegin="Variable in python can begin with LOWER case"
    print(varbegin)
    #output:
    #Variable in python can begin with LOWER case
     
    
    
    Varbegin="Variable in python can begin with CAMEL case"
    print(Varbegin)
    #output:
    #Variable in python can begin with CAMEL case
    
    
    VARBEGIN= "Variable in python can begin with UPPER case"
    print(VARBEGIN)
    #output:
    #Variable in python can begin with UPPER case
    
    
    __varbegin= "Variable in python can begin with  underscore"
    print(__varbegin)
    #output:
    #Variable in python can begin with  underscore
    
    
    9@_varbegin="Variable in python cannot begin with a number or any special character"
    print(varbegin)
    #output:
    #can't assign to operator
  • The variables in python are case sensitive (i.e AGE, age, Age are not the same these will be considered as three different variables).
  • We can assign values to multiple variables in one line.and also We can assign the same value to multiple variables in one line.
x, y, z = "Orange", "Banana", "Cherry"

print(x)
print(y)
print(z)

#output:
#Orange
#Banana
#Cherry

a=b=c = "PYTHON"

print(a)
print(b)
print(c)

#output:
#PYTHON
#PYTHON
#PYTHON​
  • Note: python print statement is an output variable.
    we can use the "+" character to combine text and variables and also use the “+” character to add two or more variables.
    Proglanguage = input("Enter your favourite programming language: ")
    print("My Favourite programming language is: " +Proglanguage)
    #output:
    #Enter your favourite programming language: 
    #PYTHON
    #My Favourite programming language is:PYTHON
    
    a="Python is a "
    d="High level Programming Language"
    print(a + d)
    #output:
    #Python is a High level Programming Language

There are two types of variables

  1. Global variable: Global variables are those variables that are created outside a function and can be used inside as well as outside the variables.
  1. Local variable: Local variables are those variables that are created inside a function and can be used in this scope only. i.e we cannot access the local variable out of its scope or outside the function.

Note: To make a local variable work like a global variable we can use global keyword with that variable. 

#CODE SHOWING SCOPE OF GLOBAL VARIABLE
x = "awesome programming language"        #global variable
def myfunction():
    print("Python is " +x)

myfunction()

#output:
#Python is an awesome programming language


##CODE SHOWING SCOPE OF LOCAL VARIABLE
def myfunction():
    x = "awesome programming language"    #local Variable
    print("Python is " +x)


myfunction()
print("Python is " +x)

#output:
#Python is awesome programming language
#NameError: name 'x' is not defined


#CODE SHOWING USE OF 'GLOBAL' KEYWORD
def myfun():
   global x      # We can either assign a value or use global keyword at a time
   x = "high level and robust programming language"                   
   print("Python is " +x)


myfun()
print("python is " +x)

#output:
#Python is high level and robust programming language
#Python is high level and robust programming language
		
#note proper indentation of code is must. 

Through this article we learned the basics of Variables in Python, In the upcoming article, we will learn about other fundamentals of Python.

So stay tunned, Hope you find this article useful.

Till now, Happy Learning🙂  

A to Z Full Forms and Acronyms

Related Article