0% found this document useful (0 votes)
30 views

Week 9

Uploaded by

alperen3803
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
30 views

Week 9

Uploaded by

alperen3803
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 28

What is Computer

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:

 increase our problem solving skills


 design good solutions to problems
 test somehow how well they are indeed solutions to the
problem

3
An analogy
Let us say that you have signed up to study French poetry (how
about Marot) in the original.

You have two problems:


• you don’t speak French
• you don’t know much about poetry

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?

• a program is a reflection of the writer and their thoughts


• First, you must have some thoughts!
• The difficulty for most people is to figure out what has to be
done, the problem solving, before writing a 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”

Python allows this to happen more readily.

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.

Try the following:

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

Edited from Punch, Enbody -


The Practice of Computing
Using Python
will discuss in detail next week.

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".

# Display two messages


print("Welcome to Python")
print("Python is fun")

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.)

Edited from Punch, Enbody -


The Practice of Computing
Using Python
# Display two messages
print("Welcome to Python")
print("Python is fun")
"""
This is a
multiple line comment 17
"""
Indentation
• The indentation matters in Python.
Note that the statements are entered from the first
column in the new line. It would cause an error if the
program is typed as follows:

# Display two messages

Edited from Punch, Enbody -


The Practice of Computing
Using Python
print("Welcome to Python")
print("Python is fun")

• We will see in the upcoming weeks when we will need to use


indentation. For now, for regular program flow, we will align all our 18
statements to the left margin.
Special Symbols

Character Name Description


() Opening and closing Used with functions or to group
parentheses arithmetic expressions.
# Pound sign Precedes a comment line.
" " Opening and closing Enclosing a string (i.e. a sequence of
quotation marks (single or characters.)
double)
''' ''' Opening and closing Enclosing a multi-line paragraph
quotation marks (single or comment.
double)

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.

Edited from Punch, Enbody -


The Practice of Computing
Using Python
• print() can display text strings, numbers or calculations.
• Text strings should be between single or double quotes.
• If we would like to output multiple values, we can separate
them with a comma.

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!“

print("\"Is this a cheese shop?\"\n\t'Yes'\n\t\"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.

Edited from Punch, Enbody -


The Practice of Computing
Using Python
• We use names to make our program more readable, so
that the object is easily understood in the program.

23
Variable Objects
• Python maintains a list of pairs for every variable:
• variable's name
• variable's value

• A variable is created when a value is assigned the first time.


It associates a name and a value
• subsequent assignments update the associated value.
• we say name references value

Name Value
my_int = 7
my_int 7
Python name conventions
• must begin with a letter or underscore _

• ab_123 is OK, but 123_abc is not.

• may contain letters, digits, and underscores

Edited from Punch, Enbody -


The Practice of Computing
Using Python
• this_is_an_identifier_123

• may be of any length

• upper and lowercase letters are different (case sensitive)


25
• Length_Of_Rope is not length_of_rope
= is assignment
• In many computer languages, = means assignment.

my_int = my_int + 7
lhs = rhs

Edited from Punch, Enbody -


The Practice of Computing
Using Python
• What assignment means is:

 evaluate the right hand side (rhs) of the =


 take the resulting value and associate it with the
name on the left hand side (lhs).
26
More Assignment
• Example:
my_var = 2 + 3 * 5

 evaluate expression (2+3*5): 17

Edited from Punch, Enbody -


The Practice of Computing
Using Python
 change the value of my_var to reference 17

• Example (my_int has value 2):


my_int = my_int + 3

 evaluate expression (my_int + 3): 5


 change the value of my_int to reference 5 27
Exercises
1. Write a program that will assign a value to a variable and
display it on the monitor.
• The variable should contain the value 10.

Edited from Punch, Enbody -


The Practice of Computing
Using Python
2. Write a program to display:
We are the so-called "Vikings" from the north

3. Write a program to assign x to 8, y to 10 and then print :


Value of x is 8
Value of y is 10
28

You might also like