CIS192: Python Programming - Introductioncis192/html/files/lec/week1/lec1.pdfCIS192: Python...

43
CIS192: Python Programming Introduction Harry Smith University of Pennsylvania January 11, 2018 Harry Smith (University of Pennsylvania) CIS 192 Lecture 1 Spring 2018 January 11, 2018 1 / 41

Transcript of CIS192: Python Programming - Introductioncis192/html/files/lec/week1/lec1.pdfCIS192: Python...

Page 1: CIS192: Python Programming - Introductioncis192/html/files/lec/week1/lec1.pdfCIS192: Python Programming Introduction Harry Smith ... CIS 192: Python Programming I Powerful scripting

CIS192: Python ProgrammingIntroduction

Harry Smith

University of Pennsylvania

January 11, 2018

Harry Smith (University of Pennsylvania) CIS 192 Lecture 1 Spring 2018 January 11, 2018 1 / 41

Page 2: CIS192: Python Programming - Introductioncis192/html/files/lec/week1/lec1.pdfCIS192: Python Programming Introduction Harry Smith ... CIS 192: Python Programming I Powerful scripting

Outline

1 LogisticsRooms and Office HoursGradingClass Materials

2 PythonWhat is Python?The Basics

3 Wrapping Up the First Class

Harry Smith (University of Pennsylvania) CIS 192 Lecture 1 Spring 2018 January 11, 2018 2 / 41

Page 3: CIS192: Python Programming - Introductioncis192/html/files/lec/week1/lec1.pdfCIS192: Python Programming Introduction Harry Smith ... CIS 192: Python Programming I Powerful scripting

What’s CIS 192?

CIS 19X coursesI 0.5 Credits eachI Designed to teach practical skillsI Intended to be lightweight and highly functional.

CIS 192: Python ProgrammingI Powerful scripting language used in academia and industryI Simple to read and write

Take this class ifI You have some programming experienceI You are relatively new to Python

Harry Smith (University of Pennsylvania) CIS 192 Lecture 1 Spring 2018 January 11, 2018 3 / 41

Page 4: CIS192: Python Programming - Introductioncis192/html/files/lec/week1/lec1.pdfCIS192: Python Programming Introduction Harry Smith ... CIS 192: Python Programming I Powerful scripting

CIS 192 Logistics

Class: CIS 192 Python ProgrammingI Listed as CIS 192 201I Room: Towne 309 (Here)I Time: Thursdays, 12:00 - 1:30pm (Now)

F Unfortunately, I have a class right afterF Talk to me in office hoursF Ask questions on PiazzaF ⇒ please don’t ask me questions right after class!

Instructor: Harry SmithI Undergrad Senior in LogicI Call me Harry! (Not a professor!)

TAI Luke Mainwaring

Harry Smith (University of Pennsylvania) CIS 192 Lecture 1 Spring 2018 January 11, 2018 4 / 41

Page 5: CIS192: Python Programming - Introductioncis192/html/files/lec/week1/lec1.pdfCIS192: Python Programming Introduction Harry Smith ... CIS 192: Python Programming I Powerful scripting

CIS 192 001

Class: CIS 19X Shared LectureI Room: Towne 100I Time: Wednesdays, 6:00 - 7:30pmI General, useful info: command line intro, Git, overview of the

Internet, HTML/CSS.I If you do not have experience with networks, HTML, HTTP, and

internet structure, please attend these lectures.

Instructor: Swapneel Sheth

Harry Smith (University of Pennsylvania) CIS 192 Lecture 1 Spring 2018 January 11, 2018 5 / 41

Page 6: CIS192: Python Programming - Introductioncis192/html/files/lec/week1/lec1.pdfCIS192: Python Programming Introduction Harry Smith ... CIS 192: Python Programming I Powerful scripting

Outline

1 LogisticsRooms and Office HoursGradingClass Materials

2 PythonWhat is Python?The Basics

3 Wrapping Up the First Class

Harry Smith (University of Pennsylvania) CIS 192 Lecture 1 Spring 2018 January 11, 2018 6 / 41

Page 7: CIS192: Python Programming - Introductioncis192/html/files/lec/week1/lec1.pdfCIS192: Python Programming Introduction Harry Smith ... CIS 192: Python Programming I Powerful scripting

Grading Policy & Philosophy

Everyone gets an A-

Harry Smith (University of Pennsylvania) CIS 192 Lecture 1 Spring 2018 January 11, 2018 7 / 41

Page 8: CIS192: Python Programming - Introductioncis192/html/files/lec/week1/lec1.pdfCIS192: Python Programming Introduction Harry Smith ... CIS 192: Python Programming I Powerful scripting

Grading Policy & Philosophy

Everyone gets an A-Really?

Harry Smith (University of Pennsylvania) CIS 192 Lecture 1 Spring 2018 January 11, 2018 8 / 41

Page 9: CIS192: Python Programming - Introductioncis192/html/files/lec/week1/lec1.pdfCIS192: Python Programming Introduction Harry Smith ... CIS 192: Python Programming I Powerful scripting

Grading Policy & Philosophy

Everyone gets an A-Really?Yes.

Harry Smith (University of Pennsylvania) CIS 192 Lecture 1 Spring 2018 January 11, 2018 9 / 41

Page 10: CIS192: Python Programming - Introductioncis192/html/files/lec/week1/lec1.pdfCIS192: Python Programming Introduction Harry Smith ... CIS 192: Python Programming I Powerful scripting

Grading Policy & Philosophy

Everyone gets an A-Really?Yes.Really really?

Harry Smith (University of Pennsylvania) CIS 192 Lecture 1 Spring 2018 January 11, 2018 10 / 41

Page 11: CIS192: Python Programming - Introductioncis192/html/files/lec/week1/lec1.pdfCIS192: Python Programming Introduction Harry Smith ... CIS 192: Python Programming I Powerful scripting

Grading Policy & Philosophy

Everyone gets an A-Really?Yes.Really really?No.

Harry Smith (University of Pennsylvania) CIS 192 Lecture 1 Spring 2018 January 11, 2018 11 / 41

Page 12: CIS192: Python Programming - Introductioncis192/html/files/lec/week1/lec1.pdfCIS192: Python Programming Introduction Harry Smith ... CIS 192: Python Programming I Powerful scripting

Grading Policy & Philosophy

Everyone gets an A-Really?Yes.Really really?No.Explanation

Harry Smith (University of Pennsylvania) CIS 192 Lecture 1 Spring 2018 January 11, 2018 12 / 41

Page 13: CIS192: Python Programming - Introductioncis192/html/files/lec/week1/lec1.pdfCIS192: Python Programming Introduction Harry Smith ... CIS 192: Python Programming I Powerful scripting

Grade Breakdown

Homework: 70%I 1 per week until some time before the end of the semesterI Programming assignmentsI These will range from collections of exercises on a week’s material

to mini-projects with time given to work in class with instructorassistance.

I Will be looking for a lot of feedback here throughout the semester.Final Project: 25%

I Anything you wantI Individually or in a pair

Participation: 5%I Attendance/Piazza/Office Hours

Homework InfoI Drop one homework (lowest grade)I Late homeworks accepted up to 24 hours late with 20% penalty.

Harry Smith (University of Pennsylvania) CIS 192 Lecture 1 Spring 2018 January 11, 2018 13 / 41

Page 14: CIS192: Python Programming - Introductioncis192/html/files/lec/week1/lec1.pdfCIS192: Python Programming Introduction Harry Smith ... CIS 192: Python Programming I Powerful scripting

HomeworksPython 3 (3.6 or latest stable)Submit on Canvas!Graded for correctness (80%) and style (20%)Due Sunday nights at 11:59pmHW1 due next Sunday (January 21)Academic Integrity

I The Office of Student ConductI Don’t copy-paste code from other peopleI Don’t have mid-level discussions

F High-Level: What are the pros/cons of using Python for X?F Low-Level: What is the syntax for decorators?F Mid-Level: How did you do HW 1?

If you are going to do "research" for a problem...I Do not copy and paste code from a StackOverflow responseI Do not look at or copy material that directly solves a questionI In the case of reasonable online research, you must absolutely

cite sources.I When in doubt, consult me or TAs.

Harry Smith (University of Pennsylvania) CIS 192 Lecture 1 Spring 2018 January 11, 2018 14 / 41

Page 15: CIS192: Python Programming - Introductioncis192/html/files/lec/week1/lec1.pdfCIS192: Python Programming Introduction Harry Smith ... CIS 192: Python Programming I Powerful scripting

Regrades

These are due one week after you receive a grade. Noexceptions.Homeworks

I If you have a homework that needs to be regraded, please speakwith Luke.

I Submit a written explanation of the small changes that need to bemade.

I You can earn three-fourths of your points back.I Changes will be made at Luke’s discretion.

Project and ParticipationI Submit these to Harry.I This will become important at the end of the semester.

Harry Smith (University of Pennsylvania) CIS 192 Lecture 1 Spring 2018 January 11, 2018 15 / 41

Page 16: CIS192: Python Programming - Introductioncis192/html/files/lec/week1/lec1.pdfCIS192: Python Programming Introduction Harry Smith ... CIS 192: Python Programming I Powerful scripting

Outline

1 LogisticsRooms and Office HoursGradingClass Materials

2 PythonWhat is Python?The Basics

3 Wrapping Up the First Class

Harry Smith (University of Pennsylvania) CIS 192 Lecture 1 Spring 2018 January 11, 2018 16 / 41

Page 17: CIS192: Python Programming - Introductioncis192/html/files/lec/week1/lec1.pdfCIS192: Python Programming Introduction Harry Smith ... CIS 192: Python Programming I Powerful scripting

Reading

No text book!Python Official DocumentationIn-class slides and code (available on CIS 192 website)Piazza

Harry Smith (University of Pennsylvania) CIS 192 Lecture 1 Spring 2018 January 11, 2018 17 / 41

Page 18: CIS192: Python Programming - Introductioncis192/html/files/lec/week1/lec1.pdfCIS192: Python Programming Introduction Harry Smith ... CIS 192: Python Programming I Powerful scripting

Programming Environment

Unix system recommendedI Eniac is one!

EditorI Can use anything you want (Sublime Text, Atom, vim, emacs, etc.)I IDEs also available: PyDev for Eclipse, PyCharmI Set your editor to interpret tabs as four spaces

F Python is whitespace-sensitive

Limited support for students not using the above.

Harry Smith (University of Pennsylvania) CIS 192 Lecture 1 Spring 2018 January 11, 2018 18 / 41

Page 19: CIS192: Python Programming - Introductioncis192/html/files/lec/week1/lec1.pdfCIS192: Python Programming Introduction Harry Smith ... CIS 192: Python Programming I Powerful scripting

Outline

1 LogisticsRooms and Office HoursGradingClass Materials

2 PythonWhat is Python?The Basics

3 Wrapping Up the First Class

Harry Smith (University of Pennsylvania) CIS 192 Lecture 1 Spring 2018 January 11, 2018 19 / 41

Page 20: CIS192: Python Programming - Introductioncis192/html/files/lec/week1/lec1.pdfCIS192: Python Programming Introduction Harry Smith ... CIS 192: Python Programming I Powerful scripting

Easy to Learn

Harry Smith (University of Pennsylvania) CIS 192 Lecture 1 Spring 2018 January 11, 2018 20 / 41

Page 21: CIS192: Python Programming - Introductioncis192/html/files/lec/week1/lec1.pdfCIS192: Python Programming Introduction Harry Smith ... CIS 192: Python Programming I Powerful scripting

Easy to Use

Harry Smith (University of Pennsylvania) CIS 192 Lecture 1 Spring 2018 January 11, 2018 21 / 41

Page 22: CIS192: Python Programming - Introductioncis192/html/files/lec/week1/lec1.pdfCIS192: Python Programming Introduction Harry Smith ... CIS 192: Python Programming I Powerful scripting

Easy to Abuse

Harry Smith (University of Pennsylvania) CIS 192 Lecture 1 Spring 2018 January 11, 2018 22 / 41

Page 23: CIS192: Python Programming - Introductioncis192/html/files/lec/week1/lec1.pdfCIS192: Python Programming Introduction Harry Smith ... CIS 192: Python Programming I Powerful scripting

History

Designer: Guido van RossumI Benevolent Dictator For Life (BDFL)

Multi-Paradigm: Object-Oriented, Functional, Imperative...Strongly and Dynamically TypedWhitespace delimited blocksGarbage Collected

Harry Smith (University of Pennsylvania) CIS 192 Lecture 1 Spring 2018 January 11, 2018 23 / 41

Page 24: CIS192: Python Programming - Introductioncis192/html/files/lec/week1/lec1.pdfCIS192: Python Programming Introduction Harry Smith ... CIS 192: Python Programming I Powerful scripting

Philosophy

The Zen of PythonI Beautiful is better than uglyI Explicit is better than implicitI Simple is better than complexI Complex is better than complicatedI Readability Counts

Other ideasI There should be one obvious way to do itI Clarity over marginal efficiencyI We’re all consenting adults here

Harry Smith (University of Pennsylvania) CIS 192 Lecture 1 Spring 2018 January 11, 2018 24 / 41

Page 25: CIS192: Python Programming - Introductioncis192/html/files/lec/week1/lec1.pdfCIS192: Python Programming Introduction Harry Smith ... CIS 192: Python Programming I Powerful scripting

Outline

1 LogisticsRooms and Office HoursGradingClass Materials

2 PythonWhat is Python?The Basics

3 Wrapping Up the First Class

Harry Smith (University of Pennsylvania) CIS 192 Lecture 1 Spring 2018 January 11, 2018 25 / 41

Page 26: CIS192: Python Programming - Introductioncis192/html/files/lec/week1/lec1.pdfCIS192: Python Programming Introduction Harry Smith ... CIS 192: Python Programming I Powerful scripting

REPL

Read Evaluate Print Loop (a.k.a. an interpreter)Type “Python” at the terminal

Test out language behavior hereGet information with dir(), help(), type()

Harry Smith (University of Pennsylvania) CIS 192 Lecture 1 Spring 2018 January 11, 2018 26 / 41

Page 27: CIS192: Python Programming - Introductioncis192/html/files/lec/week1/lec1.pdfCIS192: Python Programming Introduction Harry Smith ... CIS 192: Python Programming I Powerful scripting

Identifiers, Names, Variables

All 3 mean the same thingVariable naming convention

I Functions and variables: lower_with_underscoreF my_num = 5

I Constants: UPPER_WITH_UNDERSCOREF SECONDS_PER_MINUTE = 60

Harry Smith (University of Pennsylvania) CIS 192 Lecture 1 Spring 2018 January 11, 2018 27 / 41

Page 28: CIS192: Python Programming - Introductioncis192/html/files/lec/week1/lec1.pdfCIS192: Python Programming Introduction Harry Smith ... CIS 192: Python Programming I Powerful scripting

Binding

x = 1y = xx = ’a’

X 1

Harry Smith (University of Pennsylvania) CIS 192 Lecture 1 Spring 2018 January 11, 2018 28 / 41

Page 29: CIS192: Python Programming - Introductioncis192/html/files/lec/week1/lec1.pdfCIS192: Python Programming Introduction Harry Smith ... CIS 192: Python Programming I Powerful scripting

Binding

x = 1y = xx = ’a’

X

Y

1

Harry Smith (University of Pennsylvania) CIS 192 Lecture 1 Spring 2018 January 11, 2018 28 / 41

Page 30: CIS192: Python Programming - Introductioncis192/html/files/lec/week1/lec1.pdfCIS192: Python Programming Introduction Harry Smith ... CIS 192: Python Programming I Powerful scripting

Binding

x = 1y = xx = ’a’

X

Y

1

a

Harry Smith (University of Pennsylvania) CIS 192 Lecture 1 Spring 2018 January 11, 2018 28 / 41

Page 31: CIS192: Python Programming - Introductioncis192/html/files/lec/week1/lec1.pdfCIS192: Python Programming Introduction Harry Smith ... CIS 192: Python Programming I Powerful scripting

Objects

Python treats all data as objectsIdentity

I Memory addressI Does not change

TypeI Does not change

ValueI Mutable: value can be changed (e.g. [1,2])I Immutable: value cannot be changed after creation (e.g. (1,2))

EqualityI Use is for referential equality (do x and y point to the same object?)I Use == for structural equality (are x and y equal based on object’s__eq__ method?)

Harry Smith (University of Pennsylvania) CIS 192 Lecture 1 Spring 2018 January 11, 2018 29 / 41

Page 32: CIS192: Python Programming - Introductioncis192/html/files/lec/week1/lec1.pdfCIS192: Python Programming Introduction Harry Smith ... CIS 192: Python Programming I Powerful scripting

Types

Every object has a typeInspect types with type(object)

isinstance(object, type) checks type hierarchyTypes can be compared for equality, but you usually wantisinstance

Some types:I int, floatI strI tuple, list, dictI range, bool, NoneI function

Harry Smith (University of Pennsylvania) CIS 192 Lecture 1 Spring 2018 January 11, 2018 30 / 41

Page 33: CIS192: Python Programming - Introductioncis192/html/files/lec/week1/lec1.pdfCIS192: Python Programming Introduction Harry Smith ... CIS 192: Python Programming I Powerful scripting

Math

LiteralsI Integers: 1, 2I Floats: 1.0, 2e10I Complex: 1j, 2e10jI Binary: 0b1001, Hex: 0xFF, Octal: 0o72

OperationsI Arithmetic: + - * /I Power: **I Integer division: //I Modulus: %I Bitwise: « » & | ˆI Comparison: <, >, <=, >=, ==, !=

Assignment OperatorsI += *= /= &= ...I No ++ or --

Harry Smith (University of Pennsylvania) CIS 192 Lecture 1 Spring 2018 January 11, 2018 31 / 41

Page 34: CIS192: Python Programming - Introductioncis192/html/files/lec/week1/lec1.pdfCIS192: Python Programming Introduction Harry Smith ... CIS 192: Python Programming I Powerful scripting

Booleans

True and False

Boolean operators: and or not

Any object can be tested for truth value for use in conditionals, oras operands of the above Boolean operations."Falsy"

I NoneI 0I 0.0I Any empty string/sequence/collection ([],(), etc.)

Almost everything in Python is True.

Harry Smith (University of Pennsylvania) CIS 192 Lecture 1 Spring 2018 January 11, 2018 32 / 41

Page 35: CIS192: Python Programming - Introductioncis192/html/files/lec/week1/lec1.pdfCIS192: Python Programming Introduction Harry Smith ... CIS 192: Python Programming I Powerful scripting

Comparisons

<, <=, >, >=, ==, !=, is, is not

Chainable binary operatorsI This means that x < y <= z and x < y and y <= z are

equivalent.

x is y simplifies to True when x and y refer to the same object.

Harry Smith (University of Pennsylvania) CIS 192 Lecture 1 Spring 2018 January 11, 2018 33 / 41

Page 36: CIS192: Python Programming - Introductioncis192/html/files/lec/week1/lec1.pdfCIS192: Python Programming Introduction Harry Smith ... CIS 192: Python Programming I Powerful scripting

Strings

Can use either single or double quotesUse single to show double flip-flop "’" → ’ and ’"’ → "Triplequote for multiline stringCan concat strings by separating string literals with whitespaceAll strings are unicodePrefixing with r means raw. No need to escape characters: r’\n’

Harry Smith (University of Pennsylvania) CIS 192 Lecture 1 Spring 2018 January 11, 2018 34 / 41

Page 37: CIS192: Python Programming - Introductioncis192/html/files/lec/week1/lec1.pdfCIS192: Python Programming Introduction Harry Smith ... CIS 192: Python Programming I Powerful scripting

Conditionals

One if blockZero or more elif blocksZero or one else block

Harry Smith (University of Pennsylvania) CIS 192 Lecture 1 Spring 2018 January 11, 2018 35 / 41

Page 38: CIS192: Python Programming - Introductioncis192/html/files/lec/week1/lec1.pdfCIS192: Python Programming Introduction Harry Smith ... CIS 192: Python Programming I Powerful scripting

Sequences

ImmutableI Strings, Tuples

MutableI Lists

OperationsI len()I IndexingI SlicingI inI not in

Harry Smith (University of Pennsylvania) CIS 192 Lecture 1 Spring 2018 January 11, 2018 36 / 41

Page 39: CIS192: Python Programming - Introductioncis192/html/files/lec/week1/lec1.pdfCIS192: Python Programming Introduction Harry Smith ... CIS 192: Python Programming I Powerful scripting

Range

Immutable sequence of numbersrange(stop), range(start,stop[,step])

start defaults to 0step defaults to 1All numbers in [start,stop) by incrementing start by stepNegative steps are validMemory efficent: Calculates values as you iterate over them

Harry Smith (University of Pennsylvania) CIS 192 Lecture 1 Spring 2018 January 11, 2018 37 / 41

Page 40: CIS192: Python Programming - Introductioncis192/html/files/lec/week1/lec1.pdfCIS192: Python Programming Introduction Harry Smith ... CIS 192: Python Programming I Powerful scripting

Loops

For each loops (for item in my_list:)I Iterate over an object

While loops (while some_condition:)I Continues as long as condition holds

BothI else: executes after loop finishes

F For loops: the sequence has been exhaustedF While loops: the condition has been made False.

I break: stops the loop and skips the else clauseI continue: starts the next iteration of the loop

Harry Smith (University of Pennsylvania) CIS 192 Lecture 1 Spring 2018 January 11, 2018 38 / 41

Page 41: CIS192: Python Programming - Introductioncis192/html/files/lec/week1/lec1.pdfCIS192: Python Programming Introduction Harry Smith ... CIS 192: Python Programming I Powerful scripting

Functions

Functions are first classI They’re objects, too!I Can pass them as argumentsI Can assign them to variables

Define functions with a defreturn keyword to return a valuepass: Use this to finish a function that is empty so it compiles.If a function reaches the end of the block without returning,it will return None (null)

Harry Smith (University of Pennsylvania) CIS 192 Lecture 1 Spring 2018 January 11, 2018 39 / 41

Page 42: CIS192: Python Programming - Introductioncis192/html/files/lec/week1/lec1.pdfCIS192: Python Programming Introduction Harry Smith ... CIS 192: Python Programming I Powerful scripting

Importing Modules

Allow use of other python files and librariesimports: import math

Named imports: import math as m

Specific imports: from math import pow

Import all: from math import *

Harry Smith (University of Pennsylvania) CIS 192 Lecture 1 Spring 2018 January 11, 2018 40 / 41

Page 43: CIS192: Python Programming - Introductioncis192/html/files/lec/week1/lec1.pdfCIS192: Python Programming Introduction Harry Smith ... CIS 192: Python Programming I Powerful scripting

Last thoughts

Confirm that you are on Canvas/PiazzaFill out the anonymous survey on Piazza/in your emailIf you are not registered for the class, please come talk to meHomework 1 to be released shortly (or is already released) andwill be due next Sunday.

Harry Smith (University of Pennsylvania) CIS 192 Lecture 1 Spring 2018 January 11, 2018 41 / 41