CIS192 Python Programming - Introductioncis192/fall2015/files/lec/lec1.pdf · PyDev for Eclipse...
Transcript of CIS192 Python Programming - Introductioncis192/fall2015/files/lec/lec1.pdf · PyDev for Eclipse...
CIS192 Python ProgrammingIntroduction
Robert Rand
University of Pennsylvania
August 27, 2015
Robert Rand (University of Pennsylvania) CIS 192 August 27, 2015 1 / 30
Outline
1 LogisticsGradingOffice HoursShared LectureSoftware
2 PythonWhat is PythonThe BasicsDynamic Types
Robert Rand (University of Pennsylvania) CIS 192 August 27, 2015 2 / 30
Grading
Homeworks: 65% of grade.Final Project: 30% of grade.Participation: 5% of grade.
Robert Rand (University of Pennsylvania) CIS 192 August 27, 2015 3 / 30
Homework
Due Sunday at midnight, ten days after classOne late weekCan discuss with a partnerCode must be your ownCite partner / all references usedSubmit via Canvas
Robert Rand (University of Pennsylvania) CIS 192 August 27, 2015 4 / 30
Final Project
Work with up to one partnerProposal due Nov 8thDemo ready version due last day of classFinal version due last day of exams
Robert Rand (University of Pennsylvania) CIS 192 August 27, 2015 5 / 30
Office Hours
Instructor: Robert RandI Thursday 1:30 - 3:30pm, Levine 513
TA: Joseph CappadonaI Tuesday 3:00 - 5:00pm, DRL 4E9
TA: Raymond YinI Friday 3:00 - 5:00pm, Towne 303
Robert Rand (University of Pennsylvania) CIS 192 August 27, 2015 6 / 30
Shared Lecture
Class: CIS 19X Shared LectureI Room: Towne 100I Time: Tuesdays 6:00 - 7:30I Only a few meetings per semesterI Includes Unix skills, version control
Instructor: Swapneel Sheth
Robert Rand (University of Pennsylvania) CIS 192 August 27, 2015 7 / 30
Programming Environment
PyDev for Eclipse RecommendedI Emacs is also good
Make sure your editor interprets TAB as fourspaces
I Python is whitespace-sensitive!
Robert Rand (University of Pennsylvania) CIS 192 August 27, 2015 8 / 30
Piazza
Use Piazza to find project partners and discusslectures/homeworks with your peers.
Robert Rand (University of Pennsylvania) CIS 192 August 27, 2015 9 / 30
Outline
1 LogisticsGradingOffice HoursShared LectureSoftware
2 PythonWhat is PythonThe BasicsDynamic Types
Robert Rand (University of Pennsylvania) CIS 192 August 27, 2015 10 / 30
Easy to Learn
Robert Rand (University of Pennsylvania) CIS 192 August 27, 2015 11 / 30
Easy to Use
Robert Rand (University of Pennsylvania) CIS 192 August 27, 2015 12 / 30
Easy to Abuse
Robert Rand (University of Pennsylvania) CIS 192 August 27, 2015 13 / 30
REPL
Read Evaluate Print LoopType “Python” at the terminalTest out language behavior hereGet information with dir(), help(), type()
Robert Rand (University of Pennsylvania) CIS 192 August 27, 2015 14 / 30
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
Robert Rand (University of Pennsylvania) CIS 192 August 27, 2015 15 / 30
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.
Robert Rand (University of Pennsylvania) CIS 192 August 27, 2015 16 / 30
Static Types
What are static types?A form of integrated specificationEnforced at compile time.VerbosePython does not have these.
Robert Rand (University of Pennsylvania) CIS 192 August 27, 2015 17 / 30
Untyped?
Robert Rand (University of Pennsylvania) CIS 192 August 27, 2015 18 / 30
Dynamic Types
Basically a system of tags.Let’s see how they work in practice.
Robert Rand (University of Pennsylvania) CIS 192 August 27, 2015 19 / 30
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
Robert Rand (University of Pennsylvania) CIS 192 August 27, 2015 20 / 30
Binding
x = 1y = xx = ’a’
X 1
Robert Rand (University of Pennsylvania) CIS 192 August 27, 2015 21 / 30
Binding
x = 1y = xx = ’a’
X
Y
1
Robert Rand (University of Pennsylvania) CIS 192 August 27, 2015 21 / 30
Binding
x = 1y = xx = ’a’
X
Y
1
a
Robert Rand (University of Pennsylvania) CIS 192 August 27, 2015 21 / 30
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
Robert Rand (University of Pennsylvania) CIS 192 August 27, 2015 22 / 30
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)
Robert Rand (University of Pennsylvania) CIS 192 August 27, 2015 23 / 30
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 --
Robert Rand (University of Pennsylvania) CIS 192 August 27, 2015 24 / 30
Strings
Can use either single or double quotesUse single to show double flip-flop "’" → ’ and ’"’→ "Triplequote for multiline stringCan concat strings by sepating string literals withwhitespaceAll strings are unicodePrefixing with r means raw. No need to escape:r’\n’
Robert Rand (University of Pennsylvania) CIS 192 August 27, 2015 25 / 30
Conditionals
One if blockZero or more elif blocksZero or one else blockBooleans: True False
Robert Rand (University of Pennsylvania) CIS 192 August 27, 2015 26 / 30
Sequences
ImmutableI Strings, Tuples, Bytes
MutableI Lists, Byte Arrays
OperationsI len()I IndexingI SlicingI inI not in
Robert Rand (University of Pennsylvania) CIS 192 August 27, 2015 27 / 30
Range
Immutable sequence of numbersrange(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 validMemory efficent: Calculates values as you iterateover them
Robert Rand (University of Pennsylvania) CIS 192 August 27, 2015 28 / 30
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
Robert Rand (University of Pennsylvania) CIS 192 August 27, 2015 29 / 30
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)
Robert Rand (University of Pennsylvania) CIS 192 August 27, 2015 30 / 30
Imports
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 *
Robert Rand (University of Pennsylvania) CIS 192 August 27, 2015 31 / 30