CS101 - Python Programming Lab
Week 2 - AutoLab, Numbers
0. Recap - List new terms, concepts from the previous Lab
1. Submit code to AutoLab - A-Demo. square_root.py,
● ogin to AutoLab through IRIS
L
● A-Demo should be available to submit assignment
● Download and examine the code template attachment
● Edit the contents of the template file to achieve the task required.
● Submit the solution code to AutoLab for assessment.
● Examine the Autolab assessment
2. Numbers Presentation / Demo.
Tasks to do.
● Numbers demo
● Cookbook
● Assign Grains on a ChessBoard, and Triangle
A0 - Area of a Circle
ou now understand that Python statements can be algebraic equations. Use the IDLE text editor (File →
Y
New File) to write your first Python program. A template is provided below.
ssignment Task: Write a function that accepts the radius of a circle as input and returns its area. Your
A
function should only return the area and nothing else. No printing inside the function and no returning a
string. Do not change the name of the function.
fter you are satisfied that your function works as expected, save the file (Eg.area_circle.py(anyname will
A
do)). Submit the program through Autolab againstthe appropriate Assignment. Login to Autolab through
IRIS. Autolab will run your program against a few inputs (some are shown in the Table below) and check if
the outputs generated are as expected.
o through the Autolab output and try to make sense of the output. The input(s) given to your function, the
G
returned output and the expected output should be shown in the output.
Area of a Circle Template (area_circle.py)
''
'
CS101. A0 - Area of a Circle
'''
def area_circle(radius):
"""Calculate and return the area of a circle
param radius: float - radius of the circle.
:
:return: area of the circle.
unction to calculate the area of a circle.
F
Assume the value of pi to be 3.141593
"""
pass
Test Cases
Function Inputs Output Remarks
area_circle 1 3.141593
area_circle 1.1 3.8013275300000005
area_circle 0.1 0.03141593
area_circle 10.1 320.47390192999995
****
A0a - Square Root
he same Autolab submission and evaluation process is followed for all the assignment / quiz / assignment
T
programs throughout the Semester.
ssignment Task: Use an algebraic equation to compute the square root. Write a function that accepts a
A
positive number as input and returns its square root. Write the appropriate docstrings.
Square root Template (square_root.py)
''
'
CS101. A0a - Square root program
'''
def square_root(n):
"""Calculate and return the square root of a positive integer
param n: int positive number.
:
:return: square root of n.
unction that takes in a positive integer, calculates its square root
F
using a simple algebraic equation. Returns the result.
"""
pass
Test Cases
Function Inputs Output Remarks
square_root 100 10.0
square_root 1 1.0
square_root 2 1.4142135623730951
square_root 17 4.123105625617661
square_root 1024 32.0
square_root 6241 79.0
A0b - Energy Mass Equation
ask: Calculate the equivalent energy in Joules of a given mass as input to the emc2 function. Assume light
T
travels at 299792458 m/s. Write the appropriate docstrings.
Energy Mass Equivalence Template ([Link])
''
'
CS101. A0b - Energy Mass Equivalence
'''
def emc2(m):
pass
Test Cases
Function Inputs Output Remarks
emc2 1.0 8.987551787368176e+16
emc2 1.4142135623730951 1.2710317630226627e+17
emc2 0.0001 8987551787368.176
****
A0c - Acceleration
ask: Calculate the acceleration (rate of change in velocity) in m/s2. Inputs: initial velocity (u),final velocity
T
(v) and time taken, t, in seconds to reach velocity v m/s starting from u m/s.
Acceleration Template ([Link])
''
'
CS101. A0c - Acceleration
'''
def acceleration(u, v, t):
pass
Test Cases
Function Inputs: u, v, t Output Remarks
acceleration 1.0, 1.0, 10 0.0
acceleration 1.0, 2.0, 10 0.1
acceleration 1.0, 10.0, 1.0 9.0
acceleration 1.0, 1.0001, 0.0001 0.9999999999998899
****
A0d - Gravitational Force Equation
ask: Calculate the gravitational force exerted by two bodies of masses m1 and m2 separated by a
T
distance of r. Inputs: m1, m2, r. Assume Gravitational constant is 6.6743 × 10-11 m3 kg-1 s-2.
Gravitational Force Template ([Link])
''
'
CS101. A0d - Gravitational Force
'''
def gforce(m1, m2, r):
pass
Test Cases
Function Inputs Output
gforce 1.0, 1.0, 10 6.674299999999999e-13
gforce 75.0, 75.0, 1.0 3.75429375e-07
gforce 5.97219e+24, 7.34767309e+22, 384400 1.9820856039721175e+26
gforce 5.97219e+24, 1.989e+30, 150750000 3.4886667104109302e+28
A0e - Simple Interest
ask: Calculate and return the Simple Interest amount on a principal of INR p on a rate of r for a period of y
T
years. Inputs: m, r, y. Output should be rounded to 2 decimal places only. Hint: (a) Simple Interest (SI) =
Principal (P) × Rate (R) × Time (T). (b) Use the function round() to round a number to a given precision in
decimal digits. Eg.
>>> round(10.12356, 2)
10.12
>>> round(10.99999, 2)
11.0
Simple Interest Template (simple_interest.py)
''
'
CS101. A0e - Simple Interest
'''
def simple_interest(p, r, y):
pass
Test Cases
Function Inputs Output
simple_interest 1000, 0.05, 3 150.0
simple_interest 12345, 0.05, 10 6172.5
simple_interest 98765, 0.01, 99 97777.35
simple_interest 12345, 0.99, 10 122215.5
****
A0f - Compound Interest
ask: Calculate and return the Compound Interest amount on a principal of INR p on a rate of r for a period
T
of y years. Inputs: m, r, y. Output should be rounded to 2 decimal places only.
Hint: Final Principal Value = P × (1 + R)T.
Compound Interest Template (compound_interest.py)
''
'
CS101. A0f - Compound Interest
'''
def compound_interest(p, r, y):
pass
Test Cases
Function Inputs Output
compound_interest 1000, 0.05, 3 157.63
compound_interest 12345, 0.05, 10 7763.7
compound_interest 98765, 0.01, 99 165730.98
compound_interest 12345, 0.99, 10 12010904.47
****
A0g - Fahrenheit to Celsius
ask: Calculate and return the Celsius equivalent of the given Fahrenheit temperature. Inputs: f. Output
T
should be rounded to 2 decimal places only. Hint: C = 5/9 × (F - 32).
F2C Template ([Link])
''
'
CS101. A0g - Fahrenheit to Celsius
'''
def f2c(f):
pass
Test Cases
Function Inputs Output
f2c -40 -40.0
f2c -1 -18.33
f2c 1 -17.22
f2c 100 37.78
****
A0h - Total, Average Marks
ask: Given 5 integers as input, calculate and return both total and average (arithmetic mean). Inputs: n1,
T
n2, n3, n4, n5. Output should be rounded to 2 decimal places only. Hint: return two values like thus:
return total, avg
Total, Average and Total Template (total_avg.py)
''
'
CS101. A0h - Total and Average
'''
def total_avg(n1, n2, n3, n4, n5):
pass
Test Cases
Function Inputs Output
total_avg 10, 20, 30, 40, 50 (150, 30.0)
total_avg 10, 20, 30, 40, -50 (50, 10.0)
total_avg -10, 20, -30, 40, 50 (70, 14.0)
total_avg 10.1, 20.2, 30.3, 40.4, 50.5 (151.5, 30.3)
****
A0i - Distance between points
ask: Given 2 points on a Cartesian plane, (x1, y1) and (x2, y2), return the distance between the two points.
T
Inputs: x1, y1, x2, y2. Output should be rounded to 2 decimal places only.
Distance Template (total_avg.py)
''
'
CS101. A0i - Points distance
'''
def distance(x1, y1, x2, y2):
pass
Test Cases
Function Inputs Output
distance 10, 20, 30, 40 28.28
distance 10, 20, 30, -50 72.8
distance -10, 20, -30, 40 28.28
distance 10.1, 20.2, 30.3, 40.4 28.57
****