0% found this document useful (0 votes)
15 views129 pages

Python Control Flow for Engineers

The document outlines the syllabus for a Python course aimed at engineering students, covering control flow statements, loops, functions, and exception handling. It includes detailed explanations of various control flow constructs such as if, else, elif, and loop statements like for and while, along with examples and syntax. Additionally, it presents practical exercises and questions to reinforce learning.

Uploaded by

Soumya Panda
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views129 pages

Python Control Flow for Engineers

The document outlines the syllabus for a Python course aimed at engineering students, covering control flow statements, loops, functions, and exception handling. It includes detailed explanations of various control flow constructs such as if, else, elif, and loop statements like for and while, along with examples and syntax. Additionally, it presents practical exercises and questions to reinforce learning.

Uploaded by

Soumya Panda
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

PYTHON FOR ENGINEERS

OEEC6205
4th Semester, B. Tech(CSE)
IGIT, Sarang
SYLLABUS
Module I
Control Flow Statements, The if Decision Control Flow
Statement, The if…else Decision Control Flow Statement,
The if…elif…else Decision Control Statement, Nested if
Statement, The while Loop, The for Loop, The continue
and break Statements, Catching Exceptions Using try and
except Statement, Functions, Built-In Functions,
Commonly Used Modules, Function Definition and
Calling the Function, The return Statement and void
Function, Scope and Lifetime of Variables, Default
Parameters, Keyword Arguments, *args and **kwargs,
Command Line Arguments.
PYTHON FOR ENGINEERS By Mr. Bapuji Rao,
2
CSEA, IGIT, Sarang
FLOW CONTROL STATEMENTS

 Flow control statements are written using selection (or


decision control) statements and loop statements.

 The selection statements are written using if


statements.

 The relational or comparison operators are used to


include condition in a flow of control statements.

 A condition always returns a Boolean True or False


value.

PYTHON FOR ENGINEERS By Mr. Bapuji Rao,


3
CSEA, IGIT, Sarang
PYTHON FOR ENGINEERS By Mr. Bapuji Rao,
4
CSEA, IGIT, Sarang
if STATEMENT

 It executes the indented statement(s) when the condition


is true.
 The Indentation is always four spaces.
 SYNTAX (Single Statement):
if CONDITION:
....
....Statement

(OR)

if (CONDITION):
....Statement
....

PYTHON FOR ENGINEERS By Mr. Bapuji Rao,


5
CSEA, IGIT, Sarang
if STATEMENT
 SYNTAX (Multiple Statements):
if CONDITION:
....
....Statement-1
....
....Statement-2
....
....----------------
....
....Statement-N
(OR)
if (CONDITION):
....
....Statement-1
....
....Statement-2
....
....----------------
....
....Statement-N
 Here, ....
.... indicates an indentation of 4 spaces (by default).
PYTHON FOR ENGINEERS By Mr. Bapuji Rao,
6
CSEA, IGIT, Sarang
EXAMPLE-1

PYTHON FOR ENGINEERS By Mr. Bapuji Rao,


7
CSEA, IGIT, Sarang
EXAMPLE-2

PYTHON FOR ENGINEERS By Mr. Bapuji Rao,


8
CSEA, IGIT, Sarang
OUTPUT

PYTHON FOR ENGINEERS By Mr. Bapuji Rao,


9
CSEA, IGIT, Sarang
if...else STATEMENT
 It executes the indented statement(s) when the condition is
true; otherwise executes the indented statement(s) of else
part.
 SYNTAX (Single Statement):
if CONDITION:
.... Statement-1

else:
.... Statement-2

(OR)
if (CONDITION):
.... Statement-1

else:
.... Statement-2

PYTHON FOR ENGINEERS By Mr. Bapuji Rao,


10
CSEA, IGIT, Sarang
if...else STATEMENT
 SYNTAX (Multiple Statements):
if CONDITION: if (CONDITION):
.... Statement-1 .... Statement-1

.... Statement-2 .... Statement-2

.... ---------------- .... ----------------

.... Statement-N (OR) .... Statement-N

else: else:
.... Statement-1 .... Statement-1

.... Statement-2 .... Statement-2

.... ---------------- .... ----------------

.... Statement-N .... Statement-N

PYTHON FOR ENGINEERS By Mr. Bapuji Rao,


11
CSEA, IGIT, Sarang
EXAMPLE-1

PYTHON FOR ENGINEERS By Mr. Bapuji Rao,


12
CSEA, IGIT, Sarang
OUTPUT

PYTHON FOR ENGINEERS By Mr. Bapuji Rao,


13
CSEA, IGIT, Sarang
EXAMPLE-2

PYTHON FOR ENGINEERS By Mr. Bapuji Rao,


14
CSEA, IGIT, Sarang
OUTPUT

PYTHON FOR ENGINEERS By Mr. Bapuji Rao,


15
CSEA, IGIT, Sarang
elif STATEMENT

 It is used for more than two possibilities.


 SYNTAX:
if CONDITION-1:
.... Statement-1

elif CONDITION-2:
.... Statement-2

elif CONDITION-3:
--------------------------
--------------------------
else:
.... Statement-N

PYTHON FOR ENGINEERS By Mr. Bapuji Rao,


16
CSEA, IGIT, Sarang
elif STATEMENT
 It is used for more than two possibilities.
 SYNTAX:
if CONDITION-1:
.... Statement-1
.... --------------

.... Statement-N

elif CONDITION-2:
.... Statement-1
.... --------------

.... Statement-N

--------------------------
--------------------------
else:
.... Statement-1
.... --------------

.... Statement-N

PYTHON FOR ENGINEERS By Mr. Bapuji Rao,


17
CSEA, IGIT, Sarang
EXAMPLE-1

PYTHON FOR ENGINEERS By Mr. Bapuji Rao,


18
CSEA, IGIT, Sarang
OUTPUT

PYTHON FOR ENGINEERS By Mr. Bapuji Rao,


19
CSEA, IGIT, Sarang
EXAMPLE-2

PYTHON FOR ENGINEERS By Mr. Bapuji Rao,


20
CSEA, IGIT, Sarang
OUTPUT

PYTHON FOR ENGINEERS By Mr. Bapuji Rao,


21
CSEA, IGIT, Sarang
CONDITIONAL EXPRESSION

 It is a short form of the 'if..else statement' that returns a


value.

 It is used for inline conditional expressions.

 It is also called as ternary expression.

 Syntax:
variable_name = (Expression-1 if Condition else Expression-2)

(OR)

variable_name = Expression-1 if Condition else Expression-2


PYTHON FOR ENGINEERS By Mr. Bapuji Rao,
22
CSEA, IGIT, Sarang
EXAMPLE

PYTHON FOR ENGINEERS By Mr. Bapuji Rao,


23
CSEA, IGIT, Sarang
NESTED CONDITIONAL EXPRESSION

 It is a short form of the 'elif statement' that returns a


value.

 Syntax:
variable_name = (Expression-1 if (Condition-1) else
Expression-2 if (Condition-2) Expression-3
............. else Expression-N)

(OR)

variable_name = Expression-1 if Condition-1 else


Expression-2 if Condition-2 Expression-3
............. else Expression-N
PYTHON FOR ENGINEERS By Mr. Bapuji Rao,
24
CSEA, IGIT, Sarang
EXAMPLE

PYTHON FOR ENGINEERS By Mr. Bapuji Rao,


25
CSEA, IGIT, Sarang
OUTPUT

PYTHON FOR ENGINEERS By Mr. Bapuji Rao,


26
CSEA, IGIT, Sarang
LOGICAL OPERATORS

 Logical operators and and or are used to combine more


than one conditions to make as one condition.
 NOTE: & and | operators cannot be used in Jupiter
Notebook.
PYTHON FOR ENGINEERS By Mr. Bapuji Rao,
27
CSEA, IGIT, Sarang
LOGICAL OPERATORS
 Syntax of and:
Condition-1 and Condition-2 and ...................... and Condition-N
 Syntax of or:
Condition-1 or Condition-2 or ...................... or Condition-N

 Logical operator not is used for negation purpose. It means the


condition’s true value changes to false and vice-versa.
 Syntax of not:
not Condition
(OR)
not (Condition)

PYTHON FOR ENGINEERS By Mr. Bapuji Rao,


28
CSEA, IGIT, Sarang
EXAMPLE-1

PYTHON FOR ENGINEERS By Mr. Bapuji Rao,


29
CSEA, IGIT, Sarang
OUTPUT

PYTHON FOR ENGINEERS By Mr. Bapuji Rao,


30
CSEA, IGIT, Sarang
EXAMPLE-2

PYTHON FOR ENGINEERS By Mr. Bapuji Rao,


31
CSEA, IGIT, Sarang
OUTPUT

PYTHON FOR ENGINEERS By Mr. Bapuji Rao,


32
CSEA, IGIT, Sarang
QUESTIONS
1. Read any three numbers. Then print the largest and smallest.
2. Read a month number. Then print number of days.
3. Read a number. Then check the number is a ONE DIGIT or TWO
DIGITS or THREE DIGITS or FOUR DIGITS or More Than FOUR
DIGITS.
4. Read a number. If the number is a single digit, then print its square. If
the number is a double digit, then print the digits sum. If the number is
a triple digit, then print the digits product.
5. Read principle and time. Then calculate simple interest and amount.
The rate is calculated accordingly:
PRINCIPLE RATE
>=100000 10%
>=75000 7.5%
>=50000 5%
>=30000 1.5%
>=15000 0.5%
PYTHON FOR ENGINEERS By Mr. Bapuji Rao,
33
CSEA, IGIT, Sarang
LOOP
STATEMENTS

PYTHON FOR ENGINEERS By Mr. Bapuji Rao,


34
CSEA, IGIT, Sarang
for…in range( ) LOOP STATEMENT

 The range( ) function prints a range of values from a


starting value to the ending value.

 It can pass a minimum one argument and maximum three


arguments.

 Syntax (Single Statement):

f or variable_name in range (Initial_Value, Final_Value, Step_Value):


.... Statement

PYTHON FOR ENGINEERS By Mr. Bapuji Rao,


35
CSEA, IGIT, Sarang
for…in range( ) LOOP STATEMENT

 Syntax (Multiple Statements):

f or variable_name in range (Initial_Value, Final_Value, Step_Value):


.... Statement-1
.... Statement-2

.... ---------------

.... ---------------

.... Statement-N

PYTHON FOR ENGINEERS By Mr. Bapuji Rao,


36
CSEA, IGIT, Sarang
EXAMPLES

PYTHON FOR ENGINEERS By Mr. Bapuji Rao,


37
CSEA, IGIT, Sarang
EXAMPLES

PYTHON FOR ENGINEERS By Mr. Bapuji Rao,


38
CSEA, IGIT, Sarang
for…in( ) OR for…in[ ] LOOP STATEMENT

 It iterates over a collection of items, such as list or


dictionary, and run a block of code with each element
from the collection.
 Syntax:
for variable_name in (item1, item2,.......):
....
Statement(s)

(OR)

for variable_name in [item1, item2,.......]:


....
Statement(s)

PYTHON FOR ENGINEERS By Mr. Bapuji Rao,


39
CSEA, IGIT, Sarang
EXAMPLES

PYTHON FOR ENGINEERS By Mr. Bapuji Rao,


40
CSEA, IGIT, Sarang
EXAMPLES

PYTHON FOR ENGINEERS By Mr. Bapuji Rao,


41
CSEA, IGIT, Sarang
while LOOP STATEMENT

 It is used to repeat (or iterative) one or more statements


as long as the test condition (expression) is True.
 Syntax (Single Statement):

while (Condition):
.... Statement

(OR)

while Condition:
.... Statement

PYTHON FOR ENGINEERS By Mr. Bapuji Rao,


42
CSEA, IGIT, Sarang
while LOOP STATEMENT

 Syntax (Multiple Statements):

while (Condition):
....
Statement-1
....
---------------
....
Statement-N

(OR)
while Condition:
.... Statement-1

.... ---------------

....
Statement-N
PYTHON FOR ENGINEERS By Mr. Bapuji Rao,
43
CSEA, IGIT, Sarang
EXAMPLE

PYTHON FOR ENGINEERS By Mr. Bapuji Rao,


44
CSEA, IGIT, Sarang
QUESTIONS
1. Print the even series and the odd series from 1 to ‘N’.
2. Print the even series and its sum, and the odd series and its sum from 1 to ‘N’.
3. Read any ‘N’ numbers. Then print the sum and the average.
4. Read ‘N’ numbers. Then print the even numbers sum and the odd numbers
sum.
5. Read ‘N’ numbers. Then count number of positives, negatives and zeros.
6. Read ‘N’ numbers. Then find the sum of even numbers and its average, and
the sum of odds and its average.
7. Read a number. Then print its factorial.
8. Read a number. Then check whether it is Prime Number or Not.
9. Read a number. Then check whether it is Perfect Number or Not.
[Link] a number. Then print the sum of digits.
[Link] ‘N’ integers. Then display total numbers of single digit, double digits
and triple digits.
14. Read a number and reverse it.
15. Read three integers. Print the series from Smallest to Largest Number.
16. Read ‘N’ numbers. Then print the smallest and largest number.
PYTHON FOR ENGINEERS By Mr. Bapuji Rao,
45
CSEA, IGIT, Sarang
QUESTIONS
17. Read a number. Then check whether it is Armstrong Number or Not.
18. Read a number. Then print the sum of unit digit and the last digit.
Example:
Input:
Enter a Number = 12457
Output:
(Last Digit + Unit Digit Sum) = 8
19. Read a number. Then print the even position digits sum and odd
position digits sum.
Example:
Input:
Enter a Number = 12457
Output:
Even Position Digits Sum = 7
Odd Position Digits Sum = 12
PYTHON FOR ENGINEERS By Mr. Bapuji Rao,
46
CSEA, IGIT, Sarang
NESTED for…in range( ) LOOP

Syntax:

for variable_name in range(Initial_Value, Final_Value):


.... Statement(s)

.... for variable_name in range(Initial_Value, Final_Value):

.... .... Statement(s)

PYTHON FOR ENGINEERS By Mr. Bapuji Rao,


47
CSEA, IGIT, Sarang
EXAMPLE-1

PYTHON FOR ENGINEERS By Mr. Bapuji Rao,


48
CSEA, IGIT, Sarang
EXAMPLE-2

PYTHON FOR ENGINEERS By Mr. Bapuji Rao,


49
CSEA, IGIT, Sarang
EXAMPLE-3

PYTHON FOR ENGINEERS By Mr. Bapuji Rao,


50
CSEA, IGIT, Sarang
EXAMPLE-4

PYTHON FOR ENGINEERS By Mr. Bapuji Rao,


51
CSEA, IGIT, Sarang
EXAMPLE-5

PYTHON FOR ENGINEERS By Mr. Bapuji Rao,


52
CSEA, IGIT, Sarang
EXAMPLE-5

PYTHON FOR ENGINEERS By Mr. Bapuji Rao,


53
CSEA, IGIT, Sarang
QUESTIONS

1. Print first 'n' natural numbers multiplication table.


2. Print all the prime numbers from 1 to N.
3. Print all the Armstrong Numbers from 1 to 500.
4. Print the Number and its sum of digits from 100 to 200 with step
value 5.
5. Print all the Prime Numbers from M to N, where M is the first
value, N is the last value and M<=N.

PYTHON FOR ENGINEERS By Mr. Bapuji Rao,


54
CSEA, IGIT, Sarang
NESTED while LOOP

Syntax:

while(Outer_Condition):
.... Statement(s)

.... while(Inner_Condition):

.... .... Statement(s)

PYTHON FOR ENGINEERS By Mr. Bapuji Rao,


55
CSEA, IGIT, Sarang
EXAMPLE

PYTHON FOR ENGINEERS By Mr. Bapuji Rao,


56
CSEA, IGIT, Sarang
LOOP
CONTROL
STATEMENTS

PYTHON FOR ENGINEERS By Mr. Bapuji Rao,


57
CSEA, IGIT, Sarang
break STATEMENT

 The break statement stops execution of the statement(s)


of the loop and brings the control out of the loop.
 Syntax:
if(Condition):
....
break

PYTHON FOR ENGINEERS By Mr. Bapuji Rao,


58
CSEA, IGIT, Sarang
EXAMPLE

PYTHON FOR ENGINEERS By Mr. Bapuji Rao,


59
CSEA, IGIT, Sarang
continue STATEMENT

 The continue statement skips the statement(s) for


execution of the loop and sends the control back to the
loop.
 Syntax:
if(Condition):
....
continue

PYTHON FOR ENGINEERS By Mr. Bapuji Rao,


60
CSEA, IGIT, Sarang
EXAMPLE

PYTHON FOR ENGINEERS By Mr. Bapuji Rao,


61
CSEA, IGIT, Sarang
pass STATEMENT

 It is used when we want to do nothing when the condition


is satisfied.
 It doesn’t skip or stop the execution; it just passes the
control to the next iteration.
 Syntax:
if(Condition):
.... pass

PYTHON FOR ENGINEERS By Mr. Bapuji Rao,


62
CSEA, IGIT, Sarang
EXAMPLE

PYTHON FOR ENGINEERS By Mr. Bapuji Rao,


63
CSEA, IGIT, Sarang
BUILT-IN
FUNCTIONS

PYTHON FOR ENGINEERS By Mr. Bapuji Rao,


64
CSEA, IGIT, Sarang
Sl.
Function Description Example
No.
Returns the absolute
1. abs( )
value of a number.
Returns the binary
2. bin( )
version of a number.
Returns the Boolean
3. bool( ) value of the specified
object.
Convert an ASCII Code
4. chr( )
to the ASCII Character
Returns a complex
5. complex()
number.

PYTHON FOR ENGINEERS By Mr. Bapuji Rao,


65
CSEA, IGIT, Sarang
Sl.
Function Description Example
No.
Returns the quotient and
the remainder when
6. divmod( )
argument1 is divided by
argument2.
Evaluates and executes
7. eval( )
an expression.
Executes the specified
8. exec( )
statement.
Returns a floating point
9. float( )
number.
Converts a number into
10. hex( )
a hexadecimal value.
PYTHON FOR ENGINEERS By Mr. Bapuji Rao,
66
CSEA, IGIT, Sarang
Sl.
Function Description Example
No.

11. input ( ) Allows user input.

Returns an integer
12. int ( )
number.
Returns the length of an
13. len ( )
object.
Returns the largest item
14. max ( )
in an iterable.
Returns the smallest
15. min ( )
item in an iterable.
Converts a number into
16. oct ( )
an octal.
PYTHON FOR ENGINEERS By Mr. Bapuji Rao,
67
CSEA, IGIT, Sarang
Sl.
Function Description Example
No.
Convert an ASCII
17. ord ( ) Character to the
equivalent ASCII Code.
Returns the value of x to
18. pow ( )
the power of y.
Prints to the standard
19. print ( )
output device.
Returns a sequence of
range ( ) numbers, starting from
20. [with for
0 and increments by 1
loop]
(by default).

PYTHON FOR ENGINEERS By Mr. Bapuji Rao,


68
CSEA, IGIT, Sarang
Sl.
Function Description Example
No.
To verify an element
present in a range of
21. range ( ) values. If present then
returns True; Otherwise
returns False.

22. round ( ) Rounds a numbers.

23. str ( ) Returns a string object.

Returns the type of an


24. type ( )
[Link] FORCSEA,
ENGINEERS By Mr. Bapuji Rao,
IGIT, Sarang
69
math MODULE

 Python has a built-in module called math, which


extends the list of mathematical methods.

 To use it, you must import the math module:


import math

 When you have imported the math module, you can


start using methods and constants of the module.

PYTHON FOR ENGINEERS By Mr. Bapuji Rao,


70
CSEA, IGIT, Sarang
Sl.
Method Description Example
No.
Rounds a number up to
1. [Link]( )
the nearest integer.
Returns E raised to the
2. [Link]( )
power of x.
Returns the absolute
3. [Link]( )
value of a number.
Returns the factorial of a
4. [Link]( )
number.
Rounds a number down
5. [Link]( )
to the nearest integer.

PYTHON FOR ENGINEERS By Mr. Bapuji Rao,


71
CSEA, IGIT, Sarang
Sl.
Method Description Example
No.
Returns the remainder of
6. [Link]( ) x/y.

Returns the greatest


7. [Link]( ) common divisor of two
integers.
Returns the value of x to
8. [Link]( )
the power of y.
Returns the square root
9. [Link]( )
of a number.

PYTHON FOR ENGINEERS By Mr. Bapuji Rao,


72
CSEA, IGIT, Sarang
USER-DEFINED FUNCTIONS
 A function groups related statements that can be called
together, that typically perform a specific task.

 The function may or may not take a set of parameters.

 The function may or may not have a return a value.

 The def statement is used to define a function. This


statement is also called single clause compound
statement.

PYTHON FOR ENGINEERS By Mr. Bapuji Rao,


73
CSEA, IGIT, Sarang
USER-DEFINED FUNCTIONS
 Return Type of a Function: Python functions explicitly
declare a return type of the function.
 Python functions can return values of any type via the
return keyword.
 A function can return any number of data types.
 Syntax of return:
return Expression-1, Expression-2, ……..
 A function that reaches the end of execution without a
return statement will always return None and such
functions are called as void function.
PYTHON FOR ENGINEERS By Mr. Bapuji Rao,
74
CSEA, IGIT, Sarang
USER-DEFINED FUNCTIONS
 Syntax (Single Statement):
def function_name (Parameter(s)):
.... Statement

 Syntax (Multiple Statements):


def function_name (Parameter(s)):
....
Statement-1
.... Statement-2

.... ---------------

 function_name is known as the identifier of the function.


 Parameter(s) is an optional list of identifiers that get assigned
with the values supplied as arguments when the function is called.
 A function may have an arbitrary number of arguments which are
separated by commas.
PYTHON FOR ENGINEERS By Mr. Bapuji Rao,
75
CSEA, IGIT, Sarang
NO ARGUMENT, NO RETURN VALUE

FUNCTION DEFINITION

FUNCTION CALLING

CNU( ) Function is a void function since it has no return value.

PYTHON FOR ENGINEERS By Mr. Bapuji Rao,


76
CSEA, IGIT, Sarang
NO ARGUMENT, RETURN VALUE

FUNCTION DEFINITION

FUNCTION CALLING

PYTHON FOR ENGINEERS By Mr. Bapuji Rao,


77
CSEA, IGIT, Sarang
ARGUMENT, NO RETURN VALUE

FUNCTION DEFINITION

FUNCTION CALLING

sum_avg(a, b, c) Function is a void function since it has no return


value.
PYTHON FOR ENGINEERS By Mr. Bapuji Rao,
78
CSEA, IGIT, Sarang
ARGUMENT, RETURN VALUE
FUNCTION DEFINITION

FUNCTION CALLING

PYTHON FOR ENGINEERS By Mr. Bapuji Rao,


79
CSEA, IGIT, Sarang
PROGRAM -1

PYTHON FOR ENGINEERS By Mr. Bapuji Rao,


80
CSEA, IGIT, Sarang
PROGRAM -1

PYTHON FOR ENGINEERS By Mr. Bapuji Rao,


81
CSEA, IGIT, Sarang
OUTPUT

PYTHON FOR ENGINEERS By Mr. Bapuji Rao,


82
CSEA, IGIT, Sarang
PROGRAM -2

PYTHON FOR ENGINEERS By Mr. Bapuji Rao,


83
CSEA, IGIT, Sarang
PROGRAM -2

PYTHON FOR ENGINEERS By Mr. Bapuji Rao,


84
CSEA, IGIT, Sarang
OUTPUT

PYTHON FOR ENGINEERS By Mr. Bapuji Rao,


85
CSEA, IGIT, Sarang
RETURNING MULTIPLE
VALUES FROM A FUNCTION

 A user-defined function can return more than one value in the form
of a tuple.
 Syntax of Function Definition:
def Function_Name(Argument(s), if any):
.... Statement-1

.... Statement-2

....
---------------
....
Statement-N
....
return value-1, value-2,......., value-n

 Syntax of calling the Function:


variable-1, variable-2,...., variable-n = Function_Name(Argument(s), if any)

PYTHON FOR ENGINEERS By Mr. Bapuji Rao,


86
CSEA, IGIT, Sarang
PROGRAM-1

PYTHON FOR ENGINEERS By Mr. Bapuji Rao,


87
CSEA, IGIT, Sarang
PROGRAM -1

PYTHON FOR ENGINEERS By Mr. Bapuji Rao,


88
CSEA, IGIT, Sarang
OUTPUT

PYTHON FOR ENGINEERS By Mr. Bapuji Rao,


89
CSEA, IGIT, Sarang
PROGRAM-2

PYTHON FOR ENGINEERS By Mr. Bapuji Rao,


90
CSEA, IGIT, Sarang
PROGRAM-2

OUTPUT

PYTHON FOR ENGINEERS By Mr. Bapuji Rao,


91
CSEA, IGIT, Sarang
QUESTIONS
(USING USER-DEFINED FUNCTION)

1. Find the series sum from 1 to N.


2. Read a number. Then check the number is Prime or Not.
3. Read a number. Then print the number and its factorial.
4. Read a number. Then check the number is Armstrong or not.
5. Read a number. Then print the number and the sum of digits.
6. Read a number. Then check the number is a Palindrome or Not.
7. Read two numbers. Then print its LCM.
8. Read two numbers. Then print its HCF.
9. Read a number. Print the sum of digits in single digit form.
(Example: 1235 = 1+2+3+5 = 11 = 1+1 = 2)
[Link] a number. Print the number and the reverse number.
[Link] any three numbers. Print its sum and average.

PYTHON FOR ENGINEERS By Mr. Bapuji Rao,


92
CSEA, IGIT, Sarang
DEFAULT PARAMETERS / ARGUMENTS

 The function formal arguments / parameters are


assigned with some default values.
 When the function is called without
argument/parameter, the formal argument consider its
default value.
 The default values are assigned in the formal arguments
from right to left direction.
 Syntax (Function Definition):
def FunctionName(Variable-1=value-1, Variable-2=Value-2, .…)
Statement(s)
....

 Syntax (Function Calling):


Function_Name(Argument-1, Argument-2,…….)
PYTHON FOR ENGINEERS By Mr. Bapuji Rao,
93
CSEA, IGIT, Sarang
EXAMPLE

PYTHON FOR ENGINEERS By Mr. Bapuji Rao,


94
CSEA, IGIT, Sarang
FUNCTION WITH AN ARBITRARY
NUMBER OF ARGUMENTS
 Define a function that is capable of taking an arbitrary number
of arguments can be done by prefixing one of the arguments
with a *.
 Syntax (Function Definition):
def Function_Name(*Argument):
.... Statement-1

.... ---------------

.... Statement-N

 Syntax (Function Calling):


Function_Name(Argument-1, Argument-2, …….)

 The Argument will be a tuple containing all the values that are
passed in by calling the function.
PYTHON FOR ENGINEERS By Mr. Bapuji Rao,
95
CSEA, IGIT, Sarang
EXAMPLE-1

OUTPUT

PYTHON FOR ENGINEERS By Mr. Bapuji Rao,


96
CSEA, IGIT, Sarang
EXAMPLE-2

PYTHON FOR ENGINEERS By Mr. Bapuji Rao,


97
CSEA, IGIT, Sarang
OUTPUT

PYTHON FOR ENGINEERS By Mr. Bapuji Rao,


98
CSEA, IGIT, Sarang
EXAMPLE-3

PYTHON FOR ENGINEERS By Mr. Bapuji Rao,


99
CSEA, IGIT, Sarang
FUNCTION WITH AN ARBITRARY NUMBER
OF KEYWORD ARGUMENTS
 An arbitrary number of arguments with a name by defining an
argument in the function definition with two * prefix it.

 Syntax (Function Definition):


def
.... Function_Name(**Argument):
.... Statement-1

.... ---------------

Statement-N
 The Argument will be a dictionary containing the names as
keys and the values as values.

 Syntax (Function Calling):


Function_Name(Variable1=Value1, Variable2=Value2, …….)
PYTHON FOR ENGINEERS By Mr. Bapuji Rao,
100
CSEA, IGIT, Sarang
EXAMPLE

PYTHON FOR ENGINEERS By Mr. Bapuji Rao,


101
CSEA, IGIT, Sarang
SCOPE AND LIFETIME
OF VARIABLES

PYTHON FOR ENGINEERS By Mr. Bapuji Rao,


102
CSEA, IGIT, Sarang
LOCAL VARIABLES

 The variable(s) are created in a function are scoped only


to that function and are not accessible or visible outside
of the function.

 These variables are referred to as local variables since


they are local to the function.

PYTHON FOR ENGINEERS By Mr. Bapuji Rao,


103
CSEA, IGIT, Sarang
EXAMPLE

PYTHON FOR ENGINEERS By Mr. Bapuji Rao,


104
CSEA, IGIT, Sarang
GLOBAL VARIABLES

 Any variables declared in a program in python are


considered as global variable.

 To use any global variable inside a function definition,


then the keyword global must prefix the global variable
name.

 Syntax: global variable_name


PYTHON FOR ENGINEERS By Mr. Bapuji Rao,
105
CSEA, IGIT, Sarang
EXAMPLE-1

PYTHON FOR ENGINEERS By Mr. Bapuji Rao,


106
CSEA, IGIT, Sarang
EXAMPLE-2

PYTHON FOR ENGINEERS By Mr. Bapuji Rao,


107
CSEA, IGIT, Sarang
EXAMPLE-3

PYTHON FOR ENGINEERS By Mr. Bapuji Rao,


108
CSEA, IGIT, Sarang
EXAMPLE-4

PYTHON FOR ENGINEERS By Mr. Bapuji Rao,


109
CSEA, IGIT, Sarang
OUTPUT

PYTHON FOR ENGINEERS By Mr. Bapuji Rao,


110
CSEA, IGIT, Sarang
NON-LOCAL VARIABLES

 Local variables are local to a specific function; even


functions defined within another function cannot
modify the outer functions local variables.
 To modify the local variable of outer function inside the
inner function, then outer function’s local variable must
prefix the keyword nonlocal with the outer function’s
local variable.
 Syntax: nonlocal outer_function_local_variable
PYTHON FOR ENGINEERS By Mr. Bapuji Rao,
111
CSEA, IGIT, Sarang
EXAMPLE-1

PYTHON FOR ENGINEERS By Mr. Bapuji Rao,


112
CSEA, IGIT, Sarang
EXAMPLE-2

PYTHON FOR ENGINEERS By Mr. Bapuji Rao,


113
CSEA, IGIT, Sarang
COMMAND LINE ARGUMENTS

 The arguments that are given after the name of the Python
script are known as Command Line Arguments at the
command line while running the program.
 Input function is not used in the script.
 Three Python modules are used in parsing and managing
the command line arguments:
• sys Module
• getopt Module
• argparse Module
PYTHON FOR ENGINEERS By Mr. Bapuji Rao,
114
CSEA, IGIT, Sarang
COMMAND LINE ARGUMENTS
 sys Module
• The sys module provides functions and variables used to
manipulate different parts of the Python Runtime Environment.
• This module provides access to some variables used or
maintained by the interpreter.
 To run the Python script at the command prompt, then type
python script_name.py Data-1 Data-2 …… Data-N 

PYTHON FOR ENGINEERS By Mr. Bapuji Rao,


115
CSEA, IGIT, Sarang
EXAMPLE-1

PYTHON FOR ENGINEERS By Mr. Bapuji Rao,


116
CSEA, IGIT, Sarang
EXAMPLE-2

PYTHON FOR ENGINEERS By Mr. Bapuji Rao,


117
CSEA, IGIT, Sarang

PYTHON FOR ENGINEERS By Mr. Bapuji Rao,


118
CSEA, IGIT, Sarang
EXCEPTION HANDLING

 Errors might disrupt the normal execution of the program and are
called exceptions.
 An exception is a Python object that represents an error.
 When an error occurs during the execution of a program, an
exception is said to have been raised.
 Such an exception needs to be handled by the programmer so that
the program does not terminate abnormally.
 Examples:
Trying to open a file that does not exist.
Division by zero and so on.
 There are two types of exceptions such as BUILT-IN
EXCEPTIONS and USER-DEFINED EXCEPTIONS.
 Exception problem is solved by using try and except statements.

PYTHON FOR ENGINEERS By Mr. Bapuji Rao,


119
CSEA, IGIT, Sarang
BUILT-IN EXCEPTION

Sl.
EXCEPTION DESCRIPTION
No.
1. Exception Base class for all exceptions.
2. SystemExit Raised by the [Link]( ) function.
Base class for all errors that occur for numeric
3. ArithmeticError
calculation.
It is raised when there is an error in the syntax of
4. SyntaxError
the Python code.
It is raised when the denominator in a division
5. ZeroDivisionError
operation is zero.
It is raised when the index or subscript in a
6. IndexError
sequence is out of range.
It is raised when a built-in method receives an
7. ValueError
argument of mismatched or inappropriate values.
PYTHON FOR ENGINEERS By Mr. Bapuji Rao,
120
CSEA, IGIT, Sarang
try...except...else SYNTAX
(except clause with no EXCEPTION)

try:
# Statement(s)
except:
# Statement(s)
else:
# Statement(s)

PYTHON FOR ENGINEERS By Mr. Bapuji Rao,


121
CSEA, IGIT, Sarang
EXAMPLE

PYTHON FOR ENGINEERS By Mr. Bapuji Rao,


122
CSEA, IGIT, Sarang
try...except...else SYNTAX

try:
# Statement(s)
except Exception-1:
# Exception-1 Statement(s)
except Exception-2:
# Exception-2 Statement(s)
--------------------------------------
else:
# Statement(s)

PYTHON FOR ENGINEERS By Mr. Bapuji Rao,


123
CSEA, IGIT, Sarang
EXAMPLE

PYTHON FOR ENGINEERS By Mr. Bapuji Rao,


124
CSEA, IGIT, Sarang
try...except...else SYNTAX
(except clause with multiple EXCEPTION)

try:
# Statement(s)
except (Exception-1, [Exception-2, ........., Exception-N]):
# Statement(s)
else:
# Statement(s)

PYTHON FOR ENGINEERS By Mr. Bapuji Rao,


125
CSEA, IGIT, Sarang
EXAMPLE

PYTHON FOR ENGINEERS By Mr. Bapuji Rao,


126
CSEA, IGIT, Sarang
PYTHON FOR ENGINEERS By Mr. Bapuji Rao,
127
CSEA, IGIT, Sarang
try...finally SYNTAX
 It is an optional clause.
 The statements inside the finally block are always executed
regardless of whether an exception is occurred in the try block or
not.
 It should always be placed at the end of try clause.

try:
# Statement(s)
except Exception-1:
# Statement(s)
except Exception-2:
# Statement(s)
-------------------------
finally:
# Statement(s)
PYTHON FOR ENGINEERS By Mr. Bapuji Rao,
128
CSEA, IGIT, Sarang
EXAMPLE

PYTHON FOR ENGINEERS By Mr. Bapuji Rao,


129
CSEA, IGIT, Sarang

You might also like