Python - Data type, String, Operators

Embed Size (px)

Citation preview

  • 8/14/2019 Python - Data type, String, Operators

    1/36

    1

    Strings in Python

  • 8/14/2019 Python - Data type, String, Operators

    2/36

    2

    Data types andoperators

  • 8/14/2019 Python - Data type, String, Operators

    3/36

    3

    Variables and Types

    Variables need no declaration >>> a=1

    >>> As a variable assignment is a statement, there is no

    printed result >>> a

    1 Variable name alone is an expression, so the result

    is printed

  • 8/14/2019 Python - Data type, String, Operators

    4/36

    4

    Variables and Types

    Variables must be created before they can be used >>> b

    Traceback (innermost last): File "", line 1,in ?

    NameError: b>>>

  • 8/14/2019 Python - Data type, String, Operators

    5/36

    5

    Assignment versus EqualityTesting

    Assignment performed with single = Equality testing done with double = (==

    ! "ensible type promotions are defined! #dentity tested with is operator$

    >>> 1==11>>> 1 !==11

    >>> "1"==1!

  • 8/14/2019 Python - Data type, String, Operators

    6/36

    6

    Simple Data Types

    "trings! %ay hold any data, including embedded &' s! )eclared using either single, double, or triple quotes >>> s = " i t#ere"

    >>> s$ i t#ere$>>> s = "Embe%%e% $&uote$">>> s"Embe%%e% $&uote$"

  • 8/14/2019 Python - Data type, String, Operators

    7/367

    Simple Data Types

    ! *riple quotes useful for multi+line strings >>> s = """ a lon'

    strin' it# "&uotes" or an t#in' else""">>> s$ a lon'*!1+strin' it# "&uotes" or an t#in'else$>>> len(s)

    -

  • 8/14/2019 Python - Data type, String, Operators

    8/368

    Simple Data Types

    #nteger ob ects implemented using - longs! i.e -, integer division returns the floor >>> -.+

    +

    /loat types implemented using - doubles! &o point in having single precision since execution

    overhead is large anyway

  • 8/14/2019 Python - Data type, String, Operators

    9/369

    Simple Data Types

    ong #ntegers have unlimited si0e! imited only by available memory >>> lon' = 1/ >> lon' -+12-345!2-3+!31!!4+23-!+15!0103--+11 0!+5! -++2-00-+5033 5! 10!54+++135+-54!0 !--!!++30+!40320-50/

  • 8/14/2019 Python - Data type, String, Operators

    10/36

    10

    Variables and Types

    1b ects always have a type>>> t pe(1)

    >>> t pe(" ello")

  • 8/14/2019 Python - Data type, String, Operators

    11/36

    11

    Strings vs. Integers

    2oth strings and integer variables can storenumbers

    my_speed_str="300" my_speed_integer=300

    34 5hat is the difference between strings andintegers6

  • 8/14/2019 Python - Data type, String, Operators

    12/36

    12

    Strings in use

    34 5here are strings displayed and entered6

  • 8/14/2019 Python - Data type, String, Operators

    13/36

    13

    String composition

    "trings are a composite data type! 2uilt from another data type, characters! #n a string, characters are stored in sequence$! "trings can be any length (including empty $! "tring are enclosed in quotation mar.s$

    str_var = "300 m/s" empty_str=""

  • 8/14/2019 Python - Data type, String, Operators

    14/36

    14

    Length o a string

    'se len(str) to return string length (7 ofcharacters sample="SERIES" len(sample)=6

    empty_str="" len(empty_str) = 0

  • 8/14/2019 Python - Data type, String, Operators

    15/36

    15

    String representation

    #n strings, characters are accessed by inde! ! 8li.e mailboxes in an apartment building$

    ! /irst index is 9, not :$ s="LE EL" start! ar=s# $ %&st_v=ss# $

    ! ;ython strings are immutable"can

  • 8/14/2019 Python - Data type, String, Operators

    16/36

    16

    #perations$%oncatenation

    -ombine strings using & (concatenation operator&ll_name = " enry" * " " * "+ames"print "," * &ll_name * ","

    -oncatenation is not addition visi-n_str=".0"*".0" visi-n_val=.0*.0

    Try it out$ 2uild a string &ild="" ile len( &ild)12, &ild = &ild *"a" print &ild

    = 0=4.0.05

  • 8/14/2019 Python - Data type, String, Operators

    17/36

    17

    #perations$ Traversing through astring

    'se a loop to examine each character in a string-r in

    strng=" -&nt 7 - &s"inde8=0

    -&nt=0ile inde8 1 len(strng),

    i strng#inde8$ == "&4 -r strng#inde8$ == 49",-&nt*=:

    inde8*=: *ry it out! ow would we traverse bac.wards6

  • 8/14/2019 Python - Data type, String, Operators

    18/36

    18

    String operations$ ind

    ind() searches for a string within a string *o use it, insert this at the top of your code4

    imp-rt string

    ind() returns the first index of a substring

    &ll_name = " enry +ames" string; ind( &ll_nameou can specify the starting point of the search4

    string; ind( &ll_name

  • 8/14/2019 Python - Data type, String, Operators

    19/36

    19

    'igh Level Data Types

    List ists hold a sequence of items

    ! %ay hold any ob ect! )eclared using square brac.ets

    >>> l = 789 n empt list>>> l appen%(1)>>> l appen%(" i t#ere")>>> len(l)

    +

  • 8/14/2019 Python - Data type, String, Operators

    20/36

    20

    'igh Level Data Types

    >>> l71, $ i t#ere$8>>> >>> l = 7" i t#ere", 1, +8>>> l7$ i t#ere$, 1, +8>>> l sort()>>> l71, +, $ i t#ere$8

  • 8/14/2019 Python - Data type, String, Operators

    21/36

    21

    'igh Level Data Types

    Tuple *uples are similar to lists

    ! "equence of items! ?ey difference is they are immutable

    ! 1ften used in place of simple structures Automatic unpac.ing >>> point = +,2

    >>> ;, = point

    >>> ;+

  • 8/14/2019 Python - Data type, String, Operators

    22/36

    22

    'igh Level Data Types

    *uples are particularly useful to return multiplevalues from a function >>> ;, = et oint() As ;ython has no concept of byref parameters, this

    technique is used widely

  • 8/14/2019 Python - Data type, String, Operators

    23/36

    23

    'igh Level Data Types

    Dictionary )ictionaries hold .ey+value pairs

    ! 1ften called maps or hashes$ #mplemented usinghash+tables

    ! ?eys may be any immutable ob ect, values may be anyob ect

    ! )eclared using braces >>> %=

    >>> %7!8 = " i t#ere">>> %7"6oo"8 = 1

  • 8/14/2019 Python - Data type, String, Operators

    24/36

    24

    'igh Level Data Types

    )ictionaries (cont$

    >>> % = ! : " i t#ere", 1 : " ello">>> len(%)+>>> %7!8$ i t#ere$

  • 8/14/2019 Python - Data type, String, Operators

    25/36

    25

    #perators

  • 8/14/2019 Python - Data type, String, Operators

    26/36

    26

    #perators

    ;ython has many operators$ "ome examples are4*< < >< /< ?< @< 1< ==print

    1perators perform an action on one or more

    operands$ "ome operators accept operandsbefore and after themselves4 -perand: * -perand. , or 3 * 2

    1thers are followed by one or more operandsuntil the end of the line, such as4 print 4 iA5 Radi&s > Radi&sprint area

    will print I:H$:@ to the screen$

  • 8/14/2019 Python - Data type, String, Operators

    34/36

    34

    eywords, Name-spaces &Scope

    &ames that are defined in a function are localD tothat function$

    &ames that are defined outside of a function areglobalD to the module$

    ocal names overshadow global names wheninside the function that defined them$

    #f you want to access a global variable from insideof a function, you should declare it globalD$

  • 8/14/2019 Python - Data type, String, Operators

    35/36

    35

    1lobal vs Local e!amplemy aria le = CmyFaram = .0

    de &n :(myFaram), my aria le = .0 print myFaram

    &n :(2)print my aria le

    5hat gets printed6

  • 8/14/2019 Python - Data type, String, Operators

    36/36

    1lobal vs Local e!ample my aria le = C myFaram = .0

    de &n :(myFaram), gl- al my aria le my aria le = .0

    print myFaram

    &n :(2) print my aria le

    5hat gets printed6 @ and 9 *he localD myVariable inside func: is separate from theglobalD myVariable outside of func:

    *he function assigns 9 to the globalD myVariable,overwriting the G before it gets printed$