SlideShare a Scribd company logo
1 of 11
.
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

What's hot

Stream Graphs with Python
Stream Graphs with PythonStream Graphs with Python
Stream Graphs with Python
Nathan Bergey
 
Combinators, DSLs, HTML and F#
Combinators, DSLs, HTML and F#Combinators, DSLs, HTML and F#
Combinators, DSLs, HTML and F#
Robert Pickering
 
Learning Algorithm Presentation
Learning Algorithm PresentationLearning Algorithm Presentation
Learning Algorithm Presentation
Phuong Nguyen
 

What's hot (20)

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

Similar to 2 cs xii_python_functions _ scopes

Python Programming-1.pptx of python by computer
Python Programming-1.pptx of python by computerPython Programming-1.pptx of python by computer
Python Programming-1.pptx of python by computer
sharanyarashmir5
 
Python and Pytorch tutorial and walkthrough
Python and Pytorch tutorial and walkthroughPython and Pytorch tutorial and walkthrough
Python and Pytorch tutorial and walkthrough
gabriellekuruvilla
 

Similar to 2 cs xii_python_functions _ scopes (20)

Python ppt
Python pptPython ppt
Python ppt
 
Python interview questions and answers
Python interview questions and answersPython interview questions and answers
Python interview questions and answers
 
Python interview questions and answers
Python interview questions and answersPython interview questions and answers
Python interview questions and answers
 
Interview-level-QA-on-Python-Programming.pdf
Interview-level-QA-on-Python-Programming.pdfInterview-level-QA-on-Python-Programming.pdf
Interview-level-QA-on-Python-Programming.pdf
 
First Steps in Python Programming
First Steps in Python ProgrammingFirst Steps in Python Programming
First Steps in Python Programming
 
10.ppt
10.ppt10.ppt
10.ppt
 
Python Programming-1.pptx of python by computer
Python Programming-1.pptx of python by computerPython Programming-1.pptx of python by computer
Python Programming-1.pptx of python by computer
 
Python fundamentals
Python fundamentalsPython fundamentals
Python fundamentals
 
pythontraining-201jn026043638.pptx
pythontraining-201jn026043638.pptxpythontraining-201jn026043638.pptx
pythontraining-201jn026043638.pptx
 
Introduction to python
Introduction to pythonIntroduction to python
Introduction to python
 
Python and Pytorch tutorial and walkthrough
Python and Pytorch tutorial and walkthroughPython and Pytorch tutorial and walkthrough
Python and Pytorch tutorial and walkthrough
 
Python training
Python trainingPython training
Python training
 
Python Functions 1
Python Functions 1Python Functions 1
Python Functions 1
 
علم البيانات - Data Sience
علم البيانات - Data Sience علم البيانات - Data Sience
علم البيانات - Data Sience
 
Python - An Introduction
Python - An IntroductionPython - An Introduction
Python - An Introduction
 
Blueprints: Introduction to Python programming
Blueprints: Introduction to Python programmingBlueprints: Introduction to Python programming
Blueprints: Introduction to Python programming
 
Core python programming tutorial
Core python programming tutorialCore python programming tutorial
Core python programming tutorial
 
Python Interview Questions For Experienced
Python Interview Questions For ExperiencedPython Interview Questions For Experienced
Python Interview Questions For Experienced
 
Python Basics by Akanksha Bali
Python Basics by Akanksha BaliPython Basics by Akanksha Bali
Python Basics by Akanksha Bali
 
kecs105.pdf
kecs105.pdfkecs105.pdf
kecs105.pdf
 

More from SanjayKumarMahto1 (6)

1 cs xii_python_file_handling text n binary file
1 cs xii_python_file_handling text n binary file1 cs xii_python_file_handling text n binary file
1 cs xii_python_file_handling text n binary file
 
5 cs xii_python_functions _ passing str list tuple
5 cs xii_python_functions _ passing str list tuple5 cs xii_python_functions _ passing str list tuple
5 cs xii_python_functions _ passing str list tuple
 
4 cs xii_python_functions _ properties of data object
4 cs xii_python_functions _ properties of data object4 cs xii_python_functions _ properties of data object
4 cs xii_python_functions _ properties of data object
 
3 cs xii_python_functions _ parameter passing
3 cs xii_python_functions _ parameter passing3 cs xii_python_functions _ parameter passing
3 cs xii_python_functions _ parameter passing
 
1 cs xii_python_functions_introduction _types of func
1 cs xii_python_functions_introduction _types of func1 cs xii_python_functions_introduction _types of func
1 cs xii_python_functions_introduction _types of func
 
What is communication
What is communicationWhat is communication
What is communication
 

Recently uploaded

Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdf
ciinovamais
 
Seal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptxSeal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptx
negromaestrong
 
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in DelhiRussian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
kauryashika82
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptx
heathfieldcps1
 
Gardella_Mateo_IntellectualProperty.pdf.
Gardella_Mateo_IntellectualProperty.pdf.Gardella_Mateo_IntellectualProperty.pdf.
Gardella_Mateo_IntellectualProperty.pdf.
MateoGardella
 

Recently uploaded (20)

Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activity
 
Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024
 
Unit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxUnit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptx
 
Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1
 
Class 11th Physics NEET formula sheet pdf
Class 11th Physics NEET formula sheet pdfClass 11th Physics NEET formula sheet pdf
Class 11th Physics NEET formula sheet pdf
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdf
 
PROCESS RECORDING FORMAT.docx
PROCESS      RECORDING        FORMAT.docxPROCESS      RECORDING        FORMAT.docx
PROCESS RECORDING FORMAT.docx
 
Seal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptxSeal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptx
 
Measures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeMeasures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and Mode
 
APM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAPM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across Sectors
 
Application orientated numerical on hev.ppt
Application orientated numerical on hev.pptApplication orientated numerical on hev.ppt
Application orientated numerical on hev.ppt
 
This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.
 
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in DelhiRussian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
 
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptx
 
Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17
 
Mixin Classes in Odoo 17 How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17  How to Extend Models Using Mixin ClassesMixin Classes in Odoo 17  How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17 How to Extend Models Using Mixin Classes
 
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxBasic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
 
Gardella_Mateo_IntellectualProperty.pdf.
Gardella_Mateo_IntellectualProperty.pdf.Gardella_Mateo_IntellectualProperty.pdf.
Gardella_Mateo_IntellectualProperty.pdf.
 
Advance Mobile Application Development class 07
Advance Mobile Application Development class 07Advance Mobile Application Development class 07
Advance Mobile Application Development class 07
 

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