Momentum logo
Team 3 Classroom

Python Modules and Programs

Posted on Aug 13th, 2020

Breaking down programming problems into their smallest pieces is one of the most critical skills in programming. You’ll need to practice this for the weekend assignment.

Today’s topics

  • Modules and import
  • Exceptions
  • Program shape & design

How to approach a large project

Sketch it out before you write code

Developers need to sketch out their ideas. (This is the true purpose of whiteboards for engineering teams, not grilling job candidates on obscure algorithms!) A pencil and paper is a great tool for programming. If that isn’t your style, use a stylus and tablet, a Google Doc, or whatever you like to jot things down. Don’t start in the code editor, in other words!

  • What is the program/product’s purpose or goal? Restate it in your own words to be sure you get it.
  • What is the core of its purpose? See if you can clear away the bells and whistles and get down to the simplest version of what it does.
  • Bullet list out the main things it needs to do to achieve its purpose.
  • Then go back and look at each bullet, and break it down further. What steps might need to happen first?
    • each step could be something you know how to do OR something you don’t know how to do. Examples:
      • Get the contents of a file
      • Figure out some way to choose a random word
      • Keep track of what letters have been guessed
  • Go back and re-read your list. What are you missing? Is there anything out of order? Have a realization about a step that could be made clearer? Revise it until it looks solid to you.
  • You may have to revisit this plan and revise it as you discover new problems to solve while you work! This is ok! It is all in a day’s work for a software developer.

Once you have a plan you think is somewhat doable, then you can start writing code. Work through your steps in the order that makes sense, keeping in mind that you can hard code values as placeholders where you need to.

You must run your program repeatedly to get feedback about what is happening.

Change one thing at a time and work methodically.

Take breaks.

Talk to other developers when you are stuck. Talking through the problem will often clarify what you need to do. See Rubber Duck Debugging.

Don’t forget to use your print statements to give you necessary information as your program runs.

🐍 Code Break

Try working with a module

Project

Mystery Word

Resources

Code from class

Curated Python Cheats Sheet

Posted on Aug 12th, 2020

In response to some questions this afternoon, I put together a list of Python builtin functions and keywords. Because there are a LOT of keywords and builtin functions, I’ve tried to pare these down to those we’re going to be using or otherwise most likely to encounter (I’ve included links to the full lists at the bottom of this document.

Python Builtin Functions (does not include methods)

In the table below, ‘constructors’ will return an object of the indicated type, ‘sequences’ will return a sequence we can use in a for loop, ‘tests’ will return True or False, input/output will interact with external resources, and ‘general’ fall into none of the above categories.

constructors sequences general tests input/output
list range len callable print
str zip max isinstance input
dict enumerate min hasattr open
tuple   sum   exit
int   type    
float        
         

Python Keywords

In the table below, ‘looping’ keywords introduce a block that will be repeated some number of times, ‘conditional’ keyword separate blocks that should only be executed conditionally, ‘expression’ keywords produce a value (so far these are all booleans), and ‘other’ is a catchall for keywords that introduce other kinds of blocks.

looping conditional expression other
for/in if in def
while elif not class
  else and with/as
    or try
      except
      import
      raise
       

Common Python Modules

All of these modules are in the Python Standard Library. The column heading indicates the subject of the module.

operating system math utilities data processing and storage utilities
os math pickle string
sys statistics json datetime
pathlib random csv collections
      pdb
       

-Full List of Python Builtin Functions, with Documentation -Full List of Python Keywords -Python Keyword/Language Documentation -List of Python Standard Library Modules, with Documentation