LAB MANUAL
CSC-100: Application of Information & Communication Technology
University Institute of Information Technology PMAS-Arid Agriculture
University, Rawalpindi
Statement Purpose:
The objective of this lab is to make student familiar with programming concepts in Python.
Activity Outcomes:
This lab teaches you the following:
Introduction to Python
Start with basic programming concepts
Lists
Tuples
Dictionaries
Python Variables
Activity 1
# assign the value 299792458 to the variable speed_of_light
speed_of_light = 299792458
print(speed_of_light)
# assign a decimal number 3.14 to the variable pi
pi = 3.14
print(pi)
# assign a string
fav_lang = "python"
print(fav_lang)
Conditional Statements
Activity 2
# if statement example
if 10 > 5:
print("10 greater than 5")
print("Program ended")
Activity 3
# if..else statement example
x=3
if x == 4:
print("Yes")
else:
print("No")
Activity 4
# if..else chain statement
letter = "A"
if letter == "B":
print("letter is B")
else:
if letter == "C":
print("letter is C")
else:
if letter == "A":
print("letter is A")
else:
print("letter isn't A, B and C")
Activity 4
# if-elif statement example
letter = "A"
if letter == "B":
print("letter is B")
elif letter == "C":
print("letter is C")
elif letter == "A":
print("letter is A")
else:
print("letter isn't A, B or C")
Loops
Activity 5
# Program to display the Fibonacci sequence up to n-th term
nterms = int(input("How many terms? "))
# first two terms
n1, n2 = 0, 1
count = 0
# check if the number of terms is valid
if nterms <= 0:
print("Please enter a positive integer")
# if there is only one term, return n1
elif nterms == 1:
print("Fibonacci sequence upto",nterms,":")
print(n1)
# generate fibonacci sequence
else:
print("Fibonacci sequence:")
while count < nterms:
print(n1)
nth = n1 + n2
# update values
n1 = n2
n2 = nth
count += 1
Lists
Activity 6
# Python program to print Even Numbers in a List
# list of numbers
list1 = [10, 21, 4, 45, 66, 93]
# iterating each number in list
for num in list1:
# checking condition
if num % 2 == 0:
print(num, end=" ")
Tuple
Activity 7
thistuple = ("apple", "banana", "cherry")
print(thistuple)
# access first item
thistuple = ("apple", "banana", "cherry")
print(thistuple[1])
# acess last item
thistuple = ("apple", "banana", "cherry")
print(thistuple[-1])
# slicing
thistuple = ("apple", "banana", "cherry", "orange", "kiwi", "melon", "mango")
print(thistuple[2:5])
Sets
Activity 8
# create an empty set.
my_set = set()
# create a set with elements "apple", "banana", and "cherry".
fruits = {"apple", "banana", "cherry"}
# find the length of a set
length = len(fruits)
# add an element to a set.
fruits.add(6)
# remove an element from a set.
fruits.remove(2)
Lab Task
1) Create a program that calculates the factorial of a given number using a loop
2) Develop a program that checks whether a given number is a prime number or not.
Use loops and if-else statements to implement the logic.
3) Write a python program that take a list of subjects of your current semester and print
on the console.
4) Write a python program that take a list of food items, perform add, update, and delete
operation on the list.
5) Write a two int type list and perform addition, subtraction and multiplication
operations and print the results.
6) Create different sets for colors, animals, and fruits. Perform different operations like
union, intersection, and difference between sets. Also, perform add, update, and
remove operations on the sets.
7) Create a tuple containing different fruits. Print the content with index no.
8) Create a tuple as above, write the code for combining (“grape,”kiwi”) in the tuple
9) Write a code for to print all items in the above tuple
10) Create an empty dictionary. Add NAME, AGE, AND CITY with values in the
dictionary.
11) Perform different operations on dictionary such as keys(), values() to print the key
and values present in the dictionary
12) Use a for loop to print all the items in the dictionary