hello python
play

Hello Python! Maryam Tavakol Machine Learning Group Winter - PowerPoint PPT Presentation

Hello Python! Maryam Tavakol Machine Learning Group Winter semester 2016/17 1 Programs & Programming A program is a set of instructions Every program is written in terms of a few basic operations that its reader already understands


  1. Hello Python! Maryam Tavakol Machine Learning Group Winter semester 2016/17 1

  2. Programs & Programming • A program is a set of instructions • Every program is written in terms of a few basic operations that its reader already understands • Defining new operations and combining them to do useful things is the heart and soul of programming 2

  3. Programming Languages 3

  4. Compiler vs. Interpreter 4 *http://www.c4learn.com/c-programming/compiler-vs-interpreter/

  5. Run a Python Program • The Python program is saved in a file • The program is executed by Python interpreter • Interacting with it is possible in a program called a shell

  6. Expressions: Arithmetic >>> 4+13 17 • Unary & binary operators 6

  7. Expressions: Arithmetic • Operator precedence • A collection of rules which defined the order of operations >>> 212 - 32 * 5 / 9 194.2223 >>> (212 - 32) * 5 / 9 100.0 7

  8. Expressions: Arithmetic 8

  9. Expressions: Comparison >>> 2 == 2 #Equality True >>> 2 != 2 #Inequality Comments! False >>> 0 < 2 < 5 #Chained inequality True >>> 2 > 2 False 9

  10. Expressions: Boolean Operators • The operator not yields True if its argument is false, False otherwise >>> not 1 == 1 False • The expression x and y first evaluates x ; if x is false, its value is returned; otherwise, y is evaluated and the resulting value is returned >>> 2 == 2 and 1 != 1 True • The expression x or y first evaluates x ; if x is true, its value is returned; otherwise, y is evaluated and the resulting value is returned >>> 2 == 2 or 1 != 1 False 10

  11. Variables • A variable holds information (1.343, ‘hi’, [1,3,6,7]) • In Python this is simple: 11

  12. Variables • Their values can vary as the program executes • (no more knowledge of old values) 12

  13. Variables >>> degree_celsius = 26.0 >>> 9 /5 * degree_celsius + 32 78.8 >>> difference = 100 - degree_celsius >>> diference 74 13

  14. Types • Type consists of: • set of possible values • set of operations that can be applied to those values • Unlike C/C++ and Java, variables can change types. (Python internally keeps track of the type.) 14

  15. Computer Memory >>> degree_celsius = 26.6 • Value 26.0 has the memory address: id1 • The object at the memory address id1 has type float and the value 26.0 • Variable “ degrees_celsius” contains the memory address id1 • Variable “ degrees_celsius” refers to the value 26.0 15

  16. Assignment Statement «variable» = «expression» • Executes as follows: 1. Evaluate the expression to produce a value. (with a memory address) 2. Store the memory address of the value in the variable. (Create a new variable otherwise, or reuse the existing variable) 16

  17. Reassigning to Variables Consider this code: >>> difference = 20 >>> double = 2 * difference >>> double 40 >>> difference = 5 >>> double 40

  18. Augmented Assignment • To create shorthand notation: >>> score = 50 >>> score = 50 >>> score >>> score 50 50 >>> score += 20 >>> score = score + 20 >>> score >>> score 70 70 18

  19. Augmented Assignment 19

  20. Bugs • Something went wrong… >>> 3 + moogah Traceback (most recent call last): File "<stdin>", line 1, in <module> NameError: name 'moogah' is not defined >>> 3 + ^ SyntaxError: invalid syntax 20

  21. Statement Split • Two possible ways: 1. Make sure your line break occurs inside parentheses 2. Use the line-continuation character, backslash, \ 21

  22. Summary • Programs and Python interpreter • Expressions; arithmetic, comparison, boolean • Variables and types • Computer memory 22

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend