You are on page 1of 15

Emerging Technologies 01 (CPCM Group 1)

Student name: Navdeep kaur


Student id: c0781940

Task 3

Ans 1.
Function for calculating the area of rectangle is
>>> def area(l,b):
A = l*b #l is length and b is breadth of rectangle
return A

>>> print(area(8,10)
80

1
Ans 2.
For finding the area of square we can use the function from Ans1
By using function from Ans1:
>>> def area(l,b):
A = l*b #the value of length and breadth are same in case of square
return A
>>> print(area(8,8)
64

2
Calculating the area of square is very simple. One can just write it in their code directly, but
using a function is more beneficial because it makes the main script concise and by using
function, we can avoid repetition of codes and we can reuse the code without typing it over and
again. For example, if we want to find the area of three squares with different dimensions then
we have to write the code for three times , but if we use a function then we just have to write it
once (after that we can put the value of different dimensions in the same function to get the area).

Ans 3.
x = 4 and y = -0.4

3
Ans 4. The function which returns the absolute value of float is given below:
>>> def value(x:float)->float:
return abs(x)
>>>print(value(-5))
5.0
>>> print(value (-18.0)
18.0

4
5
Ans 5.
>>> (a,b,c) = (5,10,15)
>>> a
5
>>> b
10
>>> c
15
>>> X = c # X is a temporary variable here
>>> c = b

6
>>> b = a
>>> a = X
>>> a
15
>>> b
5
>>> c
10

Task 1

Ans 1: For every possible combination of a and b, the value of f(a,b) is given
below:
Here f(a, b) is “a or not b or (not a and b) and (b or a)”

a b f (a, b)
False False True
False True True
True False True
True True True

Ans 2:
 After defining math1 and math2 , when we try to print math1(1, 1, 1) in
python then it will show the error because a is not defined in function
math1.
 After that, when a = 2, c = 4, b = math1(a, a, a)
Then the value of a = 2, b = 9, c = 4, but in case of z it will show an error as
z is not defined.

7
 When a = 1, x = 2, thereafter if we try to print the value of math2(x, a, x)
then the output will be 19. The value of x will be 2 and the value of a will be
1.
 After adding math3(a, b, c) and math4(a, b, c) to shell,
Value of a = 1 (if we add math3 and math4 in the same shell then the value
of a will be 1 as it get derived from math1 and math2 but if we run math3
and math4 separately then it will show error as a is not defined for math3
and math4)
 After calling math3(a,b,c) the output will be 16 and the value of x will be
1.

The whole function when run in python shell, the output of that is given
below:
>>> def math1(x, y, z):
z=a+1
c=x+1
y=c+1
return x + y + z

>>> def math2(a, b, c):


a=a+c
b = math1(a, b, c)
c=x+1
return a + b + c

>>> math1(1, 1, 1)
Error
>>> a = 2
>>> c = 4

8
>>> b = math1(a, a, a)
>>> a
2
>>> b
9
>>> c
4
>>> z
NameError: name 'z' is not defined
>>> a = 1
>>> x = 2
>>> math2(x, a, x)
19
>>> x
2
>>> a
1
>>> def math3(a,b,c):
a=a+c
b = math1(a, b, c)
x=5
c=x+1
return a + b + c

>>> def math4(a, b, c):


a=a+c
b = math1(a, b, c)
9
c=x+1
x=5
return a + b + c

>>> a
1
>>> a = 1
>>> x = 1
>>> math3(x, a, x)
16
>>> x
1

Ans 3:

>>> def get_circle_info(radius):

circleArea = 3.14*((radius**2))

Circumfrence = 2*3.14*radius

return circleArea , Circumfrence

>>> radius = 5

>>> get_circle_info(radius)

(78.5, 31.400000000000002)

10
Ans 4:

In python, the value of variable given under a specific function is called local
variable and that value can only be used for that specific function, while the value
assigned to variable outside the function is called global variable and that value can
be accessed throughout the whole program .

11
In the given example, the value to variable num (i.e. num = 1) is assigned outside
my_function() so it is a global variable, whereas num = 5 is given inside the
function my_function() and is a local variable. Therefore, the statement print(num,
‘in global/main’) shows the value of num as ‘1’because it is a global variable and
written outside the function. But the output of print(num, ‘inside my_function’) is
5 because it is a local variable and the scope of local variable is limited to the
specific function only. Hence, according to the concept of local and global
variables, python knows when to print which value of num.

Ans 5:

>>> def in_range(x):


return x>=1 and x<=3

>>> in_range(1)
True
>>> in_range(3)
True
>>> in_range(3.1)
False
>>> in_range(2.8)
True
>>> in_range(1.0)
True

12
Ans 6:
>>> def solve_Eq(a, b, c):
F = b**2 - (4*a*c) # the equation is ax2+bx+c = 0
G = 2*a
x = ((-b) + F**0.5)/G , ((-b) - F**0.5)/G
return x

>>> solve_Eq(2,-8,6)
(3.0, 1.0)

13
14
15

You might also like