CH 2
CH 2
Notes
With two arguments, returns the logarithm of x to the given base calculate as
log(x)log(base)
• log10(x): Returns the base-10 logarithm of x. This is usually more accurate than
log(x,10).
• pow(x, y): Returns x raised to the power y. In particular, pow(10, x) and pow(x,
0.0) always return 1.0, even when x is a zero or a NaN. If both x and y are finite,
x is negative, and y is not an integer then pow(x, y) is undefined, and raises
ValueError.
• sqrt(x): Returns the square root of x.
• cos(x): Returns the cosine of x radians.
• sin(x): Returns the sine of x radians.
• tan(x): Returns the tangent of x radians.
• degrees(x): Converts angle x from radians to degrees.
• radians(x): Converts angle x from degrees to radians.
• A function is a block of organized and reusable code that is used to perform a
single, related action. Functions provide better modularity for your application and
a high degree of code reusability.
• Function blocks begin with the keyword def followed by the function name and
parentheses ().
• Any input parameters or arguments should be placed within these parentheses.
You can also define parameters inside these parentheses.
• The first statement of a function can be an optional statement-the documentation
string of the function or docstring.
• The code block within every function starts with a colon (:) and is indented.
• The statement return [expression] exit a function, optionally passing back an
expression to the caller. A return statement with no arguments is the same as
return None.
• Defining a function only gives a name, specifies the parameters that are to be
included in the function, a structure the blocks of code.
• The scope of a variable determines the portion of the program where you can
access a particular identifier. There are two basic scopes of variables in Python :
44
1. Global variables
2. Local variables
• Variables that are defined inside a function body have a local scope, and those
defined outside have a global scope.
• Built-in These are the functions that are always available in Python and can be
accessed by a programmer without importing any module.
• Examples of Some Built-in Functions
(i) print(): It prints objects to the text stream file.
(ii) input(): It reads the input, converts it to a string and returns that.
(iii) sorted(): Returns a new sorted list from the items in iterable..
(iv) bool(): Returns a boolean value i.e., True or False..
(v) min(): Returns the smallest of two or more arguments.
(vi) any(): Returns True if any element of the iterable is True.
• String Functions
(i) partition(): It splits the string at the first occurrence of the given argument
and returns a tuple containing three parts.
(ii) join(): It takes a list of string and joins them as a regular string.
(iii) split(): It splits the whole string into the items with separator as a delimeter.
• Modules: It is a file containing Python definitions and statements. We need to
import modules use any containing part before separator, separator parameter
and part after the separator if the separator parameter is found in the string of its
function or variable in our code.
• Examples of Some Module Functions
(i) fabs(): It returns the absolute value of a number.
(ii) factorial(): This method finds the factorial of a positive integer.
(iii) random(): It produces an integer between the limit arguments.
(iv) today(): This method returns the current date and time.
(v) search(): This function searches the pattern inside the string.
(vi) capitalize(): It returns the copy of string in capital letters.
• User-Defined Functions: User defined functions are those that we define ourselves
in our program and then call them wherever we want.
• Parameters: These are the values provided in the parentheses in the function
header when we define the function
• Arguments: These are the values provided in function call/invoke statement.
• Function Arguments: You can call a function by using the following types of formal
arguments:
o Required arguments/Positional arguments
o Keyword arguments
o Default arguments
o Variable-length arguments
Output:-
Mango
Cherry
Grapes
Banana
def prod(x,y,z):
return x*y*z
a= addEm(6,16,26)
b= prod(2,3,6)
print(a,b)
Q.25. What is the use of return statement?
Q. 1. Kids Elementary is a playway school that focuses on 'Play and learn' strategy
that helps toddlers understand concepts in a fun way. Being a senior programmer,
you have taken responsibility to develop a program using user-defined functions to
help children differentiate between upper case and lower case letters/English
alphabet in a given sentence. Make sure that you perform a careful analysis of the
type of alphabets and sentences that can be included as per age and curriculum.
Write a Python program that accepts a string and calculates the number of upper
case letters and lower case letters.
Q. 2. Traffic accidents occur due to various reasons. While problems with roads or
inadequate safety facilities lead to some accidents, majority of the accidents are
caused by drivers' carelessness and their failure to abide by traffic rules.
ITS Roadwork is a company that deals with manufacturing and installation of traffic
lights so as to minimize the risk of accidents. Keeping in view the requirements,
traffic simulation is to be done. Write a program in Python that simulates a traffic
light. The program should perform the following:
(a) A user-defined function trafficlight() that accepts input from the user, displays
an error message if the user enters anything other than RED, YELLOW and GREEN.
Function light() is called and the following is displayed depending upon return value
from light():
(i) "STOP, Life is more important than speed" if the value returned by light() is
0.
(ii) "PLEASE GO SLOW." if the value returned by light() is 1.
(iii) "You may go now." if the value returned by light() is 2.
(b) Auser-defined function light() that accepts a string as input and returns 0 when
the input is RED, 1 when the input is YELLOW and 2 when the input is GREEN. The
input should be passed as an argument.
(c) Display "BETTER LATE THAN NEVER" after the function trafficLight() is executed.
48
Summary
49