Python basics refresher

ITEC 3160 Programming for Data Analysis,
Cengiz Günay

(License: CC BY-SA 4.0)

Prev - Introduction, Next - Object Oriented Programming

The very basics

Chapter 2.3

Chapter 2.3

  • Python history: version 2 vs 3; now popular for AI & ML
  • Whitespace, quotations, language conventions, operators
  • Control statements: blocks, if-elif-else, while
  • Modules and import

A Quick Practice!

This is a good place to practice what we reviewed so far. Think of a theme and use Brython to write your program with:

  • At least three variables and do some artithmetic
  • One list, one function, and one if statement with elif and else
  • One for and one while loop, and use continue and break
  • Print a lot of things
  • Click on the “Share” button to post on forum

An ephemeral Python practice area (wait for it to load)

How to practice Python

Do one or more of these based on your preferences!

A crash course into Python

See Chapter 2.

See Chapter 2.

  • Zen of Python
  • Installing
  • Virtual environments
  • Whitespace
  • Modules and import
  • Functions
  • Strings

Practice functions

Work in teams, but only one person should submit. Write Python code online or in your own IDE to:

  • List everyone’s names at the top in a comment
  • Add a function that prints out a salutation message
  • Create a list and a for loop that prints out each element
  • Add an if-statement that behaves differently for some elements of the list

Add two more functions around a theme of your choice and make sure to use:

  • docstrings: show usage
  • f-strings: print or return
  • lambda functions: show a use example
  • default function parameters: show usage for provided vs missing parameters

Make it interesting!

Language basics

(Continuing DSS Chapter 2)

  • Lists
  • Tuples
  • Dictionaries (default, Counter)

Practice basics in-class exercise

Work in teams on any IDE. Create a program with a theme of your choice that uses all of the elements from both of the following lists:

Dictionaries:

  • Take input from a user in a loop
  • Add to a dictionary items and their associated values
  • Then check if items exist in the dictionary
  • Delete items from dictionary based on conditions
  • Print out the dictionary

Also include the following elements:

  • A function that returns multiple values using a tuple
  • Use list unpacking to assign variables from tuple
  • Access a list’s elements by using slicing

Misc Python Oddities

(Continuing DSS Chapter 2)

  • Sets
  • Control flow
  • Truthiness
  • Sorting
  • List comprehensions
  • Zip and argument unpacking
  • Type annotations (just the basics)

Practice Misc Python Stuff

Work in teams on any IDE. Create a program that includes:

  • A set: add elements and check for membership
  • An if-elif-else statement
  • A loop with use of break and continue
  • Use truthiness of data structures in a conditional statement (other than simple binary operators), could be for loop logic
  • sort a data structure and print them out
  • create a list using the list comprehension syntax, you can use the data from the set
  • use zip somewhere
  • specify type annotations in at least one place
  • make sure it’s sufficiently distinct from others’ submissions and that your program has a theme
Home