Download presentation
Presentation is loading. Please wait.
Published byGavin George Modified over 9 years ago
1
Recap – Python Basics
2
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.
3
Analogy Learning a computer program is similar to learning how to write and how to run a theatre play.
4
Analogy – Theatre Play The script file is exactly the python script.
5
Analogy – Theatre Play A programmer is a writer because the programmer writes a script.
6
Analogy – Theatre Play The Python interpreter is a director because it interprets your script and controls the flow of execution as you intended.
7
Analogy – Theatre Play Who is an actor? There are operators and functions.
8
Analogy - Orchestra You are a composer and Python is a conductor.
9
Analogy - Orchestra A python script is a music score and each note is a literal (data).
10
Analogy - Orchestra A music score can also have a loop though there is no counter nor conditions.
11
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.
12
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.
13
Analogy As a programmer, we are a composer or a writer.
14
Analogy Our program is …
15
Analogy Let’s not be fooled that we could write such a great master piece in this class or in a few days.
16
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.
17
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)
18
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”], …
19
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
20
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?
21
Variable A variable is a named storage or memory. L 100 10
22
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.
23
Assignment Operator a = 1 a = a + 1
24
Assignment Operator The assignment operator always move from the right to the left. The left always has to be a destination. a + 1 = a
25
Python handles the RHS. Python evaluates the expression which appear in the right hand side. A = 3 + 7 + 73 10 Memory A
26
Python handles the RHS. A = 3 A = A + 1 Memory A 3 Your Code 3
27
Python handles the RHS. A = A + 1 + 1A Memory A 3 3 4
28
Expression A structure for evaluation is called ‘Expression’. + 1A 3 4
29
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”
30
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
31
Assignment Operator The mover can move the result only after the expression is evaluated.
32
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
33
Change of Flows A Music sheet also has non-conditional loops
34
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
35
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
36
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 ]
37
FOR Statement When Python reads FOR statement, it will bring up each element in a list/tuple/string one by one. 1.A = 0 2.FOR x IN [1,2,3]: 3. A = A + x The order of execution is 1 [2 3] [2 3] [2 3]
38
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).
39
Practice 1 Summation What if?
40
Practice 2 Test if N is a prime
41
Practice 2 If there is any zero in L, then N is not a prime.
42
Homework Due is a week. The homework should be submitted by 28 th May. The late homework will not be scored. I will announce how to submit your homework on next Tuesday.
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.