Ict 5 and 6
Ict 5 and 6
5
Learning Outcomes
At the end of this unit, students will be able to:
Unit Overview
To ensure the safety and security of computer systems, every system user should
take preventative actions including software preventive measures. Software
preventive maintenance includes upgrading and installing software updates for
security and reliability purposes. With a strong software preventative maintenance
activity, time and money can be saved on one hand and failures can be reduced
on the other. In this unit, you will learn about the installation and uninstallation
of software, techniques used in software preventive maintenance, and basic
maintenance tools that can be applied in our day-to- task routines .
Notes
In this unit, most hands-on activities are prepared based on windows
based operating systems because of its user base and graphical based
ease to work functionality to meet the required competency. But other
operating systems can also function mentioned list of activities by
following respective instructions for each operating system. It is also
important to note that, there might be slight steps and functionality
differences even within same operating system versions. Students are
advised to refer the required steps and requirements based on types
of operating system and current software versions installed on the
computer devices.
Brainstorming 5.1
Do you think the number and types of installed applications will have
an effect on the speed and efficiency of your computing devices?
Users are able to perform some truly wonderful things with their computers and
other digital devices. All of these activities including reading books, digital photo
editing, using online social media sites, playing computer games, video streaming
etc. are made possible by various kinds of software’s. You can use computers or
other handy held smart devices for even more activities by installing the required
applications thanks to the new software apps that developers are constantly
developing. Software can also be uninstalled when required following the proper
steps and instructions.
You can purchase a software program from software stores or galleries, download
it from the source, it can be shipped to you. Software comes on CD, DVD, or USB
flash disks. It is also common to download application software’s for smart devices
(Smart Phones, Smart Watches, Personal Digital Assistances, Smart TV and more)
from software’s stores like Apple (App) store, Google play store, Amazon App
store, Samsung Galaxy Apps, Huawei App store etc. In all cases, please make sure
to download or receive all software’s from reliable and trusted sources only to
protect your machines from malicious software’s.
Links
Refer Unit 5 of the Grade 10 IT Textbook Information and Computer Security
to revise concepts related to protecting computing machines from malicious
software’s.
Step 1: Insert the installation CD /DVD into the PC’s optical drive or insert the
USB drive on the USB port.
The installation program runs automatically when you insert the disc (See Figure
5.1), or, you see the AutoPlay dialog box, from which you can choose the Install
or Setup command. If the installation program does not run automatically, follow
these sub-steps :
• Open the Computer window
Read the information carefully; sometimes they slip something important in there.
You may also be hit with a User Account Control (UAC) warning here. If so, type
the administrator password or click the Yes or Continue button to proceed.
Users have to agree to abide by the software license. The software may ask for a
serial number. It can be found somewhere inside the box, in the manual, or on a
piece of paper or on shipment email if it is purchased from online stores. If you are
asked to, replace one disc with another. This process may go on for some time. You
may also need to identify and locate the place to install the files in.
Step 4: Wait for the Installation program to end. You see a message telling you
whether the installation is successful or not.
Notes
The above steps are for Windows operating systems; other operating
systems including Unix/ Linux, Mac etc. have their procedure that you
need to follow while installing software.
Worldwide, mobile devices has grown significantly, creating new paths for
communication for individuals. Its acceptance has allowed increased access to
public information and fundamental services to people irrespective of time and
location powered by numerous apps and gadgets. This requires the right skill and
attitude to select, install and use application software’s.
It is so important to find the right source before downloading any apps on our mobile
devices (Android, Apple or other devices). You can download no-charge and paid
apps from application stores based on your device operating systems. You can also
download apps form other unknown sources on the internet when necessary. Please
use an internet and browse the appropriate app stores to download apps to mobile
devices (android / non android based mobile devices ) and other smart devices.
Notes
Your phone and personal information may be subject to damage if you
download apps from unknown sources. This includes the possibility of
data loss or damage to your phone, as well as the obvious possibility of
damage or hacking to your personal information. Review your phone’s
security settings, which scan for potentially harmful apps, alert you
when one is found, and delete the program if necessary.
Activity 5.1
1. What are major issues that should be checked before installing
software’s on our computers or other mobile devices?
o Click the link Uninstall a Program, found beneath the Programs heading.
o The Programs and Features window appears. It lists all software installed
on your PC.
You can uninstall or delete apps you have installed on your mobile phone. You
can also disable system apps that came with your phone.
Activity 5.2
1. Discusses the moments and reasons you plan to uninstall computer
applications based on your previous experience.
b. List steps you followed and is there any difference from what
is stated above? What do you think are the reasons for these
differences?
Brainstorming 5.2
What would you do if you opened your computer and discovered that
you lost all of your files?
In the case of computer systems, the term troubleshoot covers both hardware and
software issues. Hardware troubleshooting includes identifying and fixing the
problems when the hardware devices like mice, monitors, printers, modems, etc.
are malfunction. Software
troubleshooting includes identifying
and fixing problems encountered
while working with the operating
system and application softwares. It
also includes protecting our computer
systems from malicious damages
including viruses and worms. Figure 5.3: Hardware Troubleshooting
Activity 5.2
1. Assess the most common software problems caused by viruses and
worms and present your findings to the class.
Even though there are constantly new software and IT issues emerging, the bet-
ter you understand these issues, the simpler it will be for you to fix them. Office
systems can be slowed down by corrupted software and incorrect software, which
can cause delays, general inefficiencies, and a lack of productivity. Let us see few
common software problems with their possible troubleshooting techniques.
Possible Solutions: The first step to try is to close and reopen the application. If
the problem persists, update the application. To do this, click the Help menu and
look for an option to check for Updates. If you don’t find this option, another idea
is to run an online search for application updates (See Figure 5.5).
your keyboard to open the Task Manager (See Figure 5.6) under the Processes tab,
click the frozen application and click the End Task button. If you are unable to
force quit an application, restarting your computer will close all open apps.
Possible Solutions: You may have malware running in the background that is
slowing things down which needs a virus scanner with anti-virus or anti malware
software’s. Beside this, your computer may be running out of hard drive space
which needs deleting any files or programs you do not need. If you are using a PC,
you can run Disk Defragmenter and see the available disk spaces and fragment the
disk space. You will learn how to disk defragment later in this unit.
Possible Solutions: First, check the volume level. Make sure the sound is on and
the volume is up, then click the audio button. It’s also a good idea to check the
controls on the audio player. There are many players that include separate audio
controls for audio and video. Ensure that the player’s volume is set to the highest
setting and that the sound is switched on. Check that external speakers are plugged
in, switched on, and connected to the appropriate audio port or a USB port if the
issue persists (See Figure 5.7). Make sure the wireless headphones are successfully
paired and connected in this scenario. Sound drivers’ difficulties can also be related
to the other prevalent causes. The driver version you have should be shown in
device management. Next, carry out a manual update push. When you’ve found the
audio driver, right-click it and choose Update Driver if it hasn’t already been
updated; if not, acquire the most recent sound driver for the current operating
system version from reputable sources.
Activity 5.3
• Identify possible solutions for computers with black screen
Tip
If you have not been able to solve your challenge, you may need to ask for assistance
from others. Using the Internet is recommended as a starting point. It is likely that
other people have likely encountered similar issues, and solutions are frequently
shared online. Also, if you have a friend or family member who is knowledgeable
about computers, he or she may assist you, so you do not afraid to ask.
Generally, any issues can arise anytime and therefore you should know how to
avoid any problem from happening as part of preventive maintenance . But when
happened, knowing where to start troubleshooting can save you lots of time.
Most problems are software problems. Some are definitely hardware problems.
And some can be caused by one or the other. Sometimes, our software problem
can be easily resolved by running a repair utility or uninstalling and reinstalling.
It is always appreciated to know the proper troubleshooting skills to save out
time and resources. Internet can be a good resource to see similar problems and
proposed solutions for most problems easily to start with. This will also help you
to acquire better digital literacy skill which is one of the fundamental 21st skill
set for all professionals at all disciplines. Other problems may need professional
experts support to fix and maintain problems, so it is recommended to consult those
professionals and technicians based on the level of the problem and your ability to
fix it by your own.
Brainstorming 5.3
• Windows update
Over time, your disk may be fragmented. By fragmented disk, we mean files are
broken and stored in unmanaged space of the disk and hence the computer becomes
slow to bring the fragments together in order to open the files.
Disk Defragmenter is a system tool used to rearrange the files on the disk.
Defragmentation reduces file system fragmentation and, increases the efficiency
of data retrieval and thereby improves the overall performance of the computer. It
rearranges all the files so that each file is stored in one sequence (See Figure 5.8).
Notes
Type ‘Disk Cleanup’ on your window search box to access it.
Notes
Type ‘Window Update’ on your window search box to access it.
There are also additional techniques to maintain the computer
Check disk
Notes
Type ‘Disk Checkup’ on your window search box to access it.
Sometimes there are programs that you do not want to run anymore or programs
that get halted due to some problems. Although the best way to close a program is
to choose the ‘Close’ or ‘Exit’ command from the ‘File’ menu but there is another
way of closing it.
Activity 5.4
• Practice the windows operating system’s built-in system tools
including disk defragmentation, disk cleaning up, and cleaning
unwanted programs in your computer laboratory activities.
System restore is a system tool that is used to create a new restore point or to restore
the system to a previous restore point. It makes a secure copy of the system settings
when it is functioning normally, which is useful in case the system malfunctions.
Whenever you install a new software, it is always advisable to save the setting of
the system so that at a later stage if the system malfunctions, then that saved copy
of the system, can be restored. It helps in getting the system up and running for
use. To make a restore point, choose the System Restore from the Start->Programs-
>Accessories->System Tools->System Restore. To restore the system to a previous,
restore point and, follow the same steps, but just choose to restore rather than make
a new restore point.
Problem Solution
Slow Computer Disk Cleanup
Disk Defragmentation
Close programs
System restores
Check disk
Activity 5.5
• Mention major system tools that are used to speed up computer sys-
tems and explain their benefits.
Brainstorming 5.4
Two or more computers connected together to share resources (such printers and
scanners), exchange files, or enable electronic communications make up a network.
A network’s connections to its computers can be made by cables, phone lines, radio
waves, satellites, or infrared laser beams.
The collection of techniques and procedures used to locate, analyze, and fix issues
in a computer network is known as network troubleshooting. Network engineers
and technicians use a logical procedure to fix issues with networks and enhance
network performance.
When you run a network or while working in any system, there are always chances
of failure in the smooth operation owing to technical, physical, or any other faults.
For the uninterrupted running of the system, you need to resolve these raised issues
as soon as possible. Therefore, you need to detect the cause of the problem first
and then fix it. The process of detection, minimization, and resolving the faults that
arise in the network while performing the various day-to-day activities is known as
network troubleshooting.
Notes
Network problems happen when something disrupts the connection
between your computer and the content you are trying to access.
Out of Reach Issue: with wireless networks, the wireless host may be too far from
the access point, or there could be an obstructing object between the access
point and the wireless host.
Configuration Issue: Due to a wrong configuration such as; routing problem, and
other configuration issues, network fault may arise and the services can be
affected.
Traffic overload: If the link is over-utilized, then the capacity or traffic on a device
is more than its carrying capacity, and due to overload conditions, the
device can start behaving abnormally.
The first step of solving a network problem is finding the source of the problem.
Network problems are caused by a part of the network you control or outside
your control. We assume that you control your local network and do not control
anything beyond that. The way you solve the problem depends on whether or
not you control the failing part of the network. You can solve the local network
problems by yourself because you control the network. Outside network problems
require help from whoever runs that network.
Activity 5.6
Write the meaning of local network and outside network
Network troubleshooting is a repeatable process, which means that you can break
it down into clear steps that anyone can follow (See Figure 5.17).
Once you have finished gathering all the information that you can about the network
issue or issues, it is time to develop a working scenario. Sometimes a network
outage occurs because someone tripped on a wire or some other simple problem.
• Test the Scenario
Using the tools at your disposal, test your scenario. If your scenario is that the
network router is defective, try replacing it with another router to see if that fixes
the issue.
Notes
At this stage, it is important to remember that proving your own
theories wrong does not mean that you have failed. Instead, you return
to step two, develop a new scenario, and then find a way to test that
one.
• Plan of Action
Come up with a plan of action to address the problem. Sometimes your plan can
include just one step. For example, restart the router. In other cases, your plan can
be more complex and take longer, For instance, when you need to order a new part
or roll a piece of software back to a previous version on multiple users’ computers,
you need more steps.
155 Information Technology radeG12 ~ Student Textbook
Ethiofetena.com Ethiopian No 1 Educational Website
UNIT-5 Maintenance and Troubleshooting
• Implement the Solution
Now you have a plan for fixing the network, it’s time to implement it. There are
some solutions that you may do by yourself, while others may require cooperation
from other network experts or users.
• Verify System Functionality
Make sure that the issue in question has been resolved, but there is also on the
lookout for other issues that may have arisen from the changes that you have made
to the network.
• Document the Issue
Make sure to document each stage of troubleshooting the problem, including the
symptoms that appeared on the network, the scenario you developed, your strategy
for testing the scenario, and the solution you have come up with to solve the issue.
Even if you do not reference this documentation, it may be helpful to other network
technicians, students, or users at your school, home, or organization in the future
and could help to shorten network downtime.
The following commands can be entered into the command prompt one at a time to
reveal specific information about the network status:
ipconfig — A Windows Transmission
Control Protocol /Internet Protocol
(TCP/IP) utility that verifies network
settings and connections. It can tell
you a host’s IP address, subnet mask,
and default gateway, alongside other
important network information. Figure 5.19: ipconfig command
ping — A TCP/IP utility that transmits a datagram to another host, specified in the
command. If the network is functioning
properly, the receiving host returns the
datagram. See the fig 5.20 which shows
ping the default gateway which is
192.168.0.1 in this case. Check your
own default gateway and apply ping
Figure 5.20: ping command
command to see the results.
tracert —A TCP/IP utility that determines
the route data takes to get to a particular
destination. This tool can help you to
determine where you are losing packets
in the network, or helping to identify
problems.
Figure 5.21: tracert command
nslookup — A domain Name System (DNS) utility that displays the Internet
Protocol (IP) address of a hostname or vice
versa. This tool is useful for identifying
problems involving DNS name resolution.
See Fig 5.22 which shows google dns ip
address (8.8.8.8) with nslookup command.
Figure 5.22: nslookup command
netstat — A utility that shows the status of each active network connection. This
tool is useful for finding out what services are running on a particular system.
Notes
In practice, there are many command line tools used for network
troubleshooting for different operating systems including the windows
operating system. You can search online to get, learn and apply different
commands. Those mentioned commands for windows operating
system, are subject to change when you need to apply to another
operating systems like that of Linux/ Unix or Macintosh. Please browse
the equivalent commands based on device own operating system if
any. .
Activity 5.7
Practice the most common network troubleshooting command tools
including; ‘ipconfig’, ‘ping’, ‘tracert’, ‘netstat’ and ‘nslook’ up in the
windows operating system, but do not be limited to these tools.
Notes
You can get your device default gateway from your ipconfig command
practice using cmd. Please also note that the IP address mentioned
above can be changed due to server address changes by their respective
service providers.
Cable Testers — A cable tester (sometimes called Figure 5.23: Wire Crimper
a line tester) is a tool that verifies if a signal is
transmitted by a given cable. You can use one to
find out whether the cables in your network are
functioning properly when diagnosing connectivity
issues (See Figure 5.24).
Punch Down Tool — A punch-down tool is used Figure 5.24: UTP cable tester
in a wiring closet to connect cable wires directly
to a patch panel or punch-down block. This tool
makes it easier to connect wires than it would be to
do it by hand (See Figure 5.25).
Unit Summary
In this unit, you have learnt about:
UNIT FUNDAMENTALS OF
PROGRAMMING
6
Learning Outcomes
At the end of this unit, students will be able to:
In this unit, additional language constructs and concepts that can help you
develop a program in Python are introduced. First, Python for program flow
controls, conditionals, and loop statements are discussed followed by comments,
which enable you to provide some text description about your program. Finally,
interpreter and debugging the Python program are discussed.
Brainstorming 6.1
A program flow control is the order in which the program’s code executes. In
Python, the flow control of the program is regulated by the implementation of three
types of programming language constructs or program logic, namely sequential,
branching, and loop. The sequential flow control consists of a simple list of
statements that are to be executed in the order given. Everything you learnt about
declaring variables, printing output, and reading input from the keyboard in Grade
11 are sequential statements. In this unit, the implementation of the remaining two
language constructs: conditional and loop flow controls are focused.
Activity 6.1
• Discuss the three types of programming language constructs and
connect them with real-world problems.
Brainstorming 6.2
In some cases, you may need a program to choose one out of two or more alternatives,
depending on the input given. For example, suppose you design a program that
determines whether the given number is even or odd. An even number is a number
divisible by two without a remainder, whereas an odd number is divisible by two
with a reminder. We can describe the problem with the following mathematical
equation which is called expression in python.
• num % 2 = 0 num is Even, for num any integer number.
• num % 2 ≠ 0 num is Odd, for num any integer number.
A given integer number is either even or odd (i.e. two possible options), but this
can be decided based on the remainder after dividing the number by two. Python
provides conditional or branching statement to implement such solutions. Before
starting conditional statements, it is important to learn conditional expressions in
Python.
Conditional Expression
The comparisons in Table 6.1 are also called conditional expressions that correspond
to the Boolean values - true or false. For example, 3==4 results in false (as 3 is not
equal to 4). Note that operators used in python’s expressions are slightly different
from operators used in Mathematics (e.g. == vs. =, != vs. ≠ ). Python conditionals
can be used in several ways, most commonly in ‘if statements’ and loop statements.
In Python, a conditional statement is written using the if keyword. Like many other
programming languages, Python provides various versions of branching statements
that can be implemented using the if clause: simple if statement, if..else statement,
and if..elif…else statement.
If your input is 1, the expression age>0 (i.e. 1>0) is true. Therefore, the two
indented statements are executed, and they display their contents on the screen.
However, if the input is -1, the expression age>0 (i.e. -1>0) is false. Therefore, the
program ignores the indented statement.
Notes
The expression (if age>0:) must end with colon(:). Missing colon at
the end of expression is a syntax error.
Activity 6.3
• Based on the example presented under the simple if statement above:
a. What is the output if you write age 15?
b. What is the output if you write age 0? Discuss in pair.
c. What is the output of if the second print statement is unindented?
Discuss in pair.
Notes
You can have any statement before or after the ‘if statement’ which is
not considered as part of the ‘if statement’ and is executed as normal.
However, the ‘if expression’ must follow at least one statement to be
executed, otherwise it is a syntax error. See Figure 6.2 for syntactically
correct and incorrect if statements.
No error, output
Activity 6.4
1. Modify age in the example above to check if the age is greater than 18, and
then print ‘You are Adult’
2. The following code has an error. Which part of the code does cause the error?
3. If you type -1 and 1, what is the output of the following program? What do you
noticed about the first and the last statements?
As the value of age is 18, the program outputs the first print statement (See in the
example below that error: reference source not found).
Output:
The ‘if...else statement’ can make a choice between two alternative actions. Thus,
the even/odd problem discussed earlier can be accomplished with the following
python statement:
Syntax in a python program is a set of rules that defines how a python program is
written and interpreted. For instance, the expression of an ‘if statement’ must be
followed by a colon (:). Some programming languages require expressions to be
enclosed in parenthesis, but in python it is optional. So in python, “if num % 2 ==
0:” or “if (num % 2 == 0):”, this complies to Python’s syntax rule.
Python uses indentation to indicate a block of code. For example, if you ignore the
indentation in the example ‘if statement’ above, and begin the yes_statements or
the no_statements without indentation, the code generates an error. Of course, the
number of spaces is up to you as a programmer, but it has to be at least one.
Notes
Use the same number of spaces in the same block of code; otherwise,
Python gives you an error (See Figure 6.5, the last if statement). These
are important concepts when you write control statements.
It has no error; it is
a properly indented
block.
It has an error
because of, an
inconsistent
indentation in the
same block.
Activity 6.5
1. Based on the code in Figure 6.4,
2. A group activity. Write a Python program that read two numbers from
the keyboard and compute division of the first number by the second.
Apply appropriate if statement to check that the second number is differ-
ent from zero, otherwise print division by zero is not allowed. Do in pair
if...elif…else statement: Assume a problem that could have many options to
choose from or require several conditions. For example, you want to develop a
program that will print ‘Excellent, Very Good, Good, Satisfactory, or Fail’ based
on the student mark. In such situations, Python allows you to add any number of
alternatives using an elif clause after an if and before an else clause.
The elif is a keyword in Python to say “if the previous condition(s) are not true,
then try this condition”. The else keyword catches anything that is not caught by
all the preceding conditions. The general syntax for ‘if…elif…else’ is given in
if expression1:
statement1
statement2
...
elif expression2:
statement3
statement4
...
elif expression3:
statement5
statement6
....................
else:
statement7
statement8
...
Figure 6. 6 If…elif…else Syntax in Python
The interpretation of the above syntax is that first, each expression is evaluated
one after the other, and if the expression is found to be true, all statements in that
specific block are executed. Otherwise, if none of the expressions before the last
‘else’ statements are true, the statements under ‘else’ are executed.
executed (See the output in Figure 6.7 under the ‘Output’ column).
Activity 6.6
1. Replace price =49.5 by the message = “Hello Student”, and the variable
price in all expressions by the message in Figure 6.8 above. Then,
discuss the output of the code.
¾¾The program should prompt the user to enter her/his average mark in the
last or the current semester and then print excellent, very good, good,
satisfactory or fail based on the evaluation of the mark entered.
¾¾What necessary to write the program code?
¾¾The program asks the user to enter one of the four arithmetic symbols.
Then:
• It asks the user to enter two integer numbers.
• Based on the operator and numbers entered by the user, calculate
and display the result.
Using ‘and’ and ‘or’ with if…statement: The ‘and’, and ‘or’ keywords are
also used with ‘if...statement’ in Python. The ‘and’ and ‘or’ keywords are logical
operators and are used to combine conditional statements.
Output:
Figure 6. 9 and operator in ‘if statement’ - Code (left) and Output (right)
176 Information Technology radeG12 ~ Student Textbook
Ethiofetena.com Ethiopian No 1 Educational Website
UNIT-6 Fundamentals of Programming
Notes
Any statement that is preceded by the hash symbol (#) is a comment
and ignored by the Python interpreter. You will learn comments in
Section 6.2.
Activity 6.7
1. Discuss the evaluation of the expression ‘isBodyTemperatureHigh’ and
‘isCoughing’, if the value is True and False (See Figure 6.9 above).
Using ‘or’ operator with if… statement: The ‘or’ operator in Figure 6.10 is used
to combine conditional statements. The three conditional statements compare the
value of mySubject with ‘Physics’, ‘Chemistry’, and ‘Geograpy’. The evaluation
of the ‘if’ expression is True if one of these three conditions is satisfied. Otherwise,
the expression evaluates to False, and in that case, it executes the statement under
the else clause.
Notes
The ‘subject’ in Figure 6.10 is declared as array of string. An array is
a variable used to store multiple values in one single variable. In the
example, ‘subject’ stores four subject names or values.
Activity 6.8
• What is the output of the program in Figure 6.10?
Brainstorming 6.3
Most of real-world problems include some action that is repeated several number of
times. For example, consider the program you designed in (Activity 6.6 Question
2) that determines the student’s mark as “Excellent”, “Very Good”, etc. If you
have 50 students in a class, then a more complete program would repeat this status
determination 50 times (i.e. once for each student in the class). A program is often
used to automate such repetitive tasks. A portion of program code that repeats a
statement or group of statements in programming is called loop.
Loops are set of statements that run repeatedly for a fixed number of times, or until
a condition is satisfied. Loop statements control a block of code to be executed
iteratively or until a certain condition fails. Loops are a useful and frequently used
feature in all modern programming languages. Python provides several language
features to make iteration/looping easier. There are two types of loops that are built
into Python: for loops and while loops. In the following section, implementation of
‘for loop’ is discussed and then followed by ‘while loop’ in python.
for loops: The for loop in python is used to iterate over a sequence. For loop in
combination with the Python’s range() function is used for counting in all kinds of
ways (See for loop with range function at the end of this section). The for loop in
python differs a bit from other like C or Pascal. In Python for loop is used to iterate
over the items of any sequence including the python list, string, tuple, dictionary,
etc. It is also used to access elements from a container (e.g. list, string, tuple) using
a built-in function range(). The general syntax of for loop is as follows.
Syntax:
for variable_name in sequence :
statement_1
statement_2
[....]
Name Description
variable_name This represents a temporary variable that sets a new
value for each iteration of the loop.
Sequence Sequences are values that can be assigned to a
temporary variable (i.e. variable_name). Values are
provided using a list or a string or from the built-in
function range().
Statement_1, state- These represents a block of program statements.
ment_2 Python’s syntax requires this to be indented.
[..]
for loop with range() function: The range() function returns a list of consecutive
integers. The sequence of numbers starts from 0 by default, and counts by
incrementing 1(by default), and ends at a specified number. It is widely used count
controlled loops.
Notes
In the syntax below, the range() function takes one, two, or three
parameters. The last two parameters are optional.
Syntax Example
range(x) range(5)generate sequence 0 to 4
range(x,y) range(2,5)generate sequence 2 to 4
range(x,y,z) range(1,10,2)generate sequence 1,3,5,7,9
Figure 6. 12 Range Function Syntax with One, Two, and Three Parameters
Figure 6. 13 Implementation of for Loop with Range - Code (left) and Output (right)
Activity 6.9
1. The above program output is 0 to 4, not 5 (See Figure 6.13). Discuss
what the reason is with a partner.
2. Write a program that prompts the user to enter an integer number from
the keyboard and generate a sequence of numbers from 0 to user input.
Hint: use input statement to accept user input, convert the number
to a numeric type, and give as a parameter for the range function.
Activity 6.10
1. Write the output of the above program ( in Figure 6.14) in the space provided.
Activity 6.11
1. Write a program that generates a sequence of numbers from 10 to 100,
incrementing by 10.
2. Write a for loop to generate 1, 4, 7, 10, 13, 16, 19, and 22.
for loop in iterable object: Now let us see an example of a for loop in an iterable
object. Unlike the earlier example, the loop iterates while something is true. This
type of loop is called a condition controlled loop.
The for loop program in Figure 6.16 is condition controlled. The ‘regionalState’
is a sequence that contains a list of regional states. When the for loop is executed,
the first item (i.e. Afar) is assigned to the ‘region’. The ‘region’ is a temporary
variable that actually represents an element in the list. After this, the print statement
executes, and the process continues until we reach the end of the list (i.e. Sidama),
or while there is an element in the list. See the output in Figure 6.16 above on the
right hand side.
Activity 6.12
Write a for loop statement that displays the following list of fruits.
for loop can iterate through string. The string is an iterable object in python because
it contains a sequence of characters. Thus, applying for loop in a string allows us
to access the content character by character.
Activity 6.13
• Write for loop to get your name from the keyboard and print character by
character.
Hint: use input statement to read your name from the keyboard.
break Statement with for: The term break is a keyword in python. With
the break statement, you can stop the loop before it has looped through all the
items:
Python script Output on IDLE
Shell
Figure 6. 18 for Loop with break Statement – Code (left) and Output (right)
In the above program, the loop exits when the value of ‘language’ is ‘Python’.
Activity 6.14
1. Modify the program in Figure 6.17 (for loop in string) above to stop the loop if
the value of letter is white-space.
2. What is the output of Figure 6.18 if you replace the ‘if statement’ by if lan-
guage== “HTML”?
continue keyword with for loop: The term continue is a keyword in python.
With the continue statement, you can stop the current iteration of the loop, and it
continues with the next.
Figure 6.19 for loop with continue statement - Code (left) and Output (right)
Activity 6.15
• Modify the above statement to escape “Java” and “C++” using continue
keyword.
The while loop runs as long as the condition (expression) evaluates to True
and executes the program block (statement_1, statement_2 …). The expression
is checked every time at the beginning of the loop and the first time when the
expression evaluates to False, the loop stops without executing any remaining
statement(s).
The while loop above prints the value of count as long as the count is less than
5. The last statement (count += 1) is important to increment the value of count at
every iteration and eventually terminate the loop.
Notes
Don’t forget to increment count(i.e. count +=1 in Figure 6.21);
otherwise, the loop continues forever.
Activity 6.16
• Rewrite the problem described in Activity 6.11 using a ‘while’
statement.
break and continue keywords with while loop: With the break statement, you
can stop the loop even if the while condition is true. It causes the loop to quit even
before reaching the last iteration. The loop in the program below terminates when
the value of count is 5 (in the 5th iteration).
As you have learned in for loop above, the continue statement, causes the current
iteration to stop, and continues with the next.
The above program is supposed to iterate 9 times but prints 8 values by escaping
print when the value of count is equal to escape.
Activity 6.17
• If the user input is 8, what is the output of the above ( in Figure 6.23) program?
Brainstorming 6.4
Comments are descriptive texts that exist in program source code and are ignored
by a compiler and interpreter. Comments are not executable statements or part of
the program. Using comments, a program can make code more readable for other
developers as it provides some information or explanation about what each part
of a program is doing. Depending on the purpose of your program, comments can
serve as notes to yourself or reminders.
In the above example, the two comments (i.e. ones that start with #) are ignored
while executing the program.
Commenting part of the code: Comments can also be used to comment out parts
of the code that you do not want to execute while compiling or running a program.
The program in the following example has two fragments of code. The second
fragment of code (next to line pi = 3.14) is commented out to prevent that part of
the code from execution. IDLE provides features to comment out (uncomment)
parts of code in your source program.
To comment out
Click on Format
menu
Activity 6.18
1. Comment on the second part of the program in the above figure and run
the program. Then, write the output.
2. Uncomment the first part of the program in the above figure and run the
program. Then write the output.
Brainstorming 6.5
In this section, program writing and running are discussed in more detail.
Python is an interpreted programming language, not a compiled one although
compilation is a step in Python interpreter processes. Compilation is simply a
translation that generate bytecode that you will more in the next paragraph. The
interpreter is the program that is responsible for executing each line of statements
sequentially and runs the Python code or script which refers to a simple program
stored in a plain text file. You can think of the interpreter as a layer of a program
that works between your program and your computer hardware to get your code
running. Python has a wide range of interpreters including IDLE, CPython,
Jython, PyPy, and IronPython and online Python editors. Your Python source code
is always run by one of these interpreters depending on the Python installation
you use. Therefore, first, make sure that you have correctly installed one of these
interpreters on your computer.
Links
Read your Grade 11 IT Textbook for more on Python IDE
Activity 6.19
• Describe the steps involved in the python interpreter process.
Brainstorming 6.6
Once you write your program source code, you save the file and run the code to
test whether the desired or expected output is generated. The program sometimes
fails to run correctly because of a bug. A bug is an unexpected problem in your
program. Bugs can appear in many forms, and some are more difficult to fix than
others. Some bugs are tricky enough that you will not be able to catch them by just
reading through your program. Luckily, Python IDLE provides some basic tools
that help you debug your programs with ease. Debugging means, having complete
control over the program execution.
If you want to run your code with the built-in debugger in Python IDLE, you
need to turn to debug mode feature. To do so, select Debug → Debugger from the
Python IDLE menu bar. In the interpreter, you should see [DEBUG ON] appear
just before the prompt (>>>), which means the interpreter is ready and waiting (See
Figure 6.29 below).
When you run your Python file, the debugger window appears (See Figure 6.29
below):
Step: Press Step to see all the internal commands that python uses to execute the
current line and go to the next one.
Over: Press the Over option to see line-by-line execution of the program.
For our purposes, we can use ‘Go’ and ‘Step’ to trace and fix the bug in the following
program. Before running the program, set a breakpoint on a statement that causes
an error in your program.
A breakpoint is a line of code that you have selected as a place where the interpreter
should pause while running your code. To set a breakpoint, right-click on the line
of code that you wish
192 Information Technology radeG12 ~ Student Textbook
Ethiofetena.com Ethiopian No 1 Educational Website
UNIT-6 Fundamentals of Programming
to pause(See Figure 6.30 that Error: Reference source not found). This highlights
the line of code in yellow as a visual indication that a breakpoint is set. You can
set as many breakpoints in your program code as you like. To undo a breakpoint,
right-click the same line again and select Clear Breakpoint.
As shown below.
Activity 6.20
• The following code could generate error if the value for num2 is zero.
Trace and debug the error using python DEBUG control.
Unit Summary
In this unit, you have learnt about:
Key Terms
Program flow controls - A program flow control refers to the order of
execution of the program’s code. Python implement the flow control of the
program through the sequential statements, conditional statements and loop
statements.
Syntax in a python program - is a set of rules that define how a python
program is written and interpreted.
Indentation - Indentation refers to the spaces at the beginning of a code line.
Unlike other programming languages, python uses indentation to indicate a
block of code.
The number of spaces is up to you as a programmer, but it has to be at least
one.
Conditional statements -are statements in a program where there are points
at which the program decides at runtime whether some parts of the code
should or should not be executed.
UNIT-6 Fundamentals of Programming
Conditional statements -can be implemented by ‘if statement’. Such as
simple if, if…else, if…elif…else, nested…if
Key Terms
Iteration - This is a programming logic to automate repetitive tasks. Repeated
Program flow
execution of a setcontrols - A program
of statements flow
is called controlPython
iteration. refers provides
to the order of
various
execution
versions ofofforthestatements
program’sand code.
whilePython implement
statements the flowiteration.
to implement control of the
program through the sequential statements, conditional statements and loop
Continue -is a keyword which is used with for loop. With the continue
statements.
statement, you can stop the current iteration of the loop, and continue with
the next.in a python program - is a set of rules that define how a python
Syntax
program is written and interpreted.
Break - is a keyword which is used with for loop. With the break statement,
Indentation
you can stop -the Indentation
loop before refers tolooped
it has the spaces at theall
through beginning
theitems.of a code line.
Unlike other programming languages, python uses indentation to indicate a
Comments - are descriptive texts in program source code that are ignored by
block of code.
compilers and interpreters. Using comments, programs can make code more
The number
readable for of spacesasis itupprovides
humans to you assome
a programmer,
informationbut or itexplanation
has to be atabout
least
one.
what each part of a program is doing or about.
Conditional statements -are statements in a program where there are points
at which the program decides at runtime whether some parts of the code
196 InformatIon
should or should tnot
echnology
be executed. grade 12 ~ Student textbook
Interpreter
Interpreter isis aa special
special program
program thatthat executes
executes instructions
instructions written
written in
in aa
programming
programming language.
language. ItIt can
can either
either execute the
the source
source code
executeFundamentals directly
directly or
of Programming
code or
UNIT-6
translate
translate the
the source
source code
code inin aa first
first step
step into
into aa more
more efficient
efficient representation,
representation,
and
and execute
execute this
this code.
code.
A
A bug
bug is
is an
an unexpected
unexpected problem in in your program. Bugs
Bugs can
can appear in
in many
Interpreter is a specialproblem
program your program. instructions
that executes appear
written many
in a
forms,
forms, and
and some
some are
are more
more difficult
difficult to
to fix
fix than
than others.
others.
programming language. It can either execute the source code directly or
translate
A
A breakpoint
breakpoint is
is aa line
the source line of
code code
of in that
a first
code you
thatstep have
haveaidentified
you into as
as aa place
more efficient
identified where
where the
representation,
place the
interpreter
and executeshould
interpreter pause
pause while
this code.
should while running
running your
your code.
code.
A bug is an unexpected problem in your program. Bugs can appear in many
forms, and some are more difficult to fix than others.
A breakpoint is a line of code that you have identified as a place where the
interpreter should pause while running your code.