Udemy Python day 11 to 20
Day 13: Global and local variables
Constants
Day 12: Local vs. Global Scope
Constants
import math
#global scope
best_friend = "Jeff"
# All upper case is a constant
PI= math.pi
class Constants:
def __init__(self):
self._pi = math.pi
@property
def pi(self):
return self._pi
"""
AttributeError: property 'pi' of 'Constants' object has no setter
without the setter below, you can't modify the value of PI
@pi.setter
def pi(self, value):
self._pi = value
"""
BING_URL = "www.bing.com"
"""
best practice is not to modify a global variable at a different
location. You don't want to call and modify it in a local scope
"""
def increase_enemies():
# global best_friend
"""
This will bring the global variable inside a local variable
"""
best_friend = "Bob"
print (f"best friend inside local scoope {best_friend}")
print("-----------------------------------")
increase_enemies()
print(f"best friend outside function {best_friend}")
print("-----------------------------------")
print(f"pi = {PI}")
print(f"Bing: {BING_URL}")
constants = Constants()
print(constants.pi)
#this throws an Attribute error seen above.
# constants.pi = 3.14
# print(constants.pi)
-----------------------------------
best friend inside local scoope Bob
best friend outside function Jeff
-----------------------------------
pi = 3.141592653589793
Bing: www.bing.com
3.141592653589793
Process finished with exit code 0
Day 16: Tables
from tabulate import tabulate
all_data = [
[1,"Harry",88,""],
[2,"Hermione",95,""],
[3,"Ron",78,""],
[4,"Draco",75,""],
[5,"Neville",60,""]
]
# create header
headers = ["Roll \n Number","Student \n name","Marks", "Grade"]
print(tabulate(all_data, headers=headers, tablefmt="grid"))
row = 0
while True:
grade = all_data[row][2]
if grade >= 91:
all_data[row][3] = "Outstanding"
elif grade >= 81:
all_data[row][3] = "Exceeds\nExpectations"
elif grade >= 71:
all_data[row][3] = "Acceptable"
else:
all_data[row][3] = "Fail"
row +=1
if row == 5:
break
print(tabulate(all_data, headers=headers, tablefmt="grid"))
+-----------+------------+---------+---------+
| Roll | Student | Marks | Grade |
| Number | name | | |
+===========+============+=========+=========+
| 1 | Harry | 88 | |
+-----------+------------+---------+---------+
| 2 | Hermione | 95 | |
+-----------+------------+---------+---------+
| 3 | Ron | 78 | |
+-----------+------------+---------+---------+
| 4 | Draco | 75 | |
+-----------+------------+---------+---------+
| 5 | Neville | 60 | |
+-----------+------------+---------+---------+
+-----------+------------+---------+--------------+
| Roll | Student | Marks | Grade |
| Number | name | | |
+===========+============+=========+==============+
| 1 | Harry | 88 | Exceeds |
| | | | Expectations |
+-----------+------------+---------+--------------+
| 2 | Hermione | 95 | Outstanding |
+-----------+------------+---------+--------------+
| 3 | Ron | 78 | Acceptable |
+-----------+------------+---------+--------------+
| 4 | Draco | 75 | Acceptable |
+-----------+------------+---------+--------------+
| 5 | Neville | 60 | Fail |
+-----------+------------+---------+--------------+