1 of 36

Python Introduction

Credit: Slides are modified with permission from Charles Severance at the University of Michigan.

This work is licensed under an Attribution-NonCommercial-ShareAlike 3.0 Unported License

lasacs.com

2 of 36

Act in haste and repent at leisure; code too soon and debug forever.

-- R. Kennington

lasacs.com

3 of 36

Writing good, efficient code requires

thought and planning

lasacs.com

4 of 36

lasacs.com

5 of 36

lasacs.com

6 of 36

lasacs.com

7 of 36

program that uses BLEND interface to automatically manage assignments

program that runs & checks student program submissions

lasacs.com

8 of 36

Users vs Programmers

  • Users see computers as a set of tools - word processor, spreadsheet, map, todo list, etc.

  • Programmers know how computers work and how to talk with them in a computer language

  • Programmers often write little “helpers” for themselves to automate a task or sometimes write large complex programs for many many users

lasacs.com

9 of 36

Computers want to be helpful...

  • Users have it easy - they just pick the programs they want to use
  • Programmers need to speak the computer's language to describe what they want done
  • Computers are built for one purpose - to do things for us - really fast

lasacs.com

10 of 36

What is Code? Software? A Program?

A sequence of stored instructions

Program, noun:

A magic spell cast upon a computer to enable it to turn input into error messages.

lasacs.com

11 of 36

Program Steps or

Program Flow

  • Like a recipe or a manual, a program is a sequence of steps to be done in order
  • Some steps may be conditional/optional and can be skipped
  • Sometimes a step or group of steps are to be repeated

lasacs.com

12 of 36

Python was/is developed by Guido van Rossum.

In the Python community, Van Rossum is known as a "Benevolent Dictator For Life" (BDFL).

lasacs.com

13 of 36

Beginning Programmers

  • We need to learn the Python language so we can communicate our instructions to Python interpreter.
  • We must communicate very precisely when we write Python code.
  • When you make a mistake, the computer does not think you are “cute”. It says “syntax error”.
  • Python is much like a dog, which only understands a few key words but constantly looks at you with a sweet look on its face waiting for you to say something it understands.

lasacs.com

14 of 36

Beginning Programmers

  • Beginning programmers often take the fact that Python leaves no room for errors as evidence that Python is mean, hateful and cruel.

  • While Python seems to like everyone else, Python knows them personally and holds a grudge against them.

Common Reaction when program does not work Cub Fan

lasacs.com

15 of 36

Programs do EXACTLY what you tell them to. They don't know any better.

lasacs.com

16 of 36

Programming can be frustrating.

lasacs.com

17 of 36

Early

Greek Programmers

lasacs.com

18 of 36

  • Can solve lots and lots of problems
  • put a little piece of your intelligence in the computer
  • Creative art - particularly when we do a good job on the user experience

lasacs.com

19 of 36

What do we say?

lasacs.com

20 of 36

Statements and Expressions

x = 2

x = x + 2

print(x)

Variable

Operator

Literal

Function Name

Assignment Statement

Assignment with expression

Print statement

"parts of speech"

lasacs.com

21 of 36

to run Python code

Both are accessed using IDLE - Python's default Integrated Development and Learning Environment.

  1. Python Shell
    • Just type the command after >>> and hit ENTER
      • The repl.it shell has a single >
  2. Python Edit Window
    • Type in program, save, and then run
      • NOTE: Program runs in a Python Shell

lasacs.com

22 of 36

Python Shell

What's next?

What I typed in

lasacs.com

23 of 36

Let's Talk to Python...

>>> x = 1

>>> print(x)

1

>>> y = x + 1

>>> print(y)

2

>>> y

2

lasacs.com

24 of 36

Sequential Steps

Program

x = 2

print(x)

2

x = x + 2

print(x)

4

output to screen.

x = 2

print(x)

x = x + 2

print(x)

When a program is running, it flows from one step to the next. We as programmers set up “paths” for the program to follow.

lasacs.com

25 of 36

Python Edit Window

Usually we enter our program into the Python Edit Window, save it to a file, and then tell python to run the program.

lasacs.com

26 of 36

Starting Python

2) type Idle

3) right click on Idle Python 3.x and select Pin to taskbar

1) click Start

From then on click on taskbar icon to start

lasacs.com

27 of 36

Edit Window &

Show line numbers

close Idle and open it again

lasacs.com

28 of 36

lasacs.com

29 of 36

lasacs.com

30 of 36

lasacs.com

31 of 36

lasacs.com

32 of 36

Useful IDLE Stuff

Quickly open a recent file.

Quickly indent/dedent or comment/uncomment

blocks of code.

Show line numbers

(required)

lasacs.com

33 of 36

lasacs.com

34 of 36

You will submit many of your Python programs using a web application called StudentDrop that was written by a former LASA students.

Programs submitted via StudentDrop end up on my computer so I can run and verify them (using a Python program that I wrote).

Before you can submit to StudentDrop, we'll first get you registered with StudentDrop.

lasacs.com

35 of 36

Requirements for your program name

    • (1) only letters & numbers
      • start with a letter
    • (2) no spaces

You will submit many of your Python programs using PC2. You were emailed a username & password - click on the PC2 icon to login.

lasacs.com

36 of 36

PC2 submissions can only be made at school.

PC2 submissions are due when I have the PC2 judging program running (I usually keep it running at least until the beginning of the class after the class in which we have finished a PC2 assignment).

lasacs.com