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

hello python
SMART_READER_LITE
LIVE PREVIEW

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


slide-1
SLIDE 1

Hello Python!

Maryam Tavakol

Machine Learning Group

1

Winter semester 2016/17

slide-2
SLIDE 2

Programs & Programming

  • A program is a set of instructions
  • Every program is written in terms of a few basic
  • perations that its reader already understands
  • Defining new operations and combining them to do

useful things is the heart and soul of programming

2

slide-3
SLIDE 3

Programming Languages

3

slide-4
SLIDE 4

Compiler vs. Interpreter

4

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

slide-5
SLIDE 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

slide-6
SLIDE 6

Expressions: Arithmetic

>>> 4+13 17

  • Unary &

binary operators

6

slide-7
SLIDE 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

slide-8
SLIDE 8

Expressions: Arithmetic

8

slide-9
SLIDE 9

Expressions: Comparison

>>> 2 == 2 #Equality True >>> 2 != 2 #Inequality False >>> 0 < 2 < 5 #Chained inequality True >>> 2 > 2 False

9

Comments!

slide-10
SLIDE 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;
  • therwise, 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

slide-11
SLIDE 11

Variables

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

11

slide-12
SLIDE 12

Variables

  • Their values can vary as the program executes
  • (no more knowledge
  • f old values)

12

slide-13
SLIDE 13

Variables

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

13

slide-14
SLIDE 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

slide-15
SLIDE 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

slide-16
SLIDE 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

slide-17
SLIDE 17

Reassigning to Variables

Consider this code:

>>> difference = 20 >>> double = 2 * difference >>> double 40 >>> difference = 5 >>> double 40

slide-18
SLIDE 18

Augmented Assignment

  • To create shorthand notation:

18

>>> score = 50 >>> score 50 >>> score = score + 20 >>> score 70 >>> score = 50 >>> score 50 >>> score += 20 >>> score 70

slide-19
SLIDE 19

Augmented Assignment

19

slide-20
SLIDE 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

slide-21
SLIDE 21

Statement Split

  • Two possible ways:
  • 1. Make sure your line break occurs inside

parentheses

  • 2. Use the line-continuation character,

backslash, \

21

slide-22
SLIDE 22

Summary

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

22