recap – python basics. python python is an interpreter which reads and executes a python script. ...

42
Recap – Python Basics

Upload: gavin-george

Post on 16-Dec-2015

263 views

Category:

Documents


2 download

TRANSCRIPT

Page 1: Recap – Python Basics. Python  Python is an interpreter which reads and executes a python script.  A python script is a text file, which contains

Recap – Python Basics

Page 2: Recap – Python Basics. Python  Python is an interpreter which reads and executes a python script.  A python script is a text file, which contains

Python

Python is an interpreter which reads and executes a

python script.

A python script is a text file, which contains only text;

no font, no style.

Once Python reads a file, it will execute the script line

by line.

The order of execution is important to understand the

behavior of the python script.

Page 3: Recap – Python Basics. Python  Python is an interpreter which reads and executes a python script.  A python script is a text file, which contains

Analogy

Learning a computer program is similar to learning

how to write and how to run a theatre play.

Page 4: Recap – Python Basics. Python  Python is an interpreter which reads and executes a python script.  A python script is a text file, which contains

Analogy – Theatre Play

The script file is exactly the python script.

Page 5: Recap – Python Basics. Python  Python is an interpreter which reads and executes a python script.  A python script is a text file, which contains

Analogy – Theatre Play

A programmer is a writer because the programmer

writes a script.

Page 6: Recap – Python Basics. Python  Python is an interpreter which reads and executes a python script.  A python script is a text file, which contains

Analogy – Theatre Play

The Python interpreter is a director because it

interprets your script and controls the flow of

execution as you intended.

Page 7: Recap – Python Basics. Python  Python is an interpreter which reads and executes a python script.  A python script is a text file, which contains

Analogy – Theatre Play Who is an actor?

There are operators and functions.

Page 8: Recap – Python Basics. Python  Python is an interpreter which reads and executes a python script.  A python script is a text file, which contains

Analogy - Orchestra

You are a composer and Python is a conductor.

Page 9: Recap – Python Basics. Python  Python is an interpreter which reads and executes a python script.  A python script is a text file, which contains

Analogy - Orchestra

A python script is a music score and each note is a

literal (data).

Page 10: Recap – Python Basics. Python  Python is an interpreter which reads and executes a python script.  A python script is a text file, which contains

Analogy - Orchestra

A music score can also have a loop though there is

no counter nor conditions.

Page 11: Recap – Python Basics. Python  Python is an interpreter which reads and executes a python script.  A python script is a text file, which contains

Analogy – Theatre Play

Flash Back

a device in the narrative of a motion picture, novel,

etc., by which an event or scene taking place

before the present time in the narrative is inserted

into the chronological structure of the work.

Page 12: Recap – Python Basics. Python  Python is an interpreter which reads and executes a python script.  A python script is a text file, which contains

Analogy

Write a script.

A director and actors

practice together.

The show opens in a

theatre.

Write a Python script.

Python and operators

test together.

A program runs and

produces an output.

Page 13: Recap – Python Basics. Python  Python is an interpreter which reads and executes a python script.  A python script is a text file, which contains

Analogy

As a programmer, we are a composer or a writer.

Page 14: Recap – Python Basics. Python  Python is an interpreter which reads and executes a python script.  A python script is a text file, which contains

Analogy

Our program is …

Page 15: Recap – Python Basics. Python  Python is an interpreter which reads and executes a python script.  A python script is a text file, which contains

Analogy

Let’s not be fooled that we could write such a great

master piece in this class or in a few days.

Page 16: Recap – Python Basics. Python  Python is an interpreter which reads and executes a python script.  A python script is a text file, which contains

Analogy

The language of a theatre play is English.

The language of an orchestra is Music.

The language of a computer program is essentially

Mathematics.

We have to be good at (at least) basic

Mathematics.

Page 17: Recap – Python Basics. Python  Python is an interpreter which reads and executes a python script.  A python script is a text file, which contains

Programming

Writing a program is a very complex activity that

involves a different way of thinking.

Three things are different (at least):

Sequential order of Execution.

No automation; You should be very specific.

Different definition of operations; ( ‘=‘ for

assignment, ‘==‘ for equality)

Page 18: Recap – Python Basics. Python  Python is an interpreter which reads and executes a python script.  A python script is a text file, which contains

What we’ve learned

Literals: basic inputs

Numbers ( 3, 4, 3.2, 2.7 … )

String ( “hello”, ‘hi’, … )

Variable: a named storage or memory

Tuple: a composite data with an implicit form

(1,2), (“Name”, 3), …

List: a sequential container

[1,2,3], [“Hi”, “Every”, “One”], …

Page 19: Recap – Python Basics. Python  Python is an interpreter which reads and executes a python script.  A python script is a text file, which contains

Statement

Direction to a computer which is interpreted by

Python.

IF statement tests a condition (True or False)

While statement repeats while a condition meets.

For statement repeats for each element in a

list/tuple/string

Page 20: Recap – Python Basics. Python  Python is an interpreter which reads and executes a python script.  A python script is a text file, which contains

Even if we know those…

We have to know …

How to run a script?

How Python interprets your code?

What you want to do? What you want to

compute?

Page 21: Recap – Python Basics. Python  Python is an interpreter which reads and executes a python script.  A python script is a text file, which contains

Variable

A variable is a named storage or memory.

L

10010

Page 22: Recap – Python Basics. Python  Python is an interpreter which reads and executes a python script.  A python script is a text file, which contains

Assignment Operator

‘=‘ is used as assignment.

a = 3

The assignment is an action to store a value into a

variable.

The order of execution is from the right to the left.

Page 23: Recap – Python Basics. Python  Python is an interpreter which reads and executes a python script.  A python script is a text file, which contains

Assignment Operator

a = 1

a = a + 1

Page 24: Recap – Python Basics. Python  Python is an interpreter which reads and executes a python script.  A python script is a text file, which contains

Assignment Operator

The assignment operator always move from the

right to the left.

The left always has to be a destination.

a + 1 = a

Page 25: Recap – Python Basics. Python  Python is an interpreter which reads and executes a python script.  A python script is a text file, which contains

Python handles the RHS.

Python evaluates the expression which appear in

the right hand side.

A = 3 + 7

+

73

10

MemoryA

Page 26: Recap – Python Basics. Python  Python is an interpreter which reads and executes a python script.  A python script is a text file, which contains

Python handles the RHS.

A = 3

A = A + 1

Memory

A3

Your Code

3

Page 27: Recap – Python Basics. Python  Python is an interpreter which reads and executes a python script.  A python script is a text file, which contains

Python handles the RHS.

A = A + 1

+

1AMemory

A3

3

4

Page 28: Recap – Python Basics. Python  Python is an interpreter which reads and executes a python script.  A python script is a text file, which contains

Expression

A structure for evaluation is called ‘Expression’.

+

1A

3

4

Page 29: Recap – Python Basics. Python  Python is an interpreter which reads and executes a python script.  A python script is a text file, which contains

Expression

Expression is like an equation in Mathematics.

With an operator and operands (data), Python

reduces an expression into a value.

For example, 5 + 2 transforms into 7.

For example, “Hello” + “, World” changes into

“Hello, World”

Page 30: Recap – Python Basics. Python  Python is an interpreter which reads and executes a python script.  A python script is a text file, which contains

Note that

The result of operations is different between types!

7+8 15

“Alpha”+”bet””Alphabet”

7/2 3 but 7 / 2.0 3.5

Page 31: Recap – Python Basics. Python  Python is an interpreter which reads and executes a python script.  A python script is a text file, which contains

Assignment Operator

The mover can move the result only after the

expression is evaluated.

Page 32: Recap – Python Basics. Python  Python is an interpreter which reads and executes a python script.  A python script is a text file, which contains

Let’s see what Python doing

A = 1

B = 2

A = A + 1

B = A + B

A = A + B

B = A + B

A = A + B

Page 33: Recap – Python Basics. Python  Python is an interpreter which reads and executes a python script.  A python script is a text file, which contains

Change of Flows

A Music sheet also has non-conditional loops

Page 34: Recap – Python Basics. Python  Python is an interpreter which reads and executes a python script.  A python script is a text file, which contains

IF Statement

IF statement is a branch with a certain condition.

IF statement has a block of code to execute.

Code to Execute

With True

Page 35: Recap – Python Basics. Python  Python is an interpreter which reads and executes a python script.  A python script is a text file, which contains

Conditional Expression

1. A = True

2. B = False

3. C = 3 == 4

4. D = (3+2) > 5

5. F = 3 in [1,2,3]

6. If D: X = 0

Page 36: Recap – Python Basics. Python  Python is an interpreter which reads and executes a python script.  A python script is a text file, which contains

While Loop

When Python reads a WHILE statement, it will

repeat a block of code within the WHILE statement.

1 I = 0

2 WHILE I < 3:

3 I = I + 1

The order of execution is 1 [ 2 3 ] [ 2 3 ] [ 2 3 ]

Page 37: Recap – Python Basics. Python  Python is an interpreter which reads and executes a python script.  A python script is a text file, which contains

FOR Statement

When Python reads FOR statement, it will bring up

each element in a list/tuple/string one by one.

1. A = 02. FOR x IN [1,2,3]:3. A = A + x

The order of execution is 1 [2 3] [2 3] [2 3]

Page 38: Recap – Python Basics. Python  Python is an interpreter which reads and executes a python script.  A python script is a text file, which contains

A Program

Let’s write our own program.

Imagine as if you are the writer or the composer.

You have to know about characters very well

(operators).

You have to know about what you want to convey

(execute).

Page 39: Recap – Python Basics. Python  Python is an interpreter which reads and executes a python script.  A python script is a text file, which contains

Practice 1

Summation

What if?

Page 40: Recap – Python Basics. Python  Python is an interpreter which reads and executes a python script.  A python script is a text file, which contains

Practice 2

Test if N is a prime

Page 41: Recap – Python Basics. Python  Python is an interpreter which reads and executes a python script.  A python script is a text file, which contains

Practice 2

If there is any zero in L, then N is not a prime.

Page 42: Recap – Python Basics. Python  Python is an interpreter which reads and executes a python script.  A python script is a text file, which contains

Homework

Due is a week.

The homework should be submitted by 28th May.

The late homework will not be scored.

I will announce how to submit your homework on

next Tuesday.