A2SV Group 5 Python Track_ Built-In Functions and Classes
A2SV Group 5 Python Track_ Built-In Functions and Classes
class MyClass:
def __init__(self):
self.x = 5
p1 = MyClass()
print(p1.x) # 5
The __init__() function
self.name = name
self.age = age
p1 = Person("John", 36)
● The self parameter is a reference to the current instance of the class, and is
used to access variables that belongs to the class.
Object Methods
● Objects can also contain methods. Methods in objects are functions
that belong to the object.
class Person:
def __init__(self, name, age):
self.name = name
self.age = age
def myfunc(self):
print("Hello my name is " + self.name)
p1 = Person("John", 36)
p1.myfunc() # Hello my name is John
Iterators & Generators
Iterators and Iterables
● Iterable is an object, that one can iterate over. It generates an
Iterator when passed to iter() method.
my_iter = iter(my_tuple)
print(next(my_iter)) ?
Iterators - Example
my_tuple = ("apple", "banana", "cherry")
my_iter= iter(my_tuple)
print(next(my_iter)) # apple
print(next(my_iter)) ?
Iterators - Example
my_tuple = ("apple", "banana", "cherry")
my_iter= iter(my_tuple)
print(next(my_iter)) # apple
print(next(my_iter)) # banana
print(next(my_iter)) ?
Iterators - Example
my_tuple = ("apple", "banana", "cherry")
my_iter= iter(my_tuple)
print(next(my_iter)) # apple
print(next(my_iter)) # banana
print(next(my_iter)) # cherry
Iterators - Example
my_tuple = ("apple", "banana", "cherry")
my_iter = iter(my_tuple)
print(next(my_iter)) # apple
print(next(my_iter)) # banana
print(next(my_iter)) # cherry
print(next(my_iter)) # ?
Iterators - Example
my_tuple = ("apple", "banana", "cherry")
my_iter = iter(my_tuple)
print(next(my_iter)) # apple
print(next(my_iter)) # banana
print(next(my_iter)) # cherry
● The state of generators are maintained through the use of the keyword yield
and works much like using return but it has some important differences.
Generators (Continued)
● The code in generator functions only execute when next() is called on the
generator object.
● The next time the function is called, execution continues from where it left
off, with the same variable values it had before yielding, whereas the return
statement terminates the function completely.
Generators - Examples
# with out generators # with generator
def square_nums(nums): def square_nums(nums):
result = [] for i in nums:
for i in nums: yield i*i
result.append(i*i)
return result
print(my_nums) print(next(my_nums)) # 1
print(next(my_nums)) # 4
print(next(my_nums)) # 9
Collections & Containers
Collections and Containers
● The collections module in Python provides different types of
containers.
d = defaultdict(int)
L = [1, 2, 3, 4, 2, 4, 1, 2]
for i in L:
d[i] += 1
print(d)
Counter
print(Counter(['B','B','A','B','C','A','B','B','A','C']))
?
Counter - Example
from collections import Counter
print(Counter(['B','B','A','B','C','A','B','B','A','C']))
print(Counter("ABCDEFGABCDE"))
?
Counter - Example
from collections import Counter
print(Counter("ABCDEFGABCDE"))
de = deque([1,2,3])
de.append(4)
print(de) # ?
de.popleft()
print(de) # ?
deque - Example
from collections import deque
de = deque([1,2,3])
de.append(4)
print(de) # [1, 2, 3, 4]
de.popleft()
print(de) # [2, 3, 4]
Python Built-In Functions
Built-in Functions
all() Returns True if all items in an iterable object are true x = all([True, True, True])
any() Returns True if any item in an iterable object is true x = any([False, True, False])
sum() Returns the sum of all elements in a list or iterable total = sum(5, 10, 25)
nums = [5, 10, 25]
total = sum(nums)
strip() Removes leading and trailing whitespaces s = " Hello, World!\t\n "
s.strip()
● Look for opportunities to use built-in functions like map, filter, and list
comprehensions to avoid unnecessary loops.
● When iterating over multiple iterables in parallel, use zip for clean and
efficient code.