You are on page 1of 13

WHAT IS PYTHON?

• A snake? Two snakes?

• Monty Python. Guido von Rossum wanted Python to be powerful but fun, not serious like
the other programming languages of the time.

• Conceived in 1980s and appeared in 1991.

• Lots of mistakes when learning (syntax error). A language with its own syntax, its own
grammar.

• It does not understand our language, we need to understand its language to communicate.
Most of the time, it will show us where we made the mistake, but not always.

• Python vocabulary is pretty small and it is called “reserved words”. You cannot use
reserved words to name variables.

• The best way to think about this is thinking Python is kind of a dog.
RESERVED WORDS

and del global not with


as elif if or yield
assert else import pass break
except in raise class finally
is return continue for lambda
try def from nonlocal while
STRUCTURED PROGRAMMING
LANGUAGES

• CS504 is an introductory course to Structured Programing with Python.

• Structured programming is a design that is organized to minimize errors and


misinterpretation by using a structure of sequences, decisions (conditions)
and iteration (repetition).
STRUCTURED PROGRAMMING
LANGUAGES

• The three main patterns of structured programming are;

sequence, decision and iteration.


SEQUENCE
• Sequence (do one thing, then do the next thing and the next thing…)

EXAMPLE 1: Pseudocode EXAMPLE 2:

Add flour x=2


Add salt print(x)
Add yeast x=x+2
Mix print(x)
Add water
Knead
Let rise
Bake
DECISION
• Decision (if this is true, do this. If not, do something else)

EXAMPLE 1: Pseudocode EXAMPLE 2:

compute course_score x=1


if course_score is greater than or if x < 2 :
equal to 60 print(‘Small’)
assign course grade to PASS elif x < 10 :
else print(‘Medium’)
assign course grade to FAIL else :
submit course grade print(‘Large’)
ITERATION
• Repetition (repeat the same code as long as needed).

EXAMPLE 1: Pseudocode EXAMPLE 2:

get mail from mailbox import time


put mail on table
while more mail to sort x = 10
get piece of mail from table while x > 0:
if piece is personal
read it print(x)
elif piece is magazine x=x–1
put in magazine rack time.sleep(1)
elif piece is bill
pay it print('The rocket has launched!')
elif piece is junk mail
throw it in wastebasket
print(‘Done!’)
FLOWCHARTS
• Flowcharts are graphical representations of how your code works.

• There are 4 main shapes, used for 4 different purposes:

• Oval = start / end

• Parallelogram = input / output

• Rectangle = calculations

• Diamond = selection
FLOWCHARTS
• The shapes in the flowchart are connected via arrows.

• The direction of the arrows follows the direction of your code.

• A simple flowchart of Hello World!

star
t

print “Hello World”

end
FLOWCHARTS
• An example with a calculation that asks for the user to enter a number, do some calculation,
and print the result.

star
t

User input number

Multiply number by 4

Print number

end
FLOWCHARTS
• Another example code that asks for the user to enter a number, checks some
conditions, and acts accordingly.

Print “Positive” Print “Negative”

Yes Yes

start User input number If > 0 No If < 0 No Print “0” end


FLOWCHARTS
• Another example code that has an iteration pattern with ‘while’.

While
start mail in fals Print “Done” end
mailbox e

true

Read the mail and sort


Summary

• Python meaning

• Errors

• Reserved Words

• Structured Programming patterns

• Sequence

• Decision

• Iteration

• Flowcharts

You might also like