recap – python basics. python python is an interpreter which reads and executes a python script. ...
TRANSCRIPT
Recap – Python Basics
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.
Analogy
Learning a computer program is similar to learning
how to write and how to run a theatre play.
Analogy – Theatre Play
The script file is exactly the python script.
Analogy – Theatre Play
A programmer is a writer because the programmer
writes a script.
Analogy – Theatre Play
The Python interpreter is a director because it
interprets your script and controls the flow of
execution as you intended.
Analogy – Theatre Play Who is an actor?
There are operators and functions.
Analogy - Orchestra
You are a composer and Python is a conductor.
Analogy - Orchestra
A python script is a music score and each note is a
literal (data).
Analogy - Orchestra
A music score can also have a loop though there is
no counter nor conditions.
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.
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.
Analogy
As a programmer, we are a composer or a writer.
Analogy
Our program is …
Analogy
Let’s not be fooled that we could write such a great
master piece in this class or in a few days.
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.
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)
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”], …
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
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?
Variable
A variable is a named storage or memory.
L
10010
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.
Assignment Operator
a = 1
a = a + 1
Assignment Operator
The assignment operator always move from the
right to the left.
The left always has to be a destination.
a + 1 = a
Python handles the RHS.
Python evaluates the expression which appear in
the right hand side.
A = 3 + 7
+
73
10
MemoryA
Python handles the RHS.
A = 3
A = A + 1
Memory
A3
Your Code
3
Python handles the RHS.
A = A + 1
+
1AMemory
A3
3
4
Expression
A structure for evaluation is called ‘Expression’.
+
1A
3
4
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”
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
Assignment Operator
The mover can move the result only after the
expression is evaluated.
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
Change of Flows
A Music sheet also has non-conditional loops
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
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
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 ]
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]
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).
Practice 1
Summation
What if?
Practice 2
Test if N is a prime
Practice 2
If there is any zero in L, then N is not a prime.
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.