Tips to know when programming competitively with Python2 (Other language specifications)

The part about other language specifications of tips you should know when programming competition with Python2 has been divided.

The Python version is ** 2.7.5 ** (Python3 has very different specifications such as input / output, so it is recommended to refer to other articles).

Global variables

Python bite: Access to global variables | Inside ASCADE

Assign to a global variable in a function

Global variables are sometimes used when writing a full search, but in Python, there are points to be aware of when accessing global variables from within a function.

#If there is only a variable reference in the function
a = 'abc'

def function():
    print a

function() # 'abc'


#Including reassignment to a variable within a function
a = 'abc'

def function():
    a = 'def'
    print a 

function() # def
print a # abc

As in the above example, you can refer to a global variable from within a function, but if you assign to a local variable with the same name within that function, it will be treated as a local scope within that function. Therefore, the value of the global variable cannot be changed in the function as it is.

a = 'abc'

def function():
    global a
    a = 'def'
    print a

function() # def
print a # def

By declaring that the variable a is a global variable by global a, the global variable can be rewritten in the function.

Recommended Posts

Tips to know when programming competitively with Python2 (Other language specifications)
Tips you should know when programming competitive programming with Python2
Knowledge you need to know when programming competitive programming with Python2
Tips (input / output) that you should know when programming competitive programming with Python2
Tips (control structure) that you should know when programming competitive programming with Python2
Tips (data structure) that you should know when programming competitive programming with Python2
How to enjoy programming with Minecraft (Ruby, Python)
Materials to read when getting started with Python
3. 3. AI programming with Python
[Chapter 5] Introduction to Python with 100 knocks of language processing
Introduction to Python language
Tips and precautions when porting MATLAB programs to Python
Python programming with Atom
Competitive programming with python
[Chapter 3] Introduction to Python with 100 knocks of language processing
[Chapter 2] Introduction to Python with 100 knocks of language processing
Programming with Python Flask
[Chapter 4] Introduction to Python with 100 knocks of language processing
I know? Data analysis using Python or things you want to use when you want with numpy
[Tips] Dealing with errors that occur when trying to install Python 3 series less than 3.5.3 with pyenv
Solution when you want to use cv_bridge with python3 (virtualenv)
[Python] Technique to reduce dimensions with DP (Dynamic Programming) [AtCoder]
I wanted to solve the Panasonic Programming Contest 2020 with Python
[python] A note when trying to use numpy with Cython
Things to keep in mind when using Python with AtCoder
Things to keep in mind when using cgi with python.
Programming with Python and Tkinter
100 Language Processing with Python Knock 2015
Connect to BigQuery with Python
Connect to Wikipedia with Python
Post to slack with Python 3
[Tips] Handle Athena with Python
Error when playing with python
Switch python to 2.7 with alternatives
Write to csv with Python
An introduction to Python Programming
~ Tips for beginners to Python ③ ~
Network programming with Python Scapy
How to not escape Japanese when dealing with json in python
IPynb scoring system made with TA of Introduction to Programming (Python)
I want to know the weather with LINE bot feat.Heroku + Python
[Introduction to Python] What is the most powerful programming language now?
How to deal with errors when installing Python and pip with choco
[Python] How to get a value with a key other than value with Enum
[Tips] How to do template extends when creating HTML with django
Python: How to use async with
[Introduction to Python3 Day 1] Programming and Python
[Python] Write to csv file with Python
100 Language Processing Knock with Python (Chapter 1)
Create folders from '01' to '12' with python
Nice to meet you with python
Try to operate Facebook with Python
[Python] Object-oriented programming learned with Pokemon
Output to csv file with Python
Tips when Vimmer switches to Pycharm
100 Language Processing Knock with Python (Chapter 3)
Convert list to DataFrame with python
Introduction to Protobuf-c (C language ⇔ Python)
MP3 to WAV conversion with Python
To do tail recursion with Python2
How to get started with Python