Week 1

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 53

Welcome!

CS115
Introduction to Programming in Python

Week 1

Slides are mostly adapted from MIT Open Courseware 1


THIS WEEK

▶ course info & expectations


▶ what is computation
▶ python basics
▶ mathematical operations
▶ python variables and types

2
Course Information & Expectations
■ We’ll be using Moodle (All Sections)
■ From SRS enroll to CS115 Moodle

■ Moodle will contain the following information:


● Administrative information
● Course Syllabus: Course Information & Schedule
▶ Weekly topic outline
▶ Lab topics & dates
▶ Assessments and their weights
● Lab assignments
● Link for the lecture slides and lecture code
examples (sections 1, 2, 3)
3
Organization & grading...
▶ 9 sections (530+ students)
▶ 4 credits
▶ 3 hr. class time & spare time
▶ 4 hr. lab each week

▶ Important: common exams & grading


▶ Overall grade from labs and written exams

4
Lab sessions
▶ You will have 10 lab assignments which you are
required to solve individually during the lab
sessions.

▶ At the end of the lab, assistants will check and


grade the assignment.

▶ Assistants will ask students to explain their


solution in order to ensure that they really have
understood the concepts involved.

▶ There are no make-ups for lab sessions and you


must attend the lab to which you are registered.

5
Final exam eligibility and the FZ grade

▶ Students will receive an FZ grade unless they:


score more than 25% on the midterm,
AND have a minimum 75% lab average.

▶ Students receiving an FZ grade are not allowed to


enter the final exam.

6
Communication

▶ If you have difficulty in understanding course subjects,


go to the teaching assistant’s or instructor’s office
hours and ask them.

▶ Please be wise and make use of the resources provided


to you.

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.

▶ Python packages: For scientific computing and


computational modelling, we need additional
libraries (packages) that are not part of the
Python standard library. These allow us to create
plots, operate on matrices, and use specialized
numerical methods. Eg. numpy, pyplot, etc.

8
Software (Cont’d)
Anaconda also contains:

▶ Spyder: Scientific Python Development


EnviRonment. It is an integrated development
environment (IDE) for the Python language with
advanced editing, interactive testing, debugging
features. 

▶ Jupyter Notebook: The Jupyter Notebook is an


interactive computing environment that enables
users to author notebook documents that
include narrative text, live code, plots,
equations, images, video, interactive widgets.
➢ We will use both in lectures and labs. 9
How to Download Anaconda

▶ Anaconda 5.2 with Python 3.6 is available in all


BCC labs.

▶ We recommend that you install the Anaconda


Python 3.6 distribution using the instructions
found at the following link: Anaconda Python

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?

1, 2, 3, … 1: draw 10 units and turn 90 degrees left


14
Types of Knowledge -
A Numerical Example
▶ declarative knowledge
▶ Statements of fact
▶ Example: The square root of a number x is a number y
such that y * y = x
▶ It doesn’t tell how to find y

▶ 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

▶ The syntax rules of a language define how we


can put together symbols, reserved words,
and identifiers to make a valid program
▶ The semantics of a program statement define
what that statement means (its purpose or
role in a program)
▶ A program that is syntactically correct is not
necessarily logically (semantically) correct
▶ A program will always do what we tell it to
do, not what we meant to tell it to do

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

▶ commands executed by Python interpreter in a shell

▶ Commands (statements) instruct interpreter to do


something
▶ Commands can be typed directly in a shell or
stored in a file that is read into the shell and evaluated
▶ A new shell is created whenever execution of a program
begins
▶ Usually a window is associated with the shell

29
Shell vs. Script

▶ We can print ‘Hello World’ in two different ways, using


the shell (command line) or using a script (python file)

▶ The print() function prints text to the screen, so the


command print(‘Hello World’) will print the
given text to the console

30
Built-In Functions

▶ The Python interpreter has a number of functions and


types built into it that are always available

▶ Complete list of 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

Programming have rules for naming variables


▶ Variables names must start with a letter or an
underscore, such as: _underscore. underscore_
▶ The remainder of your variable name may consist of
letters, numbers and underscores. password1. n00b. ...
▶ Names are case sensitive. case_sensitive,
CASE_SENSITIVE, and Case_Sensitive are each a
different variable
▶ Variable should be given meaningful names in our
programs
▶ For example, variables with names a and p are not
meaningful, but names such as area and perimeter
are immediately understood

44
Reserved Words

▶ Reserved words are the keywords that have built-in


meanings and cannot be used as variable names
▶ Each version of Python may have a different keyword
list
▶ To see the list of reserved words in Python you can type
the commands:
import keyword
keyword.kwlist

▶ 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:

# single line comment

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

▶ All of the expressions on the right-hand side of an


assignment are evaluated before any bindings are changed
▶ Example:
x, y = 2, 3
x, y = y, x
print(‘x =‘, x)
print(‘y =‘, y)

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

You might also like