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

2nd Python Bootcamp IAG - Basics I

2nd Python Bootcamp IAG - Basics I

Bruno Quint

February 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 § Python Vs iPython 13/02/2017 Python Bootcamp - Basic I 2
  2. What is Python? High level interpreted programming language developed for

    fast development print(“Hello world!!”) 13/02/2017 3 Python Bootcamp - Basic I 1011000101010010010
  3. What is Python? High level interpreted programming language developed for

    fast development print(“Hello world!!”) 13/02/2017 4 Python Bootcamp - Basic I 1011000101010010010
  4. What is Python? High level interpreted programming language developed for

    fast development print(“Hello world!!”) 13/02/2017 5 Python Bootcamp - Basic I 1011000101010010010
  5. What is Python? High level interpreted programming language developed for

    fast development 13/02/2017 6 Python Bootcamp - Basic I if x is not 5: print “Blá”
  6. What is Python? High level interpreted programming language developed for

    fast development 13/02/2017 7 Python Bootcamp - Basic I if x is not 5: print “Blá”
  7. What will you need? • Computer • Operational System •

    Python • Python Libs • Text Editors • Integrated Development Environment (IDE) 13/02/2017 Python Bootcamp - Basic I 8
  8. What will you need? • Computer • Operational System •

    Python • Python Libs • Text Editors • Integrated Development Environment (IDE) 13/02/2017 Python Bootcamp - Basic I 9 Windows MacOs Linux TM Distributions Linux Mint TM Ubuntu TM Fedora TM
  9. What will you need? • Computer • Operational System •

    Python • Python Libs • Text Editors • Integrated Development Environment (IDE) 13/02/2017 Python Bootcamp - Basic I 10 https://www.python.org/ 2.x 3.x https://wiki.python.org/moin/Python2orPython3
  10. What will you need? • Computer • Operational System •

    Python • Python Libs • Text Editors • Integrated Development Environment (IDE) 13/02/2017 Python Bootcamp - Basic I 11 https://www.python.org/ 2.x 3.x https://wiki.python.org/moin/Python2orPython3
  11. What will you need? • Computer • Operational System •

    Python • Python Libs • Text Editors • Integrated Development Environment (IDE) 13/02/2017 Python Bootcamp - Basic I 12 SciPy Scientifical Python NumPy Numerical Python MatPlotLib Python Plotting AstroPy Astronomical Python PyFITS
  12. What will you need? • Computer • Operational System •

    Python • Python Libs • Text Editors • Integrated Development Environment (IDE) 13/02/2017 Python Bootcamp - Basic I 13 GEdit Notepad EMACS Vim
  13. What will you need? • Computer • Operational System •

    Python • Python Libs • Text Editors • Integrated Development Environment (IDE) 13/02/2017 Python Bootcamp - Basic I 14
  14. Python as a terminal Getting Started 13/02/2017 Python Bootcamp -

    Basic I 16 $ python Start typing >>> print “Hello World” Hello World Say “Hello World!” Python 2.x
  15. Python as a terminal Getting Started 13/02/2017 Python Bootcamp -

    Basic I 17 $ python Start typing >>> print(“Hello World”) Hello World Say “Hello World!” Python 2.x and 3.x!
  16. Python as a terminal Getting Started 13/02/2017 Python Bootcamp -

    Basic I 18 $ python Start typing >>> print(“Hello World”) Hello World Say “Hello World!” >>> x = 2 >>> print(x) 2 Assign a variable Python 2.x and 3.x!
  17. Python as a terminal Getting Started 13/02/2017 Python Bootcamp -

    Basic I 19 $ python Start typing >>> print(“Hello World”) Hello World Say “Hello World!” >>> mag_V = 28.1970 >>> print(mag_V) 28.1970 Assign a variable Python 2.x and 3.x!
  18. Python as a script 13/02/2017 Python Bootcamp - Basic I

    20 File “say_hello_world.py” path_to_file $ python say_hello_world.py Hello World To run this file: 01 print(“Hello World”)
  19. Python as a script 13/02/2017 Python Bootcamp - Basic I

    21 File “say_hello_world.py” path_to_file $ chmod a+x say_hello_world.py Path_to_file $ ./say_hello_world.py Hello Python To run this file: 01 #!/path/to/python 02 print(“Hello World”)
  20. Python as a script 13/02/2017 Python Bootcamp - Basic I

    22 01 #!/path/to/python 02 # -*- coding: utf8 -*- 03 print(“aá eé cç”) File “use_coding.py” path_to_file $ python use_coding.py Aá eé cç To run this file:
  21. Python as a script 13/02/2017 Python Bootcamp - Basic I

    23 01 #!/path/to/python 02 # -*- coding: utf8 -*- 03 “““ 04 This is a docstring where information 05 about what you are doing and be written. 06 ””” 07 print(“Hello World!”) File “say_hello_world.py” path_to_file $ python say_hello_world.py Hello World! To run this file:
  22. Types of variables 13/02/2017 Python Bootcamp - Basic I 24

    Integers >>> 5 + 4 9 >>> 5 – 4 1 >>> 5 * 4 20 >>> 5 / 4 1 More on operators (like +, -, /, *, **, >, <) HERE
  23. Types of variables 13/02/2017 Python Bootcamp - Basic I 25

    Integers >>> 5 + 4 9 >>> 5 – 4 1 >>> 5 * 4 20 >>> 5 / 4 1 More on operators (like +, -, /, *, **, >, <) HERE >>> 2.0 + 3.0 5.0 >>> 2.0 - 3 -1.0 >>> 2. * 3 6.0 >>> 2 / 3. 0.666666... Float/Double
  24. Types of variables 13/02/2017 Python Bootcamp - Basic I 26

    Integers >>> 5 + 4 9 >>> 5 – 4 1 >>> 5 * 4 20 >>> 5 / 4 1 More on operators (like +, -, /, *, **, >, <) HERE >>> 2.0 + 3.0 5.0 >>> 2.0 - 3 -1.0 >>> 2. * 3 6.0 >>> 2 / 3. 0.666666... Float/Double
  25. Types of variables 13/02/2017 Python Bootcamp - Basic I 27

    Integers >>> x = 2 >>> print type(x) <type 'int'> >>> >>> x = x + 2 >>> print x.__class__ <type 'int'> >>> x = 2.0 >>> print type(x) <type 'float'> >>> >>> y = 1 >>> y = x + y >>> print y.__class__ <type 'float'> Float/Double Any assigned variable in Python is an object and objects have attributes. Use .__class__ atribute to find what type is your object.
  26. Types of variables 13/02/2017 Python Bootcamp - Basic I 28

    More on operators (like +, -, /, *, **, >, <) HERE >>> (2 + 3j) + (5 - 4j) (7 - 1j) >>> (2 + 3j) – (5 - 4j) (-3 + 7j) >>> (2 + 3j) * (5 - 4j) (22 + 7j) >>> (2 + 3j) / (5 - 4j) (-0.049 + 0.561j) Complex >>> c = (1 + 3.j) >>> print c.__class__ <type ‘complex’>
  27. >>> fruit = ‘banana’ >>> fruit == ‘maçã’ False >>>

    fruit != ‘maçã’ True Types of variables 13/02/2017 Python Bootcamp - Basic I 29 More on boolean operators (like +, *, ==, !=, <, >, <=, >=, is, not) HERE Booleans
  28. >>> fruit = ‘banana’ >>> fruit == ‘maçã’ False >>>

    fruit != ‘maçã’ True Types of variables 13/02/2017 Python Bootcamp - Basic I 30 >>> mag_V = 15.5 >>> mag_V < 10.0 False >>> mag_V >= 10.0 True More on boolean operators (like +, *, ==, !=, <, >, <=, >=, is, not) HERE Booleans
  29. >>> fruit = ‘banana’ >>> fruit == ‘maçã’ False >>>

    fruit != ‘maçã’ True Types of variables 13/02/2017 Python Bootcamp - Basic I 31 >>> mag_V = 15.5 >>> mag_V < 10.0 False >>> mag_V >= 10.0 True >>> mag_V = 15.5 >>> test = mag_V < 10.0 >>> print(test) False More on boolean operators (like +, *, ==, !=, <, >, <=, >=, is, not) HERE Booleans
  30. >>> True + False True >>> True or False True

    13/02/2017 Python Bootcamp - Basic I 32 More on boolean operators (like +, *, ==, !=, <, >, <=, >=, is, not) HERE Types of variables >>> True * False False >>> True and False False Booleans >>> not False True >>> not True False
  31. 13/02/2017 Python Bootcamp - Basic I 33 More on boolean

    operators (like +, *, ==, !=, <, >, <=, >=, is, not) HERE Types of variables >>> x = 1 >>> y = 1 >>> x == y True >>> x is y False >>> x = 1 >>> y = x >>> x == y True >>> x is y True Booleans
  32. 13/02/2017 Python Bootcamp - Basic I 34 More on boolean

    operators (like +, *, ==, !=, <, >, <=, >=, is, not) HERE Types of variables >>> x = 1 >>> y = 1 >>> x == y True >>> x is y False >>> x = 1 >>> y = x >>> x == y True >>> x is y True Booleans
  33. Types of variables 13/02/2017 Python Bootcamp - Basic I 35

    >>> x = [5, 8, 2, 3] Lists >>> x + x [5, 8, 2, 3, 5, 8, 2, 3] Sum of lists (appending) >>> 3 * x [5, 8, 2, 3, 5, 8, 2, 3, 5, 8, 2, 3] Multiplication by integer
  34. Types of variables 13/02/2017 Python Bootcamp - Basic I 36

    >>> x = [5, 8, 2, 3] Lists >>> x + x [5, 8, 2, 3, 5, 8, 2, 3] Sum of lists (appending) >>> 3 * x [5, 8, 2, 3, 5, 8, 2, 3, 5, 8, 2, 3] Multiplication by integer >>> x[3] # 4th element 3 >>> x[-1] # Last element 3 >>> x[0] # First element 5 C-Like Indexing (from 0 to n-1)
  35. Types of variables 13/02/2017 Python Bootcamp - Basic I 37

    Lists Slicing >>> x[0:2] [5, 8] >>> x[i:j:k] i – start index j – last index (exclusive) k - step >>> x = [5, 8, 2, 3]
  36. Types of variables 13/02/2017 Python Bootcamp - Basic I 38

    Lists Slicing >>> x[0:2] [5, 8] >>> x[:2] [5, 8] >>> x[i:j:k] i – start index j – last index (exclusive) k - step >>> x = [5, 8, 2, 3]
  37. Types of variables 13/02/2017 Python Bootcamp - Basic I 39

    Lists Slicing >>> x[0:2] [5, 8] >>> x[:2] [5, 8] >>> x[2:-1] [2] >>> x[i:j:k] i – start index j – last index (exclusive) k - step >>> x = [5, 8, 2, 3]
  38. Types of variables 13/02/2017 Python Bootcamp - Basic I 40

    Lists Slicing >>> x[0:2] [5, 8] >>> x[:2] [5, 8] >>> x[2:-1] [2] >>> x[2:] [2, 3] >>> x[i:j:k] i – start index j – last index (exclusive) k - step >>> x = [5, 8, 2, 3]
  39. Types of variables 13/02/2017 Python Bootcamp - Basic I 41

    Lists Slicing >>> y = [0, 1, 2, 3, 4, 5, 6] >>> x[i:j:k] i – start index j – last index (exclusive) k – step >>> y[::2] [0, 2, 4, 6]
  40. Types of variables 13/02/2017 Python Bootcamp - Basic I 42

    Lists Slicing >>> y = [0, 1, 2, 3, 4, 5, 6] >>> y[1::2] [1, 3, 5] >>> x[i:j:k] i – start index j – last index (exclusive) k – step >>> y[::2] [0, 2, 4, 6]
  41. Types of variables 13/02/2017 Python Bootcamp - Basic I 43

    Lists Slicing >>> y = [0, 1, 2, 3, 4, 5, 6] >>> y[1::2] [1, 3, 5] >>> x[i:j:k] i – start index j – last index (exclusive) k – step >>> y[::2] [0, 2, 4, 6] >>> y[::-1] [6, 5, 4, 3, 2, 1, 0]
  42. Types of variables 13/02/2017 Python Bootcamp - Basic I 44

    Lists One list can hold variables of diferente types! >>> y = [0, 1, 2, 3, 4, 5, 6] >>> z = [5, 2.0, “abc”] >>> z[1] 2.0 >>> z[2] “abc”
  43. >>> s = 'I am a string' >>> r =

    “I'm another string” Strings Types of variables 13/02/2017 Python Bootcamp - Basic I 45 Strings behaves like lists >>> s[3] 'm'
  44. >>> s = 'I am a string' >>> r =

    “I'm another string” Strings Types of variables 13/02/2017 Python Bootcamp - Basic I 46 Strings behaves like lists >>> s[3] 'm'
  45. >>> s = 'I am a string' >>> r =

    “I'm another string” Strings Types of variables 13/02/2017 Python Bootcamp - Basic I 47 Strings behaves like lists >>> s[3] 'm' >>> s + r “I am a stringI'm another string” >>> 5 * “xy” 'xyxyxyxyxy'
  46. >>> s = 'I am a string' >>> r =

    “I'm another string” Strings Types of variables 13/02/2017 Python Bootcamp - Basic I 48 Strings behaves like lists >>> s[3] 'm' >>> s + r “I am a stringI'm another string” >>> 5 * “xy” 'xyxyxyxyxy'
  47. >>> s = 'I am a string' >>> r =

    “I'm another string” Strings Types of variables 13/02/2017 Python Bootcamp - Basic I 49 Strings behaves like lists >>> s[3] 'm' >>> s + r “I am a stringI'm another string” >>> 5 * “xy” 'xyxyxyxyxy' Formating strings like C printf >>> x = 7 >>> print(“%03d” % x) 003 http://pt.wikipedia.org/wiki/Printf http://www.cplusplus.com/reference/cstdio/printf/
  48. >>> s = 'I am a string' >>> r =

    “I'm another string” Strings Types of variables 13/02/2017 Python Bootcamp - Basic I 50 Strings behaves like lists >>> s[3] 'm' >>> s + r “I am a stringI'm another string” >>> 5 * “xy” 'xyxyxyxyxy' Or using .format() >>> x = 7 >>> print(“{:03d}”.format(x)) 003 Python – Format mini language specs
  49. Types of variables 13/02/2017 Python Bootcamp - Basic I 51

    Dictionaries >>> fruits = {‘apple’: 3, ‘orange’: 1.5} >>> print fruits[‘apple’] 3 Creating a dictionary and accessing its elements
  50. Types of variables 13/02/2017 Python Bootcamp - Basic I 52

    Dictionaries >>> fruits = {‘apple’: 3, ‘orange’: 1.5} >>> print fruits[‘apple’] 3 >>> fruits[‘banana’] = ‘none’ >>> print fruits {‘apple’: 3, ‘orange’: 1.5, ‘banana’: ‘none’} Creating a dictionary and accessing its elements Adding elements to a dictionary
  51. Types of variables 13/02/2017 Python Bootcamp - Basic I 53

    Dictionaries >>> fruits = {‘apple’: 3, ‘orange’: 1.5} >>> print fruits[‘apple’] 3 >>> print ‘apple’ in fruits True >>> print ‘kiwi’ in fruits False Creating a dictionary and accessing its elements Check if a dictionary has an element