SlideShare a Scribd company logo
2
Most read
3
Most read
4
Most read
.
Class-XII Computer Science (083)
All the contents used as part of the slides are either self created or from the public domain or textbooks for Class XII.
This presentation is only used for leaning purpose only. Programs used in this presentation are based on Python 3.8.0.
Computational Thinking and Programming - 2
Working with Functions
Scope of Variables
S K Mahto, PGT (Computer Science)
J.N.V East Medinipur WB
Working with Functions
Scope of Variable
● The part of the program where a variable can be used is known as
Scope of variable.
● Or we can say, Scope of a variable is the portion of code where it is
available to read and update.
● There are two types of scopes :
Global Scope
Local Scope
Presented by : S K Mahto, PGT Computer Science
Working with Functions
Scope of Variable
● Note :
● By default, in python the scope is local to a functions.
● we saw that if we use a name inside a function and that it is different
from using the same name outside the function.
● But actually this happens only when we update the name inside the
function.
Presented by : S K Mahto, PGT Computer Science
Working with Functions
Scope of Variable
● Global Scope
With global scope, variable can be used anywhere in the program
Variable defined outside all functions are global variables.
Presented by : S K Mahto, PGT Computer Science
Working with Functions
Scope of Variable
● Two Examples – Global Scope
Presented by : S K Mahto, PGT Computer Science
def fun():
y=x
print(y)
x=5
fun()
def fun():
y=x
print(y)
x=10
x=5
fun()
What happened?
Let us see in practical…
Working with Functions
Scope of Variable
● Two Examples – Global Scope
Presented by : S K Mahto, PGT Computer Science
def fun():
y=x
print(y)
x=5
fun()
def fun():
y=x
print(y)
x=10
x=5
fun()
• If x is not found in fun(), python looks at enclosing function
for global x
• if x is updated in fun(), it becomes a local name.
Fine ! Error !
Working with Functions
Scope of Variable
Presented by : S K Mahto, PGT Computer Science
def fun():
y=x[0]
print(y)
x[0]=20
x=[5]
fun()
• Actually, this applies only to
immutable values.
Working with Functions
Scope of Variable
Presented by : S K Mahto, PGT Computer Science
def fun():
y=x[0]
print(y)
x[0]=20
x=[5]
fun()
• Actually, this applies only to
immutable values
• Global names that point to
mutable values can be updated
within a function.
Fine!
Working with Functions
Scope of Variable
Global immutable values
● What, if we want a global integer?
● Why would you want a global integer?
● To count the number of times a function is called
● To access global variable inside the function
prefix keyword global with the variable.
Presented by : S K Mahto, PGT Computer Science
def fun():
global x
y=x
print(y)
x=10
x=5
fun()
print(x)
Working with Functions
Scope of Variable
● Local Scope
With local scope, variable can be used only within
the function / block that it is created
Variables defined inside any function are local variables.
● Here f and i are local variable, and these can be used only within the function.
Presented by : S K Mahto, PGT Computer Science
Working with Functions
Summery
● Python variables are looked up inside-out from within function.
● Updating a variable with immutable values creates a local copy of
that name.
● Can update global variables with mutable values.
● Use global keyword to update immutable values inside the function.
● Local variables can be used within the function.
Presented by : S K Mahto, PGT Computer Science

More Related Content

PPTX
Intellectual technologies
PPTX
Presentation on python
DOCX
Python interview questions for experience
PDF
Demo1 use numpy
PPTX
Introduction to Structure Programming with C++
PDF
Introduction to r bddsil meetup
PDF
Scipy, numpy and friends
Intellectual technologies
Presentation on python
Python interview questions for experience
Demo1 use numpy
Introduction to Structure Programming with C++
Introduction to r bddsil meetup
Scipy, numpy and friends

What's hot (20)

PPTX
Python interview question for students
PPT
Hardness of approximation
PPTX
Postfix Notation | Compiler design
PDF
Stream Graphs with Python
PDF
Python functional programming
PPTX
Introduction To Programming with Python-3
PPT
PPTX
What is Python? An overview of Python for science.
PDF
Natural language processing open seminar For Tensorflow usage
PPTX
Combinators, DSLs, HTML and F#
PPT
Computer Programming (C++)Pointers and Arrays
PPTX
Chapter 1 - INTRODUCTION TO PYTHON -MAULIK BORSANIYA
PDF
Functional programming in C++
PDF
A Gentle Introduction to Coding ... with Python
PDF
Learning Algorithm Presentation
PDF
Python Functions Tutorial | Working With Functions In Python | Python Trainin...
PPT
Chpt9 patternmatching
PDF
New Insights and Perspectives on the Natural Gradient Method
PDF
Simulating Turing Machines Using Colored Petri Nets with Priority Transitions
PDF
On First-Order Meta-Learning Algorithms
Python interview question for students
Hardness of approximation
Postfix Notation | Compiler design
Stream Graphs with Python
Python functional programming
Introduction To Programming with Python-3
What is Python? An overview of Python for science.
Natural language processing open seminar For Tensorflow usage
Combinators, DSLs, HTML and F#
Computer Programming (C++)Pointers and Arrays
Chapter 1 - INTRODUCTION TO PYTHON -MAULIK BORSANIYA
Functional programming in C++
A Gentle Introduction to Coding ... with Python
Learning Algorithm Presentation
Python Functions Tutorial | Working With Functions In Python | Python Trainin...
Chpt9 patternmatching
New Insights and Perspectives on the Natural Gradient Method
Simulating Turing Machines Using Colored Petri Nets with Priority Transitions
On First-Order Meta-Learning Algorithms
Ad

Similar to 2 cs xii_python_functions _ scopes (20)

PPT
Python ppt
DOCX
Python interview questions and answers
PDF
Python interview questions and answers
PDF
Interview-level-QA-on-Python-Programming.pdf
PPTX
python questionsfor class 8 students and
PDF
First Steps in Python Programming
PDF
Intro-to-Python-Part-1-first-part-edition.pdf
PPT
10.ppt
PPTX
Python Programming-1.pptx of python by computer
PPTX
Python fundamentals
PPTX
pythontraining-201jn026043638.pptx
PPT
Introduction to python
PDF
Python and Pytorch tutorial and walkthrough
PPTX
Python training
PPTX
Python Functions 1
PDF
علم البيانات - Data Sience
PPTX
Python - An Introduction
PPTX
Introduction to Python and Basic Syntax.pptx
PDF
Blueprints: Introduction to Python programming
PDF
Core python programming tutorial
Python ppt
Python interview questions and answers
Python interview questions and answers
Interview-level-QA-on-Python-Programming.pdf
python questionsfor class 8 students and
First Steps in Python Programming
Intro-to-Python-Part-1-first-part-edition.pdf
10.ppt
Python Programming-1.pptx of python by computer
Python fundamentals
pythontraining-201jn026043638.pptx
Introduction to python
Python and Pytorch tutorial and walkthrough
Python training
Python Functions 1
علم البيانات - Data Sience
Python - An Introduction
Introduction to Python and Basic Syntax.pptx
Blueprints: Introduction to Python programming
Core python programming tutorial
Ad

More from SanjayKumarMahto1 (6)

PPTX
1 cs xii_python_file_handling text n binary file
PPTX
5 cs xii_python_functions _ passing str list tuple
PPTX
4 cs xii_python_functions _ properties of data object
PPTX
3 cs xii_python_functions _ parameter passing
PPTX
1 cs xii_python_functions_introduction _types of func
PDF
What is communication
1 cs xii_python_file_handling text n binary file
5 cs xii_python_functions _ passing str list tuple
4 cs xii_python_functions _ properties of data object
3 cs xii_python_functions _ parameter passing
1 cs xii_python_functions_introduction _types of func
What is communication

Recently uploaded (20)

PPTX
1st Inaugural Professorial Lecture held on 19th February 2020 (Governance and...
PPTX
Lesson notes of climatology university.
PDF
102 student loan defaulters named and shamed – Is someone you know on the list?
PDF
FourierSeries-QuestionsWithAnswers(Part-A).pdf
PPTX
Cell Types and Its function , kingdom of life
PDF
Chinmaya Tiranga quiz Grand Finale.pdf
PDF
Computing-Curriculum for Schools in Ghana
PDF
OBE - B.A.(HON'S) IN INTERIOR ARCHITECTURE -Ar.MOHIUDDIN.pdf
PPTX
Tissue processing ( HISTOPATHOLOGICAL TECHNIQUE
PDF
01-Introduction-to-Information-Management.pdf
PDF
Black Hat USA 2025 - Micro ICS Summit - ICS/OT Threat Landscape
PPTX
Introduction-to-Literarature-and-Literary-Studies-week-Prelim-coverage.pptx
PDF
Abdominal Access Techniques with Prof. Dr. R K Mishra
PDF
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
PDF
Supply Chain Operations Speaking Notes -ICLT Program
PPTX
Pharmacology of Heart Failure /Pharmacotherapy of CHF
PPTX
GDM (1) (1).pptx small presentation for students
PDF
3rd Neelam Sanjeevareddy Memorial Lecture.pdf
PPTX
Final Presentation General Medicine 03-08-2024.pptx
PDF
A GUIDE TO GENETICS FOR UNDERGRADUATE MEDICAL STUDENTS
1st Inaugural Professorial Lecture held on 19th February 2020 (Governance and...
Lesson notes of climatology university.
102 student loan defaulters named and shamed – Is someone you know on the list?
FourierSeries-QuestionsWithAnswers(Part-A).pdf
Cell Types and Its function , kingdom of life
Chinmaya Tiranga quiz Grand Finale.pdf
Computing-Curriculum for Schools in Ghana
OBE - B.A.(HON'S) IN INTERIOR ARCHITECTURE -Ar.MOHIUDDIN.pdf
Tissue processing ( HISTOPATHOLOGICAL TECHNIQUE
01-Introduction-to-Information-Management.pdf
Black Hat USA 2025 - Micro ICS Summit - ICS/OT Threat Landscape
Introduction-to-Literarature-and-Literary-Studies-week-Prelim-coverage.pptx
Abdominal Access Techniques with Prof. Dr. R K Mishra
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
Supply Chain Operations Speaking Notes -ICLT Program
Pharmacology of Heart Failure /Pharmacotherapy of CHF
GDM (1) (1).pptx small presentation for students
3rd Neelam Sanjeevareddy Memorial Lecture.pdf
Final Presentation General Medicine 03-08-2024.pptx
A GUIDE TO GENETICS FOR UNDERGRADUATE MEDICAL STUDENTS

2 cs xii_python_functions _ scopes

  • 1. . Class-XII Computer Science (083) All the contents used as part of the slides are either self created or from the public domain or textbooks for Class XII. This presentation is only used for leaning purpose only. Programs used in this presentation are based on Python 3.8.0. Computational Thinking and Programming - 2 Working with Functions Scope of Variables S K Mahto, PGT (Computer Science) J.N.V East Medinipur WB
  • 2. Working with Functions Scope of Variable ● The part of the program where a variable can be used is known as Scope of variable. ● Or we can say, Scope of a variable is the portion of code where it is available to read and update. ● There are two types of scopes : Global Scope Local Scope Presented by : S K Mahto, PGT Computer Science
  • 3. Working with Functions Scope of Variable ● Note : ● By default, in python the scope is local to a functions. ● we saw that if we use a name inside a function and that it is different from using the same name outside the function. ● But actually this happens only when we update the name inside the function. Presented by : S K Mahto, PGT Computer Science
  • 4. Working with Functions Scope of Variable ● Global Scope With global scope, variable can be used anywhere in the program Variable defined outside all functions are global variables. Presented by : S K Mahto, PGT Computer Science
  • 5. Working with Functions Scope of Variable ● Two Examples – Global Scope Presented by : S K Mahto, PGT Computer Science def fun(): y=x print(y) x=5 fun() def fun(): y=x print(y) x=10 x=5 fun() What happened? Let us see in practical…
  • 6. Working with Functions Scope of Variable ● Two Examples – Global Scope Presented by : S K Mahto, PGT Computer Science def fun(): y=x print(y) x=5 fun() def fun(): y=x print(y) x=10 x=5 fun() • If x is not found in fun(), python looks at enclosing function for global x • if x is updated in fun(), it becomes a local name. Fine ! Error !
  • 7. Working with Functions Scope of Variable Presented by : S K Mahto, PGT Computer Science def fun(): y=x[0] print(y) x[0]=20 x=[5] fun() • Actually, this applies only to immutable values.
  • 8. Working with Functions Scope of Variable Presented by : S K Mahto, PGT Computer Science def fun(): y=x[0] print(y) x[0]=20 x=[5] fun() • Actually, this applies only to immutable values • Global names that point to mutable values can be updated within a function. Fine!
  • 9. Working with Functions Scope of Variable Global immutable values ● What, if we want a global integer? ● Why would you want a global integer? ● To count the number of times a function is called ● To access global variable inside the function prefix keyword global with the variable. Presented by : S K Mahto, PGT Computer Science def fun(): global x y=x print(y) x=10 x=5 fun() print(x)
  • 10. Working with Functions Scope of Variable ● Local Scope With local scope, variable can be used only within the function / block that it is created Variables defined inside any function are local variables. ● Here f and i are local variable, and these can be used only within the function. Presented by : S K Mahto, PGT Computer Science
  • 11. Working with Functions Summery ● Python variables are looked up inside-out from within function. ● Updating a variable with immutable values creates a local copy of that name. ● Can update global variables with mutable values. ● Use global keyword to update immutable values inside the function. ● Local variables can be used within the function. Presented by : S K Mahto, PGT Computer Science