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
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
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
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
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
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
Grading Policy & Philosophy
Everyone gets an A-
Harry Smith (University of Pennsylvania) CIS 192 Lecture 1 Spring 2018 January 11, 2018 7 / 41
Grading Policy & Philosophy
Everyone gets an A-Really?
Harry Smith (University of Pennsylvania) CIS 192 Lecture 1 Spring 2018 January 11, 2018 8 / 41
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
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
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
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
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
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
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
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
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
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
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
Easy to Learn
Harry Smith (University of Pennsylvania) CIS 192 Lecture 1 Spring 2018 January 11, 2018 20 / 41
Easy to Use
Harry Smith (University of Pennsylvania) CIS 192 Lecture 1 Spring 2018 January 11, 2018 21 / 41
Easy to Abuse
Harry Smith (University of Pennsylvania) CIS 192 Lecture 1 Spring 2018 January 11, 2018 22 / 41
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
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
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
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
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
Binding
x = 1y = xx = ’a’
X 1
Harry Smith (University of Pennsylvania) CIS 192 Lecture 1 Spring 2018 January 11, 2018 28 / 41
Binding
x = 1y = xx = ’a’
X
Y
1
Harry Smith (University of Pennsylvania) CIS 192 Lecture 1 Spring 2018 January 11, 2018 28 / 41
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
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
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
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
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
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
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
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
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
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
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
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
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
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