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
• Defining new operations and combining them to do useful things is the heart and soul of programming
2 Programming Languages
3 Compiler vs. Interpreter
*http://www.c4learn.com/c-programming/compiler-vs-interpreter/ 4 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 Expressions: Arithmetic
>>> 4+13
17
• Unary & binary operators
6 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 Expressions: Arithmetic
8 Expressions: Comparison
>>> 2 == 2 #Equality
True
>>> 2 != 2 #Inequality Comments! False
>>> 0 < 2 < 5 #Chained inequality
True
>>> 2 > 2
False
9 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 Variables
• A variable holds information (1.343, ‘hi’, [1,3,6,7])
• In Python this is simple:
11 Variables
• Their values can vary as the program executes
• (no more knowledge
of old values)
12 Variables
>>> degree_celsius = 26.0
>>> 9 /5 * degree_celsius + 32
78.8
>>> difference = 100 - degree_celsius
>>> diference
74
13 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 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 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 Reassigning to Variables
Consider this code:
>>> difference = 20
>>> double = 2 * difference
>>> double
40
>>> difference = 5
>>> double
40 Augmented Assignment
• To create shorthand notation:
>>> score = 50 >>> score = 50 >>> score >>> score 50 50 >>> score += 20 >>> score = score + 20 >>> score >>> score 70 70
18 Augmented Assignment
19 Bugs
• Something went wrong…
>>> 3 + moogah
Traceback (most recent call last): File "
>>> 3 +
^ SyntaxError: invalid syntax
20 Statement Split
• Two possible ways:
1. Make sure your line break occurs inside parentheses
2. Use the line-continuation character, backslash, \
21 Summary
• Programs and Python interpreter
• Expressions; arithmetic, comparison, boolean
• Variables and types
• Computer memory
22