You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
squares= [x*xforxinrange(5)]
print(squares)
print(
"""Equivalent imperative code:squares = []for x in range(5): squares.append(x * x)print(squares)"""
)
[0, 1, 4, 9, 16]
Equivalent imperative code:
squares = []
for x in range(5):
squares.append(x * x)
print(squares)
Intermediate Python
Classes & OOP
classPerson:
def__init__(self, name, age):
self.name=nameself.age=agedefintro(self):
returnf"Hello, my name is {self.name}."defold(self):
returnf"I am {self.age} years old."gabor=Person("Gabor", 42)
print(gabor.intro())
print(gabor.old())
Hello, my name is Gabor.
I am 42 years old.
Exceptions
try:
1/0exceptZeroDivisionError:
print("Cannot divide by zero!")
Cannot divide by zero!
Modules & Packages
# import entire moduleimportmath# can throw ModuleNotFoundErrorsqrt_4=math.sqrt(4)
# import specific functionfrommathimportsqrt# can throw ImportError