Python Basics
Whitespace matters! Your code will not run correctly if you use improper indentation.
#this is a comment
Basic Python Logic
if:
for:
if test:
#do stuff if test is true
elif test 2:
#do stuff if test2 is true
else:
#do stuff if both tests are

#
#
#
#
#
#
#
#
My Equation Calculator
CMPT 120
Marina Martin
February 2016
# Set variables
opList = ["plus", "minus", "times", "divided-by", "equals"]
# Read user's equation as a string
equation = input("\nPlease, enter your equation by following the syn

page 1
School of Computing Science
Simon Fraser University
CMPT 120 - Introduction to Computing Science and Programming
Term: Fall 2011-3
Instructor: Bill Havens
Second Midterm Examination
INSTRUCTIONS: Provide answers to these questions on these pages on

# CMPT 120
# author D. Cukierman
# MIDTERM 2013-3 - solutions to coding questions
# one of the versions - other versions are variations
# other solutions are possible
def addOnCh(st,ch,rep):
count = 0
for s in st:
if s = ch:
count += 1
if count < rep:
st

LEARN PYTHON THE HARD WAY
A Very Simple Introduction To The Terrifyingly
Beautiful World Of computers And Code
Third Edition
Zed A. Shaw
2
LEARN PYTHON THE HARD WAY
3
Contents
1 Preface
18
1.1 Acknowledgements . . . . . . . . . . . . . . . . . . . . . . .

September 25, 2013
Boolean Expressions
Boolean Expression:
In a computer science, a Boolean expression is an expression ina
programming language that produces a Boolean value when evaluating. i.e.
one of true or false
Recall: a numerical expression is a

October 4, 2013
Topics Included in Quiz 1
Concepts seen so far:
Algorithms: definitions, importance
Shell vs. program environment, Python Interpreter
Variables and assignment
Expressions:
o Strings expressions: using built-in operations, functions and

Description:
This function receives as an input parameter one string containing
only digits. The function should return one string with two digits
separated by two dots, where the first digit is the minimum digit
found in the original string and the secon

September 6, 2013
Computing Science:
The fundamental question CS wants to address:
What can be (efficiently) automated?
Computing system
Hardware: boxes, circuit boards, chips, wires, monitors
Software:programs,providinginstructionsthatthecomputercarries

#Assignment 1 CMPT 120
#Random Password Generator
print "Welcome to my password generating program!"
print
print "This program will create a personalized password for you depending on
what you put in."
print
print "Please follow the instructions and creat

random.randint
import random
var = random.randint(1,100)
each time we call the function it potentially produces a new random value
Its useful for games, emulation.
a void function
What is the meaning of
?
-Function that does not return any value
Example:

CMPT 120
- MIDTERM TERM REVIEW
Introduction!
- Computing Science!
The study of ALGORITHMS, including!
- formal mathematical properties!
- hardware realizations!
- linguistic realizations !
- applications!
- IEEE (the Institute of Electrical and Electroni

Cmpt 120 lecture
Sorting
-use while loops instead of for loops,
-sort beforehand
Candidate:
After Sort:
While i != 7:
For i in range
If I =7:
Print You found the element you were looking for

September 9, 2013
Algorithms Core of Computer Science
We saw some algorithms to solve the sandwich problem but were they really
algorithms
Initial Definition of Algorithm: A set of instruction, which solve a problem
An algorithm is a finite sequence of:

L S
S B
C M PT 2 0 , p ri n g2 0 L 3 , F U u rn a by
Instructor:
DianaCukierman
-March22
Midterm
Page 1 of
5
WITH SOLUTIONS
Thisis oneof theversions the Midterm.
of
thoughpresent
analogous
exercises.
Otherversions
vary slightly,
You are recommended to re-

October 18, 2013
Topics For Quiz #2
All what we have seen before quiz #1 is a basis needed
If statements
o Nesting
Defining our own functions:
o Productive, non-productive
o With and without parameters
Turtle built-in functions
For loops
o Printing
o Trac

October 23, 2013
Boolean Expressions
Given a string in a certain position st[pos] and a character ch, lets express the
condition:
Cond 1: St[i].isdigit()=false) and (st[pos]!=ch)
not (st[pos].isdigit() and not (st[pos]=ch)
at[pos].isalpha() and st[pos]!=c

October 30, 2013
While Loop, Trace by Hand:
Observe:
Values after executing loop
What happens if we change the index variable in a for loop?
o Within one iteration we can change the index, but
o A new iteration starts with the corresponding index value

October 11, 2013
Loops
Loops:
Essential idea: execute the same sequence of code statements repeated
times
This is essential if the number is large 4 or more
Range:
> range(4)
[0, 1, 2, 3]
> range(2,4)
[2, 3]
> range (3,11,2)
[3, 5, 7, 9]
> range (10,0,-

November 15, 2013
Global and Local Variables
Scope of Variables:
Where the variable are defined determines:
o Where they can be used
(Observe their value, have the variables participate in
expression or be printed for ex)
o Where they can be modified
R

October 25, 2013
Roles of Variables, Flag Variables, Linear Search
Flag Variables:
Basic case
o Two values
o We want to detect if a condition occurred or not
It could also provide more information, some value associated to the
condition happening
o Posi

October 9, 2013
Turtle Module, Using Functions
Python Modules:
Math
Random
Turtle
> Import math
> Import turtle
> Import turtle as t
Using a Turtle:
The turtle module provides functions that allow us to make graphics. We
will start using subroutines o

CMPT 120
Topic: Functions Part 2
Learning outcomes
At the end of this course, a student is expected to:
Create (design) small to medium size programs using
Python:
Decompose a Python program into functions
Use the core features of Python to design prog

CMPT 120
Topic: Recursion Part 1
Learning outcomes
At the end of this course, a student is expected to:
Describe the concept of recursion, of recursive
definitions and recursive functions.
Use recursion to solve problems:
Use box tracing to predict the

CMPT 120
Topic: Binary Encoding Part 2
Learning Outcomes
In terms of Learning Outcomes
At the end of this course, a student is expected
to:
Manipulate binary encodings of simple data
types.
Do conversions from binary system to decimal and
vice versa
D

CMPT 120
Topic: Functions Part 1
Learning outcomes
At the end of this course, a student is expected to:
Create (design) small to medium size programs using
Python:
Decompose a Python program into functions
Use the core features of Python to design prog

CMPT 120
Topic: Functions Part 3
Software Development using functions
Learning outcomes
At the end of this course, a student is expected to:
Create (design) small to medium size programs using
Python:
Decompose a Python program into functions
Use the c