Code Lagos Week 1
Code Lagos Week 1
Python
Week 1
What is CodeLagos?
●
CodeLagos is an initiative of the Lagos State
Ministry of Education aimed at making coding
education accessible to Lagos State residents
●
The program launched a successful pilot phase
in 2017 with 23 Government schools and 32
Private schools. Since then, it has expanded to
130 Government schools and 145 Private
schools
●
Started with 4 Out of School centres but has
since expanded to 21
2
Code of Conduct
●
Uphold the tenets of CodeLagos and conduct myself in a
manner that promotes the values and objectives of the program
●
Refrain from the use of tobacco, alcohol or non-prescription
controlled substances during the program
●
Follow the instructions of the program staff at all times
●
Turn off my phone or put them on silent mode while training is
on. You are not allowed to use your phones while the class is in
session
●
Show respect to my instructors, fellow scholars and any other
persons involved in this program
●
Not partake in side conversations or any other form of distraction
while training is going on
3
Code of Conduct
●
Avoid loitering during training sessions
●
Refrain from fighting, use of force or abusive
language
●
Not to litter the training facilities before, during and
after each session
●
Abide by all other rules of the program, including any
additional conduct rules or requirements set forth in
the program materials
●
Smoking is not allowed within the learning facilities
●
Alcohol is not allowed within the training facilities
4
Eligibility for Graduation
●
Attend a minimum of 85 percent of your classes
●
Complete all assignments and assessments
●
Be punctual to all training sessions
5
6
Course Objectives
●
Have a good understanding of programming
●
Construct basic programs using the Python
programming language
●
Understand how to use branching and loops in
creating programs
7
Understanding Computers
8
Computers
●
The hardware is the part of the computer that
we can touch
●
The software is the programs that run on the
hardware
●
The art of creating program is called
programming
●
To program a computer, we must use a
programming language
9
What is a
Programming Language?
●
Programming languages are used to create
programs that implement algorithms
●
It is a vocabulary and set of grammatical rules
for instructing a computer or computing device
to perform specific tasks
●
It is a language that the computer understands
10
Algorithms
●
Step by step procedure designed to perform an
operation, and which (like a map or flowchart)
will lead to the sought result if followed correctly
●
Algorithms have a definite beginning and a
definite end, and a finite number of steps
●
An algorithm produces the same output
information given the same input information
11
Class Activity
●
Write the sequence of steps you used to arrive
at this venue
●
What would be the result if a different sequence
of tasks was followed?
●
Compare the number of your steps with those
of others
12
Why We Program?
●
We program to automate a task
●
Whenever we have a task that we can express
its algorithm, we write its program
●
Note that you do not need a computer to
execute an algorithm you can do it by hand
13
Example of a
Classic Algorithm
●
Given two numbers 12 and 15, how will you find
their Lowest Common Factor / Greatest
Common Divisor?
●
Euclid’s algorithm expresses the higher number
in terms of the sum of the product of the lower
number and a factor and a remainder
●
When your remainder is 0, the algorithm stops
14
Euclid’s Algorithm
●
Given two numbers a and b, find their Lowest
Common Factor
●
Before the algorithm starts, if a is less than the
first step is to swap the numbers
●
If we are given 1071 and 462, the first step of
the algorithm would be to assign a = 1071 and
b = 462
15
Euclid’s Algorithm
The algorithm can be written as a sequence of
equations as shown below:
16
Euclid’s Algorithm
●
The solution to the problem is shown below:
17
Observations
●
Finding the LCM of 2 numbers can be done by
hand
●
Using Euclid’s Algorithm gives us the ability to
program the solution to the LCM of 2 numbers
●
Programming will allow us code the algorithm
18
Why Learn
Programming?
●
Get a job programming
●
It teaches you to be a logical thinker
●
Computers are everywhere these days, so
there's a good chance you'll use computers at
work, at school, or at home — probably all
three
19
Why Python?
●
Easy to read and learn
●
Free and open source
●
Is a real language used in industry
●
Runs on many platforms
20
Interpreted vs
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. An example of this is Java
●
Interpreted Languages take one line of the program
at a time and convert it into machine code. They then
execute the code for that line
●
Python is an interpreted language
21
Setting Up Your
Development Environment
https://www.python.org
22
Setting Up Your
Development Environment
●
Download and install the appropriate version of
Python for your machine
●
Installation of the Python setup is simple and
straightforward
●
We will be using the Python environment for
this course
23
Launch the
Python Shell
24
Shell Launches
25
Interpreted Mode
●
The >>> are for the prompt which allow you run
the shell in Interpreted Mode
●
The Python shell can produce simple output
●
You can also use the Python shell as a
calculator
26
Interpreted Mode
28
Script Creation
29
New File Opens
30
Type in Code
31
Save Your Work
32
Save to Desktop
33
Opening a File
34
Homework
●
Read about Euclid’s algorithm
●
Write a script for a Python program to print out
your name
●
Research on how to write a Python program to
ask the user for their name and print out the
entered name back to them
35
Conclusion
●
We program to automate tasks
●
Python is a simple but powerful programming
language
●
You learn best by doing
36
37
Code of Conduct
●
Uphold the tenets of CodeLagos and conduct myself in a
manner that promotes the values and objectives of the program
●
Refrain from the use of tobacco, alcohol or non-prescription
controlled substances during the program
●
Follow the instructions of the program staff at all times
●
Turn off my phone or put them on silent mode while training is
on. You are not allowed to use your phones while the class is in
session
●
Show respect to my instructors, fellow scholars and any other
persons involved in this program
●
Not partake in side conversations or any other form of distraction
while training is going on
38
Code of Conduct
●
Avoid loitering during training sessions
●
Refrain from fighting, use of force or abusive
language
●
Not to litter the training facilities before, during and
after each session
●
Abide by all other rules of the program, including any
additional conduct rules or requirements set forth in
the program materials
●
Smoking is not allowed within the learning facilities
●
Alcohol is not allowed within the training facilities
39
Eligibility for Graduation
●
Attend a minimum of 85 percent of your classes
●
Complete all assignments and assessments
●
Be punctual to all training sessions
40
Review
●
At this point in time, you should understand
Euclid’s algorithm
●
You should have written your first Python
program
●
If you have any issues from the last class, now
is a good time to speak up
41
Class Overview
●
Introduction to Variables in Python
●
Assign data to a Variable
●
Receive input from a keyboard
●
Output the contents of a Variable
42
Introduction to
Variables in Python
●
From algebra
●
What is x?
43
Introduction to
Variables in Python
●
Variables are a means of storing data in Python
●
When you create a variable you reserve some
space in memory
●
Unlike other programming languages, Python
has no command for declaring a variable
●
A variable is created the moment you first
assign a value to it
●
It Python x = 5 would be declaring x to be a
variable
44
Data Types
Python has various data types that are used to
define the operations possible on the type of
variable
●
Numbers
●
String
●
List
●
Tuple
●
Dictionary
45
Variable Names
●
A variable name must start with a letter or the
underscore character
●
A variable name cannot start with a number
●
A variable name can only contain alpha-
numeric characters and underscores (A-z, 0-9,
and _ )
●
Variable names are case-sensitive (age, Age
and AGE are three different variables)
46
Assign data
to a Variable
●
Create a Python file called assign.py
47
Assign data
to a Variable
●
The output of assign.py is shown below:
●
Note the space automatically added by the Python
interpreter
●
x is a whole number
●
y is a decimal
●
z is a string
48
Receive input
from a keyboard
●
We will create a simple Python program called
name.py
●
This program will ask the user for their name
and display a welcome message
●
To do this, we shall use the input function
●
A function is a way to group a block of code to
carry out a task
49
Receive input
from a keyboard
●
The input function assigns the characters
received from the user to the name variable
●
The print function prints out the welcome
message
50
Output the
contents of a Variable
●
To output the contents of a variable, we use the
print function
●
As we have already seen, the print function
uses a space to separate values added to it
51
Area of a Square
●
Write a Python program to find the area of a
Square where the length of a side will be
entered by the user
●
To do this, we need an algorithm
●
We’ve already seen the Euclid algorithm, now
its time to create one of our own
52
Algorithm for the
Area of a Square
1)Prompt the user to enter the length of a side
2)Multiply the length by itself and assign it to a
variable called area
3)Display the area of the square to the user
53
Program for the
Area of a Square
54
Program for the
Area of a Square
●
For line 1 we assign the value entered by the
user to the variable length. We expect an
integer so we use the int function to convert the
characters to an integer
●
In line 2 we assign the square of the length to
the area variable
●
In line 3 we print out the area
55
Homework
1)Write a Python program to find the area of a
Triangle
2)Write a Python program to find the area of a
Circle. Use the inbuilt ᴨ value from Python and
output your answer in 2 decimal places
3)Create a Python program to convert from
degrees Celsius to Fahrenheit
56
Conclusion
●
Variables in Python are used to store values
●
Variables can be of various types
●
The input function in Python is used to receive
values from a user
●
A best practice for solving programming
problems is to write an algorithm before
programming
57