Code Lagos - Learn Programming With Python
Code Lagos - Learn Programming With Python
to Programming
with Python
Python Programming
Adedipo Olagbegi
dipo@delphitechnologies.com
AIM
To know what programming languages are
To know why Python
To learn some Python programming
Programming objec)ves
Interpreted v Compiled Languages
Which ever language we write in, our program must be
converted into code that the computer hardware can
understand (machine code).
• Compiled Languages convert the whole program into
machine code to produce an executable file. You can
then run the ‘.exe’ file.
14
Programming Tools
• Three tools are used to convert algorithms
into computer programs:
• Flowchart - Graphically depicts the logical steps
to carry out a task and shows how the steps
relate to each other.
• Pseudocode - Uses English-like phrases with
some Visual Basic terms to outline the program.
15
Problem solving example
• How many stamps do you use when
mailing a letter?
• One rule of thumb is to use one stamp for
every five sheets of paper or fraction
thereof.
16
Algorithm
1. Request the number of sheets of paper;
call it Sheets. (input)
2. Divide Sheets by 5. (processing)
3. Round the quotient up to the next highest
whole number; call it Stamps. (processing)
4. Reply with the number Stamps. (output)
17
Flowcharts
• Graphically depict the logical steps to
carry out a task and show how the steps
relate to each other.
18
Flowchart symbols
19
Flowchart symbols continued
20
Flowchart
example
21
Pseudocode
22
Pseudocode example
Determine the proper number of stamps for a
letter
Read Sheets (input)
Set the number of stamps to Sheets / 5
(processing)
Round the number of stamps up to the next
whole number (processing)
Display the number of stamps (output)
23
Divide-and-conquer method
• Used in problem solving – take a large
problem and break it into smaller problems
solving the small ones first
• Breaks a problem down into modules
24
Statement structures
25
Sequence
flow chart
26
Decision flow chart
27
Looping flow chart
28
Direction of Numbered NYC Streets
Algorithm
• Problem: Given a street number of a one-
way street in New York City, decide the
direction of the street, either eastbound or
westbound
• Discussion: in New York City even
numbered streets are Eastbound, odd
numbered streets are Westbound
29
Flowchart
30
Pseudocode
Program: Determine the direction of a numbered
NYC street
Get street
If street is even Then
Display Eastbound
Else
Display Westbound
End If
31
Class Average Algorithm
• Problem: Calculate and report the grade-point
average for a class
• Discussion: The average grade equals the sum
of all grades divided by the number of students
Output: Average grade
Input: Student grades
Processing: Find the sum of the grades; count the
number of students; calculate average
32
Flowchart
33
Pseudocode
Program: Determine the average grade of a class
Initialize Counter and Sum to 0
Do While there are more data
Get the next Grade
Add the Grade to the Sum
Increment the Counter
Loop
Computer Average = Sum / Counter
Display Average
34
Tips and tricks of flowcharts
• Flowcharts are time-consuming to write and
difficult to update
• For this reason, professional programmers are
more likely to favor pseudocode and hierarchy
charts
• Because flowcharts so clearly illustrate the
logical flow of programming techniques, they are
a valuable tool in the education of programmers
35
Tips and tricks of pseudocode
• There are many styles of pseudocode
• Some programmers use an outline form
• Some use a form that looks almost like a
programming language
• The pseudocode in the case studies of this text
focus on the primary tasks to be performed by
the program and leaves many of the routine
details to be completed during the coding
process
36
URL’s for free Flowcharting
software
• www.smartdraw.com
• www.gliffy.com/uses/flowchart-so7ware/
• www.breezetree.com/flowchar:ng-
so7ware/
Flowchart So7ware, FREE Flowchart Examples
and Templates ...
• www.edrawso7.com/flowchart.php
37
Programming Environment
What you need
• A text editor (NOT a word processor)
• A python interpreter.
• hTp://www.python.org
• Subtract ‘-’
– Used with integers for subtrac)on
– Integers can handle –ve numbers
– Makes no sense with strings
Basic Math func)ons 2
• Mul)ply ‘*’ - NOTE this is a ‘star’ not ‘x’
– Can be used with integers to mul)ply
– It makes no sense to mul)ply strings.
– What does “Hello “ * 5 result in ?
Condi)onal statements
if <I can understand this>:
i will be happy
else:
i will be sad L
Boolean Types
• So far we have looked at 3 types of data:-
– integers – whole numbers (+ve and –ve) no limit
– strings – sequence of ASCII characters
– floats – decimal numbers – watch precision
• Boolean Type
– True or False
– Usually generated as the result of a test
• legal = age >= 18
– Can be sta)cally signed
• brown_eyes = True
Tests compare data items..so.
Comparators in Python
• ==
Used to test if the two operands are equal
name == "George"
• > or >=
Used to test if the two operands are ‘greater than’ or
‘greater than or equals’ to another item. )
height >= 2000
Strings are compared leTer by leTer star)ng from the
first leTer using each characters ASCII value
Thus
"Bob" > "Bill"
Comparators in Python
• < or <=
Used to test if the two operands are ‘less than’ or ‘less
than or equals’ to another item. )
item_cost < 450
Strings are compared leTer by leTer star)ng from the
first leTer using each characters ASCII value
Thus
"Big" < "Small" returns True
• !=
Used to test if two operands are not equal
passcode != "12$A*djm"
Logic and mul)ple comparators
We can combine comparators using logical operators.
• A and B
– True when A and B are both True
• age > 21 and sex == “F”
• A or B
– True when either A or B are True (or both of then are True)
• temp > 100 or pressure > 5
• not A
– True when A is False
• not passed_exam
:
True
False
If
block 1
a == b
if a == b:
block 1
block2
block 2 block 1 indented and only
executed if the test block 2 aligned with the if.
returned True It is executed
independently of the test
no maTer what happens
if, else block 1 indented and
only executed if the test
returned True
Logical test
:
True
False
block 3
If
Age != block 1
if a == b:
18 block 1
else:
block 3
block 2 block2
Block 3 indented and block 2 aligned with the if. It is
only executed if the test executed independently of the
returned False test no maTer what happens