Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 29

LAB

MANUAL
Course: CSC101-Introduction To ICT

Department of Computer Science

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)

COMSATS University Islamabad, Attock Campus


Table of Contents

Lab # Topics Covered Page #

Lab # 01

Lab # 02

Lab # 03

Lab # 04

Lab # 05 if-else structure

Lab Sessional 1

Lab # 06 Loops

Lab # 07 Lists and Tuples

Lab # 08 Functions

Lab # 09

Lab # 10

Lab Sessional 2

Lab # 11

Lab # 12

Lab # 13

Lab # 14

Terminal Examination

CCS-101 –Lab Manual 2


LAB # 05

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.

CCS-101 –Lab Manual 3


1) Stage J (Journey)

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.

The basic and shortest form of if statement is as below:

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.

You will get the following output.

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.

You will get the following output.

CCS-101 –Lab Manual 5


Activity 3:
Let us accept two integer values from user and compare which one is the larger value. If
both are equal values then we should display that both are equal values. In this activity
you will use if-elif-else structure of if statement. This is used when we need to check
more than one conditions to perform certain action.

Solution:
A. Create a new Python file from Python Shell and type the following code.
B. Run the code by pressing F5.

You will get the following output.

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.

You will get the following output.

CCS-101 –Lab Manual 6


3) Stage v (verify)
Home Activities:
Activity 1:
Write a Python code to accept marks of a student from 1-100 and display the grade
according to the following formula.

Grade F if marks are less than 50


Grade E if marks are between 50 to 60
Grade D if marks are between 61 to 70
Grade C if marks are between 71 to 80
Grade B if marks are between 81 to 90
Grade A if marks are between 91 to 100

Activity 2:
Write a Python code to accept temperature value from user (in centigrade) and display
an appropriate message as below.

FREEZING if temperature in less than 0


COLD if temperature is between 0 to 15
WARM if temperature is between 16 to 30
HOT if temperature is between 31 to 40
VERY HOT if temperature is greater than 40

4) Stage a2 (assess)
Assignment:
For this student will submit Lab Assignment before the deadline.

CCS-101 –Lab Manual 7


LAB # 06

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.

CCS-101 –Lab Manual 8


5) Stage J (Journey)

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.

This lab session covers both of the loop structures.

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

CCS-101 –Lab Manual 9


for loop
for loop can be used in a number of ways. One of its syntax is as below.

for var in range(value1,value2,value3):


statement1
statement2
statement3

Where …

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.

for var in range(value1,value2,value3):


statement1
statement2
statement3

else:
statement1
statement2
statement3

There is also use of for loop to be used with lists. It will be covered in the next lab
session.

CCS-101 –Lab Manual 10


6) Stage a1 (apply)
Lab Activities:
Activity 1:
Calculate the sum of all the values between 0-10 using for loop.

Solution:
C. Create a new Python file from Python Shell and type the following code.
D. Run the code by pressing F5.

You will get the following output.

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.

You will get the following output.

CCS-101 –Lab Manual 11


Activity 3:
Accept 5 integer values from user and display their sum.

Solution:
C. Create a new Python file from Python Shell and type the following code.
D. Run the code by pressing F5.

You will get the following output.

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.

You will get the following output.

CCS-101 –Lab Manual 12


Activity 5:
Write a Python code to accept an integer value from user and check that whether the
given value is prime number or not.

Solution:
A. Create a new Python file from Python Shell and type the following code.
B. Run the code by pressing F5.

You will get the following output.

If we run the program again and enter 17 as input value then the output will be as
below.

CCS-101 –Lab Manual 13


7) Stage v (verify)
Home Activities:
Activity 1:
Replace the for loop with while loop in the activity 5 discussed above.

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

So the series becomes


0 1 1 2 3 5 8 13 21 34 55 ……………………………………

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

CCS-101 –Lab Manual 14


8) Stage a2 (assess)
Assignment:
For this student will submit Lab Assignment before the deadline.

CCS-101 –Lab Manual 15


LAB # 07

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:

 How to use lists


 How to use tuples

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.

CCS-101 –Lab Manual 16


9) Stage J (Journey)

Introduction:

Python provides different types of data structures as sequences. In a sequence, there


are more than one values and each value has its own index. The first value will have an
index 0, the second value will have index 1 and so on. These indices are used to access a
particular value in the sequence.

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.

CCS-101 –Lab Manual 17


10) Stage a1 (apply)
Lab Activities:
Activity 1:
Accept 5 values from user and store them in a list. Display all the values (objects) of the
list.

Solution:
E. Create a new Python file from Python Shell and type the following code.
F. Run the code by pressing F5.

You will get the following output.

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.

CCS-101 –Lab Manual 18


You will get the following output.

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.

You will get the following output.

CCS-101 –Lab Manual 19


Activity 4:
Accept two lists from user and display their join.

Solution:
E. Create a new Python file from Python Shell and type the following code.
F. Run the code by pressing F5.

You will get the following output.

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.

CCS-101 –Lab Manual 20


You will get the following output.

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.

CCS-101 –Lab Manual 21


You will get the following output.

11) Stage v (verify)


Home Activities:
Activity 1:
Create two lists based on the user values. Merge both the lists and display in sorted
order.

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)

CCS-101 –Lab Manual 22


Activity 4:
Accept a list of 10 integer values from user and display the list in descending order.

12) Stage a2 (assess)


Assignment:
For this student will submit Lab Assignment before the deadline.

CCS-101 –Lab Manual 23


LAB # 08

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:

 How to define own functions


 How to use user-defined functions
 Passing different types of arguments

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.

CCS-101 –Lab Manual 24


13) Stage J (Journey)

Introduction:

It is usually a better approach to divide a large code in small functions. A function is a


small piece of code used to perform a specific purpose. Functions are defined first then
called whenever needed. A program may have as many functions as required. Similarly
a function may be called as many times as required.

How to Define Functions


Functions are defined as below.

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.

How to Call A Function

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.

CCS-101 –Lab Manual 25


14) Stage a1 (apply)
Lab Activities:
Activity 1:
Define a function to accept an integer value and return its factorial.

Solution:
G. Create a new Python file from Python Shell and type the following code.
H. Run the code by pressing F5.

You will get the following output.

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.

You will get the following output.

CCS-101 –Lab Manual 26


Activity 3:
Define a function to accept an integer value from user and check that whether the given
value is prime number or not. If the given value is a prime number then it will return
true otherwise false.

Solution:
G. Create a new Python file from Python Shell and type the following code.
H. Run the code by pressing F5.

You will get the following output.

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.

CCS-101 –Lab Manual 27


You will get the following output.

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.

You will get the following output.

CCS-101 –Lab Manual 28


15) Stage v (verify)
Home Activities:
Activity 1:
Write a function to accept an integer ‘n’ and display ‘n’ elements of Fibonacci series.

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.

16) Stage a2 (assess)


Assignment:
For this student will submit Lab Assignment before the deadline.

CCS-101 –Lab Manual 29

You might also like