[go: up one dir, main page]

0% found this document useful (0 votes)
4 views5 pages

DIPLab 1

Dip

Uploaded by

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

DIPLab 1

Dip

Uploaded by

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

LAB #01: Introduction to Python

Lab Objective:

To introduce students with python programming language.

Lab Description:

Installation:

Download and install Python interpreter and PyCharm IDE from the following links below.
Python interpreter: https://www.python.org/downloads/
PyCharm IDE: https://www.jetbrains.com/pycharm/download/download-
thanks.html?platform=windows&code=PCC

Setup:

1. Create ‘New Project’


2. Now Check ‘Existing interpreter’ and Add local python interpreter that you’ve installed,
and click Create.

3. Now Right click on the folder and create new python file.
Digital Image Processing Lab Manual

4. After writing your python code right click on the window


and run the project.

Python is an interpreted high-level programming language for general-purpose programming.


Python is meant to be an easily readable language. Its formatting is visually uncluttered, and it
often uses English keywords where other languages use punctuation. Unlike many other
languages, it does not use curly brackets to delimit blocks, and semicolons.

Operation Syntax Explanation / Example Fill the outputs


of all the print
statements
Comment # # This is a comment.
Print print ( ) print (10)
( is used to display print (12+6)
anything in console print (5, end=” ”)
window) print (“END”)
Operators + plus
- minus print ( 5**2 )
* multiply print ( 5//2 )
** power print (5/2 )
/ divide print (5%2)
// divide and floor
% modulus
Variables x=5 right side is evaluated
(Python automatically y, z = 3.14, 17.2 first and then assigned to
guess the data type. There left side with
is no need to explicitly corresponding values
define data type in print (x + y)
python) print(type(x))
print(type(y))
Strings a=”py” both single and double
b=’charm’ quotes works exactly
same
print (a + b)
print (a, b)
print (a*5)
print (“hello’ \”world\”
”)

1
Digital Image Processing Lab Manual

Operation Syntax Explanation / Example Fill the outputs


of all the print
statements
Lists input ( ) function always
List in memory stores x=int (input (“Enter a input string, int ( )
references to objects. number”) function is used to
Each memory location is y = 3.14 convert string to int
a pointer to an object. z = "HELLO"
There is no obligation of li = [x, y, z, 4] print(li)
similar data types
List Indexing
# From left to right:
012 Q = li [2] [-4] print ( Q )
# From right to left:
-1  -2  -3
List Slicing print (li [1:3])
list [start: end: step size] print (li [0:4:2])
start is inclusive and end R = li [1:3] print (li [:])
is exclusive print (li [0:])
defaults values are print (li [:3])
list [ 0 : end : 1] print (li [2] [1:4])

Copy w = [1, 2, 3, 4] x [0] = 6


The assignment copies x=w y [1] = 9
the reference to the y = w [:] print(w)
original list while slicing
creates a new list
Indentation x=2 Whitespace (spaces and
if x==10: tabs) at the beginning of
print("inside”) the logical line is used to
print("inside") determine the
print("outside") indentation level of the
logical line, which in
turn is used to determine
the grouping of
statements.
Boolean operations < (less than)
> (greater than)
<=(less than/equal to)
>=(greaterthan/equal)
== (equal to)
! = (not equal to)
not (Boolean NOT) if not x:
and (Boolean AND) if x==2 and y>4:
or (Boolean OR) print ( 2==4 )

2
Digital Image Processing Lab Manual

Operation Syntax Explanation / Example Fill the outputs


of all the print
statements
if number = 23 If statement does not
If the Boolean expression if number == 24: include brackets and
evaluates to true, then the print (‘equal’) ends with colons. The if
if block of code will be elif number<24: block is determine by
executed print (‘less’) indentation level
else:
print (‘greater’)

while number = 23
Repeats a statement or while number<30:
group of statements while print(number)
a given condition is true number+=1

for for i in [2,3,4,1,5]: Range function is used


Execute a sequence of print(i) to generate a list of
statements multiple times for j in range(1,9,2): numbers, which is
and abbreviates the code print(j) generally used to iterate
that manages the loop over with for loops
variable
Functions def max (x, y):
A function is a block of if x > y:
code which only runs return x
when it is called else:
return y
m=max (3,5)
print(m)

Lab Tasks:
1:
x = [[1, 2, 3, 4, 5], [21, 22, 23, 24, 25], [31, 32, 33, 34, 35]]

 Write python code using python indexing and slicing for the following output. Use
only one print statement for each output.
i. [21, 22, 23, 24, 25]
ii. 3
iii. [32, 33]
iv. [1, 3, 5]
 Declare y = [0, 0, 0], now using for loop write average of first list in list ‘x’ on first
index of list y and so on. The print(y) should give the following output
o [3.0, 23.0, 33.0] // average of [1, 2, 3, 4, 5] = 3.0

3
Digital Image Processing Lab Manual

 Declare z = [0, 0, 0, 0, 0], now using for loop write average of each index of each
list in ‘x’ on corresponding index of list y. The print(z) should give the following
output
o [17.66, 18.66, 19.66, 20.66, 21.66] // average of [1, 21, 31] = 17.66

2:
x = [1, 3, 5, 6, 7, 8, 6, 1, 2, 3]

y = [0, 0, 0, 0, 0, 0, 0, 0]

 Write python code using while loop that write average of first three items on first
index of y and so on. The print(y) should give the following output
o [3.0, 4.666666666666667, 6.0, 7.0, 7.0, 5.0, 3.0, 2.0]
 Define a function that takes list as argument and returns the average of it. Then
calculate the average of x and y.
Home Tasks:

Write a code that takes two integers from user as input and store in x and y variables. Now generate
a list (w) of x elements and each element contains y random integers ranging from 1 to 10 and
display this list.

 Now ask user to enter a number from 1 to 10 and display the number of times that
integer occurs in the whole list (w).
 Write a code that calculate mean, median and mode of the list (w).
 Write a code that make another list (u) of same dimensions and compare each number
of original list (w) with the average of it calculated in the previous part, if the number
is less than the average, the code should place 0 on the corresponding index in newly
created list (u) else it should place 10. Display the list (u).

THINK!!

1. While copying, why x changed w while y didn’t? See above.


2. What will be the data type of result of a Boolean operation?
3. In Task 1, x is a list of what dimensions? Is it 3x5, 15 or just 3?
4. Can a list be created from range function?

You might also like