Professional Documents
Culture Documents
Week 1
Week 1
Week 1
CS115
Introduction to Programming in Python
Week 1
2
Course Information & Expectations
■ We’ll be using Moodle (All Sections)
■ From SRS enroll to CS115 Moodle
4
Lab sessions
▶ You will have 10 lab assignments which you are
required to solve individually during the lab
sessions.
5
Final exam eligibility and the FZ grade
6
Communication
7
Software
▶ For the course, we will be using the Anaconda
Python 3.6 distribution.
▶ Anaconda contains:
▶ Python: a programming language in which we
write computer programs.
8
Software (Cont’d)
Anaconda also contains:
10
Goals of the Course
11
12
Types of Knowledge
▶ declarative knowledge
▶ Statements of fact
▶ Example: A house picture consists of a square and
triangle
▶ It doesn’t tell how to draw a house
▶ imperative knowledge
▶ “how to” knowledge (or recipe)
▶ Example: Suppose I know how to
1. draw lines along a given direction, and
2. turn a given number of degrees as I draw.
▶ Can you give me instructions to draw this house now?
13
Types of Knowledge
▶ declarative knowledge
▶ Statements of fact
▶ Example: A house picture consists of a square and a triangle
▶ It doesn’t tell how to draw a house
1.
▶ imperative knowledge
3.
▶ “how to” knowledge (or recipe)
▶ Example: Suppose I know how to
1. draw lines along a given direction, and 2.
2. turn a given number of degrees as I draw.
▶ Can you give me instructions to draw this house now?
▶ imperative knowledge
▶ “how to” knowledge (or recipe)
15
16
17
18
19
20
21
22
Aspects of Languages
▶ Primitive constructs
▶ English: words
▶ Programming language: numbers, strings, simple
operators
▶ Syntax:
▶ English:
"cat dog boy" -> not a syntactically valid sentence
"cat hugs boy" -> a syntactically valid sentence
▶ Programming language:
"hi"5 -> not a syntactically valid python statement
3.2*5 -> syntactically valid python statement
23
Aspects of Languages
▶ Static semantics: syntactically valid strings
have meaning
▶ English: "I are hungry"
● syntactically valid but static semantic error
▶ programming language:
● 3.2 * 5 syntactically valid
● 3 + ’hi' static semantic error (cannot add a
number with a word)
▶ Semantics:
▶ the meaning associated with a syntactically
correct string of symbols with no static semantic
errors
▶ English: can have many meanings "Flying planes
can be dangerous“
▶ programming languages: have only one meaning
but may not be what programmer intended
24
Syntax and Semantics
25
Errors
▶ Syntax Error: ‘Cat dog boy’ is not syntactically
valid, English syntax does not accept a sentence
with <noun noun noun>
▶ Static semantics errors: ‘I runs fast’ is
syntactically correct, however is not valid English
because the noun and verb do not agree
▶ Semantics error: ‘I cannot praise this student too
highly’ is a valid English sentence, however its
meaning is ambiguous. It could either be high
praise (student is very good) or not (student is not
good)
26
Errors in Programs
▶ Syntactic errors
▶ common and easily caught
▶ Static semantic errors
▶ can cause unpredictable behavior
▶ some languages like Java check for these before
running program, some such as Python do less
checking
▶ Semantic errors
▶ Code has different meaning than what
programmer intended
▶ program crashes, stops running
▶ program runs forever
▶ program gives an answer but different than
expected
27
Introduction to Python
▶ General purpose programming language
▶ Can be used effectively to build almost any type of
program
▶ Advantages:
▶ Relatively simple language
▶ Easy to learn
▶ Large number of freely available libraries
▶ Easy to download and install
▶ Runs under all operating systems
▶ Disadvantages:
▶ Because of minimal static semantic checking, not optimal for:
▶ programs that have high reliability constraints (air traffic
control systems, medical device)
▶ large team projects, or extended length projects (“too
many cooks spoil the soup”)
28
Python Programs
▶ A program (script) is a sequence of definitions and
commands
▶ definitions evaluated
29
Shell vs. Script
30
Built-In Functions
31
32
33
34
35
36
▶ i // j --> integer division (value is truncated)
37
38
Python Arithmetic Operator
Precedence
▶ The order of evaluation can be changed by using
parentheses to group sub-expressions
▶ For example: (x + y) * 2, first adds x and y then
multiplies the product by 2
▶ The table below shows the arithmetic operator
precedence
39
40
41
42
43
Naming Variables
44
Reserved Words
▶ Example list:
45
45
Comments
▶ Comments explain the purpose and process.
▶ Comments are not interpreted and do not affect
program execution.
▶ Python comments can take two forms:
OR
"""
multi-line comment, continues to end
symbol, across line breaks
"""
▶ Comments should describe ‘why’ rather than how
▶ Multi-line comments are also called docstrings
▶ Docstrings provide documentation for functions,
modules and classes which we will discuss later
46
Multiple Assignment
will print:
x = 3
y = 2
47
Assignment Operators
48
Quick Check 1
What are the results of the following expressions?
12 / 5
12 // 5
10 / 4.0
10 // 4.0
4 / 10
4 // 10
12 % 3
10 % 3
3 % 10
2.2 * 3
49
Quick Check 1
What are the results of the following expressions?
12 / 5 = 2.4
12 // 5 = 2
10 / 4.0 = 2.5
10 // 4.0 = 2.0
4 / 10 = 0.4
4 // 10 = 0
12 % 3 = 0
10 % 3 = 1
3 % 10 = 3
2.2 * 3 = 6.600000000000005
50
50
Quick Check 2
In what order are the operators evaluated in
the following expressions?
a + b + c + d + e a + b * c - d / e
a / (b + c) - d % e
a / (b * (c + (d - e)))
51
Quick Check 2
In what order are the operators evaluated in the
following expressions?
a + b + c + d + e a + b * c - d / e
1 2 3 4 3 1 4 2
a / (b + c) - d % e
2 1 4 3
a / (b * (c + (d - e)))
4 3 2 1
52
Terms of Use
➢ This presentation was adapted from lecture materials provided in MIT
Introduction to Computer Science and Programming in Python.
➢ Licenced under terms of Creative Commons License.
53