Day 12: 100 Days of Code

Day 12: 100 Days of Code

Python

Namespaces:

A namespace is a system that has a unique name for each and every object in Python.

Scope:

A lifetime of a namespace depends upon the scope of objects, if the scope of an object ends, the lifetime of that namespace comes to an end. Hence, it is not possible to access the inner namespace’s objects from an outer namespace. geeksforgeeks

Any thing you give a name has a namespace. That namespace is valid in certain Scope.

  • Local Scope
#Local Scope

def drink_potion():
    potion_strength = 2#Can not be accessed outside the function drink_potion.
    print(potion_strength)

drink_potion()
print(potion_strength)

Output:
NameError: name 'potion_strength' is not defined
  • Global Scope
#Global Scope
player_health = 10 #Global scope and it can be accessed anywhere in the file
def drink_potion():
    potion_strength = 2
    print(player_health)

drink_potion()
print(player_health)

Does python have block scope?

No, there is no block scope in Python. Blocks like IF/ELSE/FOR/WHILE, all the blocks with colon and indentation do not count as a local scope. Inside a IF/ELSE/FOR/WHILE code block is same as outside.

#There is no block scope

game_level = 3
def create_enemy():
    enemies = ["Skeleton","Zombie","Alien"]
    if game_level < 5:
        new_enemy = enemies[0]

    print(new_enemy) 

create_enemy()

Output:
Skeleton

Modifying a Global variable

Its terrible idea to modify global variable but you can do it by following ways:

  • Using global keyword
#2.Modifying using Global Keyword
enemies = 1
def increase_enemies():
  global enemies
  enemies += 1
  print(f"enemies inside function: {enemies}")

increase_enemies()
print(f"enemies outside function: {enemies}")

Output:
enemies outside function: 2
  • You can use return to change the value of global variable
enemies = 1
def increase_enemies():
  print(f"enemies inside function: {enemies}")
  return enenies + 1

enenies = increase_enemies()
print(f"enemies outside function: {enemies}")

Output:
enemies outside function: 2

If you do as in example below it means you have two different variables with same name enemies. It do not consider enemies variable as global variable inside the function but instead it is declaring new variable enemies and assigning it a value.

enemies = "Skeleton"
#Its terrible idea to use two different variables with same name
def increase_enemies():
  enemies = "Zombie" #We are not changing the global variable, we are creating an entirely new variable
  print(f"enemies inside function: {enemies}")

increase_enemies()
print(f"enemies outside function: {enemies}")

Global Constants vs Global variables

  • Global Constants:

These are the values that you never have a plan of changing it, you wanna look to it once(Never going to change) e.g., value of pi.

pi = 3.14159
  • Global Variables:

Global variables which you are likely to change.

Naming convention for Global Constants

For global constants use all upper case letters.

PI = 3.14159
URL = "https://www.google.com"
TWITTER_HANDLE = "@yu_angela"

Project:

Number Guessing Game: Run a game by forking your own copy on Replit or check out it on Github

Tips on how to complete your project:

  1. Write Todo List of small tasks that your project includes.
  2. Add that task list into your project file as a comment.
  3. Start with the simplest one.
  4. Write code of each Todo task.
  5. Test every time, after adding a Todo, check either it is working as expected or not.

To get your own Logo for the project : ASCII Text