-
Notifications
You must be signed in to change notification settings - Fork 0
/
10th_june_python_(functions_assignment)_1.py
163 lines (119 loc) · 4.33 KB
/
10th_june_python_(functions_assignment)_1.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
# -*- coding: utf-8 -*-
"""10th June - Python (Functions Assignment) - 1
Automatically generated by Colaboratory.
Original file is located at
https://colab.research.google.com/drive/1kOpWX6CRj-77umdoIebwMrDUd1fsP5uU
**10th June - Python (Functions Assignment) - 1**
1. In Python, what is the difference between a built-in function and a user-defined function? Provide an
example of each.
"""
#Calculate sum of the given digits
def addNum(a,b):#User defined function
return a+b
print(addNum(5,20))#Calling User defined function
print(sum([5,20]))# built-in function
"""2. How can you pass arguments to a function in Python? Explain the difference between positional
arguments and keyword arguments.
"""
def addNum(a,b):
print( a+b)
def greet(name, message):
print(f"Hello, {name}! {message}")
addNum(10,20) #positional arguments
greet(name="Shankar", message="How are you?")#keyword arguments.
"""3. What is the purpose of the return statement in a function? Can a function have multiple return
statements? Explain with an example.
"""
''' return statement helps to return a value in functions.
Yes, A function have multiple return statements. It can be achieved using if else statements
'''
def get_grade(score):
if score >= 90:
return "A"
elif score >= 80:
return "B"
elif score >= 70:
return "C"
elif score >= 60:
return "D"
else:
return "F"
print("Grade",get_grade(87))
"""4. What are lambda functions in Python? How are they different from regular functions? Provide an
example where a lambda function can be useful.
"""
# lambda function is a anynamous function.It is used for Short terms operations.
# It does not have any method name but it can have multiple arguments and only one expression.
# We can achecive functonality using lambda keyword.
# The expression evaluted when it called and return values.
# return values implicitly no need to diclare
# The difference from regular functions that we dont diclare method name.
add=lambda a,b:a+b
print(add(3,9))
"""5. How does the concept of "scope" apply to functions in Python? Explain the difference between local
scope and global scope.
"""
# A scope is the region of a variable. If a variable diclared in a function , scope is local and it can't available outside of function
# If we diclare a varible with Global keyword it acts as Global scope. Variable can access inside and outside of the function.
def printValue1():
global x
x=200
printValue1()
print(x) # 200 ,, x is in Global scope
def printValue():
y=200
printValue()
print(y) #NameError: name 'y' is not defined since y is in the local scope
"""6. How can you use the "return" statement in a Python function to return multiple values?
"""
def calculate(a,b):
add=a+b # Addition
sub=a-b # Substraction
mul=a*b # Multiplication
return add,sub, mul
print(calculate(5,3))
"""7. What is the difference between the "pass by value" and "pass by reference" concepts when it
comes to function arguments in Python?
"""
# In python immuatables are passby value and mutables are passby reference
#pass by value
def increment_number(n):
n=n+1
return n
value=10
increment_number(value)
print(value) #10
#pass by reference
def append_value(lst):
lst.append(4)
lst=[1,2,3]
append_value(lst)
print(lst)#[1, 2, 3, 4]
"""8. Create a function that can intake integer or decimal value and do following operations:
a. Logarithmic function (log x)
b. Exponential function (exp(x))
c. Power function with base 2 (2x)
d. Square root
"""
import math
def perform_operations(value):
logarithm = math.log(value) # Logarithmic function (log x)
exponential = math.exp(value) # Exponential function (exp(x))
power = math.pow(2, value) # Power function with base 2 (2^x)
square_root = math.sqrt(value) # Square root
return logarithm, exponential, power, square_root
# Example usage
number = 4.0
log, exp, pow, sqrt = perform_operations(number)
print("Logarithm:", log)
print("Exponential:", exp)
print("Power with base 2:", pow)
print("Square Root:", sqrt)
"""9. Create a function that takes a full name as an argument and returns first name and last name."""
def extract_first_last_name(fullname):
names= fullname.split()
return names[0],names[1]
full_name = "John Doe"
first, last = extract_first_last_name(full_name)
print("First Name:", first)
print("Last Name:", last)