Lecture 1: Introduction to Programming
Contents
Lecture 1: Introduction to Programming¶
We will begin at 1:30 PM! Until then, feel free to use the chat to socialize, and enjoy the music!

Firas Moosvi
Learning Context¶

Academic year: Majority in first year
~200 students in the class
Service course: required by many programs
Programming: Varied
Least experienced: Only ever used iPads and Tablets for school work
Most experienced: AP Computer Science (Java) in highschool
Motivation: Mixed
Some would rather be taking more courses in in their program
Some are really interested in programming
Learning Objectives¶
Become familiar with the course tools and how lectures will be run.
Look at some lines of code and predict what the output will be.
Convert an English sentence into code.
Recognize the order specific lines of code need to be run to get the desired output.
Imagine how programming can be useful to your life!
Part 1: Introduction (5 mins)¶

(Skipping for today’s sample lecture)
Welcome to the course!
Course Syllabus
How this course will be taught
Ice breaker activity.
Lecture Logistics
Introduction to course website
Lots of Live coding
Some Pair Coding
“Get back on the bus” at the Bus Stops
Click this button to follow along with the lecture slides:
Part 2: Writing and Running code (15 mins)¶

Using Python to do Math¶
Task |
Symbol |
---|---|
Addition |
|
Subtraction |
|
Multiplication |
|
Division |
|
Square, cube, … |
|
Squareroot, Cuberoot, … |
|
Trigonometry (sin,cos,tan) |
Later… |
Demo (side-by-side)¶
# Addition
4+5
9
# Subtraction
4-5
-1
# Multiplication
4*5
20
# Division
10/2
5.0
# Square, cube, ...
2**3
8
# Squareroot, Cuberoot, ...
4**(1/2)
2.0
Assigning numbers to variables¶
You can assign numbers to a “variable”.
You can think of a variable as a “container” that represents what you assigned to it
There are some rules about “valid” names for variabes, we’ll talk about the details later
Rule 1: Can’t start the name of a variable with a number!
General guideline for now: just use a combination of words and numbers
Demo (side-by-side)¶
# Two numbers
num1 = 4000000000000
num2 = 8
print(num1,num2)
4000000000000 8
# Multiply numbers together
num1*num2
32000000000000
Assigning words to variables¶
You can also assign words and sentences to variables!
Surround anything that’s not a number with double quotes ” and “
mysentence = "This is a whole sentence with a list of sports, swimming, tennis, badminton"
print(mysentence)
This is a whole sentence with a list of sports, swimming, tennis, badminton
Using Python to work with words¶
Python has some nifty “functions” to work with words and sentences.
Here’s a table summarizing some interesting ones, we’ll keep adding to this as the term goes on.
Task |
Function |
---|---|
Make everything upper-case |
|
Make everything lower-case |
|
Capitalize first letter of every word |
|
Count letters or sequences |
|
Demo (side-by-side)¶
mysentence
'This is a whole sentence with a list of sports, swimming, tennis, badminton'
# split on a comma
mysentence.split(',')
['This is a whole sentence with a list of sports',
' swimming',
' tennis',
' badminton']
# make upper case
mysentence.upper()
'THIS IS A WHOLE SENTENCE WITH A LIST OF SPORTS, SWIMMING, TENNIS, BADMINTON'
# make lower case
mysentence.lower()
'this is a whole sentence with a list of sports, swimming, tennis, badminton'
# count the times "i" occurs
mysentence.count('i')
8
# count two characters: hi
mysentence.count('hi')
1
Extending Python¶
Python is a very powerful programming language that allowing you to do a lot out of the box (as you saw!)
We will see lots of examples of interesting things you can do with python
But eventually, you will want to write your own functions to make specific tasks easier
To share code that others have written, they “package” it up and then “import” it so they can use it
Demo (side-by-side)¶
# Numpy is a very popular package to add many more math functions. You can import it like this:
import numpy
# pi
numpy.pi
3.141592653589793
# sin(pi)
numpy.sin(numpy.pi)
1.2246467991473532e-16
# cos(pi)
Activity: Philosophy of Python¶
There are often MANY different ways of doing things; same output, different “code”
There is a philosophy of writing python code, the “pythonic” way of doing things.
Let’s look at this philosophy a bit more carefully.
Demo (side-by-side)¶
import this
The Zen of Python, by Tim Peters
Beautiful is better than ugly.
Explicit is better than implicit.
Simple is better than complex.
Complex is better than complicated.
Flat is better than nested.
Sparse is better than dense.
Readability counts.
Special cases aren't special enough to break the rules.
Although practicality beats purity.
Errors should never pass silently.
Unless explicitly silenced.
In the face of ambiguity, refuse the temptation to guess.
There should be one-- and preferably only one --obvious way to do it.
Although that way may not be obvious at first unless you're Dutch.
Now is better than never.
Although never is often better than *right* now.
If the implementation is hard to explain, it's a bad idea.
If the implementation is easy to explain, it may be a good idea.
Namespaces are one honking great idea -- let's do more of those!
Activity Instructions¶
You will be put into breakout groups of 3-5 people
Introduce yourself! Say Hello, what year you’re in, and what program you’re from
Answer the following questions:
Why do you think these guidelines exist?
Do you think everyone follows these guidelines?
Pick one of the guidelines above and explain what you think it means.
Part 3: Interpreting code (15 mins)¶

Q1: Interpret Code¶
Look at the following code chunk, can you predict what the output will be?
some_numbers = [1, 50, 40, 75, 400, 1000]
for i in some_numbers:
print(i*5)
A. Prints 6 random numbers.
B. Prints the 6 numbers in some_numbers
.
C. Prints the 6 numbers in some_numbers
multiplied by 5.
D. I don’t know.
# Let's try it!
some_numbers = [1, 50, 40, 75, 400, 1000]
for i in some_numbers:
print(i*5)
5
250
200
375
2000
5000
Q2: Interpret Code¶
Look at the following code chunk, can you predict what the output will be?
some_numbers = [1, 50, 40, 75, 400, 1000]
for i in some_numbers:
if i > 50:
print(i/5)
else:
print(i)
A. Prints the 6 numbers in some_numbers
.
B. Prints the number in some_numbers
if it is less than 50, otherwise prints the number divided by 5.
C. Prints the number in some_numbers
if it is greater than 50, otherwise print the number divided by 5.
D. I don’t know.
# Let's try it!
some_numbers = [1, 50, 40, 75, 400, 1000]
for i in some_numbers:
if i > 50:
print(i/5)
else:
print(i)
1
50
40
15.0
80.0
200.0
Q3: Interpret Code¶
Look at the following code chunk, can you predict what the output will be?
some_numbers = [1, 50, 40, 75, 400, 1000]
def process_number(number):
return (number**2)/10
for i in some_numbers:
if i > 50:
print(process_number(i))
A. Prints the number in some_numbers
if it is greater than 50, otherwise prints nothing.
B. Prints the output of the process_number()
function applied to some_numbers
.
C. Prints the output of the process_number()
function if the original number is greater than 50, otherwise prints nothing.
D. Prints the output of the process_number()
function if the original number is greater than 50, otherwise prints nothing.
E. I don’t know.
# Let's try it!
some_numbers = [1, 50, 40, 75, 400, 1000]
def process_number(number):
return (number**2)/10
for i in some_numbers:
if i > 50:
print(process_number(i))
562.5
16000.0
100000.0
Q4: Order matters!¶
Suppose you are asked to complete the following operation:
Take the number 5, square it, subtract 2, and then multiply the result by 10
Does the order of the operations you do matter? Yes!
# Let's try it:
((5**2) -2)*10
230
# Here is the same operation as above but in multiple lines```
number = 5
number = number**2
number = number - 2
number = number * 10
print(number)
230
Q5: Parson’s problem¶
A Parson’s Problem is one where you are given all the lines of code to solve the problem, but they are jumbled and it’s up to you to get the right order.
A student would like to get this as the final output of some code that they are writing:
3 is smaller than, or equal to 10.
4 is smaller than, or equal to 10.
5 is smaller than, or equal to 10.
6 is smaller than, or equal to 10.
7 is smaller than, or equal to 10.
8 is smaller than, or equal to 10.
9 is smaller than, or equal to 10.
10 is smaller than, or equal to 10.
11 is bigger than 10!
12 is bigger than 10!
13 is bigger than 10!
14 is bigger than 10!
15 is bigger than 10!
Here are ALL the lines of code they will need to use, but they are scrambled in the wrong order. Can you produce the desired output?
Hint: Pay attention to the indents!
my_numbers = [3,4,5,6,7,8,9,10,11,12,13,14,15]
for i in my_numbers:
if i > 10:
print(i,'is smaller than, or equal to 10.')
else:
print(i,'is bigger than 10!')
Congratulations!!¶
You have just shown that you can program!
Over 80% of the course content will be focused on details of the things you’ve seen above:
Numbers and Strings
Loops and Conditionals
Functions
If you followed along with most of what we covered, you’re in good shape for this course

Part 5: Review and Recap (5 mins)¶

Using Python to do Math¶
Task |
Symbol |
---|---|
Addition |
|
Subtraction |
|
Multiplication |
|
Division |
|
Square, cube, … |
|
Squareroot, Cuberoot, … |
|
Trigonometry (sin,cos,tan) |
|
Using Python to work with words (Strings)¶
Python has some nifty “functions” to work with words and sentences.
Here’s a table summarizing some interesting ones, we’ll keep adding to this as the term goes on.
Task |
Function |
---|---|
Make everything upper-case |
|
Make everything lower-case |
|
Capitalize first letter of every word |
|
Count letters or sequences |
|
Zen of Python¶
import this
produces the pythonic philosophy:
The Zen of Python, by Tim Peters
Beautiful is better than ugly.
Explicit is better than implicit.
Simple is better than complex.
Complex is better than complicated.
Flat is better than nested.
Sparse is better than dense.
Readability counts.
Special cases aren't special enough to break the rules.
Although practicality beats purity.
Errors should never pass silently.
Unless explicitly silenced.
In the face of ambiguity, refuse the temptation to guess.
There should be one-- and preferably only one --obvious way to do it.
Although that way may not be obvious at first unless you're Dutch.
Now is better than never.
Although never is often better than *right* now.
If the implementation is hard to explain, it's a bad idea.
If the implementation is easy to explain, it may be a good idea.
Namespaces are one honking great idea -- let's do more of those!
Learning Objectives¶
Become familiar with the course tools and how lectures will be run.
Describe how to use built-in python functions to do operations on numbers
Describe how to use built-in python functions to do operations on words
Look at some lines of code and predict what the output will be.
Recognize the order specific lines of code need to be run to get the desired output.
Imagine how programming can be useful to your life!
Next Class¶
We will dive into details on some data “structures” that will make manipulation easier:
- Lists
- Dictionaries
- Sets
See you next class!