Unit III
Unit III
com
The continue statement is used to skip the rest of the code inside a loop for the
current iteration only. Loop does not terminate but continues on with the next
iteration.
iamriteshkandari 9259069231 riteshkandarics@gmail.com
Syntax of Continue
continue
# Program to show the use of continue statement inside loops
for val in "string":
if val == "i":
continue
print(val)
print("The end")
Output
s
t
r
n
g
The end
This program is same as the above example except the break statement has been
replaced with continue.
We continue with the loop, if the string is i, not executing the rest of the block.
Hence, we see in our output that all the letters except i gets printed.
Functions
A function is a group of statements that exists within a program for the purpose of
performing a specific task. Instead of writing a large program as one long sequence
of instructions, it can be written as several small functions, each performing a
specific part of the task. They constitute line of codes that are executed sequentially
from top to bottom by python interpreter.
Functions can be categorized into the following three types:
i. Built-in
ii. Modules
iii. User-defined
Built in functions are the predefined functions that are already available in Python.
Functions provide efficiency and structure to a programming language. Python has
many useful built in functions to make programming easier, faster and more
powerful.
Modules
A module is a file containing functions and variables defined in separate files. A
module is simply a file that contains python code or a series of instructions. When
we break a program into modules, each module should contain functions that
perform related tasks. There are some commonly used modules in python that are
used for certain predefined tasks and they are called libraries.
Modules also make it easier to reuse the same code in more than one program. If
we have written a set of functions that is needed in several different programs, we
can place those functions in a module. Then, we can import the module in each
program that needs to call one of the functions.
iamriteshkandari 9259069231 riteshkandarics@gmail.com
Python language provides two important methods to import modules in a program
which are as follows:
i. import statement: To import entire module
ii. from: To import all functions
iii. import: To use modules in a program, we import them using the import
statement.
For example: import math
Math module:
i. ceil(x): returns the smallest integer that is greater than or equal to x.
ii. floor(x): returns the largest integer that is less than or equal to x.
iii. pow(x,y): returns the value of Xy
iv. fabs(): returns the absolute value(positive value)
v. sqrt(x): returns the square root of x
vi. log10(x): returns the base-10 logarithm of x.
vii. cos(x): returns the cosine of x in radians
viii. sin(x): returns the sin of x in radians
ix. tan(x): returns the tangent of x in radians
String module:
i. isalpha(): returns true if the string contains only letters
ii. Isdigit(): returns true if the string contains only digits.
iii. lower(): Convert all the uppercase letters in the string to lowercase
iv. islower(): returns true if all the letters in the string are in lowercase
v. upper(): Convert lowercase letters in the string to uppercase
vi. isupper(): returns true if the string is in uppercase
vii. ord(): returns the ASCII code of the character
viii. chr(): returns character represented by the inputted ASCII number
Scope of Variables
All variables in a program may not be accessible at all locations in that program. This
depends on where you have declared a variable.
Scope of variables refers to the part of the program where it is visible, i.e area where
you can refer(use) it. Two types of scope of variables-global scope and local scope
➢ Global
• Names assigned at the top level of a module, or directly in the
interpreter
• Names declared global in a function
➢ Local
• Names assigned inside a function definition or loop
For Example:
>>>a=2 #a is assigned in the interpreter, so its Global
>>>def f(x): # x is assigned in the interpreter, so its Local
y=x+a #y is assigned in the interpreter, so its Local
return y
>>>p=f(5)
>>>print(p)
7
iamriteshkandari 9259069231 riteshkandarics@gmail.com
Recursion:
Recursion is one of the most powerful tools in a programming language. It is a
function calling itself again and again. Recursion is defined as defining anything in
terms of itself. In other words, it is a method in which a function calls itself one or
more times in its body.
Example:
def fact(x,n):
if n==0:
return 1
else:
return x*power(x,n-1)
a=5
Print(fact(a,4))