CS1010S Lecture 01 - Introduction PDF
CS1010S Lecture 01 - Introduction PDF
Lecture 1
Introduction to CS1010S
and Python
14 Aug 2019
Welcome
3 things
WHY
should you take
CS1010S
WHAT
to expect in CS1010S
HOW
to learn Python
(In 15 mins)
But first… The Cast
Prof Ben Leong
Aaron Andrew Bao Bill Boyi
Tutors Nicholas Nigel
Peng
Seng
Phillmon
t Sabrina
Changda Chloe Darren E-Shin Ethan Gorden Subbash Vincent Wenjing Yi Hng Yi Zhe
Hui Hui Jao Ming Jeffrey Jen Han Jiaming Jit Wu Jolene Yihui Yixuan Yongyan Yu Hang Yuchen Zhenyan
Samuel
Churn Liyi
Valary Alfaro
Hian
WHY
should you take
CS1010S
WHY
are you taking
CS1010S
WHY are you taking CS1010S?
• Because I love programming.
• Because I want to learn programming.
• Because I am interested in computing.
• Bo bian, core requirement.
Biz Others
SDE 1% 7%
1%
FASS
2%
Science
49%
SoC
40%
WHY you should
take CS1010S
1. Because computers are
useful
1. Because computers are
useful
but they are dumb
They will precisely execute
every instruction given, even
bad ones
Personal Computer
What will you use to
• Write a report?
• Do accounting?
• Statistical analysis?
• Do accounting?
• Statistical analysis?
• Do accounting?
• Statistical analysis?
Mill Jianprasert
NUS B.B.A
”
Management Associate, Garena
Help simplify your
work
CS1010S vs COS2000
Programming Methodology Computational Thinking for
Scientists
ü Computational Biology üLife Science
ü Data Science Analytics üPhysics
ü Math, Applied Math, QF
üPharmacy
ü Statistics
üChemistry
Belongs to a family of üFood Science
CS1101S, CS1010, CS1010E,
etc.
CS1010S vs COS2000
https://calendar.google.com/calendar/ical/0eqa645n5o9cc8t1idjjlludss%40
group.calendar.google.com/public/basic.ics
No textbook
Save money
The trick:
Consistency
The problem:
Staying consistent is
HARD
“better world
gaming can make a
- Jane McGonigal
17 Mar 2010
https://coursemology.org
7 Problem Sets
15 Main Missions
+ Side quests & Contests
+ 24/7 Grading
+ Unlock Achievements
+ Badges
+ Leaderboard
COMPLETE
+XP
MISSION
COMPLETE
+XP
MISSION
COMPLETE
SIDEQUESTS
COMPLETE
+XP WIN CONTESTS
MISSION
COMPLETE
SIDEQUESTS
COMPLETE
+XP WIN CONTESTS
MISSION
ATTEND
COMPLETE TUTORIALS
SIDEQUESTS
COMPLETE
+XP WIN CONTESTS
MISSION
ATTEND
COMPLETE TUTORIALS
SIDEQUESTS
POST ON
FORUM
(Reflections)
ENOUGH XP?
LEVEL UP!
Lectures
Trainings
Exams Finish by Friday
Forums Recitations
Side-quests Tutorials
+ Trainings
Contests
Missions
Blended Learning
• Online resources on Coursemology
- Video lectures, tutorials and recitations
- Trainings, Missions and Side Quests
Wk 1 Semester 1 Wk 10 Semester 2 Wk R
PASS
The Theory of Aiming
PASS
Not to worry.
Help is always
available
Remedial
TBA
Technical Issues?
Email Us
[email protected]
Need Help?
Discussion Forum
Do Subscribe
CS1010S Road Map
Memoization Searching & Sorting
ADVANCED
Dynamic Object-Oriented
Programming Programming
INTERMEDIATE
Higher-Order List Multiple
Procedures Processing Representations
BASIC
Procedural Data
Iteration Mutation &
Abstraction Abstraction
State
Wishful Recursion
Order of
Thinking Growth
Fundamental concepts of computer programming
Bust some Myths
Myth 1
“Not everyone can learn to
program”
Myth 1
“Not everyone can learn to
program algebra”
Myth 2
“I have no programming
experience. How to survive?”
Michael Yong
Year 2
Google Intern
USD7k a month
Myth 3
“I'm a girl, how can you
expect me to program?”
http://slatestarcodex.com/2017/08/07/contra-grant-on-exaggerated-differences/
Myth 4
“The bell-curve will save me.”
Grade-less
35
30
25
20
15
10
0
<20 25 30 35 40 45 50 55 60 65 70 75 80 85 90 95 100
2013 2014
35
30
25
20
15
10
0
<20 25 30 35 40 45 50 55 60 65 70 75 80 85 90 95 100
2013 2014
Myth 5
“No one will bother if I copy
my friend/senior’s code.”
Caught plagiarizing
.py .py
Plagiarism #2
“Referencing”
.py
.py .py
Plagiarism #3
“Discussion”
.py
.py
.py
Not Plagiarism
Discussion (the proper way)
.py
.py
Why should you take CS1010S
1. Because computers are useful
2. Because pain is good for you
3. Because coding is the new literacy
Well… not really
What are we teaching you?
What are you learning?
Python?
Not quite.
Python is just the tool
Critical Thinking
Problem Solving
Programming Methodology
Foundations of
programming
HOW
to learn Python
(In 15 mins)
IDLE
http://www.python.org
Python 2
vs
Python 3
Python 3.7.4
for CS1010S
Why Python
• Clear and readable syntax
• Intuitive
• Natural expression
• Powerful
• Popular & Relevant
Example: PayPal
• ASF XML Serialization
- C++ à 1580 lines
- Python à 130 lines
https://www.paypal-engineering.com/2014/12/10/10-myths-of-enterprise-python/
Simplicity
Futile to Teach a
Language
Here today, gone tomorrow
Computational
Thinking
What is a program?
• A sequence of instructions
• That modifies some state (of the computer)
What is a program?
Example:
1. Pick up the crate
2. Move 5 meters
3. Turn right
4. Move 5 meters
5. Put down the crate
What is the state
being modified?
Python program
a = 1
b = 2
c = a + b
if c < 0:
print('Yes')
else:
print('No')
Solve problems
• Suppose you have to program an oven to bake
bread
Idea
• Bake at 375°F for 30-35 minutes or until
golden brown and bread sounds hollow
when tapped.
Implementation?
1. Start timer
2. Measure temperature
3. If temp < 375°F then turn on heating element
4. If temp > 375°F then turn off heating element
5. If timer < 35 mins go back to step 2
Elements of
Programming
1. Primitives
• Numbers:
4 , 7/2 , 428.3
• Operators:
+ , - , * , /
• Symbols:
a , pi , foo
2. Means of
Combination
5 + 3 8
((5 + 3)-(2 * 3)) 2
3. Means of
Abstraction
7 + 6 13
a = 3
a + 6 9
4. Controlling Logic
If the light is red,
stop.
Else if it is amber,
accelerate.
Else,
proceed with caution.
While you are hungry,
eat a spoonful of rice.
Burp.
Variables
• Start with 'a'-'z' or 'A'-'Z' or '_'
• Contain only alphanumeric characters or '_'
• Case sensitive
Ian_Lee != ian_lee
Variables
• Avoid reserved keywords e.g. if
• Python convention: lower case letters separated
by '_'
•e.g. count_change
• Store memory addresses
Types
int 8 45 1234
float 2.3 3.14159
bool True False
str "cs1010s"
'cs1010s'
None
Type(…)
>>> type(123)
<class 'int'>
>>> type('123')
<class 'str'>
>>> type(None)
<class 'None'>
Type conversion
>>> str(123)
'123'
>>> float('45.2')
45.2
>>> int(23.8)
23
>>> int('cs1010s')
ValueError!
Assignment
>>> abc = 18
>>> my_string = 'This is my string'
>>> x, y = 1, 2
id1: int
variable address 18
abc id1 id2: str
“This is my string”
my_string id2
id3: int
x id3 1
y id4 id4: int
2
Operators
Arithmetic: + - * / ** // %
>>> a = 2 * 3
>>> a
6
>>> 2 ** 3
8
Operators
Arithmetic: + - * / ** // %
>>> 11 / 3
3.6666666666666665
>>> 11 // 3
3
>>> 11 % 3
2
Truth Values
Statements can be either True or False
2 > 1 is True
5 < 3 is False
Operators
Comparison: > >= < <= == !=
>>> 1 <= 10
True
>>> 5 > 15
False
>>> 5 <= 5
True
Operators
Comparison: > >= < <= == !=
>>> 2 != 3
True
>>> '1' == 1
False
>>> False == False
True
Operators
Logic: or and not
>>> True or False
True
>>> True and False
False
>>> not False
True
Operators
Logic: or and not
a or b True if either a or b is True
a and b True if both a and b are True
not a True if a is not True
Truth Tables
OR True False AND True False
True True True True True False
False True False False False False
NOT
True False
False True
Truth Value Revisted
• Python has keywords True and False
• In Python 3.x, True and False will be equal to
1 and 0
• Anything that is not 0 or empty will be
evaluated as True
Operators
Logic: or and not
>>> True and 0
0
>>> not 'abc'
False
>>> 1 or 0
1
Strings
>>> s = 'ba' >>> 'z' in t
>>> t = 'ck' False
>>> s + t
>>> 'bananb' > t
'back'
True
>>> t = s + 'na' * 2
>>> 'banan' <= t
>>> t
True
'banana'
>>> 'c' < t
False
String Slicing
• A String is a sequence of characters
• We can index a string, i.e.
>>> s = 'abcd'
>>> s[0]
'a'
>>> s[2]
'c'
• First character is 0
String Slicing
s[start:stop:step]
non-inclusive
>>> s = 'abcdef'
>>> s[0:2] >>> s[1:5:3]
'ab' 'be'
>>> s[1:2] >>> s[::2]
'b' 'ace'
>>> s[:2]
'ab'
Conditional
if <expr>:
statement(s)
e.g.
>>> a = 3
>>> if a > 0:
indentation print('Good')
'Good'
Conditional
if <expr>: e.g.
statement(s) >>> a = 3
else: >>> if a > 0:
statement(s) print('yes')
else:
print('no')
'yes'
Conditional
if <expr>: e.g.
statement(s) >>> a = -3
elif <expr>: >>> if a > 0:
statements(s) print('yes')
elif a == 0:
else:
print('no')
statement(s) else:
print('huh')
'huh'
Conditional
if <expr>: e.g.
>>> a = 4
statement(s)
>>> if a > 0:
elif <expr>: print('yes')
statements(s) elif a == 0:
elif <expr>: print('no')
elif a == 4:
statements(s) print('ahh')
else: else:
statement(s) print('huh')
'yes'
Conditional
while <expr>: >>> a = 0
statement(s) >>> while a < 5:
a = a + 1
print(a)
1
2
3
4
5
Python
in 15 mins
Am I now a
programmer?
What does it take to
be a programmer?
Do you know
1. how to read?
2. how to form words?
3. how to write sentences?