Lecture#2: Introductory Python

Embed Size (px)

Citation preview

  • 7/29/2019 Lecture#2: Introductory Python

    1/21

    High performance computing

    for non-programmers

    Lecture #2:Python programminglanguage: Overview

    Glib Ivashkevych

    junior researcher, NSC KIPT

  • 7/29/2019 Lecture#2: Introductory Python

    2/21

    cross-platform

    simple to learn

    general-purpose

    flexible & extendable

    Huge standard library

    Tons of third-party packages

    Documentation

  • 7/29/2019 Lecture#2: Introductory Python

    3/21

    web-development

    scripting in VFX

    science & engineering

    system administration

    Youtube, Google, Instagram, DropBox

    NASA, CERN

    Philips, ILM and more

    Is used for:

  • 7/29/2019 Lecture#2: Introductory Python

    4/21

    data processing

    glue layer

    GUI

    code generation & prototyping

    Python has a lot of tools for scientific

    computing: NumPy, SciPy, SymPy, scikit-learn

    In computing:

  • 7/29/2019 Lecture#2: Introductory Python

    5/21

    Scripting Compiled

    language instructions areexecuted one by one

    flexible, but slow

    allow for very fastdevelopment cycle

    perfect forcomplex automation

    language instructions arecompiled to assembly

    rigid, but fast

    development cycle is usuallylonger

    perfect forperformance

  • 7/29/2019 Lecture#2: Introductory Python

    6/21

    How Python code is

    executed

    file.py file.pyc

    sourcecode

    bytecode

    file.pyc

    PythonVirtual

    Machine

    compilation

    execution

  • 7/29/2019 Lecture#2: Introductory Python

    7/21

    Python type system

    Dynamic,

    variables are references

    no definitions, created automatically

    but strong typing

    objects have type

    + garbage collector

  • 7/29/2019 Lecture#2: Introductory Python

    8/21

    x == y x is y

    some string

    x

    y

    True True

  • 7/29/2019 Lecture#2: Introductory Python

    9/21

    x == y x is y

    some string

    x

    y

    True False

    some string

  • 7/29/2019 Lecture#2: Introductory Python

    10/21

    Mutable vs Immutable

    Mutable objects can be changed

    lists, dictionaries, sets, byte arrays

    can be changed in-place

    Immutable objects can not be changed once

    creatednumbers, strings, tuples

    new object is created at each

    reassignment

  • 7/29/2019 Lecture#2: Introductory Python

    11/21

    Naming

    First symbol_ (underscore) or letter

    Symbols_ (underscore), letters, digits

    _myfunc

    __somemagic

    generate_file

    generate-file

    6func

    fancy#name

  • 7/29/2019 Lecture#2: Introductory Python

    12/21

    Numbers

    Integer32 bit -2147483648...2147483647

    long - arbitrary

    boolean - True, False

    Floatdouble precision, no support for single precision

    Complextwo double precision numbers

  • 7/29/2019 Lecture#2: Introductory Python

    13/21

    Sequences

    List (mutable)

    [1,2,Hello World, True]

    Tuple (immutable)

    (1,2,Hello World, True)

    String (immutable)

    HPC, new string, Some multiline string

  • 7/29/2019 Lecture#2: Introductory Python

    14/21

    Mappings

    Dictionary (mutable)

    key-value storage

    my_dict = {a:2, string:42, (1,2,3): [1,2,3]}

    my_dict[a] == 2 #True

    my_dict[(1,2,3)] == [1,2,3] #True

    Only immutable types can be a key

  • 7/29/2019 Lecture#2: Introductory Python

    15/21

    Logic

    and, or, not

    non-zero numbers, non-empty objects: True

    zero-numbers, empty-objects, None: Falsecomparison and equality: True or False

    and, or: return operand

    if elif ... else statement

    a ifb else c statement

  • 7/29/2019 Lecture#2: Introductory Python

    16/21

    Blocks

    Blocks are intended (with spaces, not tabs!)

    ifx > y:

    print x > y x = y

    elifx == y: print X = Y

    else: print X < Y

    Empty block: pass

  • 7/29/2019 Lecture#2: Introductory Python

    17/21

    Linebreaks

    Break the line:

    x = [1, 2, \3,4]

    a = Somemultiline

    string

    x = [1, 2,3,4]

    x = (1, 2,

    3,4)

    x = {a:1,

    b:4)

    x = {a:1,

    b:4)

  • 7/29/2019 Lecture#2: Introductory Python

    18/21

    Iterations

    for i in some_iterable:

    do_something(i)

    some_iterable:

    list, tuple, string, set, other iterable objects

    while (x > 5):

    do_something(x)

    x += 1

  • 7/29/2019 Lecture#2: Introductory Python

    19/21

    Functions

    defmy_func(a, b=None):

    print a and b are positional arguments

    print b has default value

    my_func(1) #the same as my_func(1, None)

    my_func(1, 2)

    Can have variable number of arguments

  • 7/29/2019 Lecture#2: Introductory Python

    20/21

    Files

    f = open(filename, mode), f.close()mode:

    r - read (default),

    w - write (existing file is erased),

    a - append,

    r+ - read and write

    f.read() or f.read(size) #read entire file (be careful) or size bytes

    f.readline() #read line

    Iterate over file:

    for line in f:

    print line

  • 7/29/2019 Lecture#2: Introductory Python

    21/21

    Questions?