Grandparents U, 2017Part 1
Computer Programming for Beginners
Filip Jagodzinski
CSCS 141Computer Programming I
Preliminaries : Course Website
All of these slides will be provided for you online ...
... So for the time being just relax and listen!
CSCS 141Computer Programming I
Introduction
Who here has played Pokemon Go?
CSCS 141Computer Programming I
Introduction
Who here has played Pokemon Go?
What about Super Mario Brothers?
Candy Land?
CSCS 141Computer Programming I
Introduction
Who here has played Pokemon Go?
What about Super Mario Brothers?
Candy Land?
Q: How are such games
made?
CSCS 141Computer Programming I
Learning how to Program …
Computer programming is done using a computer
• CODE, or “coding”
• You learn by making mistakes, sort of like learning how to ride the bicycle
Q: How many people were
involved in making Pokemon Go
CSCS 141Computer Programming I
Learning how to Program …
Computer programming is done using a computer
• CODE, or “coding”
• You learn by making mistakes, sort of like learning how to ride the bicycle
Hundreds!
Q: How many people were
involved in making Pokemon Go
CSCS 141Computer Programming I
Learning how to Program …
Learning how to program is like learning a new language
• You learn by making mistakes• Then you try some more
CSCS 141Computer Programming I
Learning how to Program …
Learning how to program is like learning a new language
• You learn by making mistakes• Then you try some more• Then you make more mistakes
CSCS 141Computer Programming I
Difficult question : What happens next?
Learning how to Program …
Learning how to program is like learning a new language
• You learn by making mistakes• Then you try some more• Then you make more mistakes• Then you try some more
CSCS 141Computer Programming I
Learning how to Program …
Learning how to program is like learning a new language
• You learn by making mistakes• Then you try some more• Then you make more mistakes• Then you try some more• Then you make bigger mistakes and everything breaks
CSCS 141Computer Programming I
Programming … is an art and a science
Q: These all depict programs … what do they have in common? How are they different?
Programs (apps) on your smartphone
A nice game of thermonuclearwar
A simple python program
CSCS 141Computer Programming I
Programming … is an art and a science
Solutions:(as perceived by the end-
user)
CSCS 141Computer Programming I
Programming … is an art and a science
Solutions:(as perceived by the end-
user)
Programs:(implementation
of a program)
CSCS 141Computer Programming I
CPU – The “brain” of the computer
CPUCentral Processing Unit
Q: What does a CPU “do”?Q: How does a CPU work?
CSCS 141Computer Programming I
CPU – The “brain” of the computer
CPUCentral Processing Unit
Q: What does a CPU “do”?Q: How does a CPU work?
Instruction(input)
Result(output)
CSCS 141Computer Programming I
CPU – The “brain” of the computer
Instruction(input)
Result(output)
That’s what we want to write … the “instructions” for a computer so that when the computer runs our
program the program behaves as designed
CSCS 141Computer Programming I
Our goals – Overview of the Course
Write a program that prints “Hello world” to the screen
Write a program to store and display your name and age
CSCS 141Computer Programming I
Our goals – Overview of the Course
Write a program that prints “Hello world” to the screen
Write a program to store and display your name and age
Write a program to predict what clothing you should wearAI
CSCS 141Computer Programming I
Our goals – Overview of the Course
Write a program that prints “Hello world” to the screen
Write a program to store and display your name and age
Write a program to predict what clothing you should wear
Write a program that prints your name to the screen using turtles
AI
DEMO
CSCS 141Computer Programming I
The programming language that we’ll be using …
CSCS 141Computer Programming I
Luckily, python has many built in functions – these are features of a program that somebody else has written, and which you will use to build your custom program
One of the most basic function is named print … here’s how you use it
CSCS 141Computer Programming I
Variables
Variables are one of the most basic parts of ANY program and ANY programming language … why do we need them?
CSCS 141Computer Programming I
Variables
Variables are one of the most basic parts of ANY program and ANY programming language … why do we need them?
Computers are really good at storing (and “remembering”) information (data).
Humans on the other hand are
pretty lousy at it
CSCS 141Computer Programming I
Variables
A variable is a name that refers to a value. Computers can store (remember) many values, hence a program can have many (millions, if the conditions are
right), of variables
When you (a human) try to “remember” something, you
make associations
Q: how does a computer program “remember” things
CSCS 141Computer Programming I
Variables
A variable is a name that refers to a value. Computers can store (remember) many values, hence a program can have many (millions, if the conditions are
right), of variables
When you (a human) try to “remember” something, you
make associations
• My mom’s hair color is blond• My dog’s name is Emma• I teach at WWU
Q: how does a computer program “remember” things
CSCS 141Computer Programming I
Variables
A variable is a name that refers to a value. Computers can store (remember) many values, hence a program can have many (millions, if the conditions are
right), of variables
When you (a human) try to “remember” something, you
make associations
• My mom’s hair color is blond• My dog’s name is Emma• I teach at WWU
My mom’s hair color …
My dog’s name …
I teach at …
EMMA
Q: how does a computer program “remember” things
CSCS 141Computer Programming I
Variables
A variable is a name that refers to a value. Computers can store (remember) many values, hence a program can have many (millions, if the conditions are
right), of variables
It is a three step process … you must come up with a name for a variable, and assign it a value
Q: how does a computer program “remember” things
CSCS 141Computer Programming I
Variables
A variable is a name that refers to a value. Computers can store (remember) many values, hence a program can have many (millions, if the conditions are
right), of variables
Step 1 … come up with
variable names
Q: What are good variable names to hold the data
“blond”, “Emma” and 34
My mom’s hair color …
My dog’s name … EMMA
My favorite number … 34
Q: how does a computer program “remember” things
CSCS 141Computer Programming I
Variables
A variable is a name that refers to a value. Computers can store (remember) many values, hence a program can have many (millions, if the conditions are
right), of variables
myMomsHairColor
myDogsName
faveNum
My mom’s hair color …
My dog’s name … EMMA
My favorite number … 34
Q: how does a computer program “remember” things
CSCS 141Computer Programming I
Variables
A variable is a name that refers to a value. Computers can store (remember) many values, hence a program can have many (millions, if the conditions are
right), of variables
myMomsHairColor
myDogsName
faveNum
Step 2 : use the assignment operator to
“set” the value of a variable
My mom’s hair color …
My dog’s name … EMMA
My favorite number … 34
Q: how does a computer program “remember” things
CSCS 141Computer Programming I
Variables
A variable is a name that refers to a value. Computers can store (remember) many values, hence a program can have many (millions, if the conditions are
right), of variables
myMomsHairColor =
myDogsName =
faveNum =
My mom’s hair color …
My dog’s name … EMMA
My favorite number … 34
Q: how does a computer program “remember” things
CSCS 141Computer Programming I
Variables
A variable is a name that refers to a value. Computers can store (remember) many values, hence a program can have many (millions, if the conditions are
right), of variables
myMomsHairColor =
myDogsName =
faveNum =
Step 3 : to the right of the assignment
operation you place the value that you
want to assign to the variable name on the
left
My mom’s hair color …
My dog’s name … EMMA
My favorite number … 34
Q: how does a computer program “remember” things
CSCS 141Computer Programming I
Variables
A variable is a name that refers to a value. Computers can store (remember) many values, hence a program can have many (millions, if the conditions are
right), of variables
myMomsHairColor = “blond”
myDogsName = “Emma”
faveNum = 34
Q: how does a computer program “remember” things
Now you can use the variable in your program, and the
computer will know it is referring to the data that you
assigned to that variable
CSCS 141Computer Programming I
Variables
Putting it all together, what does the below program do when you run it?
CSCS 141Computer Programming I
The input function
One other important function in python is named input
Use it to ask the user to provide an answer to a question, which you can then store in a variable
CSCS 141Computer Programming I
Doing something over and over again …
Humans are also really good at making mistakes if we are tired or doing something over and over again … but computers are REALLY good at
doing something over and over again
Write a program that prints “Hello”
9 times
But there’s an easier way!
CSCS 141Computer Programming I
Doing something over and over again …
Humans are also really good at making mistakes if we are tired or doing something over and over again … but computers are REALLY good at
doing something over and over again
Write a program that prints “Hello”
9 times
But there’s an easier way!
CSCS 141Computer Programming I
Doing something over and over again …
Humans are also really good at making mistakes if we are tired or doing something over and over again … but computers are REALLY good at
doing something over and over again
Write a program that prints “Hello”
9 times
But there’s an easier way!
CSCS 141Computer Programming I
Doing something over and over again …
Humans are also really good at making mistakes if we are tired or doing something over and over again … but computers are REALLY good at
doing something over and over again
Write a program that prints “Hello”
9 times
But there’s an easier way!
CSCS 141Computer Programming I
Doing something over and over again …
Humans are also really good at making mistakes if we are tired or doing something over and over again … but computers are REALLY good at
doing something over and over again
Write a program that prints “Hello”
9 times
But there’s an easier way!
CSCS 141Computer Programming I
Doing something over and over again …
Humans are also really good at making mistakes if we are tired or doing something over and over again … but computers are REALLY good at
doing something over and over again
Write a program that prints “Hello”
9 times
But there’s an easier way!
CSCS 141Computer Programming I
Doing something over and over again …
For this morning’s task, write a program that
asks you to input your name, your age, and
prints that information to the screen 15 times
CSCS 141Computer Programming I
Conditional
One neat thing about programs is that they seem to be “smart”
Goal : Write a program that will tell you want clothing to wear
CSCS 141Computer Programming I
Conditional
One neat thing about programs is that they seem to be “smart”
Goal : Write a program that will tell you want clothing to wear
If you want your program to be intelligent, use if
CSCS 141Computer Programming I
Conditional
One neat thing about programs is that they seem to be “smart”
Goal : Write a program that will tell you want clothing to wear
age = int(input(“what is your age”))
if (age > 10) :
print(“you are older than 10 years”)
CSCS 141Computer Programming I
Conditional
One neat thing about programs is that they seem to be “smart”
Goal : Write a program that will tell you want clothing to wear
age = int(input(“what is your age”))
if (age > 10) :
print(“you are older than 10 years”)
If the input is a number, use the int function in combination with input
CSCS 141Computer Programming I
Conditional
One neat thing about programs is that they seem to be “smart”
Goal : Write a program that will tell you want clothing to wear
age = int(input(“what is your age”))
if (age > 10) :
print(“you are older than 10 years”)
The if is used to check whether something is true or not
CSCS 141Computer Programming I
Conditional
One neat thing about programs is that they seem to be “smart”
Goal : Write a program that will tell you want clothing to wear
age = int(input(“what is your age”))
if (age > 10) :
print(“you are older than 10 years”)
The code inside the if is run ONLY If the if statement was true
CSCS 141Computer Programming I
Conditional
One neat thing about programs is that they seem to be “smart”
Goal : Write a program that will tell you want clothing to wear
age = int(input(“what is your age”))
if (age > 10) :
print(“you are older than 10 years”)
raining= input(“is it raining”)
temp = int(input(“what is the temp”))
if (raining == “yes” and temp < 40) :
print(“wear a hat, globes, and jacket!”)
CSCS 141Computer Programming I
Conditional
One neat thing about programs is that they seem to be “smart”
Goal : Write a program that will tell you want clothing to wear
age = int(input(“what is your age”))
if (age > 10) :
print(“you are older than 10 years”)
raining= input(“is it raining”)
temp = int(input(“what is the temp”))
if (raining == “yes” and temp < 40) :
print(“wear a hat, globes, and jacket!”)
Check the value of a variable by using the double equal sign!
CSCS 141Computer Programming I
Conditional
One neat thing about programs is that they seem to be “smart”
Goal : Write a program that will tell you want clothing to wear
age = int(input(“what is your age”))
if (age > 10) :
print(“you are older than 10 years”)
raining= input(“is it raining”)
temp = int(input(“what is the temp”))
if (raining == “yes” and temp < 40) :
print(“wear a hat, globes, and jacket!”)
Use and to check the values of several things. The above code checks if the variable raining has the value “yes” and the temperature is less than 40
CSCS 141Computer Programming I
Doing something over and over again …
For your second program, you’ll write a program that asks what temperature it is
and whether it is raining, and it will tell you what clothing
to wear!
CSCS 141Computer Programming I
Hands-on exercises in the lab!
Up Next