CIS192 Python Programming - Introductioncis192/fall2015/files/lec/lec1.pdf · PyDev for Eclipse...

33
CIS192 Python Programming Introduction Robert Rand University of Pennsylvania August 27, 2015 Robert Rand (University of Pennsylvania) CIS 192 August 27, 2015 1 / 30

Transcript of CIS192 Python Programming - Introductioncis192/fall2015/files/lec/lec1.pdf · PyDev for Eclipse...

Page 1: CIS192 Python Programming - Introductioncis192/fall2015/files/lec/lec1.pdf · PyDev for Eclipse Recommended I Emacs is also good Make sure your editor interprets TAB as four spaces

CIS192 Python ProgrammingIntroduction

Robert Rand

University of Pennsylvania

August 27, 2015

Robert Rand (University of Pennsylvania) CIS 192 August 27, 2015 1 / 30

Page 2: CIS192 Python Programming - Introductioncis192/fall2015/files/lec/lec1.pdf · PyDev for Eclipse Recommended I Emacs is also good Make sure your editor interprets TAB as four spaces

Outline

1 LogisticsGradingOffice HoursShared LectureSoftware

2 PythonWhat is PythonThe BasicsDynamic Types

Robert Rand (University of Pennsylvania) CIS 192 August 27, 2015 2 / 30

Page 3: CIS192 Python Programming - Introductioncis192/fall2015/files/lec/lec1.pdf · PyDev for Eclipse Recommended I Emacs is also good Make sure your editor interprets TAB as four spaces

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

Page 4: CIS192 Python Programming - Introductioncis192/fall2015/files/lec/lec1.pdf · PyDev for Eclipse Recommended I Emacs is also good Make sure your editor interprets TAB as four spaces

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

Page 5: CIS192 Python Programming - Introductioncis192/fall2015/files/lec/lec1.pdf · PyDev for Eclipse Recommended I Emacs is also good Make sure your editor interprets TAB as four spaces

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

Page 6: CIS192 Python Programming - Introductioncis192/fall2015/files/lec/lec1.pdf · PyDev for Eclipse Recommended I Emacs is also good Make sure your editor interprets TAB as four spaces

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

Page 7: CIS192 Python Programming - Introductioncis192/fall2015/files/lec/lec1.pdf · PyDev for Eclipse Recommended I Emacs is also good Make sure your editor interprets TAB as four spaces

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

Page 8: CIS192 Python Programming - Introductioncis192/fall2015/files/lec/lec1.pdf · PyDev for Eclipse Recommended I Emacs is also good Make sure your editor interprets TAB as four spaces

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

Page 9: CIS192 Python Programming - Introductioncis192/fall2015/files/lec/lec1.pdf · PyDev for Eclipse Recommended I Emacs is also good Make sure your editor interprets TAB as four spaces

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

Page 10: CIS192 Python Programming - Introductioncis192/fall2015/files/lec/lec1.pdf · PyDev for Eclipse Recommended I Emacs is also good Make sure your editor interprets TAB as four spaces

Outline

1 LogisticsGradingOffice HoursShared LectureSoftware

2 PythonWhat is PythonThe BasicsDynamic Types

Robert Rand (University of Pennsylvania) CIS 192 August 27, 2015 10 / 30

Page 11: CIS192 Python Programming - Introductioncis192/fall2015/files/lec/lec1.pdf · PyDev for Eclipse Recommended I Emacs is also good Make sure your editor interprets TAB as four spaces

Easy to Learn

Robert Rand (University of Pennsylvania) CIS 192 August 27, 2015 11 / 30

Page 12: CIS192 Python Programming - Introductioncis192/fall2015/files/lec/lec1.pdf · PyDev for Eclipse Recommended I Emacs is also good Make sure your editor interprets TAB as four spaces

Easy to Use

Robert Rand (University of Pennsylvania) CIS 192 August 27, 2015 12 / 30

Page 13: CIS192 Python Programming - Introductioncis192/fall2015/files/lec/lec1.pdf · PyDev for Eclipse Recommended I Emacs is also good Make sure your editor interprets TAB as four spaces

Easy to Abuse

Robert Rand (University of Pennsylvania) CIS 192 August 27, 2015 13 / 30

Page 14: CIS192 Python Programming - Introductioncis192/fall2015/files/lec/lec1.pdf · PyDev for Eclipse Recommended I Emacs is also good Make sure your editor interprets TAB as four spaces

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

Page 15: CIS192 Python Programming - Introductioncis192/fall2015/files/lec/lec1.pdf · PyDev for Eclipse Recommended I Emacs is also good Make sure your editor interprets TAB as four spaces

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

Page 16: CIS192 Python Programming - Introductioncis192/fall2015/files/lec/lec1.pdf · PyDev for Eclipse Recommended I Emacs is also good Make sure your editor interprets TAB as four spaces

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

Page 17: CIS192 Python Programming - Introductioncis192/fall2015/files/lec/lec1.pdf · PyDev for Eclipse Recommended I Emacs is also good Make sure your editor interprets TAB as four spaces

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

Page 18: CIS192 Python Programming - Introductioncis192/fall2015/files/lec/lec1.pdf · PyDev for Eclipse Recommended I Emacs is also good Make sure your editor interprets TAB as four spaces

Untyped?

Robert Rand (University of Pennsylvania) CIS 192 August 27, 2015 18 / 30

Page 19: CIS192 Python Programming - Introductioncis192/fall2015/files/lec/lec1.pdf · PyDev for Eclipse Recommended I Emacs is also good Make sure your editor interprets TAB as four spaces

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

Page 20: CIS192 Python Programming - Introductioncis192/fall2015/files/lec/lec1.pdf · PyDev for Eclipse Recommended I Emacs is also good Make sure your editor interprets TAB as four spaces

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

Page 21: CIS192 Python Programming - Introductioncis192/fall2015/files/lec/lec1.pdf · PyDev for Eclipse Recommended I Emacs is also good Make sure your editor interprets TAB as four spaces

Binding

x = 1y = xx = ’a’

X 1

Robert Rand (University of Pennsylvania) CIS 192 August 27, 2015 21 / 30

Page 22: CIS192 Python Programming - Introductioncis192/fall2015/files/lec/lec1.pdf · PyDev for Eclipse Recommended I Emacs is also good Make sure your editor interprets TAB as four spaces

Binding

x = 1y = xx = ’a’

X

Y

1

Robert Rand (University of Pennsylvania) CIS 192 August 27, 2015 21 / 30

Page 23: CIS192 Python Programming - Introductioncis192/fall2015/files/lec/lec1.pdf · PyDev for Eclipse Recommended I Emacs is also good Make sure your editor interprets TAB as four spaces

Binding

x = 1y = xx = ’a’

X

Y

1

a

Robert Rand (University of Pennsylvania) CIS 192 August 27, 2015 21 / 30

Page 24: CIS192 Python Programming - Introductioncis192/fall2015/files/lec/lec1.pdf · PyDev for Eclipse Recommended I Emacs is also good Make sure your editor interprets TAB as four spaces

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

Page 25: CIS192 Python Programming - Introductioncis192/fall2015/files/lec/lec1.pdf · PyDev for Eclipse Recommended I Emacs is also good Make sure your editor interprets TAB as four spaces

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

Page 26: CIS192 Python Programming - Introductioncis192/fall2015/files/lec/lec1.pdf · PyDev for Eclipse Recommended I Emacs is also good Make sure your editor interprets TAB as four spaces

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

Page 27: CIS192 Python Programming - Introductioncis192/fall2015/files/lec/lec1.pdf · PyDev for Eclipse Recommended I Emacs is also good Make sure your editor interprets TAB as four spaces

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

Page 28: CIS192 Python Programming - Introductioncis192/fall2015/files/lec/lec1.pdf · PyDev for Eclipse Recommended I Emacs is also good Make sure your editor interprets TAB as four spaces

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

Page 29: CIS192 Python Programming - Introductioncis192/fall2015/files/lec/lec1.pdf · PyDev for Eclipse Recommended I Emacs is also good Make sure your editor interprets TAB as four spaces

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

Page 30: CIS192 Python Programming - Introductioncis192/fall2015/files/lec/lec1.pdf · PyDev for Eclipse Recommended I Emacs is also good Make sure your editor interprets TAB as four spaces

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

Page 31: CIS192 Python Programming - Introductioncis192/fall2015/files/lec/lec1.pdf · PyDev for Eclipse Recommended I Emacs is also good Make sure your editor interprets TAB as four spaces

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

Page 32: CIS192 Python Programming - Introductioncis192/fall2015/files/lec/lec1.pdf · PyDev for Eclipse Recommended I Emacs is also good Make sure your editor interprets TAB as four spaces

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

Page 33: CIS192 Python Programming - Introductioncis192/fall2015/files/lec/lec1.pdf · PyDev for Eclipse Recommended I Emacs is also good Make sure your editor interprets TAB as four spaces

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