Week 9
Week 9
Science?
Definition
• Computer science is a discipline that involves the
understanding and design of computers and computational
processes.
2
Our goals
• Our goals are not to just write copious amounts of code, our
goals are to:
3
An analogy
Let us say that you have signed up to study French poetry (how
about Marot) in the original.
4
How does this apply
You have two related problems:
• the syntax of French is something you have to learn
• the semantics of poetry is something you have to learn
You have two problems you have to solve at the same time.
5
A similar approach in programming
• You have to learn the syntax of a particular programming
language
• many details about the language, how to debug and use it
• You have to learn about problem solving and how to put it
down on computer.
6
Good Program
What makes a good program?
7
Why Python : Simpler
• Python is a simpler language
• Simpler means:
• Fewer alternatives (one way to do it)
• Better alternatives (easier to accomplish common tasks)
• This allows us to focus less on the language and more on
problem solving
8
Computational Thinking
Having finished this course, we want you to have the following
thought in your subsequent college career.
“Hey, I’ll just write a program for that”. For us, that is
“computational thinking”
9
The Rules
1. Rule 1: Think before you program
Determine the necessary steps to solve the problem.
2. Rule 2: A program is a human-readable essay on problem
solving that also happens to execute on a computer.
Your program should be understandable by other people
(and you) so that it can be worked with, updated,
improved, etc.
3. Rule 3: The best way to improve your programming and
problem solving skills is to practice.
Problem solving and problem solving using
programming to record your solution requires practice. So
experiment. Try it, try again, see if you can fix it.
10
Software
• For the course, we will be using the Anaconda Python 3.9 Individual
distribution.
• We recommend that you install the Anaconda Python 3.9
distribution using the instructions found at the following link:
Anaconda Python
11
First Commands
• Step 1: Open the development environment.
• Step 2: Type the command(s).
• Step 3: Evaluate the results.
12
Things to Notice
• Whitespace characters:
• Include spaces, newlines, tabs and they make programs and commands
easier to read.
• Notice in the examples, we put spaces before and after the arithmetic
operators ( 3 + 5 )
• This is an example of using whitespace to improve readability.
• The python interpreter ignores these extra characters, as they are for
the reader of the program (us!) rather than the interpreter.
• With the spaces removed, the program would behave in the same way.
13
Things to Notice
• Also notice that for multiplication, we use the * symbol
instead of 3 x 5.
• These are examples of the syntax rules of python, which we
14
Anatomy of a Python Program
• Statements
• Comments
• Indentation
15
Statements
• A statement represents an action or a sequence of
actions.
• The statement print("Welcome to Python")in the
program will display the greeting "Welcome to
Python".
16
Comments
• A comment is and explanation in a program written to make
the code more readable by the programmers.
• They are ignored by the Python interpreter (i.e. They are not
executed.)
19
print() Function
• Python provides functions to carry out common tasks, the
print function is an example of this.
• The print() statement allows us to print messages to the
console window in our programs.
20
Printing Special Characters
• Sometimes we want to include characters in our output that
have special meaning.
• This includes characters such as quotes, newlines, etc.
• The table below shows a number of special characters in
python.
Special Meaning
Character
\n New line character
\t Tab character
\\ Backslash character
\' Single quote
21
\" Double quote
Printing Special Characters -
Example
"Is this a cheese shop?"
'Yes'
"We have all kinds!“
22
Variable
• A variable is a name we designate to represent an object
(number, data structure, function, etc.) in our program.
23
Variable Objects
• Python maintains a list of pairs for every variable:
• variable's name
• variable's value
Name Value
my_int = 7
my_int 7
Python name conventions
• must begin with a letter or underscore _
my_int = my_int + 7
lhs = rhs