Interaction of Scopes in Python

There are cases when external (global) and internal (local) variables have the same name:

pythonnum = 1

def func():
  num = 2
  print(num)

It is important to understand that the local num, although it has the same name, is also global, but for Python it is a different variable. This means that the global num is NOT overwritten inside the function, but retains its original value. When calling the func function, only the value of the local num will be printed to the console, and via print - the value of the global variable:

pythonfunc() # 2
print(num) # 1
Задание 1

What will be the result of running the following code:

pythonnum = 10

def func():
  num = 5
  return num

func()
print(num)
Задание 2

What will be the result of running the following code:

pythonnum = 3

def func():
  num = 4
  return num

num = func()
print(num)
Задание 3

What will be the result of running the following code:

pythonnum = 1

def func():
  num = 2
  return 1

num = func()
print(num)
Задание 4

What will be the result of running the following code:

pythonnum1 = 1

def func():
  num2 = 2

func()
print(num1)
Задание 5

What will be the result of running the following code:

pythonnum1 = 1

def func():
  num2 = 2

func()
print(num2)
Задание 6

What will be the result of running the following code:

pythonnum1 = 1

def func():
  num2 = 2

func()
num2 = 3
print(num2)
Задание 7

What will be the result of running the following code:

pythonnum1 = 1
num2 = 2

def func():
  num2 = 3

func()
print(num2)
Задание 8

What will be the result of running the following code:

pythonnum1 = 1

def func():
  num1 = 2

print(num1)