0% found this document useful (0 votes)
8 views

DIP lab 1

This document outlines a laboratory exercise for a Digital Image Processing course focused on Python programming. It includes objectives such as creating variables, implementing loops, and file handling, along with specific lab tasks that require writing code for comparisons, list slicing, function creation, dictionary usage, and file operations. The document emphasizes proper conduct during the lab and provides a theoretical overview of Python syntax and functions.

Uploaded by

sdocrsf
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views

DIP lab 1

This document outlines a laboratory exercise for a Digital Image Processing course focused on Python programming. It includes objectives such as creating variables, implementing loops, and file handling, along with specific lab tasks that require writing code for comparisons, list slicing, function creation, dictionary usage, and file operations. The document emphasizes proper conduct during the lab and provides a theoretical overview of Python syntax and functions.

Uploaded by

sdocrsf
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

Department of Electrical Engineering

Faculty Member: Date:

Semester:

Digital Image Processing


Lab 1: Programming in Python

Lab Report Quiz/viva


Name Reg. 10 Marks 5 Marks
No
Introduction

This laboratory exercise is meant to introduce the fundamental aspects of the


python programming language which will be very important in the later labs of
the course.

Objectives

The following are the main objectives of this lab:

• Create variables of different data types in python


• Use arithmetic and logical operations in python
• Implement conditional statements and loops in python
• Create functions and call them in python
• Implement lists and dictionaries in python
• Read and write to files in python

Lab Conduct

• Respect faculty and peers through speech and actions


• The lab faculty will be available to assist the students. In case some aspect
of the lab experiment is not understood, the students are advised to seek
help from the faculty.
• In the tasks, there are commented lines such as #YOUR CODE STARTS
HERE# where you have to provide the code. You must put the code
between the #START and #END parts of these commented lines. Do NOT
remove the commented lines.
• Use the tab key to provide the indentation in python.
• When you provide the code in the report, keep the font size at 12
Theory
Python is an interpreted language which is commonly used in many fields
including networks, databases, robotics and artificial intelligence etc. It has an
easy-to-learn syntax and is ideal for developing prototypes in a short amount of
time. Python scripts are written in .py file which is then interpreted and
executed by the python interpreter.

A brief summary of the relevant keywords and functions in python is provided


below. (For more details, check the slides for this lab)

print() output text on console


input() get input from user on console
range() create a sequence of numbers
len() gives the number of characters in a string or list
if contains code that executes depending on a logical condition
else connects with if and elif, executes when conditions are not met
elif equivalent to else if
while loops code as long as a condition is true
for loops code through a sequence of items in an iterable object
break exit loop immediately
continue jump to the next iteration of the loop
def used to define a function
Lab Task 1
Write a program that prompts the user for two numbers as input. Then,
the program must compare the two numbers and print if they are equal or
not. If the numbers are not equal, it must also print which number is
greater (or lesser) than the other. The syntax for conditional statements is
given below:

if condition:
statement_1
else:
statement_2

### TASK 1 CODE STARTS HERE ###

### TASK 1 CODE ENDS HERE ###

### TASK 1 SCREENSHOT STARTS HERE ###

### TASK 1 SCREENSHOT ENDS HERE ###

Lab Task 2
Create a list with the sequence 1, 2, 3… 20. Then using the slice operation
(:) on this list, print the following sub-lists:

5, 6, 7… 20
1, 2, 3… 12
7, 8, 9 … 16
4, 5
11, 12, 13, 14
### TASK 2 CODE STARTS HERE ###

### TASK 2 CODE ENDS HERE ###

### TASK 2 SCREENSHOT STARTS HERE ###

### TASK 2 SCREENSHOT ENDS HERE ###

Lab Task 3
Write a function that takes 2 lists as arguments. Both the lists must be of
the same length. The function should calculate the product of the
corresponding items and place them in a third list. You must NOT use the
product operator (*). You need to provide the function definition and the
function call in the code. (Hint: You need to make use of loops in your
function.) The function definition syntax is given as follows:

def function_name:
statement_1

return output

### TASK 3 CODE STARTS HERE ###

### TASK 3 CODE ENDS HERE ###

### TASK 3 SCREENSHOT STARTS HERE ###

### TASK 3 SCREENSHOT ENDS HERE ###


Lab Task 4
In this task, you will make use of dictionaries. Write a program that first
prompts the user to input five strings which will be the keys of the
dictionary. Then, the program must prompt the user to input the values of
the respective keys. When entering the values, the user must be shown the
key whose value is being input. Once all values are entered, display the
dictionary.

### TASK 4 CODE STARTS HERE ###

### TASK 4 CODE ENDS HERE ###

### TASK 4 SCREENSHOT STARTS HERE ###

### TASK 4 SCREENSHOT ENDS HERE ###

Lab Task 5
In this task, you will focus on file handling. Write code that first creates a
text file “lab2.txt” with the message “My name is <your_name>”. Then,
your code must open the file in read mode and display the contents of the
text file. Next, the file must be opened in append mode and the message
“My registration number is <reg_number>” must be added to the text file.
Finally, the file is read again to display the modified contents.

### TASK 5 CODE STARTS HERE ###

### TASK 5 CODE ENDS HERE ###

### TASK 5 SCREENSHOT STARTS HERE ###

### TASK 5 SCREENSHOT ENDS HERE ###

You might also like