Breaking

Be Strong Let's Try!

Thursday, 29 July 2021

Introduction to Python, OpenCV & Numpy

 To introduce you with python programming language ,opencv and numpy

How to 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/downloadthanks.html?platform=windows&code=PC

Setup for creating new project in python: 

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.

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

How to Install OpenCv :

Open Source Computer Vision Library (OpenCv)is an open source software library written in C++ for machine learning and computer vision applications. 
 To install packages in PyCharm Click, Files > Settings > Project: Name > Project Interpreter Now click on ‘+’ icon to install new packages

Some of task for the practice:

Practice Task 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. [ 24, 25] 
ii. [3,4]
iii. [32]
iv. [21,23,25]
*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 *[3.0, 23.0, 33.0] 

(a)

Code

import numpy as np
x = [[
1, 2, 3, 4, 5], [21, 22, 23, 24, 25], [31, 32, 33, 34, 35]]
print(x[1][3:5])
print(x[0][2:4])
print(x[2][1:2])
print(x[1][0:5:2
])

OUTPUT:

(b)

Code:

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

lists =
len(x)                  #number of lists
#nester for loop
for i in range(lists):
    elements =
len(x[i])        #number of elements in each list
   
for j in range(elements):
        y[i] = y[i]+x[i][j]
    y[i] = y[i]/elements       
#calculate average

print
(y)
 

Output:




Practice Task 2:

Write python code to count the number of strings from the list where the string length is 2 or more and the first and last character are same from a given list of strings. x=[‘abc’,’1121’, ‘mnnm’ , ‘aba’] 

Code:


x=[
'abc','1121', 'mnnm' , 'aba']

length = 
len(x)                       #number of strings in x
string_count = 0                       #initialize counter

for i in range(length):                #for each string in x
   
elements = len(x[i])               #number of elements in each string
   
if x[i][0]==x[i][-1]:              #check if frist and last elements are equal
       
string_count = string_count + 1                     #increment counter

print('String count is : ', string_count)   #print string count
                                     

 Output:

String count is : 3

Practice Task 3:

Write a function which takes a list as input argument and generate a new list after removing the 0 th , 3 th and 5 thindex value of list. x=[‘apple’, ’banana’, ‘mango’, ‘orange’, ‘peach’ , ‘grapes’] Output of this function:x=[’banana’, ‘mango’, ‘peach’]

Code:

x = ['apple', 'banana', 'mango', 'orange', 'peach' , 'grapes']
x.
__delitem__(5)
x.
__delitem__(3)
x.
__delitem__(0)
print('New List is : ', x)          #print new list 

 

Output:











No comments:

Post a Comment

Pages