34
CIS192 Python Programming Introduction Raymond Yin University of Pennsylvania August 31, 2016 Raymond Yin (University of Pennsylvania) CIS 192 August 31, 2016 1 / 32

CIS192 Python Programming - Introductioncis192/fall2016/files/lec/lec...IDEs also available: PyDev for Eclipse, PyCharm Set your editor to interpret tabs as four spaces Python is whitespace-sensitive

  • Upload
    others

  • View
    5

  • Download
    0

Embed Size (px)

Citation preview

  • CIS192 Python ProgrammingIntroduction

    Raymond Yin

    University of Pennsylvania

    August 31, 2016

    Raymond Yin (University of Pennsylvania) CIS 192 August 31, 2016 1 / 32

  • Outline

    1 LogisticsRooms and Office HoursGradingClass Materials

    2 PythonWhat is Python?The Basics

    Raymond Yin (University of Pennsylvania) CIS 192 August 31, 2016 2 / 32

  • What’s CIS 192?

    CIS 19X courses0.5 Credits eachDesigned to teach practical skills

    CIS 192: Python ProgrammingPowerful scripting language used in academia and industrySimple to read and write in

    Take this class ifYou have some programming experienceYou are relatively new to Python

    Raymond Yin (University of Pennsylvania) CIS 192 August 31, 2016 3 / 32

  • CIS 192 Logistics

    Class: CIS 192 Python ProgrammingListed as CIS 192 201Room: Towne 303Time: Wednesdays, 12:00 - 1:30pm

    Instructor: Raymond YinUndergrad in CS (Not a professor)

    TAsHarry SmithAlex Frias

    Raymond Yin (University of Pennsylvania) CIS 192 August 31, 2016 4 / 32

  • CIS 192 001

    Class: CIS 19X Shared LectureRoom: Towne 100Time: Tuesdays, 6:00 - 7:30pmGeneral, useful info: command line intro, Git, overview of theInternet

    Instructor: Swapneel Sheth

    Raymond Yin (University of Pennsylvania) CIS 192 August 31, 2016 5 / 32

  • Outline

    1 LogisticsRooms and Office HoursGradingClass Materials

    2 PythonWhat is Python?The Basics

    Raymond Yin (University of Pennsylvania) CIS 192 August 31, 2016 6 / 32

  • Grade Breakdown

    Homework: 70%1 per weekProgramming assignments

    Final Project: 25%Anything you wantIndividually or in a pair

    In Class: 5%Participation/Attendance/Piazza

    Late PolicyDrop one homework (lowest grade)

    Raymond Yin (University of Pennsylvania) CIS 192 August 31, 2016 7 / 32

  • Homeworks

    Python 3 (3.42 or latest stable)Submit on Canvas!Graded for correctness (80%) and style (20%)Due Sunday nights at 11:59pmHW1 due next Sunday, September 11Academic Integrity

    The Office of Student ConductDon’t copy-paste code from other peopleDon’t have mid-level discussions

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

    Raymond Yin (University of Pennsylvania) CIS 192 August 31, 2016 8 / 32

  • Outline

    1 LogisticsRooms and Office HoursGradingClass Materials

    2 PythonWhat is Python?The Basics

    Raymond Yin (University of Pennsylvania) CIS 192 August 31, 2016 9 / 32

  • Reading

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

    Raymond Yin (University of Pennsylvania) CIS 192 August 31, 2016 10 / 32

  • Programming Environment

    Unix system recommendedUbuntu Virtual Machine / Eniac

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

    Python is whitespace-sensitive

    Raymond Yin (University of Pennsylvania) CIS 192 August 31, 2016 11 / 32

  • Outline

    1 LogisticsRooms and Office HoursGradingClass Materials

    2 PythonWhat is Python?The Basics

    Raymond Yin (University of Pennsylvania) CIS 192 August 31, 2016 12 / 32

  • Easy to Learn

    Raymond Yin (University of Pennsylvania) CIS 192 August 31, 2016 13 / 32

  • Easy to Use

    Raymond Yin (University of Pennsylvania) CIS 192 August 31, 2016 14 / 32

  • Easy to Abuse

    Raymond Yin (University of Pennsylvania) CIS 192 August 31, 2016 15 / 32

  • History

    Designer: Guido van RossumBenevolent Dictator For Life (BDFL)

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

    Raymond Yin (University of Pennsylvania) CIS 192 August 31, 2016 16 / 32

  • Philosophy

    The Zen of PythonBeautiful is better than uglyExplicit is better than implicitSimple is better than complexComplex is better than complicatedReadability Counts

    Other ideasThere should be one obvious way to do itClarity over marginal efficiencyWe’re all consenting adults here

    Raymond Yin (University of Pennsylvania) CIS 192 August 31, 2016 17 / 32

  • Outline

    1 LogisticsRooms and Office HoursGradingClass Materials

    2 PythonWhat is Python?The Basics

    Raymond Yin (University of Pennsylvania) CIS 192 August 31, 2016 18 / 32

  • 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()

    Raymond Yin (University of Pennsylvania) CIS 192 August 31, 2016 19 / 32

  • Identifiers, Names, Variables

    All 3 mean the same thingVariable naming convention

    Functions and variables: lower_with_underscoremy_num = 5

    Constants: UPPER_WITH_UNDERSCORESECONDS_PER_MINUTE = 60

    Raymond Yin (University of Pennsylvania) CIS 192 August 31, 2016 20 / 32

  • Binding

    x = 1y = xx = ’a’

    X 1

    Raymond Yin (University of Pennsylvania) CIS 192 August 31, 2016 21 / 32

  • Binding

    x = 1y = xx = ’a’

    X

    Y

    1

    Raymond Yin (University of Pennsylvania) CIS 192 August 31, 2016 21 / 32

  • Binding

    x = 1y = xx = ’a’

    X

    Y

    1

    a

    Raymond Yin (University of Pennsylvania) CIS 192 August 31, 2016 21 / 32

  • Objects

    Python treats all data as objectsIdentity

    Memory addressDoes not change

    TypeDoes not change

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

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

    Raymond Yin (University of Pennsylvania) CIS 192 August 31, 2016 22 / 32

  • 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:int, floatstrtuple, list, dictrange, bool, Nonefunction

    Raymond Yin (University of Pennsylvania) CIS 192 August 31, 2016 23 / 32

  • Math

    LiteralsIntegers: 1, 2Floats: 1.0, 2e10Complex: 1j, 2e10jBinary: 0b1001, Hex: 0xFF, Octal: 0o72

    OperationsArithmetic: + - * /Power: **Integer division: //Modulus: %Bitwise: « » & | ˆComparison: , =, ==, !=

    Assignment Operators+= *= /= &= ...No ++ or --

    Raymond Yin (University of Pennsylvania) CIS 192 August 31, 2016 24 / 32

  • Booleans

    True and FalseBoolean operators: and or notAny object can be tested for truth value for use in conditionals, oras operands of the above Boolean operations."Falsy"

    None00.0Any empty string/sequence/collection ([],(), etc.)

    Raymond Yin (University of Pennsylvania) CIS 192 August 31, 2016 25 / 32

  • 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 with whitespaceAll strings are unicodePrefixing with r means raw. No need to escape characters: r’\n’

    Raymond Yin (University of Pennsylvania) CIS 192 August 31, 2016 26 / 32

  • Conditionals

    One if blockZero or more elif blocksZero or one else block

    Raymond Yin (University of Pennsylvania) CIS 192 August 31, 2016 27 / 32

  • Sequences

    ImmutableStrings, Tuples

    MutableLists

    Operationslen()IndexingSlicinginnot in

    Raymond Yin (University of Pennsylvania) CIS 192 August 31, 2016 28 / 32

  • 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 by stepNegative steps are validMemory efficent: Calculates values as you iterate over them

    Raymond Yin (University of Pennsylvania) CIS 192 August 31, 2016 29 / 32

  • Loops

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

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

    Bothelse: executes after loop finishesbreak: stops the loop and skips the else clausestarts the next iteration of the loop

    Raymond Yin (University of Pennsylvania) CIS 192 August 31, 2016 30 / 32

  • Functions

    Functions are first classThey’re objects, too!Can pass them as argumentsCan assign them to variables

    Define functions with a defreturn keyword to return a valueIf a function reaches the end of the block without returning,it will return None (null)

    Raymond Yin (University of Pennsylvania) CIS 192 August 31, 2016 31 / 32

  • Importing Modules

    Allow use of other python files and librariesimports: import mathNamed imports: import math as mSpecific imports: from math import powImport all: from math import *

    Raymond Yin (University of Pennsylvania) CIS 192 August 31, 2016 32 / 32

    LogisticsRooms and Office HoursGradingClass Materials

    PythonWhat is Python?The Basics