100 Days of Python: Days 16-20

Day 16

  • Began move from Procedural Programming to Object Oriented Programming
  • OOP
    • Splits larger tasks into smaller pieces that can be worked on separately.
    • Used illustration of a restaurant and the division of responsibilities among employees
    • Projects can become more complex with OOP.
  • Classes and Objects
    • Modeling includes determining what something has and what something does
    • What something has == attributes / variables
    • What something does == methods
    • Modeling determines what an object should have. This blueprint is a “class”, the individual instances of a class are called objects
    • Reviewed creating new objects from existing classes.
Virtual Coffee Machine

Day 17

  • Reviewed building classes from scratch.
  • Reviewed “init” function, which is referenced when creating an object from a class.  This constructor determines the pre-loaded attributes that will generate with the object.
  • Functions that will become class methods will be defined underneath the Class initialization.
  • All class methods need to list “self” as their first parameter (this is to associate the action with the object that called it).
  • Discussed using ‘pass’ to avoid indentation errors on empty code blocks.
  • Reviewed default attributes (initialized attributes that accept no parameters for their initial values)
  • Apps Created:
    • Quiz Game – Simple True or False question game.
    • Question database, quiz logic, question model and main logic are separated into separate files and classes.
    • Final illustration: This app was created with total modularity.  Each component was developed independently and did not rely on the procedures of the other modules, as long as the inputs and outputs were correct.
Not exactly an API call, but I’m getting there…

Day 18

  • Revisited Python Turtle to attack GUI-based problems.
  • Overviewed using Python documentation to learn about attributes and methods in a class.
  • Used refactoring to rename an object throughout the code.
  • Reviewed importing modules:
    • Keyword / module name imports
    • Import module = acceptable, but makes references in the code longer (they will need to reference the module for every call.
    • From module import module_item = ideal when possible.  Keeps code shorter / cleaner.
    • Avoid: from module import *
  • Reviewed aliasing modules (e.g. import turtle as t).
    • Useful for long-named modules
  • Some modules require package downloading.
  • Learned about tuples.
  • Tuples are similar to list, containing multiple values.  However, tuples are immutable (elements cannot be added, changed or removed).
  • Apps Created:
    • Several “turtle challenges”
    • Had the turtle draw a dashed line.
    • Had the turtle draw all of the shapes from 3 to 10 sides with a random color for each shape.
    • Had the turtle take a “random walk” (10 pace walks with random heading changes in between)
    • Had the turtle create a Hirst Dot Painting
Read about Damien Hirst’s spot paintings here.
Turtle may have drunk the virtual espresso from Day 16.

Day 19

  • Reviewed Event Listeners
  • Reviewed passing functions as arguments
    • Higher order functions can accept other functions as arguments / parameters.
    • Try to use keyword arguments in these scenarios.
  • Discussed creating multiple objects from the same class.
  • Discussed object states.
  • Apps Created
    • Etch-a-sketch (Turtle Graphics project)
    • Turtle Race – Generate 6 turtle objects and have them race.  Have the user bet on who the winner will be.
This project reminded me of a classic game from Mario Party

Day 20

  • Introduced Snake project (popular game on Nokia phones) and the smaller elements that will need constructed.
    • Part 1 – Used for loop to generate snake segment objects and move them to their starting position.
    • Part 2 – Reviewed screen tracing / when to update the screen.  Discussed making the snake move (the snake actually moves from aft to forward as trailing segments assume the position of their predecessor).
    • Part 3 – Split code into classes for OOP.  Created the create-snake segment
    • Part 4 – Setup event listeners to accept keyboard inputs to move the snake
    • Remainder of game functionality to be completed on Day 21.
Halfway there! Food, game dynamics and scorekeeping coming next week!
Tagged in:,

0 Comments

Leave a Reply

Your email address will not be published. Required fields are marked *