Professional Documents
Culture Documents
Lab Manual Csc101 Ict v2.0
Lab Manual Csc101 Ict v2.0
MANUAL
Course: CSC101-Introduction To ICT
Learning Procedure
1) Stage J (Journey inside-out the concept)
2) Stage a1 (Apply the learned)
3) Stage v (Verify the accuracy)
4) Stage a2 (Assess your work)
Lab # 01
Lab # 02
Lab # 03
Lab # 04
Lab Sessional 1
Lab # 06 Loops
Lab # 08 Functions
Lab # 09
Lab # 10
Lab Sessional 2
Lab # 11
Lab # 12
Lab # 13
Lab # 14
Terminal Examination
Statement Purpose:
This lab will give you practical implementation of different types of Conditional Statements (if-
else).
Activity Outcomes:
This lab teaches you the following topics:
Use of indentation
Use of simple if statement
Use of if-else statement
Use of nested-if statement
Instructor Note:
As pre-lab activity, read Chapter 12 from the book (Learning Python, Mark Lutz, 5th Edition (2013),
O'Reilly Media), and also as given by your theory instructor.
Introduction:
‘if’ statement is used to perform logical operation. In order to perform decision making, we need
to check certain condition(s) before processing. Python supports if statement for doing so.
There are various formats of if statement including if-else and if-elif.
if condition:
statement1
statement2
…
…
If the condition is true then the specified block will be executed. It is important to note that the
block is specified by the use of indentation. Python does not use a pair of curly bracket { } to
specify the block.
We can also write the else block associated with the if statement as below.
if condition:
statement1
statement2
…
…
else:
statement3
statement4
…
…
If we are required to test a number of conditions and want to execute one of the many blocks of
statements, then we can use if-elif-else statement as below.
if condition1
statement1
statement2
…
…
elif condition2:
Statement3
Statement4
…
…
elif condition3:
Statement5
Statement6
…
…
else:
Statement7
Statement8
CCS-101 –Lab Manual 4
2) Stage a1 (apply)
Lab Activities:
Activity 1:
Let us take an integer from user as input and check whether the given value is even or
not.
Solution:
A. Create a new Python file from Python Shell and type the following code.
B. Run the code by pressing F5.
Activity 2:
Let us modify the code to take an integer from user as input and check whether the
given value is even or odd. If the given value is not even then it means that it will be odd.
So here we need to use if-else statement an demonstrated below.
Solution:
A. Create a new Python file from Python Shell and type the following code.
B. Run the code by pressing F5.
Solution:
A. Create a new Python file from Python Shell and type the following code.
B. Run the code by pressing F5.
Activity 4:
Let us modify the above code in order to apply nested if structure. Sometimes we need
to use an if statement within the block of another if to find the solution of the problem.
The following code example illustrates that how nested if can be used in Python.
Solution:
A. Create a new Python file from Python Shell and type the following code.
B. Run the code by pressing F5.
Activity 2:
Write a Python code to accept temperature value from user (in centigrade) and display
an appropriate message as below.
4) Stage a2 (assess)
Assignment:
For this student will submit Lab Assignment before the deadline.
Statement Purpose:
This lab will give you practical implementation of different types of loops including for
loop, while loop and nested loops.
Activity Outcomes:
This lab teaches you the following topics:
while loop
for loop
Use of break statement
Nested loops
Instructor Note:
As pre-lab activity, read Chapter 13 from the book (Learning Python, Mark Lutz, 5th Edition
(2013), O'Reilly Media), and also as given by your theory instructor.
Introduction:
Loops are one of the basic structures of a programming language. Loops are performed
to repeat a step or steps for a certain number of times. Python offers two looping
statements called while and for. Both have their own uses and advantages.
while loop
The commonly used syntax of while loop is as below.
while condition:
statement1
statement2
statement3
…
The statements written after the while statements (indented block) are repeated as long
as the condition is true. The control will transfer to the statement written outside the
indented block when the condition becomes false. We can also break the continuation of
the loop by writing break statement within indented block as below.
while condition1:
statement1
statement2
if condition2:
break
statement3
…
In the above case, the while loop will be stopped if the result of condition2 is true.
While loop can also have an optional else part and is executed if break statement was
not executed. The syntax of while loop having else block is as below.
while condition:
statement1
statement2
statement3
…
else:
statement1
statement2
statement3
…
value1 refers to the initial value of the range. It will be considered 0 if not
mentioned.
Value2 refers to the final value of the range. It will be required to perform the
loop.
Value3 refers to the step value. It will be added (incremented) every time the
loop is repeated. It will considered 1 if skipped.
for loop has an optional else part as well. It works exactly as in while loop. It is
executed only if break statement is not executed. The syntax becomes as below.
There is also use of for loop to be used with lists. It will be covered in the next lab
session.
Solution:
C. Create a new Python file from Python Shell and type the following code.
D. Run the code by pressing F5.
Activity 2:
Repeat the above code using while loop.
Solution:
C. Create a new Python file from Python Shell and type the following code.
D. Run the code by pressing F5.
Solution:
C. Create a new Python file from Python Shell and type the following code.
D. Run the code by pressing F5.
Activity 4:
Write a Python code to keep accepting integer values from user until 0 is entered.
Display sum of the given values.
Solution:
C. Create a new Python file from Python Shell and type the following code.
D. Run the code by pressing F5.
Solution:
A. Create a new Python file from Python Shell and type the following code.
B. Run the code by pressing F5.
If we run the program again and enter 17 as input value then the output will be as
below.
Activity 2:
Write a Python code to display all the prime numbers between 100 to 200.
Activity 3:
Write a program that takes a number from user and calculate the factorial of that
number.
Activity 4:
Fibonacci series is that when you add the previous two numbers the next number is
formed. You have to start from 0 and 1.
E.g. 0+1=1 → 1+1=2 → 1+2=3 → 2+3=5 → 3+5=8 → 5+8=13
Steps: You have to take an input number that shows how many terms to be displayed.
Then use loops for displaying the Fibonacci series up to that term e.g. input no is =6 the
output should be
011235
Statement Purpose:
This lab will give you practical implementation of different types of sequences including
Lists and Tuples. Lists are the most commonly used data structure in Python. Tuples are
also very useful and widely used by Python programmers.
Activity Outcomes:
This lab teaches you the following topics:
Instructor Note:
As pre-lab activity, read Chapter 8 from the book (Learning Python, Mark Lutz, 5th Edition
(2013), O'Reilly Media), and also as given by your theory instructor.
Introduction:
Python offers different types of sequences but we will discuss two of them, lists and
tuples.
Lists
Lists are the most important type of sequence being used in Python. It is a collection of
same or different type of objects. These objects are separated by commas to distinguish
from each other enclosed in square brackets. The following activities show that how
lists are used in Python.
Tuples
Tuples are almost same as Lists but the main difference them is that objects defined in
tuples cannot be changed while they can be changed in lists. A minor difference is that
lists are enclosed in square brackets while tuples are enclosed parenthesis (round
brackets). The following activities show that how tuples are used in Python.
Solution:
E. Create a new Python file from Python Shell and type the following code.
F. Run the code by pressing F5.
Activity 2:
Repeat the above code by accepting 5 integer values from user. Store these values in a
list and display the sum of given values.
Solution:
E. Create a new Python file from Python Shell and type the following code.
F. Run the code by pressing F5.
Activity 3:
Accept 5 integer values from user. Store these values in a list and display the list in
ascending order.
Solution:
E. Create a new Python file from Python Shell and type the following code.
F. Run the code by pressing F5.
Solution:
E. Create a new Python file from Python Shell and type the following code.
F. Run the code by pressing F5.
Activity 5:
Write a Python code to accept a list from user and find a required element in it.
Solution:
C. Create a new Python file from Python Shell and type the following code.
D. Run the code by pressing F5.
If we run the program again and enter 55 to find in the list then the output will be as
below.
Activity 6:
Write a Python code to accept a tuple from user and display the maximum and
minimum value of the tuple.
Solution:
A. Create a new Python file from Python Shell and type the following code.
B. Run the code by pressing F5.
Activity 2:
Repeat the above activity to find the smallest and largest element of the list. (Suppose
all the elements are integer values)
Activity 3:
Repeat the above activity to find the index of the smallest and largest element of the list.
(Suppose all the elements are integer values)
Statement Purpose:
This lab will give you practical implementation of different types of user-defined
functions.
Activity Outcomes:
This lab teaches you the following topics:
Instructor Note:
As pre-lab activity, read Chapters 16,17,18 from the book (Learning Python, Mark Lutz, 5th
Edition (2013), O'Reilly Media), and also as given by your theory instructor.
Introduction:
def function_name(list_of_parameters):
statement1
statement2
statement3
…
return value
The list of parameters is optional if a function is not accepting any value but is usually
required while defining a function. Similarly, return statement is optional but is
required if a function returns a value.
Once a function is defined then it can be called by using its name and providing values to
the parameters. Calling a function in Python is same as in other programming
languages.
The following activities demonstrate that how functions are defined and called in
Python.
Solution:
G. Create a new Python file from Python Shell and type the following code.
H. Run the code by pressing F5.
Activity 2:
Write a function to accept 2 integer values from user and return their sum.
Solution:
G. Create a new Python file from Python Shell and type the following code.
H. Run the code by pressing F5.
Solution:
G. Create a new Python file from Python Shell and type the following code.
H. Run the code by pressing F5.
Activity 4:
Define a function to accept a list of integer values and return the sum of it.
Solution:
G. Create a new Python file from Python Shell and type the following code.
H. Run the code by pressing F5.
Activity 5:
Write a Python code to accept a list of integers and sort it in descending order.
Solution:
E. Create a new Python file from Python Shell and type the following code.
F. Run the code by pressing F5.
Activity 2:
Write a function to accept two lists of integer values and return the largest value.
Activity 3:
Write a function to accept two integer values and swap them with each other.