Upload
hoanganh
View
243
Download
0
Embed Size (px)
Citation preview
CIS192 Python ProgrammingIntroduction
Dan Gillis
University of Pennsylvania
January 20th, 2016
Dan Gillis (University of Pennsylvania) CIS 192 January 20th, 2016 1 / 37
Outline
1 About this CoursePeopleCurriculum
2 LogisticsGradingOffice HoursSoftware
3 PythonWhat is PythonThe BasicsDynamic Types
Dan Gillis (University of Pennsylvania) CIS 192 January 20th, 2016 2 / 37
People
Instructor: Robert Rand
TAs: Adel Qalieh, Dan Gillis, Harry Smith
Dan Gillis (University of Pennsylvania) CIS 192 January 20th, 2016 3 / 37
What are CIS 110-121?
Core computer science courses at Penn.Teach fundamentals of programming and criticalconcepts like recursion and data structures.Much more important than this course.But maybe less fun.
Dan Gillis (University of Pennsylvania) CIS 192 January 20th, 2016 4 / 37
What is CIS 192?
Let’s learn Python!Part One - Python Basics (∼6 weeks)
I How to program in Python.I Enough to add “Python” to your resume.
Part Two - Fun with Python (∼8 weeks)I Cool stuff we can do in Python (eg. Machine Learning,
Web Apps)I Check out previous iterations of course.I Send me ideas (suggestion box is open all semester)
Dan Gillis (University of Pennsylvania) CIS 192 January 20th, 2016 5 / 37
What is the CIS 19x Shared Lecture?
Learn CS skills with Swapneel Seth.Some of these will be used in the course(particularly command line arguments).No homeworkMeets on Tuesdays
Dan Gillis (University of Pennsylvania) CIS 192 January 20th, 2016 6 / 37
Outline
1 About this CoursePeopleCurriculum
2 LogisticsGradingOffice HoursSoftware
3 PythonWhat is PythonThe BasicsDynamic Types
Dan Gillis (University of Pennsylvania) CIS 192 January 20th, 2016 7 / 37
Grading
Homeworks: 65% of grade.Final Project: 30% of grade.Participation: 5% of grade.
Dan Gillis (University of Pennsylvania) CIS 192 January 20th, 2016 8 / 37
Homework
Due Sunday at midnight, eleven days after classOne “Late Week” extension
I Subsequent late homeworks will be penalized 10%I Homework that is more than one week late will not be
accepted.
Submit via Canvas
Dan Gillis (University of Pennsylvania) CIS 192 January 20th, 2016 9 / 37
Homework
Will be graded for correctness, efficiency andstyle.Correctness: We will often include “test” files forunit testing - use them and add your own tests.Style: Be sure to follow the PEP-8 style guidelines- you can add an automatic checker to most IDEs.Efficiency: Think before coding and try to avoidunbounded recursion and highly nested loops.
Dan Gillis (University of Pennsylvania) CIS 192 January 20th, 2016 10 / 37
Policies
General: Code of Academic IntegrityI http://www.upenn.edu/academicintegrity/ai_codeofacademicintegrity.html
Specifics:I Discuss with up to one partner.I Write your own code.I Do not look up sources that directly solve the problemI Cite partner and sources consulted (if any).
Dan Gillis (University of Pennsylvania) CIS 192 January 20th, 2016 11 / 37
Final Project
Work with up to one partnerI You can search for partners on Piazza
Proposal due March 13thFinal Projects due April 27thProject Demos will be scheduled for during thedepartment-wide Demo Day and Reading Days.Start thinking about a project now!
Dan Gillis (University of Pennsylvania) CIS 192 January 20th, 2016 12 / 37
Office Hours
Robert Rand: Friday 2:30 - 4:30pm, Levine 513and by appointment.
I Not including this Friday
Adel Qalieh: Monday 3-5pm, Location TBD.Dan Gillis: Thursday 4-6pm, DRL 4N30Harry Smith: Tuesday 3-5pm, Moore 212
Dan Gillis (University of Pennsylvania) CIS 192 January 20th, 2016 13 / 37
Programming Environment
PyDev for Eclipse RecommendedI Emacs/vim are also goodI Plenty of alternatives for various OSs
Set your editor to interpret tabs as four spacesI Python is whitespace-sensitive
Dan Gillis (University of Pennsylvania) CIS 192 January 20th, 2016 14 / 37
Piazza
Use Piazza to find project partners and discusslectures/homeworks with your peers.
Dan Gillis (University of Pennsylvania) CIS 192 January 20th, 2016 15 / 37
Outline
1 About this CoursePeopleCurriculum
2 LogisticsGradingOffice HoursSoftware
3 PythonWhat is PythonThe BasicsDynamic Types
Dan Gillis (University of Pennsylvania) CIS 192 January 20th, 2016 16 / 37
REPL
Read Evaluate Print LoopType “Python” at the terminalTest out language behavior hereGet information with dir(), help(), type()
Dan Gillis (University of Pennsylvania) CIS 192 January 20th, 2016 20 / 37
2.7 vs. 3.4
Python 2.7 Python 3.4print “hello world!” print(“hello world!”)1/2 = 0 1/2 = 0.5
1 // 2 = 0No Unicode support Unicode supportMore popular Gaining popularityBetter library support Good library support
Dan Gillis (University of Pennsylvania) CIS 192 January 20th, 2016 21 / 37
Solution
from __future__ importabsolute_import, division,print_function
Changes Python 2.7’s behavior to mimic 3.x’sBest of both worldsAdd to the top of every .py file.
Dan Gillis (University of Pennsylvania) CIS 192 January 20th, 2016 22 / 37
Static Types
What are static types?A form of integrated specificationEnforced at compile time.VerbosePython does not have these.
Dan Gillis (University of Pennsylvania) CIS 192 January 20th, 2016 23 / 37
Dynamic Types
Basically a system of tags.Let’s see how they work in practice.
Dan Gillis (University of Pennsylvania) CIS 192 January 20th, 2016 25 / 37
Identifiers, Names, Variables
All 3 mean the same thing[A-Za-z0-9_] First character cannot be a numberVariable naming convention
I Functions and variables: lower_with_underscoreI Constants: UPPER_WITH_UNDERSCORE
Dan Gillis (University of Pennsylvania) CIS 192 January 20th, 2016 26 / 37
Binding
x = 1y = xx = ’a’
X 1
Dan Gillis (University of Pennsylvania) CIS 192 January 20th, 2016 27 / 37
Binding
x = 1y = xx = ’a’
X
Y
1
Dan Gillis (University of Pennsylvania) CIS 192 January 20th, 2016 27 / 37
Binding
x = 1y = xx = ’a’
X
Y
1
a
Dan Gillis (University of Pennsylvania) CIS 192 January 20th, 2016 27 / 37
Types
Every object has a typeInspect types with type(object)
isinstance(object, type) checks typeheirarchyTypes can be compared for equality but usuallywant isinstance()Some types:
I int, float, complexI str, bytes, tupleI list,bytearrayI range, bool, NoneI function
Dan Gillis (University of Pennsylvania) CIS 192 January 20th, 2016 28 / 37
Objects
Python treats all data as objectsIdentity
I Memory addressI Does not change
TypeI Does not change
ValueI Mutable→ [1,2]I Immutable→ (1,2)
Dan Gillis (University of Pennsylvania) CIS 192 January 20th, 2016 29 / 37
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 --
Dan Gillis (University of Pennsylvania) CIS 192 January 20th, 2016 30 / 37
Strings
Can use either single or double quotesUse single to show double flip-flop "’"→ ’ and ’"’→ "Triplequote for multiline stringCan concatenate strings by sepating string literalswith whitespaceStrings are not unicode
I One of the major differences between Python 2 and Python 3
Prefixing with r means raw. No need to escape:r’\n’
Dan Gillis (University of Pennsylvania) CIS 192 January 20th, 2016 31 / 37
Conditionals
One if blockZero or more elif blocksZero or one else blockBooleans: True False
Dan Gillis (University of Pennsylvania) CIS 192 January 20th, 2016 32 / 37
Sequences
ImmutableI Strings, Tuples, Bytes
MutableI Lists, Byte Arrays
OperationsI len()I IndexingI SlicingI inI not in
Dan Gillis (University of Pennsylvania) CIS 192 January 20th, 2016 33 / 37
Range
List of numbersI Stores all values in memory
range(stop), range(start,stop)range(start,stop,step])
start defaults to 0step defaults to 1All numbers in [start,stop) by incrementing start bystepNegative steps are validAlternate function: xrange
I Immutable sequence of numbersI Memory efficient: Calculates values as you iterate over them
Dan Gillis (University of Pennsylvania) CIS 192 January 20th, 2016 34 / 37
Loops
For each loopsI Iterate over an object
While loopsI Continues as long as condition holds
BothI else: executes after loop finishesI break: stops the loop and skips the else clauseI continue: starts the next iteration of the loop
Dan Gillis (University of Pennsylvania) CIS 192 January 20th, 2016 35 / 37
Functions
Functions are first classI Can pass them as argumentsI Can assign them to variables
Define functions with a defreturn keyword to return a valueIf a function reaches the end of the block withoutreturningIt will return None (null)
Dan Gillis (University of Pennsylvania) CIS 192 January 20th, 2016 36 / 37