Week1 Lecture1
Week1 Lecture1
Download and open the Jupyter Notebook (.ipynb) for • Weekly Exit Quiz is available when you are ready
Lecture 1.1 (take your time, you have unlimited tries)
• Reflection 1 released after Thursday’s Tutorial
You may also use this lecture’s JupyterHub link instead
– in Tutorial, ask a mentor to help with your Jupyter • All Tutorials running this week
installation J
This Week’s Content
§ Lecture 1.1
§ Introduction
§ Variables, Expressions, and Operators
§ Lecture 1.2
§ Functions, Input/Output
Reminder for Success: Practice!
§ Programming is a language
§ Everything is cumulative
§ Practice Problems released weekly
§ Use practice problem sets as litmus tests
§ Do them all, take note of which questions give you trouble
What is Programming?
§ A way of telling a computer what to do.
§ A computer can’t infer (…yet).
§ Need to tell a computer every single step it needs to do in a language
it can understand.
§ How would you request an egg for breakfast to a chef and to a
computer/robot?
§ To a Chef § To a Computer
1. “Turn on stove”
1. Sunny-side up, please!
2. “Take out pan”
3. “Take one egg out of fridge”
4. “Crack egg”
5. “Pour egg into pan”
6. “Wait 5 minutes”
How to
Programmer
English
Program a Pseudocode
Computer.
Programing Language
Compiler
Assembly Code
Assembler
Machine Code
are
ardw
H
The power of programming languages
§ if x > 10:
print(”x is greater than 10”)
Introducing Python
§ No end-of-instruction separators, such as semicolons (like in C
or Java)
§ Programs are stored in .py files
§ Comments start with a # character
§ Whitespace matters (exactly 4 spaces means indentation)
§ Python is an interpreted language (not a compiled one)
§ You can run code one statement at a time, just like a calculator or
Matlab
§ This means variables can change type during runtime, and do not have
to be declared before running
Arithmetic Operators
Operator Operation Expression English description Result
+ addition 11 + 56 11 plus 56 67
* multiplication 4 * 5 4 multiplied by 5 20
% modulo 9 % 2 9 mod 2 1
(remainder)
Arithmetic Operator Precedence
§ When multiple operators are combined in a single expression,
the operations are evaluated in order of precedence (from left to
right)
Operator Precedence
** highest
- (negation)
*, /, //, %
>>> difference = 5
>>> double
40
Python Tutor Visualization
§ http://pythontutor.com/visualize.html
§ Code might seem simple now…
Variables and Memory
§ Let’s go experiment with some of
what we just saw
§ Assignment statements Open your
§ Declaring variables
§ Different variable names notebook
§ Memory locations
§ (and the famous print statement!)
Click Link:
2. Variables and
Memory
Variable Types
• A type is a set of values and the
operations that can be performed on
those values.
• int: integer
§ ex. 3, 4, 894, 0, -3, -18
• float: floating point number
§ ex. -5.6, 7.342, 53452.0, -89.34
Numerical Type Examples
>>> 5 + 2 * 4
13
>>> 5.0 + 2 * 4
13.0
>>> 30/6
5.0
>>> 30//6
5
Type: str (pronounced string)
• str: string literal is a sequence of characters
• Start and end with single quotes (') or double
quotes (")
§ ex. 'hello', "What is 10 * (2 + 9)?”
§ Just like writing in English, the quote type
must match (i.e. 2 singles or 2 doubles, not 1
of each)
String Type Examples
>>> ‘how are you?’
-= x=7
x -= 2
x=7
x=x-2
x refers to 5
Open your
*= x=7
x *= 2
x=7
x=x*2
x refers to 14
notebook
/= x=7 x=7 x refers to 3.5
x /= 2 x=x/2