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

BasicCoding1.0 2

Uploaded by

Asuva Musari
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

BasicCoding1.0 2

Uploaded by

Asuva Musari
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 25

Mark Essien & Jennifer Essien

BASIC CODING

1.0For Ages 9 - 12
Lesson 1: Printing Text on the Screen
Print shows text on the screen. When you want something
shown, you enter the command “print” to display it.

print(“hello”)

hello
Entering this in the
program will print
hello on the screen

Try It!

Open https://repl.it in your browser, create


Step 1
new repl (select Python).

Type print(“hello”) in the editor


Step 2
(main.py). Press

Result: You will see hello on the screen.

More Exercises: (1) Try printing your name on the screen.

(2) Try printing other text on the screen.

Basic Coding, Book 1 1


Advanced Notes for Lesson 1
If you do not have online access to use repl.it, there are
alternatives you can use for coding

Python & Notepad++

Download and install Python 3 and Notepad++. Open the


command line and run python main.py to run the
program.

VSCode

To get a full fledged development environment, you can


download and install Visual Studio Code. Be careful, it’s a
very big file!

Any issues learning?

Join the Telegram group.


https://bit.ly/basiccoding1

Basic Coding, Book 1 2


Lesson 2: Variables
We want to read what a person types on the keyboard. You
can type this in your program:

x = input()

Anything the person types will be stored in x. X is called a


variable. Variables are the ‘memory’ of the computer. That is
how the computer can remember things.

x is 10 OK

One hour later...

What is x? You told me

earlier that

x is 10

Basic Coding, Book 1 3


Lesson 2: Try it on the Computer
x = input()

means that anything the user types should be

stored in x.
Try It!
Step 1 Open https://repl.it in your browser, create
new repl (select python).

Step 2 Type x = input()

Step 3 Type print(x) in a new line. Press

Result: The computer will wait for you to type


something. Once you type it and press enter, it will
display that same thing.

Basic Coding, Book 1 4


Lesson 3: Different Variable Names

y = input()

means that anything the user types should be

stored in the variable y.

Try changing the name of the variable from

Exercise
y to x and printing the input.

nameofperson = input()

means that anything the user types should be

stored in nameofperson.

1) Change the name of the variable to


Exercises
nameofperson2 in your code.

2) Enter different things in the input. Type your first


name, your last name and any other words you want.

Basic Coding, Book 1 5


Lesson 4: Two Statements in a Program

You can type two lines in a single program. You can have

one line printing something on the screen, and another line

asking for an input.

print(“What is your name?”)

personsname = input()

The program will ask you for your name,

afterwards, it will wait for you to type your

name, then store it in the variable personsname

Expand the program to print the name of the


Exercise
person.

Try asking for a person’s first name, last


Exercise
name and then tell them what their full

name is in this way:

Your full name is John Doe

Basic Coding, Book 1 6


Lesson 5: Multiple Variables in a Program
You can store things in two different variables in the same
program.

print(“What is your name?”)


firstname = input()


print(“Where are you from?”)

from = input()

print(“Your first name is “ + firstname)

print(“And you come from “ + from)

Result:

Your first name is Jones

And you come from Jonestown

Exercise Ask for five pieces of information from the


person and print them.
Basic Coding, Book 1 7
Lesson 6: Combining Variables

You can combine variables together in one line

name1 = input()

name2 = input()

print(“Your names are “ + name1 + “ and “ + name2)

Exercise Combine three different variables in one line

Advanced Exercises

To find the answers to some things in coding,


you will need to research on Google.

1) Read the person’s name and output it also. If you


meet an error, research on Google how to solve it

2) How can you combine print and input together into


one line? Research using Google.

Basic Coding, Book 1 8


Lesson 7: Advanced Variables
You can have variables with different names and output

them in different lines. You can output the same variable

multiple times.

name = input(“What is your name? Type and press enter:”)

age = input(“Enter your age: ”)

print(“Your name is “ + name + “ and age is “ + age)

print(“I repeat. Your age is “ + age + “, name is “ + name)

Exercises

1) Write a program that asks for two different pieces of


information. It should output the information in 5
different sentences.

2) Store something in a variable x. Store something


else in a variable y. Combine both variables into
another variable z. Output the resulting variable z.
Hint: You can use = and + to combine variables.

3) Use == to check if the value in a variable is what you


expect. Use Google to learn how to do this.

Search for: compare strings python reference

Basic Coding, Book 1 9


Lesson 8: Functions
Your code is getting very long, right? Well, you can put your
code into groups that do a specific thing. These groups are
called functions.

def do_something():

print(“Hello, my name is ”)

print(“John Student!”)

This is a function. It contains two lines of


code. If you call this function, it will run both
lines of code.

The name of the function is the word after the ‘def’, in this
case do_something. If you write do_something() in your
code, it will instantly call the function.

print(“I will now call a function”)

do_something()

Result:

I will now call a function

Hello, my name is

John Student

Try it!

Basic Coding, Book 1 10


Lesson 9: Multiple Functions

You can add multiple functions to your code and group


related lines of code all together. Here is an example:

def print_names_of_places():

print(“London”)

print(“Lagos”)

print(“Lima”)

def print_names_of_animals():

print(“Ladybird”)

print(“Lion”)

print(“Lizard”)

print(“I will call two functions”)

print_names_places()

print_names_of_animals()

Exercise

Write a program with 3 functions. The first function


should ask the name of the person using the ‘input’
command. The second should ask the age. The third
should ask for the Home Town of the person. Call all
three functions.

Basic Coding, Book 1 11


Lesson 10: Function Parameters
Imagine you wanted a function to print the name of
someone. How do you make the function know the name
you want it to print? Well, it turns out that you can send a
variable to a function.

def print_this_name(name1):

print(“I am going to print a name”)

print(“This is the name you sent: ” + name1)

print(“I will call a function now”)

x = “John”

print_this_name(x)

print(“I have finished calling the function”)

Run the code. Look at the output and try to understand how
it worked.

Exercise

1) Call two functions, both of which should have


function parameters.

2) Write a function that has two different

parameters. Use google to research how

to achieve this. Send user’s input as parameter.

Basic Coding, Book 1 12


Lesson 11: Multiple Function Parameters

You do not need to only send one parameter to a function.


You can send as many parameters to the function as you
want.

def print_full_name(first_name, middle_name, last_name):

print(“Your full name is: ”)

print(first_name)

print(middle_name)

print(last_name)

print(“I will call the function now”)

print_full_name(“John”, “Jane”, “Doe”)

Exercise

1) Write a function with 4 parameters and call it.

2) Write a function with five parameters. Inside this


function, call another function with 2 parameters.

3) Read 6 different input from the user, store them in


variables, and send the information to two different
functions, which should then output them on screen.

Basic Coding, Book 1 13


Lesson 12: Conditionals

You can test if a variable has a particular value using the if

statement.

x = input()

if x == “hello”:

print(“hi”)

if x == “hi”:

print(“hello”)

Exercises

1) Write a program that tests for symptoms a person


has and tells them the sickness that they have. Use
functions and the if statement.

2) Write a calculator. It should request two numbers


from the user, and then request the operation that
should be done, for example ‘plus’, ‘minus’, ‘divide’,
‘multiply’. Each operation should have its own function.

Basic Coding, Book 1 14


Lesson 13: Function Return Values

A function can return a value back to be processed. For


example, you could add two numbers together and return
the result as the function return value.

def add_these_text(x, y):

return x + y

print(“I am an adding machine”)

z = add_these_text(“Hello”, “World”)

print(“The answer is “ , z)

Notice all the colors? This is called syntax highlighting, and is


done by editors to allow you easily see the different parts of
the code.

Exercise

Add a new function that adds numbers and returns the


result.

Hint: To print a number, you may need to wrap the


number in the function called str. For example: str(z).

Basic Coding, Book 1 15


Lesson 14: Simple Data Types

Variables can be of different types. Some are numbers,


some are text. Computer programs do not treat them all
the same - they store the variable differently, depending on
the type inside. That is why you had to convert the number
to text using the str function in the previous exercise.

Numbers can be of type Integer


(whole numbers - for example 1,
Text is called a
2, 3), or Float - which is a
String data type. fractional number, for example
1.4 or 3.14

Exercise

Write a program which contains a function that takes a


variable x = “4532”, and multiplies it by two. It should
return the result. The correct answer is 9064, not
45324532. This result should then be printed out.

Basic Coding, Book 1 16


Lesson 15: Boolean Data Type
There is a special variable type called the Boolean. It has
two states - True or False. You use this data type to test if
something is true or not.

x = True

y = False

if x == True:

print(‘X is True’)

if x:

print(‘X is True’)

if y:

print(‘This text will not display’)

The boolean data type is very important because it allows


you to make decisions as to which code path to follow.

Exercise

Write a function that returns True or False, depending


on the user input.



Check if the result of the function is True or False, and


print out different text depending on what it is.

Basic Coding, Book 1 17


Lesson 16: Loops

What if you wanted the computer to repeat something 10


times? Instead of writing the same code 10 times, you can
use a loop.

Drop 3 bricks over here

It’s done this way:

for x in range(0, 3):

print(x)

Result:
The code will repeat itself three times,
0 1 2
each time increasing the value of x.

Exercise

Loop from 1 to 10, and call a function that multiplies x


by 2 in each loop.

Basic Coding, Book 1 18


Lesson 17: Breaking out of Loops

What happens if you are in a loop and you want to end the
loop? You use the break keyword.

for x in range(0, 20):

print(str(x))

if x == 10:

break

The loop is set up to run to 20, but the break will make it
end once it reaches 10.

Exercises

Write a program that asks the user for a password. If


the password is wrong, it should ask again. It keeps
asking till the answer is correct, in which case it exits
the program.

You can also use the while loop for this. Search on
Google for ‘python while loop’ to research about this
type of loop.

Basic Coding, Book 1 19


Lesson 18: Lists

What if you want to store a list of things, for example a list


of all cities in a country?

xlist = [“London”, “Mumbai”, “Lagos”]

x now contains a list of all those cities. You can loop over all
items in the list and output them.

for y in xlist:

print(y)

Try the above in your editor.

Basic Coding, Book 1 20


Lesson 19: Using Modules
Your programs are getting quite long now! It would be great
to put related functions into a single file. That is possible,
and such files are called modules.

Module with
Module that

password
reads user

checking input

You can use functions from one module in another module


by importing them.

import password

def check_pass():

s = input()
x = password.check_pass()

if s == “secret”:
if x == True:

return True
print(“Pass Correct”)

else:
else:

return False print(“Pass Wrong”)

password.py myapp.py

Basic Coding, Book 1 21


Lesson 20: Importing
You should split your program across several files. You can
put functions in any of the files. However, the functions will
not be available till you import the function.

import nameoffile

This will make all the functions in the file available. You
reference them by using nameoffile.function_name.

You can also import a single function in this way:

from nameoffile import function_name

This makes that function available in the file.

Exercise

Write a program that lists 10 countries of the world. To


access this list, you must enter a correct password. Use
two different files for this, and be sure to import some
functions.

Basic Coding, Book 1 22


Lesson 21: Using in-built Functions

Programming languages come with thousands of functions


that you can import and use.

Search for ‘Python Standard Library’ to see a huge

list of a lot of functions available for your use.

Exercise

Research and find out how to use an in-built function


to convert the text in a variable to all upper-case.
Output it.

You can always use Google to search for a library to solve a


particular task. Before you start coding your own version,
always check if the functionality is already available in a
library.

Exercise

Write a program that returns the square root of any


number provided to it. Be sure to import the math
module.

Basic Coding, Book 1 23


Final Lesson: Learn to learn

Coding is all about research. When you are trying to solve a

problem, you research on Google to find out a possible

solution for the problem. Without research you will often

not be able to solve the problem.

Last Exercise

Code an address book app. You can press L to list all

your contacts. It should be possible to filter by

alphabet and to press D to list details of a particular

contact.

Basic Coding, Book 1 24

You might also like