Upgrade to Pro — share decks privately, control downloads, hide ads and more …

2nd Python Bootcamp IAG - Basics II

2nd Python Bootcamp IAG - Basics II

Bruno Quint

April 13, 2017
Tweet

More Decks by Bruno Quint

Other Decks in Programming

Transcript

  1. Table of Contents § What is Python? § What will

    you need? § Python as a terminal § Python as a script § Types of variables § Loops and Control § Using Methods and Libs § Gathering Information
  2. 3 if/elif/else fruit = ‘banana’ if fruit is ‘apple’: ....eat_it()

    elif fruit is ‘orange’: ....make_a_juice() else: ....leave_it() Loops and control 13/02/2017 Python Bootcamp – An(other) Python introduction II More at https://docs.python.org/2/tutorial/controlflow.html
  3. 4 if/elif/else fruit = ‘banana’ if fruit is ‘apple’: ....eat_it()

    elif fruit is ‘orange’: ....make_a_juice() else: ....leave_it() Loops and control 13/02/2017 Python Bootcamp – An(other) Python introduction II More at https://docs.python.org/2/tutorial/controlflow.html
  4. 5 Python relies on identation, so DON’T MESS UP! if/elif/else

    Loops and control 13/02/2017 Python Bootcamp – An(other) Python introduction II More at https://docs.python.org/2/tutorial/controlflow.html fruit = ‘banana’ if fruit is ‘apple’: ....eat_it() elif fruit is ‘orange’: .....make_a_juice() else: ....leave_it()
  5. 6 if/elif/else Loops and control 13/02/2017 Python Bootcamp – An(other)

    Python introduction II More at https://docs.python.org/2/tutorial/controlflow.html PEP8 HIGHLY recommends you to use 4 spaces. And NEVER mix spaces and tabs. For more informations, read the PEP-8. fruit = ‘banana’ if fruit is ‘apple’: ....eat_it() elif fruit is ‘orange’: ....make_a_juice() else: ....leave_it()
  6. 7 if/elif/else fruit = ‘banana’ if fruit is ‘apple’: ....eat_it()

    elif fruit is ‘orange’: ....make_a_juice() else: ....leave_it() Loops and control 13/02/2017 Python Bootcamp – An(other) Python introduction II More at https://docs.python.org/2/tutorial/controlflow.html
  7. 8 if/elif/else Loops and control 13/02/2017 Python Bootcamp – An(other)

    Python introduction II More at https://docs.python.org/2/tutorial/controlflow.html fruit = ‘banana’ if fruit == ‘apple’: ....eat_it() elif fruit == ‘orange’: ....make_a_juice() else: ....leave_it()
  8. >>> my_list = [‘a’, ‘b’, ‘c’] >>> >>> for my_item

    in my_list: ....print my_item .... a b c 9 for Loops and control 13/02/2017 Python Bootcamp – An(other) Python introduction II More at https://docs.python.org/2/tutorial/controlflow.html List, tuples, arrays, matrixes, dictionaries
  9. >>> my_list = [‘a’, ‘b’, ‘c’] >>> >>> for my_item

    in my_list: ....print my_item .... a b c 10 for Loops and control 13/02/2017 Python Bootcamp – An(other) Python introduction II More at https://docs.python.org/2/tutorial/controlflow.html List, tuples, arrays, matrixes, dictionaries
  10. >>> my_list = [‘a’, ‘b’, ‘c’] >>> >>> for index

    in range(len(my_list)): ....print index, my_list[index] .... 0 a 1 b 2 c 11 for Loops and control 13/02/2017 Python Bootcamp – An(other) Python introduction II More at https://docs.python.org/2/tutorial/controlflow.html List, tuples, arrays, matrixes, dictionaries Using indexes
  11. >>> my_list = [‘a’, ‘b’, ‘c’] >>> >>> for index

    in range(len(my_list)): ....print index, my_list[index] .... 0 a 1 b 2 c 12 for Loops and control 13/02/2017 Python Bootcamp – An(other) Python introduction II More at https://docs.python.org/2/tutorial/controlflow.html List, tuples, arrays, matrixes, dictionaries Using indexes len(x) → return the number of elements of x.
  12. >>> my_list = [‘a’, ‘b’, ‘c’] >>> >>> for index

    in range(3): ....print index, my_list[index] .... 0 a 1 b 2 c 13 for Loops and control 13/02/2017 Python Bootcamp – An(other) Python introduction II More at https://docs.python.org/2/tutorial/controlflow.html List, tuples, arrays, matrixes, dictionaries Using indexes len(x) → return the number of elements of x.
  13. >>> my_list = [‘a’, ‘b’, ‘c’] >>> >>> for index

    in range(3): ....print index, my_list[index] .... 0 a 1 b 2 c 14 for Loops and control 13/02/2017 Python Bootcamp – An(other) Python introduction II More at https://docs.python.org/2/tutorial/controlflow.html List, tuples, arrays, matrixes, dictionaries Using indexes range(n) → return a list with n integers starting at 0.
  14. >>> my_list = [‘a’, ‘b’, ‘c’] >>> >>> for index

    in [0, 1, 2]: ....print index, my_list[index] .... 0 a 1 b 2 c 15 for Loops and control 13/02/2017 Python Bootcamp – An(other) Python introduction II More at https://docs.python.org/2/tutorial/controlflow.html List, tuples, arrays, matrixes, dictionaries Using indexes range(n) → return a list with n integers starting at 0.
  15. >>> while some_condition_is_true: ....do_something() 16 while Loops and control 13/02/2017

    Python Bootcamp – An(other) Python introduction II More at https://docs.python.org/2/tutorial/controlflow.html
  16. test_while.py >>> n_interested = 5 >>> >>> while n_interested <

    0: ....print(“ #Success :D ”) ....n_interested = n_interested - 1 .... >>> print(“ #Fail :( ”) 17 while Loops and control 13/02/2017 Python Bootcamp – An(other) Python introduction II More at https://docs.python.org/2/tutorial/controlflow.html
  17. test_while.py 18 while Loops and control 13/02/2017 Python Bootcamp –

    An(other) Python introduction II More at https://docs.python.org/2/tutorial/controlflow.html >>> n_interested = 5 >>> >>> while n_interested < 0: ....print(“ #Success :D ”) ....n_interested = n_interested - 1 .... >>> print(“ #Fail :( ”) $ python foo.py #Success :D #Success :D #Success :D #Success :D #Success :D #Fail :(
  18. Methods Defining your own 19 >>> def my_method(x, y): ....“““Add

    here some description””” ....k = 2 * x - y ....return k More on methods (Functions): Here 13/02/2017 Python Bootcamp – An(other) Python introduction II Define a method
  19. Methods Defining your own 20 >>> def my_method(x, y): ....“““Add

    here some description””” ....k = 2 * x - y ....return k .... >>> my_method(2, 4) 0 More on methods (Functions): Here 13/02/2017 Python Bootcamp – An(other) Python introduction II Define a method x and y are required parameters.
  20. Methods Defining your own 21 Define a method x and

    y are now keyword parameters. >>> def my_method(x, y): ....“““Add here some description””” ....k = 2 * x - y ....return k .... >>> my_method(y=4, x=2) 0 More on methods (Functions): Here 13/02/2017 Python Bootcamp – An(other) Python introduction II
  21. Methods Defining your own 22 Define a method z and

    w are now default parameters. >>> def my_method(x, y, z=1, w=0): ....“““Add here some description””” ....k = 2 * x - y / z + w ....return k .... >>> my_method(y=4, x=2) 0 More on methods (Functions): Here 13/02/2017 Python Bootcamp – An(other) Python introduction II
  22. >>> def my_method(x, y, z=1, w=0): ....“““Add here some description”””

    ....k = 2 * x - y / z + w ....return k .... >>> my_method(y=4, x=2) 0 Methods Defining your own 23 Define a method z and w are now default parameters. More on methods (Functions): Here 13/02/2017 Python Bootcamp – An(other) Python introduction II Default parameters have default values.
  23. Methods Defining your own 24 Define a method z and

    w are now default parameters. >>> def my_method(x, y, z=1, w=0): ....“““Add here some description””” ....k = 2 * x - y / z + w ....return k .... >>> my_method(y=4, x=2, w=5) 5 More on methods (Functions): Here 13/02/2017 Python Bootcamp – An(other) Python introduction II
  24. Methods Defining your own 25 Watch out namespaces! >>> >>>

    def my_method(x, y): ....“““Add here some description””” ....k = 2 * x - y ....return k .... >>> my_method(2, 4) 0 More on methods (Functions): Here 13/02/2017 Python Bootcamp – An(other) Python introduction II
  25. Methods Defining your own 26 Watch out namespaces! >>> >>>

    def my_method(x, y): ....“““Add here some description””” ....k = 2 * x - y ....return k .... >>> my_method(4) More on methods (Functions): Here 13/02/2017 Python Bootcamp – An(other) Python introduction II
  26. Methods Defining your own 27 Watch out namespaces! >>> >>>

    def my_method(x, y): ....“““Add here some description””” ....k = 2 * x - y ....return k .... >>> my_method(4) NameError: global name 'x' is not defined More on methods (Functions): Here 13/02/2017 Python Bootcamp – An(other) Python introduction II
  27. Methods Defining your own 28 Watch out namespaces! >>> x

    = 2 >>> def my_method(x, y): ....“““Add here some description””” ....k = 2 * x - y ....return k .... >>> my_method(4) 0 More on methods (Functions): Here 13/02/2017 Python Bootcamp – An(other) Python introduction II
  28. Methods Defining your own 29 Watch out namespaces! >>> >>>

    def my_method(x, y): ....“““Add here some description””” ....k = 2 * x - y ....return k .... >>> x = 2 >>> my_method(4) 0 More on methods (Functions): Here 13/02/2017 Python Bootcamp – An(other) Python introduction II
  29. Methods Defining your own 30 Watch out namespaces! >>> x

    = 2 >>> def my_method(x, y): ....“““Add here some description””” ....k = 2 * x - y ....return k .... >>> my_method(3, 4) More on methods (Functions): Here 13/02/2017 Python Bootcamp – An(other) Python introduction II
  30. Methods Defining your own 31 Watch out namespaces! >>> x

    = 2 >>> def my_method(x, y): ....“““Add here some description””” ....k = 2 * x - y ....return k .... >>> my_method(3, 4) 2 More on methods (Functions): Here 13/02/2017 Python Bootcamp – An(other) Python introduction II
  31. 32 >>> X = 2 >>> my_method = lambda x,

    y: 2 * x – y >>> >>> my_method(3, 4) 2 Use lambda! More on methods (Functions): Here Methods Defining your own 13/02/2017 Python Bootcamp – An(other) Python introduction II
  32. 13/10/2015 Python Bootcamp - Basic I 33 foo.py 01 def

    my_method(x, y): 02 ....“““Some description””” 03 ....k = 2 * x - y 04 ....return k 05 06 print my_method(3, 4) More on methods (Functions): Here Methods Defining your own Re-using your functions
  33. 13/10/2015 Python Bootcamp - Basic I 34 foo.py 01 import

    foo 02 foo.my_method(5, 2) use.py 01 def my_method(x, y): 02 ....“““Some description””” 03 ....k = 2 * x - y 04 ....return k 05 06 print my_method(3, 4) More on methods (Functions): Here Methods Defining your own Re-using your functions
  34. 13/10/2015 Python Bootcamp - Basic I 35 foo.py 01 def

    my_method(x, y): 02 ....“““Some description””” 03 ....k = 2 * x - y 04 ....return k 05 06 print my_method(3, 4) More on methods (Functions): Here Methods Defining your own Re-using your functions 01 import foo 02 foo.my_method(5, 2) use.py $ python use.py 2 8
  35. 13/10/2015 Python Bootcamp - Basic I 36 foo.py 01 def

    my_method(x, y): 02 ....“““Some description””” 03 ....k = 2 * x - y 04 ....return k 05 06 07 print my_method(3, 4) More on methods (Functions): Here Methods Defining your own Re-using your functions 01 import foo 02 foo.my_method(5, 2) use.py $ python use.py 8
  36. 13/10/2015 Python Bootcamp - Basic I 37 foo.py 01 def

    my_method(x, y): 02 ....“““Some description””” 03 ....k = 2 * x - y 04 ....return k 05 06 if __name__ == ‘__main__’: 07 print my_method(3, 4) More on methods (Functions): Here Methods Defining your own Re-using your functions 01 import foo 02 foo.my_method(5, 2) use.py $ python use.py 8
  37. 13/10/2015 Python Bootcamp - Basic I 38 foo.py 01 def

    my_method(x, y): 02 ....“““Some description””” 03 ....k = 2 * x - y 04 ....return k 05 06 if __name__ == ‘__main__’: 07 ....print my_method(3, 4) More on methods (Functions): Here Methods Defining your own Re-using your functions 01 import foo 02 foo.my_method(5, 2) use.py $ python use.py 8
  38. 39 Methods Using yours >>> len(‘Hello World!’) 12 >>> range(5)

    [0, 1, 2, 3, 4] >>> type(‘Aloha!’) <type ‘string’> >>> abs(-5.3) 5.3 Built-in methods (or Functions) Check all the build-in functions at https://docs.python.org/2/library/functions.html 13/02/2017 Python Bootcamp – An(other) Python introduction II
  39. 40 >>> import math >>> math.sqrt(9) 3 Using methods from

    libs For more, check: https://docs.python.org/2/tutorial/modules.html Methods Using yours 13/02/2017 Python Bootcamp – An(other) Python introduction II
  40. 41 >>> import math >>> math.sqrt(9) 3 >>> import math

    as m >>> m.sqrt(9) 3 Using methods from libs For more, check: https://docs.python.org/2/tutorial/modules.html Methods Using yours 13/02/2017 Python Bootcamp – An(other) Python introduction II
  41. 42 >>> from math import sqrt, log >>> sqrt(9) 3

    >>> log(10) 1 Using methods from libs For more, check: https://docs.python.org/2/tutorial/modules.html Methods Using yours 13/02/2017 Python Bootcamp – An(other) Python introduction II
  42. 43 >>> from math import * >>> sqrt(9) 3 >>>

    log(10) 1 Using methods from libs For more, check: https://docs.python.org/2/tutorial/modules.html Methods Using yours 13/02/2017 Python Bootcamp – An(other) Python introduction II
  43. 44 >>> from math import * >>> from numpy import

    * >>> sqrt(9) 3 >>> x = array([1, 4, 9]) >>> sqrt(x) [1, 2, 3] Using methods from libs For more, check: https://docs.python.org/2/tutorial/modules.html Methods Using yours 13/02/2017 Python Bootcamp – An(other) Python introduction II
  44. 45 >>> from numpy import * >>> from math import

    * >>> sqrt(9) 3 >>> x = array([1, 4, 9]) >>> sqrt(x) TypeError: only length-1 arrays can be converted to Python scalars Using methods from libs For more, check: https://docs.python.org/2/tutorial/modules.html Methods Using yours 13/02/2017 Python Bootcamp – An(other) Python introduction II
  45. 46 >>> from math import sqrt as msqrt >>> from

    numpy import sqrt as nsqrt >>> msqrt(9) 3. >>> nsqrt([1, 4, 9]) [1., 2., 3.] Using methods from libs For more, check: https://docs.python.org/2/tutorial/modules.html Methods Using yours 13/02/2017 Python Bootcamp – An(other) Python introduction II
  46. 47 >>> s = ‘Hello World!’ >>> s.lower() ‘hello world!’

    >>> s.isdigit() False Using methods from objects For more, check: https://docs.python.org/2/tutorial/modules.html Methods Using yours 13/02/2017 Python Bootcamp – An(other) Python introduction II
  47. 48 Gathering information >>> def double(x): >>> ....“““Doubles the value

    of x””” >>> ....return 2 * x >>> The built-in function help() Help on built-in function help() here. 13/02/2017 Python Bootcamp – An(other) Python introduction II
  48. 49 Gathering information The built-in function help() >>> help(double_value) Help

    on function double_value: double_value(x) Return the double of x Help on built-in function help() here. 13/02/2017 Python Bootcamp – An(other) Python introduction II
  49. 50 Gathering information >>> y = 3. >>> help(y) Help

    on float object: class float(object) | float(x) -> floating point number | | Convert a string or number to a floating point | number, if possible. ... The built-in function help() Help on built-in function help() here. 13/02/2017 Python Bootcamp – An(other) Python introduction II
  50. 51 Gathering information >>> y = 3. >>> help(y) The

    built-in function help() >>> z = 3 >>> help(z) >>> s = ‘a string’ >>> help(s) Help on built-in function help() here. 13/02/2017 Python Bootcamp – An(other) Python introduction II
  51. 52 >>> y = 3. >>> dir(y) ['__abs__', '__add__', ...,

    'is_integer', 'real'] The built-in function dir() Help on built-in function dir() here. Gathering information 13/02/2017 Python Bootcamp – An(other) Python introduction II
  52. 53 >>> y = 3. >>> dir(y) ['__abs__', '__add__', ...,

    'is_integer', 'real'] The built-in function dir() >>> y = 3 >>> dir(y) ['__abs__', '__add__', ..., ‘real', ‘to_bytes'] Help on built-in function dir() here. Gathering information 13/02/2017 Python Bootcamp – An(other) Python introduction II
  53. 54 >>> y = 3. >>> dir(y) ['__abs__', '__add__', ...,

    'is_integer', 'real'] The built-in function dir() ._variable is semiprivate and meant just for convention .__variable is considered superprivate and gets namemangled to prevent accidental access .__variable__ is typically reserved for builtin methods or variables Help on built-in function dir() here. Gathering information 13/02/2017 Python Bootcamp – An(other) Python introduction II