Introduction to image processing
Assignment 1
Question 1:
If you are familiar with any other programming language, list the differences between that
language and Python.
Answer:
I am familiar with c++ and following are the differences between c++ and python.
Python has a large number of pre-built libraries and modules that can be used for a wide
range of tasks, while C++ has a limited set of libraries.
C++ requires explicit memory management, which can make programming more
complex, while Python uses automatic garbage collection to manage memory.
C++ is generally faster than Python due to its low-level nature and the ability to optimize
code for specific hardware, while Python is slower but more flexible and easier to use.
C++ uses a more complex syntax with curly braces and semicolons to indicate the
beginning and end of statements, while Python uses indentation to indicate code blocks.
In c++ variable types must be declared explicitly, while Python is dynamically typed,
which means that variable types are determined at runtime.
C++ has a more rigid implementation of classes and objects, while Python has a more
flexible and dynamic approach.
C++ is often used for applications that require high performance, such as video games,
operating systems, and other systems programming tasks. Python is often used for web
development, data analysis, scientific computing, and other applications that require
quick prototyping and flexibility.
Question 2:
Write a Python program that will print numbers from 10 to 20 using for-loop.
Answer:
for number in range(10,21):
Print(number)
Question 3:
Create a list of state names such as states = [’Minnesota’,’Texas’,’New York’,’Utah’,’Hawaii’].
Add another entry ’California’ to the end of the list. Then, print all the values of this list.
Answer:
states = ['Minnesota', 'Texas', 'New York', 'Utah', 'Hawaii']
[Link]('California')
print(states)
Question 4:
Print the content of list from Question 3 and also the corresponding index using the Python
command enumerate in the for-loop.
Answer:
states = ['Minnesota', 'Texas', 'New York', 'Utah', 'Hawaii']
[Link]('California')
for i, state in enumerate(states):
print(f"index {i}: {state}")
Question 6:
Create a 2D list of size 3-by-3 with the following elements: 1, 2, 3|4, 5, 6|6, 7, 8.
Answer:
array = [[1, 2, 3], [4, 5, 6], [6, 7, 8]]
print([array[0][0],array[0][1],array[0][2]])
print([array[1][0],array[1][1],array[1][2]])
print([array[2][0],array[2][1],array[2][2]])
Question 6:
It is easy to convert a list to set and viceversa. For example a list ‘mylist = [1, 1, 2, 3, 4, 4, 5]’ can
be converted to set using the command newset = set(mylist). The set can be converted back to
list using newlist = list(newset). Compare the contents of mylist and newlist. What do you infer?
Answer:
If we convert a list mylist to a set newset by using the set() function that removes the
duplicates from the list. Then by converting newset back to a list newlist by using the
list() function we get a list with the unique elements from the set as all duplicates have
been removed.
Question 7:
Look up documentation for join method and join the content of the list
[’Minneapolis’,’MN’,’USA’] and obtain the string ’Minneapolis, MN, USA.’
Answer:
list = ['Minneapolis', 'MN', 'USA']
string = ', '.join(my_list)
print(string + '.')
Question 8:
Consider the following Python code:
a = [1,2,3,4,2,3,5]
b = []
for i in a:
if i>2:
[Link](i)
print b
Rewrite the above code using list comprehension and reduce the number of lines.
Answer:
a = [1, 2, 3, 4, 2, 3, 5]
b = [i for i in a if i > 2]
print(b)