Python Quick review - PowerPoint PPT Presentation

1 / 13
About This Presentation
Title:

Python Quick review

Description:

Python Quick review CS360 Dick Steflik Starting with Hello World start the python shell type: print – PowerPoint PPT presentation

Number of Views:78
Avg rating:3.0/5.0
Slides: 14
Provided by: DICK134
Category:

less

Transcript and Presenter's Notes

Title: Python Quick review


1
Python Quick review
  • CS360
  • Dick Steflik

2
Starting with Hello World
  • start the python shell type print
    "Hello World!"
    orput into file hello.py with text editor
    restart the python shell and type
    import hello
    orat OS command prompt type python
    hello.py

3
Variables, Expressions and Statements
  • type(ltexpressiongt) returns the type of the
    expression print ltexprgt, ..., ltexprgt print
    several expressions ltvariablegt ltexpressiongt
    assign value to variable operators - /
    normal arithmetic operators operators
    is exponentiation, is mod () gt gt / gt
    - precedence (otherwise mostly

    left-to-right) ltvargt raw_input(ltpromptgt) read
    a text line of input ltvargt input(ltpromptgt)
    read a numerical line of input text
    comment

4
Functions
  • int(ltfloat or stringgt) convert to int
    str(ltnonstring expressiongt) convert to string
    import math make math functions
    availmath.sin, math.cos, math.sqrt examples of
    math functions help('math') list of all math
    functions
    (q to quit help) defining
    functions def ltnamegt(ltparamgt, ...,
    ltparamgt) ltstatementsgtreturn statement
    (immediately exits function, returning
    a value) return ltexpressiongt

5
Conditionals
  • type boolean (logical tests) either True or
    False x ! y x is not equal to y x gt y
    x is greater than y x lt y x is less
    than y x gt y x is greater than or equal to
    y x lt y x is less than or equal to y x
    and y x and y are both true x or y x or
    y (or both) are true not x x is not true

6
if/else statements
  • if lttestgt ltstatementsgt if lttestgt
    ltstatementsgt else ltstatementsgt if
    lttest1gt ltstatementsgt elif lttest2gt
    ltstatementsgt else ltstatementsgt

7
Strings
  • strings are immutable to change one construct a
    new onesindex return character at index (0
    based) len(s) length of string s-1, s-2,
    s-3 last character, 2nd to last, 3rd to last
    sij slice of string i through (j - 1)
    sj slice from beginning through (j - 1)
    si slice from i to end of string ltstringgt
    ltstringgt concatenate two strings help('str')
    show string functions (q to quit help)
    s.lower() returns lowercase version of s
    str.isalpha is alphabetic character (boolean
    function) foreach loop over string for
    ltvariablegt in ltstringgt ltstatementsgt

8
Lists
  • ltexprgt, ..., ltexprgt a list same use of , len
    lists are very similar to stringsrange(n)
    list of integers 0 through (n - 1)range(i, j)
    list of integers i through (j - 1)
    empty list ltexpressiongt in ltlistgt test for
    list membership (boolean) ltlistgt ltlistgt
    append two lists together to form new list
    list1 list(list2) make a copy of list2,
    store in list1 ltvargt list(ltstringgt) convert
    string to list ltlist vargt.append(ltexprgt) append
    value to end of list ltlist vargt.remove(ltexprgt)
    remove given value from list del lsti
    remove value at index i foreach loop over
    list for ltvariablegt in ltlistgt ltstatementsgt

9
Files
  • ltvargt open(ltnamegt, 'r') open a file for input
    ltvargt open(ltnamegt) same as above, r is
    default ltfile variablegt.readLine() return next
    line of file as a string ltfile
    variablegt.readLines() return all lines of file
    as list of strings ltvargt open(ltnamegt,
    w') open a file for output ltfile
    variablegt.write(ltstringgt) write ltstringgt to the
    file ltfile variablegt.close() close the file

10
Statements
  • assignment a,b,c 1,2,3function
    calls log.write(some text\n)print print(some
    fun\n)if/elseif/else conditional
    executionfor/else fixed iterationwhile/else
    indeterminate iterationpass empty statement
    (no-op)break, continue force exit a
    looptry/except/finally catching
    exceptionsraise trigger an exceptionimport,from
    make members of a module accessibledef,return,yi
    eld defining functions

11
Statements (cont.)
  • class Defining objectsglobal namespacesdel dele
    ting referencesexec running code strings, like
    interpreting a string containing python
    codeassert debugging your codewith/as context
    managers

12
(No Transcript)
13
(No Transcript)
Write a Comment
User Comments (0)
About PowerShow.com