Lab 01
Lab 01
Machine Learning
Introduction
This laboratory exercise will introduce the fundamental aspects of the Python
programming language which is a very popular language and used extensively in
the area of Machine Learning.
Objectives
Lab Conduct
Machine Learning
Theory
Python is an open-source, interpreted language which is widely used for machine
learning tasks in research, academia and industry. It has an easy-to-learn syntax
and is ideal for writing programs in a short duration. The python interpreter can
be downloaded from the website and installed on the system. By default, the
IDLE program is installed. For machine learning, it is recommended to switch to
a more powerful IDE such as PyCharm, Spyder and Jupyter etc. For this lab, we
will use Google Colab for writing python code. Google Colab is a cloud-based
platform that allows you to write python code in your web browser and provides
free access to computing resources such as GPUs.
Machine Learning
Lab Task 1 _________________________________________________________________________ [2]
Write a program which evaluates the following three expressions for when x =
1,2,3,4 and 5.
(b) Provide the code for both expressions in the indicated regions:
Machine Learning
### EXPRESSION 2 CODE STARTS HERE ###
def evaluate_expression(x):
result = ((3*x**2 + 7*x)/2) - (2*x/5)
return result
for x in range(1,6):
result = evaluate_expression(x)
print(f"When x = {x}, the result is {result}")
### EXPRESSION 2 CODE ENDS HERE ###
variable = input(“prompt_message”)
Remember that the above function returns a string which is stored in the
variable. You need to explicitly convert the string variable to an integer type
using the int() casting. Provide the code and screenshot of the result.
Machine Learning
print(f"{num1} is a multiple of {num2}")
else:
print(f"{num1} is not a multiple of {num2}")
### TASK 2 CODE ENDS HERE ###
if condition:
statement_1
else:
statement_2
### TASK 3 CODE STARTS HERE ###
# Prompt the user for the first number
if num1 == num2:
Machine Learning
print("Both numbers are equal.")
else:
else:
Machine Learning
Lab Task 4 _________________________________________________________________________ [1]
Write a program that takes two numbers as inputs. Then, the program must
compare the two numbers and print appropriately from among the following
lines:
• Both numbers are positive
• Both numbers are negative
• Both numbers are zero
• At least one number is zero
• One number is positive and the other number is negative
Machine Learning
print("Both numbers are zero.")
else:
while condition:
statement_1
statement_2
Machine Learning
### TASK 5 CODE STARTS HERE ###
# Prompt the user for an integer input
if num < 0:
else:
# Initialize variables
factorial = 1
i=1
factorial *= i
i += 1
Machine Learning
Lab Task 6 _________________________________________________________________________ [1]
Write a function that takes 2 integer arguments and returns their product but
you must NOT use the product operator (*). You will need to provide the
function definition and the function call. (Hint: You need to make use of loops in
your function.) The function definition syntax is given below:
def function_name:
statement_1
statement_2
…
return output
result = 0
# Check if either x or y is negative, and set a flag for the final result sign
x = abs(x)
y = abs(y)
Machine Learning
while y > 0:
if y % 2 == 1:
result += x
x <<= 1
y >>= 1
# If one of the input numbers was negative, make the result negative
if is_negative:
result = -result
return result
num1 = 5
num2 = 3
Machine Learning
Lab Task 7 _________________________________________________________________________ [1]
Write a program that prompts the user for 3 strings variables. The user will
input the strings separately at the prompt, e.g. “TRI”, “GONO” and “METRY”. The
strings will then be passed to a function as arguments. The function must use a
for loop to iterate through the characters and print each character on a new
line. The function must also print the total number of characters in the final
string. For this, you can use the len() function. Note that the “TRIGONOMETRY”
string is just an example and you need to use your own string for the
submission. You also need to take screenshot of this task showing the entire
output. The for loop syntax is given as follows:
print(char)
Total_Characters = len(comb_str)
Machine Learning
str2 = input("Enter the second string: ")
1, 2, 3… 20
2, 4, 6… 40
3, 6, 9… 60
Machine Learning
4, 8, 12 … 80
…
10, 20, 30… 200
result = i * j
if j == 20:
print(result)
else:
# Print with a comma and space for other numbers in the line
Machine Learning