01C Programming Part 1

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 22

SMU Classification: Restricted

COR-IS1702: COMPUTATIONAL THINKING


WEEK 1C: PROGRAMMING PART 1

2022/23 Term 2
SMU Classification: Restricted

Road Map

Algorithm Design and Analysis


✦ Week 1: Introduction, Counting, Programming
✦ Week 2: Programming
Here now
✦ Week 3: Complexity
✦ Week 4: Iteration & Decomposition
✦ Week 5: Recursion

Fundamental Data Structures


(Weeks 6 - 10)

Computational Intractability and Heuristic Reasoning


(Weeks 11 - 13)
SMU Classification: Restricted

Programming-related Activities for this course

Wk 1 in-class lesson (1 hr)


Wk 1-7 
Wk 2 in-class lesson (2 hr) Homework Labs 
• Lab 0
• Lab 1
• Lab 2
• Lab 3
• Lab 4

Wk 10-13
Group Project
SMU Classification: Restricted

Labs – Compulsory
You are given 5 take-home labs to prepare you for the group project: 
– Labs are mandatory – you must submit a working solution to eLearn
– Timely completion of your labs contributes to your class part score

La Coverage Release Date Due Date


b
0 Conditionals/loops Wk 1 in class Wk 2 in class 
1 Converting pseudocode to Python code Wk 2 Wk 3 before class starts
2 Lists (binary search) Wk 3 Wk 5 before class starts
3 Recursion Wk 5 Wk 6 before class starts
4 Putting it all together Wk 6 End of Wk 8 (recess)
SMU Classification: Restricted

Programming-related Assessment

Assessment Mode Week Weight


Project Team (2-3 members) 10-13 20%
Release: Wk 10
Due: Wk 13 
SMU Classification: Restricted

Using Python on Google Colab


• We will be using Python 3 for this course
• No installation is required on your laptop; we will be
using Google Colab for our Python programming
– colab uses Python 3
– you will need a Google account
• However, you will still need to extract out the Python
code from colab into a .py file when submitting your lab
exercises & project code.
SMU Classification: Restricted

Reference
• "Explorations in Computing" by John S. Conery (JSC)
– Publisher: Taylor & Francis CRC Press
– Homepage: http://ix.cs.uoregon.edu/~conery/eic/python/index.html
– PDF: ftp://ftp.cs.uoregon.edu/pub/conery/freeman/EiC.Mar.27.pdf

– The programming lessons for weeks 1 and 2 follow chapters 2 (Python Workbench) and 3
(Sieve of Eratosthenes) closely.
– Remaining lessons of this course will not be based on this textbook, but it's still an easy
introductory read for some of the topics that will be covered.
– JSC uses an IDE (Integrated Development Environment) such as IDLE for its "Tutorial
Projects", but we will use Colab instead.
SMU Classification: Restricted

Learning Outcomes
• By the end of this session, you should:

– Be aware of the programming-related activities & requirements for this


course (labs, test, project)

– Be familiar with
• (i) arithmetric operators (+, -, *, /)
• (ii) variables,
• (iii) integers & floats,
• (iv) using functions (such as sqrt),
• (v) Using Colab
SMU Classification: Restricted

In-class Ex: Let's Get Our Hands Dirty!


• Try Tutorial Project T1-8 (p.36 of the PDF or p.22 of textbook)

– Go to
https://colab.research.google.com/drive/1cqEOlPw-18KvqHaMs66YS
YqIrUh9CkY3

– Textbook:
ftp://ftp.cs.uoregon.edu/pub/conery/freeman/EiC.Mar.27.pdf
SMU Classification: Restricted

Reflecting on Tutorial Project T1-8


• There seems to be precedence rules:
– Multiplication and division have higher precedence than addition and subtraction
– If same level of precedence, evaluation is from left to right
– Round brackets are used if necessary
– Search for "python operator precedence table"
operands

• Terminology: operator, operand


3+1/ 7

operators
• Other useful operators:

% (mod, or remainder)

** (exponentiation)
SMU Classification: Restricted

…Reflecting on Tutorial Project T1-8


• Terminology:
Integer = whole number (e.g. 3, -1).
Float = decimal number (e.g. 3.0, 3.14, -6.9)
• An integer divided by another integer always
returns a float.

• There is a "floor division" operator that returns


an integer from a division: //
This integer is always less than or equal to the
quotient (answer of a division operation)
Returns 1, not 2 --> truncates to the integer
lesser than or equal to the quotient
SMU Classification: Restricted

In-class Ex: T9-19


• Try Tutorial Project T9-19 (p.40 of the PDF or p.26 of textbook)
– Go to
https://colab.research.google.com/drive/1hzNY9Qc7tDqGUdOxhZ0rG7
VXmgBksXM2
SMU Classification: Restricted

Reflecting on Tutorial Project T9-19


• You have seen 2 "types" of data: floats and integers.
int * int = int
float * int = float

• Use sqrt() to find the square root of an integer

• sqrt() is what we call a "function".


– Someone has written some code and placed that code in a function
– You "call" or "invoke" that function in order to run that code

• T15 requires you to "import sqrt", before using the sqrt() function. Try to call sqrt()
without first importing sqrt() and see what happens.
– You need to import the necessary module that a particular function is in before calling that function.
SMU Classification: Restricted

…Reflecting on Tutorial Project T9-19


A function is like a "system":

Function
Input(s) Something happens here Output

A system takes in something, does


something, and may return something.

sqrt()
25 Something happens here 5.0

Call the sqrt() function like this: sqrt(25)


SMU Classification: Restricted

Variables
• Variables are temp “storage areas” for a value. You use an assignment statement to
store a value into a variable.
• Use the = operator to assign.

Circled in light blue: assignment statement.


x is the name of a variable

These statements do NOT change the value


of the variable. They are not assignment
statements.

Another assignment statement.


y is the name of a 2nd variable
SMU Classification: Restricted

…Variables
• You can change the value of a variable.
Simply assign it a new value in another assignment statement:
SMU Classification: Restricted

Variable Names
• There are a few rules for defining variable names:
• Names must start with a letter: 1a is not allowed
• You can have a mix of upper & lowercase letters, digits, or underscores
• case is important (a is not the same as A)
• Naming conventions in Python for variables (and functions):
• Use all lowercase letters with words separated by underscores to
improve readability. e.g. no_of_students
• Some programmers prefer noOfStudents (camel notation). This is also
fine, as long as you keep to the same notation consistently.
SMU Classification: Restricted

In-class Ex 3: T20-30
• The next Tutorial Project that you will attempt has got to do with computing the area of the
countertop:

• Try Tutorial Project T20-30 (p.44 of the PDF or p.30 of textbook)


– Go to https://colab.research.google.com/drive/1bhpaq89dL7wVOat2rW2ZH4V1VnIcD8Eg
SMU Classification: Restricted

Reflecting on Tutorial Project T20-30


• Remember that assignment statements are not mathematical
formulas:
– T24:
>>> x = 109
– T27:
>>> triangle = ((x/2) ** 2) / 2
>>> triangle
10395.875
– T29: Does this change the value
>>> x = 107 stored in triangle?
SMU Classification: Restricted

Summary
What we have just covered:
• Arithmetic operations (operand, operator)
• Integers vs. floats
• Calling a function (e.g. sqrt)
– may need to import the relevant module which
contains the function (e.g. sqrt is in the math module)
• Using variables
SMU Classification: Restricted

Homework (to be done by next week's lesson)


• Slides/Video lectures on :
– boolean expressions
– conditionals (if/elif/else)
– strings
SMU Classification: Restricted

Lab 0 – (To be solved before next week’s class)

1. Attempt Lab 0:
– https://colab.research.google.com/dr
ive/1ngyjYByDKC7rzMHsPOPZrMIXpu
2DFu0Q

– you may want to make a copy of the


lab to your own Google Drive
(optional)
2. You must complete the homework (slides/video) to
attempt Lab 0.
3. We will submit Lab 0 to red server in Wk2 class.

You might also like