Skip to content

Clarifying some basics

surendrabisht edited this page Aug 18, 2020 · 3 revisions

Scope of Variables

nonlocal: The nonlocal keyword adds a scope override to the inner scope.

you must use nonlocal in a nested function.

def counter():
  num = 0
   def incrementer():
        nonlocal num # without nonlocal keyword will not be able to access outside variable
        num += 1
        return num
  return incrementer

local:
variables inside functions are considered local if and only if they appear in the left side of an assignment
statement. function can read outside variable inside function.

global:
To access variables outside the function, we have to declare global variable name inside function where we want to change its value .


Use of print function()

In Python 3.x, the print function has an optional end parameter that is what it prints at the end of the given string. By default it's a newline character, so equivalent to this:

print("Hello, ", end="\n")
print("World!")
Hello,
World!

print("Hello, ", end="")
print("World!")
Hello, World!

Clone this wiki locally