Variable in Python
Variable in Python
Topperworld.in
Variable
Example:
Here we have stored “Topper World” in a var which is variable, and when we
call its name the stored information will get printed.
Output:
Topper World
Note:
The value stored in a variable can be changed during program
execution.
A Variables in Python is only a name given to a memory location, all the
operations done on the variable effects that memory location.
©Topperworld
Python Programming
Example:
# display
print( Number)
Output:
100
©Topperworld
Python Programming
Types of Variable
There are two types of variables in Python - Local variable and Global variable.
Let's understand the following variables.
Local Variable
Local variables are the variables that declared inside the function and have
scope within the function.
Example:
# This function uses global variables
def f():
s = "Topper World"
print(s)
Output:
f()
Topper World
Global Variables
Global variables can be used throughout the program, and its scope is in the
entire program. We can use global variables inside or outside the function.
Example:
# This function has a variable with name same as s.
def f():
print(s)
# Global scope
s = "I love Topper World"
f()
©Topperworld
Python Programming
Output:
Delete a variable
We can delete the variable using the del keyword. The syntax is given
below.
Syntax -
1. del <variable_name>
Example:
a = 10
b = 20
print(a+b)
a = "Topper"
Output:
b = "World"
30
print(a+b)
TopperWorld
©Topperworld