Python Classroom Series – 04/Sept/2020

Iterating through lists and more

  • Iterating through using for each
colors = ['red', 'blue', 'green']
for color in colors:

  • For with else
colors = None
for color in colors:
    print("no colors found")
  • Iterating multiple sequences with zip()
def using_zip_demo():
    days = ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"]
    fruits = ["Apple", "Orange", "Banana", "Watermelon", "Pomegranate", "blueberry", "grapes"]
    drinks = ["coffee", "tea", "beer", "whiskey", "wine", "rum", "vodka"]
    count = len(days)
    index = 0
    for day, fruit, drink in zip(days, fruits, drinks):
        print(f"{day=} \n diet plan \n fruit = {fruit=} \n {drink=}")
  • Using zip to create lists Preview
  • Lists can be created in numeric ways Preview
  • Out of all of the approaches mentioned above each approach is value and will produce same result. However the more Pythonic way to build a list is
[expression for item in iterable ]
  • Example
number_list = [ number for number in range(1,5) ]
square_list = [ number*number for number in range(1,5) ]
  • Lets make this more interesting we can add conditions
[expression for item in iterable if condition ]
  • lets create a list of even number from 1 to 1000 Preview
  • To achieve the same stuff as shown in the above image, the traditional code
a_list = []
for number in range(1, 11):
    if number%2 == 0:
  • Lets iterate through rows and columns in a 3*3 matrix
def print_matrix_traditional():
    This method will demonstrate creating a matrix
    for row in range(1, 4):
        for col in range(1, 4):
            print(row, col)

def print_matrix_pythonic():
    This method prints the matrix in a pythonic fashion
    rows = range(1, 4)
    cols = range(1, 4)
    cells = [(row, col) for row in rows for col in cols]
    for cell in cells:

Tuples vs Lists

  • Tuples use less space.
  • Tuples can be used as dictionary keys
  • Named Tuples can be a simple alternative to objects
  • If your collection is static use tuple and if collection is dynamic use lists

Copy with copy(), list() and slicing

  • You can copy values of list to an fresh list by using
    • the list copy() method
    • The list() conversion function
    • The list slice [:]
  • Now lets experiment Preview
  • Changing a value of a does not effect copies b,c and d Preview
  • In lists when we copy elements of list to other list. There are two kinds of copies
    • deep copy Preview
    • shallow copy: when we changed the value of list in a list all the copy methods values were changed, this is called as shallow Preview

Importance of Python Data Model

  • Simple Card deck
import collections

Card = collections.namedtuple('Card', ['rank', 'suit'])

class CardDeck:
    ranks = [str(n) for n in range(2, 11)] + list('JQKA')
    suits = 'spades diamonds clubs hearts'.split()

    def __init__(self):
        self._cards = [Card(rank, suit) for suit in self.suits for rank in self.ranks]

    def __len__(self):
        return len(self._cards)

    def __getitem__(self, position):
        return self._cards[position]

  • Vector
import math

class Vector:

    def __init__(self, x, y):
        self.x = x
        self.y = y

    def __repr__(self):
        return f'Vector({self.x!r}, {self.y!r})'

    def __abs__(self):
        return math.hypot(self.x, self.y)

    def __bool__(self):
        return bool(abs(self))

    def __add__(self, other):
        x = self.x + other.x
        y = self.y + other.y
        return Vector(x,y)

    def __mul__(self, other):
        return Vector(self.x*other, self.y*other)

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.

About learningthoughtsadmin