Programming
Form 2 Computer Studies
▪ By the end of the chapter or unit learners should be able to:
▪ Outline the purpose of program libraries.
▪ Identify and know how to use library functions in text-based programs.
Objectives
▪ Know how to develop text-based programs with conditional (selection)
statements.
▪ Understand and use rules using AND, OR and NOT to create logic within
algorithms.
▪ Know how to develop text-based programs which use rules involving AND, OR
and NOT.
What is programming?
▪ Programming is giving a set of instructions to a computer to
execute.
▪ Programming is the process of creating a set of instructions
Programming that tell a computer how to perform a specific task.
What is a programming language?
▪ It is software that is used in writing computer programs for
example Python.
What is a program library?
▪ A pre-written and pre-tested programs that can be called in another
program.
How do you use a program library in Python?
▪ You import the library and then call the functions.
Program What are some examples of program libraries in Python?
Libraries ▪ Random and math.
▪ Why are program libraries useful?
They save time in writing new functions. They are also pre-tested so
should already work.
Why are program libraries useful?
1. Program libraries save programmers time in writing and
testing newly created functions.
2. Libraries allow less experienced programmers to
Program include complex things that they may not yet be able to
Libraries create for themselves.
3. They are also pre-tested so should already work.
4. Using libraries in this way can simplify the whole
programming process.
Random
▪ It is used generate random events, select random choices or make
decisions, such as which move a computer should make during a game.
▪ Random numbers do not exist in computers, because there is always an
algorithm within the operating system to generate them.
▪ When you want to use the Random library in Python you need to import
Program the random library first like:
Libraries in import random
▪ Then you write your code of whatever you want to randomly generate, for
Python example:
randomNumber = random.randint(0, 100)
print(randomNumber)
▪ This will generate random numbers between 0 and 100 inclusive each time
the program is executed.
Random
In pairs do the following Activities
▪ Generate a random number between 1 and 10
▪ Generate a random number between 1 and 4,
output a different message depending on the
Program number selected
Libraries in ▪ Simulate the roll of a dice (between 1 and 6)
Python Extent your programs to:
▪ Generate a random number with a single
decimal digit between 0.1 and 1.0
▪ Generate a random number with double
decimal digit between 2.50 and 5.50
Math Function
▪ It allows you to perform mathematical tasks on numbers.
▪ For example:
▪ When calculating the are of a circle you can use math.pi instead of
the value of pi.
radius = 10
circleArea = math.pi * radius * radius
Program circleCircumference = math.pi * radius * 2
Library ▪ Where math.pi returns the value of pi.
▪ Where the number in the brackets is rounded up to the nearest
whole number (integer).
▪ roundUp = math.ceil(2.66)
▪ Where the number in the brackets is rounded down to the nearest
whole number (integer).
▪ roundDown = math.floor(2.66)
Conditional statement
▪ It is a question that has a true or false answer.
▪ The three component parts of a conditional statement are
the condition; the code to run if true; the code to run if
not true.
Conditional ▪ The symbol for a condition in a flowchart is a diamond.
statement ▪ The conditional operators that we can use are:
Selection ▪ <,
statement
▪ >,
▪ =,
▪ !=,
▪ <=,
▪ >=
Conditional statement structure
▪ Using singular if, as well as if else.
▪ The format of these is as follows:
▪ IF:
Conditional ▪ if condition:
statement
▪ code to run if true
Selection
statement ▪ IF else:
▪ if condition:
▪ code to run if true
▪ else:
▪ code to run if false
Conditional statement in Python
number = 10
if number == 10:
print("Yes")
Conditional else: NB: The indents are important in
Python.
statement print("No") Once the user has typed the colon and
pressed enter, the cursor should be
Selection auto-indented to the correct position
within the IDE.
statement first = 20
if first < 20:
print("Less than 20")
else:
print("20 or more")
Boolean data type
▪ It is a type of data that stores only two types of values
Boolean operators
▪ Boolean operators are words and symbols, such as AND
or NOT, that let you expand or narrow your search
Boolean data type parameters when using a database or search engine.
Boolean operators ▪ When you search using these operators, it is known as a
Boolean search.
▪ You can use Boolean operators such as AND, OR, and NOT
alongside keywords to create a Boolean string that will
refine your search to find the most relevant results and
sources.
Boolean Operators in Python
first = 10
second = 20
• The Boolean operators ‘and’ and
if first < 20 and second < 20:
‘or’ are written in lower case and
print("Both less than 20") need to have a condition on either
side.
Boolean data type if first < 20 or second < 20: • The Boolean operator ‘not’ needs
to be before a condition.
Boolean operators print("At least one is less than 20")
value = True
if not value:
print("False")
else:
print("True")
Boolean Operators in Python
▪ Example of a program that asks the user to guess what
number is stored in the computer. Output ‘Correct’ for a
correct guess or ‘Incorrect’ for an incorrect guess.
number = 100
Boolean data type guess = int(input("Guess what number I am thinking
Boolean operators of"))
if guess == 100:
print("Correct")
else:
print("Incorrect")
Boolean Operators in Python
▪ A program that asks the user to enter a
number between 1 and 10 and output
whether she did this successfully or not.
Boolean data typenumberInput = int(input("Enter a number between 1 and 10")
Boolean operatorsif numberInput >= 1 and numberInput <= 10:
print("Success")
else:
print("Incorrect")
Boolean Operators in Python
▪ A program that takes two numbers as
input from the user and output the
number which is larger.
first = int(input("Enter a number"))
Boolean data typesecond = int(input("Enter a number"))
Boolean operatorsif first > second:
print(first)
else:
print(second)
Activity: Amend the program so that it takes three numbers
and output the largest.
▪ A data type is data that is put into categories, so the program
knows what is expected e.g. Integer, Real and String
▪ Integer stores whole numbers
▪ Real stores decimal numbers
Other Data ▪ String stores characters that can include letters, symbols and
Types numbers that cannot be used mathematically.
- Integer ▪ An Integer is a whole number, a Real number is a decimal
- String number
- Real
▪ A string data type is one or more characters that can include
- Char letters, symbols and numbers that are not used in mathematics
calculations, such as telephone numbers or credit card numbers.
▪ Boolean data type is data that can be represented in two state as
‘true’ or ‘false’ because it can only be one of two values.
Other Data
Types
- Integer
- String
- Real
- Char
▪ Numbers that are not used in calculations, such as telephone
numbers or credit card numbers, will always be a string.
▪ Integer data types cannot start with a 0. Any leading 0s will be
removed. Therefore, any integer that needs to have leading 0s,
such as an ID number, will need to be stored as a string.
Other Data ▪ In the previous table, if the ID number of the book was stored as
Types an integer, it would be 1182738, which might be the ID number
- Integer of a different book.
- String ▪ Boolean data can be used as a ‘flag’ meaning that it is used to
- Real indicate if something has occurred, or is valid or correct. If the
- Char flag is:
▪ true then it is positive, meaning that it has occurred, it is valid or
it is correct
▪ false it is negative, meaning that it has not occurred, it is not
valid or it is incorrect.
▪ A program that asks the user to enter True or False
and outputs different messages depending on their
answer.
entered = input("Enter True or False")
Other Data if entered == "True":
Types result = True
- Integer
- String else:
- Real result = False
- Char if result == True:
print("Yes you entered True")
else:
print("Oh no, you did not enter True")
▪ A questionnaire to ask the user the questions and
store their answers.
Other Data firstName = input("Enter your first name")
Types age = int(input("Enter your age"))
- Integer pocketMoney = float(input("Enter the amount of pocket
- String money you get"))
- Real likeGames = input("Enter True if you like video games")
- Char if likeGames == "True":
likeGames = True
else:
likeGames = False
print("What data type is more appropriate for:")
answer1 = input("True or False")
if answer1 == "Boolean":
print("Correct")
else:
Other Data print("Wrong it is Boolean")
Types answer2 = input("1, 4, 6, 12")
- Integer if answer2 == "Integer":
- String print("Correct")
- Real else:
- Char print("Wrong they are integers")
▪ Activity
▪ Extend this activity using OR conditions to allow different spellings, for
example by checking if the user has entered "Integer" or "integer" or "int" or
"Int“ and also to include real and string data types.
▪ An iterative process is the process of writing
part of a program, testing it and editing it,
then adding to the program, testing it and
editing it, and then adding to it continually
until a final, working, program is produced.
Iterative ▪ Repeatedly editing and testing a program
process until it is complete.
▪ The program can be split it into smaller
parts, with those parts being worked on
individually. New features and parts can
then be added to the program throughout
the iterative process.
▪ Why do we need to test programs?
▪ Answer: to make sure that they work; to check
there are no errors; to check that they give suitable
responses to unexpected input
▪ How do we test that a program works?
Program ▪ Answer: we use the program, for example by
testing and entering data and pressing buttons, then check
whether it does what is expected to meet its criteria
test plans
▪ Why should you test a program using a range of data?
▪ Answer: It may work with one input but not any other
inputs. Testing with a range of data also checks
whether the program works in all reasonable cases.
▪ Why do we need to test programs?
▪ Answer: to make sure that they work; to check
there are no errors; to check that they give suitable
responses to unexpected input
▪ How do we test that a program works?
▪ Answer: we use the program, for example by
Program entering data and pressing buttons, then check
testing and whether it does what is expected to meet its criteria
test plans ▪ Why should you test a program using a range of data?
▪ Answer: It may work with one input but not any other
inputs. Testing with a range of data also checks
whether the program works in all reasonable cases.
▪ To test programs you need a test plan which could be
in form of Test table template.