#Python basics (scope)

1. Scope

A note about Python scope

Create a variable val with the same name as a global variable and a local variable and check the movement

val = 100        #Global variables
def showVal():
    val = 1      #Local variables
    print("local val : ",val)
    
showVal()
print("global val : ",val)

Execution result


local val :  1
global val :  100

Assign a value to a global variable in a function

val = 100        #Global variables
def showVal():
    global val      #Clarify that variables are global
    val = 1 
    print("local val : ",val)
    
showVal()
print("global val : ",val)

Execution result


local val :  1
global val :  1

Recommended Posts

#Python basics (scope)
Python basics ⑤
Python basics
Python basics ④
Python basics
Python basics
Python basics
Python basics ③
Python basics ②
Python basics ②
Python basics: list
#Python basics (#matplotlib)
Python CGI basics
Python basics: dictionary
Basics of Python ①
Python slice basics
#Python basics (#Numpy 1/2)
#Python basics (#Numpy 2/2)
#Python basics (functions)
Python array basics
Python profiling basics
Python #Numpy basics
Python basics: functions
#Python basics (class)
Python basics summary
Python basics ② for statement
Basics of Python scraping basics
Python basics 8 numpy test
Errbot: Python chatbot basics
#Python DeepLearning Basics (Mathematics 1/4)
Python basics: Socket, Dnspython
# 4 [python] Basics of functions
Basics of python: Output
Python
Python class member scope summary
python: Basics of using scikit-learn ①
Python basics: conditions and iterations
Paiza Python Primer 4: List Basics
Basics of Python × GIS (Part 1)
Paiza Python Primer 5: Basics of Dictionaries
Getting Started with Python Basics of Python
Review of the basics of Python (FizzBuzz)
Basics of Python x GIS (Part 2)
About the basics list of Python basics
(python) Deep Learning Library Chainer Basics Basics
Learn the basics of Python ① Beginners
kafka python
Basics of binarized image processing with Python
Python: Basics of image recognition using CNN
python + lottery 6
Python Summary
Built-in python
Python technique
Studying python
Statistical basics and Python, graphing, etc. (memo)
Python 2.7 Countdown
Python memorandum
Differences between Ruby and Python in scope
python tips
Linux basics
python function ①