Introduction to Programming using Python

Guided self-study course, Session 1
Fall 2014

Welcome!

You're our first cohort for this method.  So expect some hiccups, and please provide feedback.

 

Instruction time is focused on what the students need that week: lecture, demos, discussion, etc.

Expectations

Students should:

  • at a minimum, skim through the assigned readings
  • perhaps watch the relevant videos
  • hopefully attempt some of the practice problems.

 

Try to have at least one or two questions for class.

My instruction style

  • Informal
    • There will be memes
  • Interactive
    • I will make you talk, but usually as a group.
  • Focus on practicing vocabulary in class
  • There are no stupid questions
    • Only things I haven't explained yet, or explained poorly.  Call me out!
  • We're all students and peers in this room.
  • Let me know if you have learning needs I can accommodate.

"Priming" information

  • Some of what I'm going to show you will be to "prime" you for later material.
  • Don't expect to understand everything perfectly
  • Aim for "optimistically hazy"
  • I'll try to note when I'm going for priming

Py-CU

Champaign Urbana's Python User Group

http://www.py-cu.org

  • Weekly hack/project nights on Tuesdays, from 7-9
  • Located in Makerspace Urbana

ASIS&T

Student chapter of ASIS&T at GSLIS.

Association for Information Science and Technology

 

Just starting up!

Who is doing this?

Instructor

Elizabeth Wickes

@elliewix

wickes1@illinois.edu

www.elizabethwickes.com

 

Co-organizer of Py-CU

From sociology/psychology

 

{quant,qual}//<3

 

Current GSLIS student in 

curation and SODA

How far will we get?

  • Through the foundational material, hopefully.
  • What does that include?
    • Syntax
    • Logic and control
    • Functions
    • Loops
    • Lists, strings, dictionaries, and tuples
    • File processing

But you won't be done

  • Prep to move on with existing or more advanced materials
  • Not teaching you deep Python stuff
  • You can stay with Python, or move to another language
    • What you get here is reasonably universal

Unless you want to be

Programming languages

Purpose

Web languages

HTML, JavaScript, PHP..

Software languages

Java, C++, Visual Basic...

Content

Language style

 

Abstraction level

 

Methodological approach

 

and so many more...

Classification systems

That's neat, but what is Python?

Title Text

  • Bullet One
  • Bullet Two
  • Bullet Three
>>> import this
The Zen of Python, by Tim Peters

Beautiful is better than ugly.
Explicit is better than implicit.
Simple is better than complex.
Complex is better than complicated.
Flat is better than nested.
Sparse is better than dense.
Readability counts.
Special cases aren't special enough to break the rules.
Although practicality beats purity.
Errors should never pass silently.
Unless explicitly silenced.
In the face of ambiguity, refuse the temptation to guess.
There should be one-- and preferably only one --obvious way to do it.
Although that way may not be obvious at first unless you're Dutch.
Now is better than never.
Although never is often better than *right* now.
If the implementation is hard to explain, it's a bad idea.
If the implementation is easy to explain, it may be a good idea.
Namespaces are one honking great idea -- let's do more of those!

For what?

  • General purpose
    • web, software, utility...
  • Platform independent
  • An industry standard
    • readas: in job titles
  • Currently the most popular language for intro classes [1]

How so?

  • Interpreted
  • Strong
  • Dynamic

      ...

[1] http://cacm.acm.org/blogs/blog-cacm/176450-python-is-now-the-most-popular-introductory-teaching-language-at-top-us-universities/fulltext

Let's just get out a chart...

Strongly
typed

Weakly
typed

Dynamic

Static/
Declared

>>> import this
The Zen of Python, by Tim Peters

Beautiful is better than ugly.
Explicit is better than implicit.
Simple is better than complex.
Complex is better than complicated.
Flat is better than nested.
Sparse is better than dense.
Readability counts.
Special cases aren't special enough to break the rules.
Although practicality beats purity.
Errors should never pass silently.
Unless explicitly silenced.
In the face of ambiguity, refuse the temptation to guess.
There should be one-- and preferably only one --obvious way to do it.
Although that way may not be obvious at first unless you're Dutch.
Now is better than never.
Although never is often better than *right* now.
If the implementation is hard to explain, it's a bad idea.
If the implementation is easy to explain, it may be a good idea.
Namespaces are one honking great idea -- let's do more of those!

How do you write things in Python?

 

Lesson 1: Punctuation

White space

Warning! Incoming cliche!

A classic:

Python vs. Java

>>> if 5 == 5:
...     print "new hotness"
...     print "because math"
if (5 == 5) {
    System.out.println("old and busted");
    System.out.println("because math");
}

Python

Java

(I kid, I kid...)

␠␠␠␠

␠␠␠␠

>>> import this
The Zen of Python, by Tim Peters

Beautiful is better than ugly.
Explicit is better than implicit.
Simple is better than complex.
Complex is better than complicated.
Flat is better than nested.
Sparse is better than dense.
Readability counts.
Special cases aren't special enough to break the rules.
Although practicality beats purity.
Errors should never pass silently.
Unless explicitly silenced.
In the face of ambiguity, refuse the temptation to guess.
There should be one-- and preferably only one --obvious way to do it.
Although that way may not be obvious at first unless you're Dutch.
Now is better than never.
Although never is often better than *right* now.
If the implementation is hard to explain, it's a bad idea.
If the implementation is easy to explain, it may be a good idea.
Namespaces are one honking great idea -- let's do more of those!

 

This is just an example.

You aren't expected to know this syntax.

Yet.

 

>>> if 5 == 5:
...     if 6 == 6:
...         print "fhew"
...         print "whoa nesting"
>>> if 5 == 5 and 6 == 6:
...     print "math reality still works"
...     print "with less nesting"

Style choices

␠␠␠␠

␠␠␠␠

␠␠␠␠

␠␠␠␠

␠␠␠␠

␠␠␠␠

␠␠␠␠

Let's start typing things!

Go here: http://repl.it/

 

Choose Python.

 

Start Pythoning.

How do you write things in Python?

 

Lesson 2: Nouns

Objects!

Everything in Python is an object

We'll keep building up this concept.

Let it be hazy for now.

type() is your friend

  • type() tells you what something is

 

Try some examples as we go.

These are your "things"

  • Numbers
  • Words
  • Containers
  • Boolean
  • int
    • integers
    • 1, 2, 3, ...
  • float
    • floats
    • 1.1, 2.1, 3.14159...
  • there are more, but that's enough

These are your "things"

  • Numbers
  • Words
  • Containers
  • Boolean
  • str
    • strings
    • """,",'
    • "hello world", "1", 'one', 'foo'
    • """This is a multiline string.  You use three "s to indicate it."""

These are your "things"

  • Numbers
  • Words
  • Containers
  • Boolean
  • list
    • Lists
    • ['apple', 'banana']
  • dict
    • dictionaries
    • {'apple': 1.25, 'banana': .75}
  • tuple
    • tuples
    • (1, 2, 3)

We'll talk about these a lot more later on.

These are your "things"

  • Numbers
  • Words
  • Containers
  • Boolean
  • True
    • Logical true
  • False
    • Logical false
  • Note the caps!

We'll talk about these a lot more later on.

Variables!

We can name things now

Buckets and buckets of kittens

...err, values.

Names are handy

Let's say I want to give the int value of 1,203,109 a name.  How about my_big_number?

This also works with text.

>>> my_big_number = 1203109
>>> my_big_number
1203109
>>> my_big_number * 2
2406218
>>> my_big_number /3
401036
>>> my_big_number % 3
1
>>> my_big_number / 3.0
401036.3333333333
>>> my_other_number = my_big_number * 4
>>> my_other_number
4812436
>>> first = "Elizabeth"
>>> family = "Wickes"
>>> full = first + " " + family
>>> full
'Elizabeth Wickes'

Remember that dynamic thing?

>>> stuff = 1
>>> stuff
1
>>> type(stuff)
<type 'int'>
>>> stuff = "I'm now a string"
>>> type(stuff)
<type 'str'>

Dynamic typing:

The bucket doesn't care, the bucket just contains.

Duck typing

  • "walks like a duck and swims like a duck and quacks like a duck, I call that bird a duck"
  • Python says: "If it walks like a string, talks like a string, and I can do string-like things to it, I call that object a string"

100% bunny-like object

How do you write things in Python?

 

Lesson 3: Verbs

Operators

Math trigger warning

Spoiler alert:  you'll be fine

Basic operators

* Multiply: 2 * 3 = 6

+ Addition: 2 + 2 = 4

-  Subtraction:  2 - 2 = 0

** Exponent: 4 ** 2 = 16

% Modulo (find remainder):

     4 % 2 = 0

     12 % 6 = 0

     3 % 2 = 1

== Equality check (logical operator): 5 == 5 = True

Division gets weird

 

1 / 2 = 0 but 1.0 / 2.0 = 0.5

 

You get what you give.

 

Don't worry so much about this yet.  You'll get it in practice.

Remember strong typing?

Some operators work with non-numbers.

Plus (+) will work between two strings.

Multiply (*) will work between a string and an integer.

>>> 2+2
4
>>> "two" + "two"
'twotwo'
>>> "two" * 2
'twotwo'

But some don't work...

>>> "2" + 2
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: cannot concatenate 'str' and 'int' objects
>>> "two" * 2.3
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: can't multiply sequence by non-int of type 'float'

How do you write things in Python?

 

Lesson 4: Sentences

Expressions

Gettin' things done!

Expressions are how you make things happen

  • You've already seen some of these:
    • print "hello world"
    • 2 + 2
    • "hello * 4
    • 5 == 5​
  • Get a little fancy
    • ("fifty" + "five") * 4
  • You'll learn more as you expand your vocabulary

Class participation!

  1. Come to the front of the class, and introduce yourself as a variable with your name.
  2. Grab a paper with a noun.
  3. Introduce yourself with your variable name, your type, and your value.
  4. I'll bring up some verbs.
  5. Pair up as valid expressons and determine your output.
  6. Don't pair up if you'll produce an error!

Homework!

Read/watch

Python for Informatics

  • Chapter 1
  • Chapter 2

Attempt

  • After 2.9: Python Syntax
  • After 2.9: Tip Calculator
  • Chapter 2 exercises

Session 1: Intro to Programming using Python

By Elizabeth W.

Session 1: Intro to Programming using Python

Lecture slides for Day 1 of http://elizabethwickes.com/classroom-lesson-plan/

  • 1,128