0% found this document useful (0 votes)
4 views4 pages

Exploring Variables

The document explains the difference between local and global variables in Python, highlighting their accessibility within functions. It includes examples of defining and using these variables, as well as demonstrating variable assignment and formatting output. Additionally, it provides a list of Python keywords and showcases how to manipulate and delete variables.

Uploaded by

Mohan sharma
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views4 pages

Exploring Variables

The document explains the difference between local and global variables in Python, highlighting their accessibility within functions. It includes examples of defining and using these variables, as well as demonstrating variable assignment and formatting output. Additionally, it provides a list of Python keywords and showcases how to manipulate and delete variables.

Uploaded by

Mohan sharma
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Types of Variables

[Link] Variable >> not accessible outside the function and are defined inside the function [Link] Variable
>> accessible throught the program / accessible outside the function

In [10]:

1 import keyword
2 # print([Link])
3 help('keywords')

Here is a list of the Python keywords. Enter any keyword to get more hel
p.

False class from or


None continue global pass
True def if raise
and del import return
as elif in try
assert else is while
async except lambda with
await finally nonlocal yield
break for not

In [12]:

1 # Local Variable
2
3
4 def my_function():
5 x=500 # Local Variable
6 print(f"X = {x}")
7
8 my_function()
9
10

X = 500
In [14]:

1 def my_function():
2 x=500 # Local Variable
3
4
5 my_function()
6 print(f"X = {x}")
7

-------------------------------------------------------------------------
--
NameError Traceback (most recent call las
t)
Cell In[14], line 6
2 x=500 # Local Variable
5 my_function()
----> 6 print(f"X = {x}")

NameError: name 'x' is not defined

In [17]:

1 # Global Variable Program


2
3 g=800 # Global Variable
4 def my_function():
5 x=500 # Local Variable
6 print(f"X = {x}")
7 print(f"g = {g}")
8
9 my_function()
10 print(f"g = {g}")
11

X = 500
g = 800
g = 800

In [47]:

1 # local var >> how to access local var outside the function Stuent Assignment
2
3 def my_function():
4 global x
5 x=500 # Local Variable
6 print(f"X inside the function = {x}")
7
8
9 my_function()
10 print(f"x outise the function = {x}")

X inside the function = 500


x outise the function = 500
del keyword

In [23]:

1 x=300
2 print(x)

300

In [27]:

1 y=500.890
2 print(y)

500.89

In [25]:

1 # del y

In [28]:

1 y

Out[28]:

500.89

In [29]:

1 del y

In [30]:

1 y

-------------------------------------------------------------------------
--
NameError Traceback (most recent call las
t)
Cell In[30], line 1
----> 1 y

NameError: name 'y' is not defined

In [32]:

1 #assigning same value to diff variables


2 x=y=z=400
3 print(f"x = {x} , y = {y} , z = {z}")

x = 400 , y = 400 , z = 400


In [35]:

1 #assigning multiple values to muliple variables


2 name,age,location='Akshay',23,'USA'
3 print("Name is ",name)
4 print("Age is ",age)
5 print("Location is ",location)

Name is Akshay
Age is 23
Location is USA

In [50]:

1 print(f"Name is {name} , Age is {age} , Location is {location}")

Name is Akshay , Age is 23 , Location is USA

In [37]:

1 print("Name is {} , Age is {} , Location is {}".format(name,age,location))

Name is Akshay , Age is 23 , Location is USA

In [38]:

1 print("Name is {0} , Age is {1} , Location is {2}".format(name,age,location))

Name is Akshay , Age is 23 , Location is USA

In [40]:

1 print("Name is {1} , Age is {2} , Location is {0}".format(name,age,location))

Name is 23 , Age is USA , Location is Akshay

In [41]:

1 print("Name is {0} , Age is {1} , Location is {2}".format(age,name,location))

Name is 23 , Age is Akshay , Location is USA

In [ ]:

You might also like