SlideShare una empresa de Scribd logo
1 de 116
Python for Machine Learning
Course Code : CST283
Category : Minor
Offered by : Dept. of Computer Science and Engg.
Faculty in charge : Jaseela Beevi S
1
Dept. of CSE, TKMCE
Syllabus - Module II
Building Python Programs:
Control statements - Selection structure (if-else, switch-case), Iteration structure(for,
while), Testing the control statements, Lazy evaluation. Functions - Hiding
redundancy and complexity, Arguments and return values, Variable scopes and
parameter passing, Named arguments, Main function, Working with recursion,
Lambda functions. Strings and number systems - String function, Handling numbers
in various formats.
2
Dept. of CSE, TKMCE
Control Statements
3
Dept. of CSE, TKMCE
control statements—statements that allow the computer to select or repeat an
action.
üSelection structure : statements which determines whether other statements
will be executed
e.g. if-else, switch-case
üIteration structure : statements which determines how many time to execute
another statements.
e.g. for, while
Selection: if and if-else Statements
4
Dept. of CSE, TKMCE
• if-else statement:
ü It is also called a two-way selection statement, because it directs the computer
to make a choice between two alternative courses of action.
üIt is often used to check inputs for errors and to respond with error messages if
necessary.
if <condition>:
<sequence of statements–1>
else:
<sequence of statements–2>
ü The condition in the if-else statement must be a Boolean expression—that is, an
expression that evaluates to either true or false.
if-else Statements
5
Dept. of CSE, TKMCE
e.g., prints the maximum and minimum of two input numbers.
Selection: if and if-else Statements
6
Dept. of CSE, TKMCE
• if statement:
üThe simplest form of selection is the if statement. This type of control statement
is also called a one-way selection statement.
üit consists of a condition and just a single sequence of statements. If the
condition is True, the sequence of statements is run. Otherwise, control
proceeds to the next statement following the entire selection statement.
if <condition>:
<sequence of statements>
7
Dept. of CSE, TKMCE
if x < 0:
x = –x
if - Statements
8
Dept. of CSE, TKMCE
The process of testing several conditions and responding accordingly can be
described in code by a multi-way selection statement.
if <condition-1>:
<sequence of statements-1>
.
.
.
elif <condition-n>:
<sequence of statements-n>
else:
<default sequence of statements>
Multiway if statement
9
Dept. of CSE, TKMCE
Introduction to Python Switch Statement
10
Dept. of CSE, TKMCE
• A switch statement is a very useful and powerful programming feature. It is an
alternate to if-else-if ladder statement and provides better performance and more
manageable code than an if-else-if ladder statement.
• Python language does not provide any inbuilt switch statements. We can
implement this feature with the same flow and functionality but with different
syntax and implementation using Python Dictionary.
Flow Chart
11
Dept. of CSE, TKMCE
Syntax of Switch Statement
12
Dept. of CSE, TKMCE
1. Switch in Other Languages (c, Java,..)
switch(N)
{
case 1: Statement if N = 1;
break;
case 2: Statement if N = 2;
break;
::
case n: Statement if N = n;
break;
default: Statement if N doesn't match any
}
13
Dept. of CSE, TKMCE
2. Switch Implementation in Python
switcher=
{
key_1: value_1/method_1(),
key_2: value_2/method_2(),
key_3: value_3/method_3(),
::
key_n: value_n/method_n(),
}
key = N
value = switcher.get(key, "default")
Implementation of Switch statement in Python
14
Dept. of CSE, TKMCE
def get_week_day(argument):
switcher = {
0: "Sunday",
1: "Monday",
2: "Tuesday",
3: "Wednesday",
4: "Thursday",
5: "Friday",
6: "Saturday"
}
return switcher.get(argument, "Invalid day")
print (get_week_day(6))
print (get_week_day(8))
print (get_week_day(0))
The Boolean Type, Comparisons, and Boolean
Expressions
15
Dept. of CSE, TKMCE
Boolean Data Type: The Boolean data type consists of only two data values—true
and false. Simple Boolean expressions consist of the Boolean values True or False,
variables bound to those values, function calls that return Boolean values, or
comparisons.
Logical Operators and Compound Boolean Expressions
16
Dept. of CSE, TKMCE
number = int(input("Enter the numeric grade: "))
if number > 100:
print("Error: grade must be between 100 and 0")
elif number < 0:
print("Error: grade must be between 100 and 0")
else:
# The code to compute and print the result goes here
• The two conditions can be combined in a Boolean expression that uses the logical operator
or. The resulting expression is compound Boolean expression
number = int(input("Enter the numeric grade: "))
if number > 100 or number < 0:
print("Error: grade must be between 100 and 0")
else:
# The code to compute and print the result goes here
17
Dept. of CSE, TKMCE
• Another way to describe this situation using and operator is
number = int(input("Enter the numeric grade: "))
if number >= 0 and number <= 100:
# The code to compute and print the result goes here
else:
print("Error: grade must be between 100 and 0")
Precedence of Logical Operators
18
Dept. of CSE, TKMCE
>>> A = True
>>> B = False
>>> A and B
False
>>> A or B
True
>>> not A
False
The logical operators are evaluated after comparisons but before the assignment
operator. The not operator has a higher precedence than the and operator, which has
a higher precedence than the or operator.
Thus, in our example, not A and B evaluates to False, whereas not (A and B) evaluates
to True.
19
Dept. of CSE, TKMCE
Short-Circuit Evaluation
20
Dept. of CSE, TKMCE
The Python virtual machine sometimes knows the value of a Boolean expression before it
has evaluated all of its operands.
For instance, in the expression A and B, if A is false, there is no need to evaluate B.
Likewise, in the expression A or B, if A is true, there is no need to evaluate B.
This approach, in which evaluation stops as soon as possible, is called short-circuit
evaluation.
count = int(input("Enter the count: "))
sum = int(input("Enter the sum: "))
if count > 0 and sum // count > 10:
print("average > 10")
else:
print("count = 0 or average <= 10")
21
Dept. of CSE, TKMCE
Exercises:
• 1. Assume that x is 3 and y is 5. Write the values of the following expressions:
a) x == y
b) x > y – 3
c) x <= y – 2
d) x == y or x > 2
e) x != 6 and y > 10
f) x > 0 and x < 100
2. Assume that x refers to a number. Write a code segment that prints the number’s
absolute value without using Python’s abs function.
Definite Iteration: The for Loop
22
Dept. of CSE, TKMCE
Loops - They are the control statements with repetition statements, also known as
loops, which repeat an action. Each repetition of the action is known as a pass or
an iteration.
There are two types of loops—
üthose that repeat an action a predefined number of times (definite iteration)
üthose that perform the action until the program determines that it
needs to stop (indefinite iteration).
23
Dept. of CSE, TKMCE
24
Dept. of CSE, TKMCE
Syntax:
for <variable> in range(<an integer expression>):
<statement-1>
.
.
<statement-n>
• The first line of code in a loop is sometimes called the loop header.
• The integer value or expression in the range() tells the loop how many times to call
the below statements.
• The loop body comprises the statements in the remaining lines of code, below the
header. These statements are executed in sequence on each pass through the loop.
25
Dept. of CSE, TKMCE
• Implementation of exponentiation operator using for loop
#computation of 23
>>> number = 2
>>> exponent = 3
>>> product = 1
>>> for eachPass in range(exponent):
product = product * number
print(product, end = " ")
2 4 8
>>> product
8
Count-Controlled Loops
26
Dept. of CSE, TKMCE
üLoops that count through a range of numbers are also called count-
controlled loops.
üIt counts from 0 to the value of the header’s integer expression minus 1.
üOn each pass through the loop, the header’s variable is bound to the
current value of this count.
e.g.,
>>> for count in range(4):
print(count, end = " ")
0 1 2 3
üTo count from an explicit lower bound, the programmer can supply a second integer
expression in the loop header. When two arguments are supplied to range, the
count ranges from the first argument to the second argument minus 1.
27
Dept. of CSE, TKMCE
# method - 2 : computation of factorial of 4
>>> product = 1
>>> for count in range(1, 5):
product = product * count
>>> product
24
Syntax:
for <variable> in range(<lower bound>, <upper bound + 1>):
<loop body>
# method - 1 : computation of factorial of 4
>>> product = 1
>>> for count in range(4):
product = product * (count + 1)
>>> product
24
28
Dept. of CSE, TKMCE
# program to find the sum of first 10 integer numbers:
>>> lower = int(input("Enter the lower bound: "))
Enter the lower bound: 1
>>> upper = int(input("Enter the upper bound: "))
Enter the upper bound: 10
>>>
>>>theSum = 0
>>>for number in range(lower, upper+1):
theSum = theSum + number
>>>print(theSum)
55
Augmented Assignment
29
Dept. of CSE, TKMCE
• The assignment symbol can be combined with the arithmetic and concatenation
operators to provide augmented assignment operations.
üa = 17
üs = "hi"
üa += 3 # Equivalent to a = a + 3
üa -= 3 # Equivalent to a = a - 3
üa *= 3 # Equivalent to a = a * 3
üa /= 3 # Equivalent to a = a / 3
üa %= 3 # Equivalent to a = a % 3
üs += " there" # Equivalent to s = s + " there"
All these examples have the format
<variable> <operator>= <expression>
Loop Errors: Off-by-One Error
30
Dept. of CSE, TKMCE
• Once we get the syntax correct, we need to be concerned about only one other
possible error: The loop fails to perform the expected number of iterations.
Because this number is typically off by one, the error is called an off-by-one error.
• For the most part, off-by-one errors result when the programmer incorrectly
specifies the upper bound of the loop.
• # Count from 1 through 4, we think
• >>> for count in range(1,4):
• print(count)
•
• 1 2 3
Traversing the Contents of a Data Sequence
31
Dept. of CSE, TKMCE
• The for loop itself visits each number in a sequence of numbers generated by the
range function.
• The sequence of numbers generated by the function range is fed to Python’s
list function, which returns a special type of sequence called a list.
>>> list(range(4))
[0, 1, 2, 3]
>>> list(range(1, 5))
[1, 2, 3, 4]
Strings are also sequences of characters. The values contained in any sequence can
be visited by running a for loop, as follows:
for <variable> in <sequence>:
<do something with variable>
Specifying the Steps in the Range
32
Dept. of CSE, TKMCE
• In some programs we might want a loop to skip some numbers.
• A variant of Python’s range function expects a third argument that allows you to
nicely skip some numbers. The third argument specifies a step value, or the interval
between the numbers used in the range.
>>> list(range(1, 6, 1)) # Same as using two arguments
[1, 2, 3, 4, 5]
>>> list(range(1, 6, 2)) # Use every other number
[1, 3, 5]
>>> list(range(1, 6, 3)) # Use every third number
[1, 4]
33
Dept. of CSE, TKMCE
Suppose you had to compute the sum of the even numbers between 1 and 10.
>>> theSum = 0
>>> for count in range(2, 11, 2):
theSum += count
>>> theSum
30
Loops that Count Down
When the step argument is a negative number, the range function generates a
sequence of numbers from the first argument down to the second argument plus 1.
Thus, in this case, the first argument should express the upper bound, and the second
argument should express the lower bound minus 1.
34
Dept. of CSE, TKMCE
Exercises
1. Write the outputs of the following loops:
a. for count in range(5):
print(count + 1, end = " ")
b. for count in range(1, 4):
print(count, end = " ")
c. for count in range(1, 6, 2):
print(count, end = " ")
d. for count in range(6, 1, –1):
print(count, end = " ")
Formatting Text for Output
35
Dept. of CSE, TKMCE
To maintain the margins between columns of data, left-justification requires the addition
of spaces to the right of the datum, whereas right-justification requires adding spaces to
the left of the datum. A column of data is centered if there are an equal number of
spaces on either side of the data within that column.
The total number of data characters and additional spaces for a given datum in a
formatted string is called its field width.
how to right-justify and left-justify the string "four" within a field width of 6:
>>> "%6s" % "four" # Right justify
' four'
>>> "%-6s" % "four" # Left justify
'four '
36
Dept. of CSE, TKMCE
The simplest form of this operation is the following:
<format string> % <datum>
üThe format string can contain string data and other information about the format of
the datum.
ü When the field width is positive, the datum is right-justified; when the field width is
negative, you get left-justification.
üIf the field width is less than or equal to the datum’s print length in characters, no
justification is added.
ü To format a sequence of data values,
<format string> % (<datum–1>, ..., <datum–n>)
37
Dept. of CSE, TKMCE
The format information for a data value of type float has the form
<%field width> . <precision>f
>>> salary = 100.00
>>> print("Your salary is $" + str(salary))
Your salary is $100.0
>>> print("Your salary is $%0.2f" % salary)
Your salary is $100.00
To use a field width of 6 and a precision of 3 to format the float value 3.14:
>>> "%6.3f" % 3.14
' 3.140'
38
Dept. of CSE, TKMCE
Exercises:
• 1. Assume that the variable amount refers to 24.325. Write the outputs of the following
statements:
a. print("Your salary is $%0.2f" % amount)
b. print("The area is %0.1f" % amount)
2. Write a code segment that displays the values of the integers x, y, and z on a single
line, such that each value is right-justified with a field width of 6.
3. Write a format operation that builds a string for the float variable amount that has
exactly two digits of precision and a field width of zero.
4. Write a loop that outputs the numbers in a list named salaries. The outputs should be
formatted in a column that is right-justified, with a field width of 12 and a precision of 2.
Conditional Iteration: The while Loop
39
Dept. of CSE, TKMCE
In many situations, however, the number of iterations in a loop is unpredictable. The
loop eventually completes its work, but only when a condition changes. The process
continues to repeat as long as a condition remains true. This type of process is called
conditional iteration, meaning that the process continues to repeat as long as a
condition remains true.
Syntax:
while <condition>:
<sequence of statements>
The Structure and Behavior of the Loop
40
Dept. of CSE, TKMCE
• The form of this statement is almost identical to that of the one-way selection
statement.
• The use of the reserved word while instead of if indicates that the sequence of
statements might be executed many times, as long as the condition remains true.
• Clearly, something eventually has to happen within the body of the loop to make the
loop’s continuation condition become false. Otherwise, the loop will continue forever,
an error known as an infinite loop.
• At least one statement in the body of the loop must update a variable that affects the
value of the condition.
41
Dept. of CSE, TKMCE
Consider the pseudocode given below:
set the sum to 0.0
input a string
while the string is not the empty string
convert the string to a float
add the float to the sum
input a string
print the sum
• The first input statement initializes a variable to a value that the loop condition can
test. This variable is also called the loop control variable.
• The second input statement obtains the other input values, including one that will
terminate the loop.
42
Dept. of CSE, TKMCE
theSum = 0.0
data = input("Enter a number or just enter to quit: ")
while data != "":
number = float(data)
theSum += number
data = input("Enter a number or just enter to quit: ")
print("The sum is", theSum)
Output:
Enter a number or just enter to quit: 3
Enter a number or just enter to quit: 4
Enter a number or just enter to quit: 5
Enter a number or just enter to quit:
The sum is 12.0
The while loop is also called an entry-
control loop, because its condition is
tested at the top of the loop. This
implies that the statements within
the loop can execute zero or more
times.
Count Control with a while Loop
43
Dept. of CSE, TKMCE
# Summation with a for loop
theSum = 0
for count in range(1, 11):
theSum += count
print(theSum)
It includes a Boolean expression and two extra statements that refer to the count
variable. This loop control variable must be explicitly initialized before the loop header
and incremented in the loop body. *
# Summation with a while loop
theSum = 0
count = 1
while count <= 10:
theSum += count
count += 1
print(theSum)
The while True Loop and the break Statement
44
Dept. of CSE, TKMCE
#Computation of sum using break within while loop
theSum = 0.0
while True:
data = input("Enter a number or just enter to quit: ")
if data == "":
break
number = float(data)
theSum += number
print("The sum is", theSum)
Within this body, the input datum is received. It is then tested for the loop’s termination
condition in a one-way selection statement. If the user wants to quit, the input will equal the
empty string, and the break statement will cause an exit from the loop. Otherwise, control
continues beyond the selection statement to the next two statements that process the input.
The “break” Statement
45
Dept. of CSE, TKMCE
The “continue” Statement
46
Dept. of CSE, TKMCE
Random Numbers
47
Dept. of CSE, TKMCE
üTo simulate randomness in computer applications, programming languages include
resources for generating random numbers.
üThe function random.randint returns a random number from among the numbers
between the two arguments and including those numbers.
#Code to simulate the role of a die 10 times.
>>> import random
>>> for roll in range(10):
print(random.randint(1, 6), end = " ")
2 4 6 4 3 2 3 6 2 2
48
Dept. of CSE, TKMCE
Develop a simple guessing game using random.randint
At start-up, the user enters the smallest number and the largest number in the
range. The computer then selects a number from this range. On each pass through the
loop, the user enters a number to attempt to guess the number selected by the
computer. The program responds by saying “You’ve got it,” “Too large, try again,” or “Too
small, try again.” When the user finally guesses the correct number, the program
congratulates him and tells him the total number of guesses.
49
Dept. of CSE, TKMCE
import random
smaller = int(input("Enter the smaller number: "))
larger = int(input("Enter the larger number: "))
myNumber = random.randint(smaller, larger)
count = 0
while True:
count += 1
userNumber = int(input("Enter your guess: "))
if userNumber < myNumber:
print("Too small!")
elif userNumber > myNumber:
print("Too large!")
else:
print("Congratulations! You've got it in", count,"tries!")
break
50
Dept. of CSE, TKMCE
Exercises
1. Translate the following for loops to equivalent while loops:
a. for count in range(100):
print(count)
b. for count in range(1, 101):
print(count)
c. for count in range(100, 0, –1):
print(count)
2. The factorial of an integer N is the product of the integers between 1 and N, inclusive.
Write a while loop that computes the factorial of a given integer N.
51
Dept. of CSE, TKMCE
Exercises
3. Write a program that accepts the lengths of three sides of a triangle as inputs.
The program output should indicate whether or not the triangle is an equilateral
triangle.
4. Write a program that accepts the lengths of three sides of a triangle as inputs.
The program output should indicate whether or not the triangle is a right triangle. Recall
from the Pythagorean theorem that in a right triangle, the square of one side equals the
sum of the squares of the other two sides.
Strings and Number System
52
Dept. of CSE, TKMCE
The Structure of Strings:
String is a data structure.
A string is a sequence of zero or more characters.
eg. "Hi there!"
empty string “”
A string’s length is the number of characters it contains. Python’s len function returns
this value when it is passed a string.
>>> len("Hi there!")
9
>>> len("")
0
53
Dept. of CSE, TKMCE
The positions of a string’s characters are numbered from 0, on the left, to the length of
the string minus 1, on the right.
The string is an immutable data structure. This means that its internal data elements,
the characters, can be accessed, but cannot be replaced, inserted, or removed.
The Subscript Operator:
a simple for loop can access any of the characters in a string, sometimes you just want
to inspect one character at a given position without visiting them all. The subscript
operator [] makes this possible.
<a string>[<an integer expression>]
54
Dept. of CSE, TKMCE
The integer expression is also called an index.
>>> name = "Alan Turing"
>>> name[0]
'A'
>>> name[3]
'n'
>>> name[len(name)]
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
IndexError: string index out of range
>>> name[len(name) - 1]
'g'
>>> name[-l]
'g'
>>> name[-2]
'n'
55
Dept. of CSE, TKMCE
The next code segment uses a count-controlled loop to display the characters and their
positions:
>>> data = "apple"
>>> for index in range(len(data)):
print(index, data[index])
0 a
1 p
2 p
3 l
4 e
Slicing for Substrings
You can use Python’s subscript operator to obtain a substring through a process called
slicing. To extract a substring, the programmer places a colon (:) in the subscript.
56
Dept. of CSE, TKMCE
>>> name = "myfile.txt" # The entire string
>>> name[0 : ]
'myfile.txt'
>>> name[0 : 1] # The first character
'm'
>>> name[ 0 : 2] # The first two characters
'my'
>>> name[ : len(name)] # The entire string
'myfile.txt'
>>> name[-3 : ] # The last three characters
'txt'
>>> name[ 2 : 6] # Drill to extract 'file'
'file'
Testing for a Substring with the in Operator
57
Dept. of CSE, TKMCE
Python’s in operator : We can search for a substring or a character using this operator.
üWhen used with strings, the left operand of in is a target substring, and the right
operand is the string to be searched.
üThe operator in returns True if the target string is somewhere in the search string, or
False otherwise.
eg.,
>>> fileList = ["myfile.txt", "myprogram.exe", "yourfile.txt"]
>>> for fileName in fileList:
if ".txt" in fileName:
print(fileName)
o/p - myfile.txt
yourfile.txt
58
Dept. of CSE, TKMCE
Exercises
1. Assume that the variable data refers to the string "myprogram.exe". Write the values
of the following expressions:
a. data[2]
b. data[-1]
c. len(data)
d. data[0:8]
2. Assume that the variable data refers to the string "myprogram.exe". Write the
expressions that perform the following tasks:
a. Extract the substring "gram" from data.
b. Truncate the extension ".exe" from data.
c. Extract the character at the middle position from data.
59
Dept. of CSE, TKMCE
3. Assume that the variable myString refers to a string. Write a code segment that
uses a loop to print the characters of the string in reverse order.
4. Assume that the variable myString refers to a string, and the variable
reversedString refers to an empty string. Write a loop that adds the characters
from myString to reversedString in reverse order.
Number System
60
Dept. of CSE, TKMCE
The value of each digit in a number can be determined using −
ü The digit
ü The position of the digit in the number
ü The base of the number system (where the base is defined as the total number of
digits available in the number system)
1) decimal number system - base 10 number system
0, 1, 2, 3, 4, 5, 6, 7, 8, and 9 as digits
2) binary number system - base 2 number system
binary 0 and 1
3) octal number system - base 8 number system
0, 1, 2, 3, 4, 5, 6, and 7
4) hexadecimal number system - base 16 number system
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, A, B, C, D, E, F
61
Dept. of CSE, TKMCE
To identify the system being used, you attach the base as a subscript to the number.
415 in binary notation 1100111112
415 in octal notation 6378
415 in decimal notation 41510
415 in hexadecimal notation 19F16
üThe digits used in each system are counted from 0 to n – 1, where n is the system’s base.
üThus, the digits 8 and 9 do not appear in the octal system.
üTo represent digits with values larger than 910, systems such as base 16 use letters. Thus,
A16 represents the quantity 1010, whereas 1016 represents the quantity 1610
The Positional System for Representing Numbers
62
Dept. of CSE, TKMCE
positional notation—that is, the value of each digit in a number is determined by the
digit’s position in the number. In other words, each digit has a positional value.
How to find positional value of a digit in a number?
The positional value of a digit is determined by raising the base of the system to the
power specified by the position . (baseposition)
For an n-digit number, the positions are numbered from n – 1 down to 0, starting with
the leftmost digit and moving to the right.
eg., the positional values of the three-digit number 41510 are:
100 (10 ) 2 , 10 (10 )1 , and 1 (10 ) 0 , moving from left to right in the number.
63
Dept. of CSE, TKMCE
To determine the quantity represented by a number in any system from base 2 through
base 10, you multiply each digit (as a decimal number) by its positional value and add
the results.
The following example shows how this is done for a three-digit number in base 10:
Converting Binary to Decimal
64
Dept. of CSE, TKMCE
conversion process : Multiply the value of each bit (0 or 1) by its positional value and
add the results.
65
Dept. of CSE, TKMCE
Converts a string of bits to a decimal integer.
bitString = input("Enter a string of bits: ")
decimal = 0
exponent = len(bitString) - 1
for digit in bitString:
decimal = decimal + int(digit) * 2 ** exponent
exponent = exponent - 1
print("The integer value is", decimal)
Enter a string of bits: 1111
The integer value is 15
Converting Decimal to Binary
66
Dept. of CSE, TKMCE
1) This algorithm repeatedly divides the decimal number by 2.
2) After each division, the remainder (either a 0 or a 1) is placed at the beginning of a
string of bits.
3) The quotient becomes the next dividend in the process.
4) The string of bits is initially empty, and the process continues while the decimal
number is greater than 0.
67
Dept. of CSE, TKMCE
decimal = int(input("Enter a decimal number: "))
if decimal == 0:
print(0)
else:
bitString = ""
while decimal > 0:
remainder = decimal % 2
decimal = decimal // 2
bitString = str(remainder) + bitString
print("The binary representation is", bitString)
Enter a decimal number: 156
The binary representation is 00111001
Conversion Shortcuts
68
Dept. of CSE, TKMCE
69
Dept. of CSE, TKMCE
üNote the rows that contain exact powers of 2 (2, 4, and 8 in decimal).
üEach of the corresponding binary numbers in that row contains a 1 followed by a
number of zeroes that equal the exponent used to compute that power of 2.
üThus, a quick way to compute the decimal value of the number 100002 is 24 or 1610.
üThe rows whose binary numbers contain all 1s correspond to decimal numbers that are
one less than the next exact power of 2.
üFor example, the number 1112 equals 23-1 , or 710.
Thus, a quick way to compute the decimal value of the number 111112 is 25 -1 or 3110.
Octal Number System
70
Dept. of CSE, TKMCE
• It requires only 3 bits to represent value of any digit.
• Octal numbers are indicated by the addition of either an 0o prefix or an 8 subscript.
• Position of every digit has a weight which is a power of 8.
• Numeric value of an octal number is determined by multiplying each digit of the
number by its positional value and then adding the products.
• The main advantage of using Octal numbers is that it uses less digits than decimal and
Hexadecimal number system. So, it has fewer computations and less computational
errors.
Conversions
71
Dept. of CSE, TKMCE
Binary to Octal Conversion
72
Dept. of CSE, TKMCE
Octal to Binary Conversion
73
Dept. of CSE, TKMCE
Octal to Decimal Conversion
74
Dept. of CSE, TKMCE
Decimal to Octal Conversion
75
Dept. of CSE, TKMCE
Binary to Octal Conversion
76
Dept. of CSE, TKMCE
bitString = input("Enter a string of bits: ")
decimal = 0
exponent = len(bitString) - 1
for digit in bitString:
decimal = decimal + int(digit) * 2 ** exponent
exponent = exponent - 1
i=1
octal=0
while decimal != 0:
octal += int(decimal % 8)*i
decimal /= 8
i *= 10
print("octal number is: ",octal)
Octal to Binary Conversion
oc = int(input("Enter the octal number: "))
dec = 0
i = 0
while oc != 0:
dec = dec + (oc % 10) * pow(8,i)
oc = oc // 10
i = i+1
bi = ""
while dec != 0:
rem = dec % 2
dec = dec // 2
bi = str(rem) + bi
print("binary number is:", bi)
77
Dept. of CSE, TKMCE
Hexadecimal Number System
• It requires only 4 bits to represent value of any digit.
• Hexadecimal numbers are indicated by the addition of either an 0x prefix or an 16
as subscript.
• Position of every digit has a weight which is a power of 16.
• Numeric value of a hexadecimal number is determined by multiplying each digit
of the number by its positional value and then adding the products.
• So, it is also a positional (or weighted) number system.
78
Dept. of CSE, TKMCE
Binary to Hexadecimal Conversion
Factor the bits into groups of four and look up the corresponding hex digits.
79
Dept. of CSE, TKMCE
Hexadecimal to Binary Conversion
• Each digit in the hexadecimal number is equivalent to four digits in the binary
number.
• Thus, to convert from hexadecimal to binary, you replace each hexadecimal digit
with the corresponding 4-bit binary number.
80
Dept. of CSE, TKMCE
String Methods
• Python includes a set of string operations called methods.
• A method behaves like a function but has a slightly different syntax.
• Unlike a function, a method is always called with a given data value called an object,
which is placed before the method name in the call.
<an object>.<method name>(<argument-1>,..., <argument-n>)
Methods can also expect arguments and return values.
A method knows about the internal state of the object with which it is called.
81
Dept. of CSE, TKMCE
82
Dept. of CSE, TKMCE
83
Dept. of CSE, TKMCE
some string methods in action:
>> s = "Hi there!"
>>> len(s)
9
>>> s.center(11)
' Hi there! '
>>> s.count('e') 2
>>> s.endswith("there!")
True
>>> s.startswith("Hi")
True
>>> s.find("the")
3
84
Dept. of CSE, TKMCE
>>> s.isalpha()
False
>>> 'abc'.isalpha()
True
>>> "326".isdigit()
True
>>> words = s.split()
>>> words
['Hi', 'there!']
>>> " ".join(words)
'Hithere!'
>>> " ". join(words)
'Hi there!'
>>> s.lower()
'hi there!'
>>> s.upper()
'HI THERE!'
>>> s.replace('i', 'o')
'Ho there!'
• Extracting a filename’s extension using split() method
• >>> "myfile.txt". split('.')
['myfile', 'txt']
• >>> "myfile.py". split('.')
['myfile', 'py']
• >>> "myfile.html". split('.')
['myfile', 'html']
To write a general expression for obtaining any filename’s extension, as follows:
filename.split('.')[-1]
>>> filename="myfile.txt"
>>> filename.split('.')[-1]
'txt'
85
Dept. of CSE, TKMCE
• Exercises
1. Assume that the variable data refers to the string "Python rules!". Use a string
method from perform the following tasks:
a. Obtain a list of the words in the string.
b. Convert the string to uppercase.
c. Locate the position of the string "rules".
d. Replace the exclamation point with a question mark.
• 2. Using the value of data from Exercise 1, write the values of the following
expressions:
a. data.endswith('i')
b. " totally ". join(data.split())
86
Dept. of CSE, TKMCE
Functions
Functions are the primary and most important method of code organization and
reuse in Python. Functions can also help make your code more readable by giving a name
to a group of Python statements.
Functions are declared with the def keyword and returned from with the return key- word:
def my_function(x, y, z=1.5):
if z > 1:
return z * (x + y)
else:
return z / (x + y)
87
Dept. of CSE, TKMCE
There are many built-in functions provided by Python such as dir(), len(), abs(), etc.
Users can also build their own functions, which are called user-defined functions.
There are many advantages of using functions:
a) They reduce duplication of code in a program.
b) They break the large complex problems into small parts.
c) They help in improving the clarity of code.
d) A piece of code can be reused as many times as we want with the help of functions.
88
Dept. of CSE, TKMCE
When a function is called, any expressions supplied as arguments are first evaluated.
Their values are copied to temporary storage locations named by the parameters in the
function’s definition.
The parameters play the same role as variables in the code that the function then
executes.
A function may have one or more return statements, whose purpose is to terminate the
execution of the function and return control to its caller.
A return statement may be followed by an expression. In that case, Python evaluates the
expression and makes its value available to the caller when the function stops execution.
89
Dept. of CSE, TKMCE
Built-In Functions
• They are already defined in Python Programming Language.
• We can directly invoke them to perform a specific task.
• Examples of Built-In Functions
a) Type Conversion / explicit conversion - int(), float(), char()
b) Type Coercion / implicit conversion
c) Mathematical Functions - sin(), log(), cos(), cosec(), etc.
d) Date and Time
e) dir() Function
f) help() Function
90
Dept. of CSE, TKMCE
ü Functions as Abstraction Mechanisms
ü Functions Eliminate Redundancy
ü Functions Hide Complexity
91
Dept. of CSE, TKMCE
Functions As Abstract Mechanism,
An abstraction hides detail and thus allows a person to view many things as just one thing.
Functions Eliminate Redundancy
The first way that functions serve as abstraction mechanisms is by eliminating redundant,
or repetitious, code.
def summation(lower, upper):
result = 0
while lower <= upper:
result += lower
lower += 1
return result
>>> summation(1,4) #The summation of the numbers 1..4
10
>>> summation(50,100) # The summation of the numbers 50..100
3825
92
Dept. of CSE, TKMCE
User Defined Functions
• Python also allows users to define their own functions.
• To use their own functions in Python, users have to define the function first -
Function Definition.
• In a function definition, users have to define a name for the new function and also
the list of the statements that will execute when the function will be called.
Syntax:
def functionname(parameters):
“function docstring”
statement(s)
return [expression]
93
Dept. of CSE, TKMCE
Parameters and Arguments
They are the values or expressions passed to the functions between paranthesis.
The value of the argument is always assigned to a variable known as parameter.
There can be four types of formal arguments using which a function can be called
which are as follows:
1) Required Arguments
2) Keyword Arguments
3) Default Arguments
4) Variable-length arguments
94
Dept. of CSE, TKMCE
1. Required Arguments : When we assign the parameters to a function at the time of
function definition, at the time of calling, the arguments should be passed to a function
in correct positional order; furthermore, the no: of arguments should match the defined
no: of parameters.
2. Keyword Arguments: The caller recognises the arguments by the parameter's names.
This type of argument can also be out of order.
3. Default Arguments: We can assign a value to a parameter at the time of function
definition. This value is considered the default value to that parameter. If we do not
provide a value to the parameter at the time of calling, it will not produce an error.
Instead it will pick the default value and use it.
4. Variable Length Arguments: The names for these arguments are not specified in the
function definition. Instead we use * before the name of the variable which holds the
value for all non-keyword variable arguments.
95
Dept. of CSE, TKMCE
Function Calls
A function is called using the name with which it was defined earlier, followed by a
pair of parantheses. Any input parameters or arguments are to be placed within
these calling parantheses.
All parameters which are passed in functions are always passed by reference
in Python. This means that if the values of the parameters are changed in the
function, it will also reflect the change in the calling function.
The 'return' Statement
The return statement is used to exit a function.
If a function returns a value, it is passed back by the return statement as argument to
the caller.
If it does n't return a value, we simply write return with no arguments.
96
Dept. of CSE, TKMCE
Design with Recursive Functions
In some cases, you can decompose a complex problem into smaller problems of the
same form. In these cases, the subproblems can all be solved by using the same
function. This design strategy is called recursive design, and the resulting functions
are called recursive functions.
Defining a Recursive Function
A recursive function is a function that calls itself. To prevent a function from repeating
itself indefinitely, it must contain at least one selection statement. This statement
examines a condition called a base case to determine whether to stop or to continue
with another recursive step.
97
Dept. of CSE, TKMCE
def displayRange(lower, upper):
"""Outputs the numbers from lower through upper."""
while lower <= upper:
print(lower)
lower = lower + 1
How would we go about converting this function to a recursive one? First, you should
note two important facts:
1. The loop’s body continues execution while lower <= upper.
2. When the function executes, lower is incremented by 1, but upper never changes
98
Dept. of CSE, TKMCE
def displayRange(lower, upper):
"""Outputs the numbers from lower through upper."""
if lower <= upper:
print(lower)
displayRange(lower + 1, upper)
99
Dept. of CSE, TKMCE
Using Recursive Definitions to Construct Recursive Functions
Define recursive definitions for Fibonacci Series:
Fibonacci sequence is a series of values with a recursive definition. The first and second
numbers in the Fibonacci sequence are 1. Thereafter, each number in the sequence is the
sum of its two predecessors, as follows:
1 1 2 3 5 8 13 . . .
A recursive definition of the nth Fibonacci number is the following:
Fib(n) = 1, when n = 1 or n = 2
Fib(n) = Fib(n - 1) + Fib(n - 2), for all n > 2
def fib(n):
if n < 3:
return 1
else:
return fib(n - 1) + fib(n - 2) 100
Dept. of CSE, TKMCE
Factorial using Recursion
101
Dept. of CSE, TKMCE
Infinite Recursion
• When a function continue executing forever, that situation is known as infinite recursion.
• Infinite recursion arises when the programmer fails to specify the base case or to reduce
the size of the problem in a way that terminates the recursive process.
• This will results in stack overflow error.
>>> def runForever(n):
if n > 0:
runForever(n)
else:
runForever(n - 1)
>>> runForever(1)
102
Dept. of CSE, TKMCE
The Costs and Benefits of Recursion
• At program startup, the PVM reserves an area of memory named a call stack.
• For each call of a function, recursive or otherwise, the PVM must allocate on the call
stack a small chunk of memory called a stack frame.
• In this type of storage, the system places the values of the arguments and the
return address for each function call.
• Space for the function call’s return value is also reserved in its stack frame.
• When a call returns or completes its execution, the return address is used to locate
the next instruction in the caller’s code, and the memory for the stack frame is
deallocated.
103
Dept. of CSE, TKMCE
104
Dept. of CSE, TKMCE
The stack frames for displayRange(1, 3)
Higher-Order Functions
A function is called Higher Order Function if it contains other functions as a parameter
or returns a function as an output.
i.e, The functions that operate with another function are known as Higher order
Functions.
Functions as First-Class Data Objects:
In Python, functions can be treated as first-class data objects. This means that
- they can be assigned to variables
- passed as arguments to other functions
- returned as the values of other functions
- and stored in data structures such as lists and dictionaries.
105
Dept. of CSE, TKMCE
üPassing a function as an argument to another function is no different from passing any
other datum.
üThe function argument is first evaluated, producing the function itself, and then the
parameter name is bound to this value.
üThe function can then be applied to its own argument with the usual syntax.
eg.,
>>> def example(functionArg, dataArg):
return functionArg(dataArg)
>>> example(abs, -4)
4
>>> example(math.sqrt, 2)
1.4142135623730951
106
Dept. of CSE, TKMCE
Mapping, Filtering, Reducing
Mapping : The first type of useful higher-order function to consider is called a mapping.
This process applies a function to each value in a sequence (such as a list, a tuple, or a string) and returns a
new sequence of the results.
Python includes a map function for this purpose.
Suppose we have a list named words that contains strings that represent integers. We want to replace each
string with the corresponding integer value.
>>> words = ["231", "20", "-45", "99"]
>>> map(int, words) # Convert all strings to ints
<map object at 0xl4cbd90>
>>> words # Original list is not changed
['231', '20', '-45', '99']
>>> words = list(map(int, words)) # Reset variable to change it
>>> words
[231, 20, -45, 99]
107
Dept. of CSE, TKMCE
# Python program to demonstrate working of map.
# Return double of n
def addition(n):
return n + n
# We double all numbers using map()
numbers = [1, 2, 3, 4]
result = map(addition, numbers)
print(list(result))
NOTE : The returned value from map() (map object) then can be passed to functions like
list() (to create a list), set() (to create a set) .
108
Dept. of CSE, TKMCE
Output:
[2, 4, 6, 8]
Filtering : A second type of higher-order function is called a filtering. In this process, a
function called a predicate is applied to each value in a list.
If the predicate returns True, the value passes the test and is added to a filter object
(similar to a map object). Otherwise, the value is dropped from consideration.
Python includes a filter function that is used to produce a list of the odd numbers in
another list:
>>> def odd(n):
return n % 2 == 1
>>> list(filter(odd, range(10)))
[1, 3, 5, 7, 9]
109
Dept. of CSE, TKMCE
# function that filters vowels
def fun(variable):
letters = ['a', 'e', 'i', 'o', 'u']
if (variable in letters):
return True
else:
return False
sequence = ['g', 'e', 'e', 'j', 'k', 'i', 'p', 'r']
filtered = filter(fun, sequence)
print('The filtered letters are:')
for s in filtered:
print(s)
110
Dept. of CSE, TKMCE
Output:
The filtered letters are:
e
e
i
Reducing: Here we take a list of values and repeatedly apply a function to accumulate a
single data value. A summation is a good example of this process. The first value is added
to the second value, then the sum is added to the third value, and so on, until the sum of
all the values is produced.
eg.,
>>> from functools import reduce
>>> def add(x, y): return x + y
>>> def multiply(x, y): return x * y
>>> data = [1, 2, 3, 4]
>>> reduce(add, data)
10
>>> reduce(multiply, data)
24
111
Dept. of CSE, TKMCE
Boolean Functions
• A Boolean function usually tests its argument for the presence or absence of some property.
• The function returns True if the property is present, or False otherwise.
def odd(x):
"""Returns True if x is odd or False otherwise."""
if x % 2 == 1:
return True
else:
return False
>>> odd(5)
True
112
Dept. of CSE, TKMCE
Using lambda to Create Anonymous Functions
• Python includes a mechanism called lambda that allows the programmer to create
functions in this manner.
• A lambda is an anonymous function. It has no name of its own, but it contains the
names of its arguments as well as a single expression.
• When the lambda is applied to its arguments, its expression is evaluated, and its
value is returned.
The syntax of a lambda is very tight and restrictive:
lambda <argname-1, ..., argname-n>: <expression>
>>> mul = lambda a,b : a*b
>>> print(mul(12,12))
144
113
Dept. of CSE, TKMCE
Use of range, reduce, and lambda to simplify the definition of the summation
function
from functools import reduce
def summation(lower, upper)
if lower > upper:
return 0
else:
return reduce(lambda x,y : x+y , range(lower,upper+1))
114
Dept. of CSE, TKMCE
# Python code to illustrate filter() with lambda()
li = [5, 7, 22, 97, 54, 62, 77, 23, 73, 61]
final_list = list(filter(lambda x: (x%2 != 0) , li))
print(final_list)
# Python code to illustrate map() with lambda() to get double of a list.
li = [5, 7, 22, 97, 54, 62, 77, 23, 73, 61]
final_list = list(map(lambda x: x*2, li))
print(final_list)
115
Dept. of CSE, TKMCE
Output:
[5, 7, 97, 77, 23, 73, 61]
Output:
[10, 14, 44, 194, 108, 124,
154, 46, 146, 122]
116
Dept. of CSE, TKMCE

Más contenido relacionado

La actualidad más candente

Lect 1-class and object
Lect 1-class and objectLect 1-class and object
Lect 1-class and objectFajar Baskoro
 
Python Exception Handling
Python Exception HandlingPython Exception Handling
Python Exception HandlingMegha V
 
Conditional and control statement
Conditional and control statementConditional and control statement
Conditional and control statementnarmadhakin
 
interface with mysql.pptx
interface with mysql.pptxinterface with mysql.pptx
interface with mysql.pptxKRITIKAOJHA11
 
primitive data types
 primitive data types primitive data types
primitive data typesJadavsejal
 
9. Input Output in java
9. Input Output in java9. Input Output in java
9. Input Output in javaNilesh Dalvi
 
Exception handling
Exception handlingException handling
Exception handlingIblesoft
 
JavaScript Objects
JavaScript ObjectsJavaScript Objects
JavaScript ObjectsReem Alattas
 
cpp input & output system basics
cpp input & output system basicscpp input & output system basics
cpp input & output system basicsgourav kottawar
 
Variables in python
Variables in pythonVariables in python
Variables in pythonJaya Kumari
 
Heap and stack space in java
Heap and stack space in javaHeap and stack space in java
Heap and stack space in javaTalha Ocakçı
 
Operators in Python
Operators in PythonOperators in Python
Operators in PythonAnusuya123
 
[OOP - Lec 19] Static Member Functions
[OOP - Lec 19] Static Member Functions[OOP - Lec 19] Static Member Functions
[OOP - Lec 19] Static Member FunctionsMuhammad Hammad Waseem
 
Exception handling and function in python
Exception handling and function in pythonException handling and function in python
Exception handling and function in pythonTMARAGATHAM
 
Control Statements in Java
Control Statements in JavaControl Statements in Java
Control Statements in JavaNiloy Saha
 

La actualidad más candente (20)

Python programming : Strings
Python programming : StringsPython programming : Strings
Python programming : Strings
 
Lect 1-class and object
Lect 1-class and objectLect 1-class and object
Lect 1-class and object
 
Python Exception Handling
Python Exception HandlingPython Exception Handling
Python Exception Handling
 
Conditional and control statement
Conditional and control statementConditional and control statement
Conditional and control statement
 
interface with mysql.pptx
interface with mysql.pptxinterface with mysql.pptx
interface with mysql.pptx
 
primitive data types
 primitive data types primitive data types
primitive data types
 
9. Input Output in java
9. Input Output in java9. Input Output in java
9. Input Output in java
 
Exception handling
Exception handlingException handling
Exception handling
 
Files in java
Files in javaFiles in java
Files in java
 
JavaScript Objects
JavaScript ObjectsJavaScript Objects
JavaScript Objects
 
This pointer
This pointerThis pointer
This pointer
 
cpp input & output system basics
cpp input & output system basicscpp input & output system basics
cpp input & output system basics
 
Loops c++
Loops c++Loops c++
Loops c++
 
Variables in python
Variables in pythonVariables in python
Variables in python
 
Heap and stack space in java
Heap and stack space in javaHeap and stack space in java
Heap and stack space in java
 
Operators in Python
Operators in PythonOperators in Python
Operators in Python
 
[OOP - Lec 19] Static Member Functions
[OOP - Lec 19] Static Member Functions[OOP - Lec 19] Static Member Functions
[OOP - Lec 19] Static Member Functions
 
Python Functions
Python   FunctionsPython   Functions
Python Functions
 
Exception handling and function in python
Exception handling and function in pythonException handling and function in python
Exception handling and function in python
 
Control Statements in Java
Control Statements in JavaControl Statements in Java
Control Statements in Java
 

Similar a Python for Machine Learning

control statements of clangauge (ii unit)
control statements of clangauge (ii unit)control statements of clangauge (ii unit)
control statements of clangauge (ii unit)Prashant Sharma
 
Lec16.pptx problem specifications computer
Lec16.pptx problem specifications computerLec16.pptx problem specifications computer
Lec16.pptx problem specifications computersamiullahamjad06
 
[C++][a] tutorial 2
[C++][a] tutorial 2[C++][a] tutorial 2
[C++][a] tutorial 2yasir_cesc
 
Control structures ii
Control structures ii Control structures ii
Control structures ii Ahmad Idrees
 
Python programming –part 3
Python programming –part 3Python programming –part 3
Python programming –part 3Megha V
 
Dsp lab _eec-652__vi_sem_18012013
Dsp lab _eec-652__vi_sem_18012013Dsp lab _eec-652__vi_sem_18012013
Dsp lab _eec-652__vi_sem_18012013amanabr
 
Dsp lab _eec-652__vi_sem_18012013
Dsp lab _eec-652__vi_sem_18012013Dsp lab _eec-652__vi_sem_18012013
Dsp lab _eec-652__vi_sem_18012013Kurmendra Singh
 
Iterative control structures, looping, types of loops, loop working
Iterative control structures, looping, types of loops, loop workingIterative control structures, looping, types of loops, loop working
Iterative control structures, looping, types of loops, loop workingNeeru Mittal
 
Python programing
Python programingPython programing
Python programinghamzagame
 

Similar a Python for Machine Learning (20)

Chap 5 c++
Chap 5 c++Chap 5 c++
Chap 5 c++
 
Python Control structures
Python Control structuresPython Control structures
Python Control structures
 
control statements of clangauge (ii unit)
control statements of clangauge (ii unit)control statements of clangauge (ii unit)
control statements of clangauge (ii unit)
 
Lec16.pptx problem specifications computer
Lec16.pptx problem specifications computerLec16.pptx problem specifications computer
Lec16.pptx problem specifications computer
 
C++ Language
C++ LanguageC++ Language
C++ Language
 
Control statements in c
Control statements in cControl statements in c
Control statements in c
 
[C++][a] tutorial 2
[C++][a] tutorial 2[C++][a] tutorial 2
[C++][a] tutorial 2
 
Conditional Statements
Conditional StatementsConditional Statements
Conditional Statements
 
Python Lecture 2
Python Lecture 2Python Lecture 2
Python Lecture 2
 
Python_Module_2.pdf
Python_Module_2.pdfPython_Module_2.pdf
Python_Module_2.pdf
 
lecture 2.pptx
lecture 2.pptxlecture 2.pptx
lecture 2.pptx
 
Unit - 2 CAP.pptx
Unit - 2 CAP.pptxUnit - 2 CAP.pptx
Unit - 2 CAP.pptx
 
Control structures ii
Control structures ii Control structures ii
Control structures ii
 
Python programming –part 3
Python programming –part 3Python programming –part 3
Python programming –part 3
 
Dsp lab _eec-652__vi_sem_18012013
Dsp lab _eec-652__vi_sem_18012013Dsp lab _eec-652__vi_sem_18012013
Dsp lab _eec-652__vi_sem_18012013
 
Dsp lab _eec-652__vi_sem_18012013
Dsp lab _eec-652__vi_sem_18012013Dsp lab _eec-652__vi_sem_18012013
Dsp lab _eec-652__vi_sem_18012013
 
Introduction to python programming
Introduction to python programmingIntroduction to python programming
Introduction to python programming
 
Iterative control structures, looping, types of loops, loop working
Iterative control structures, looping, types of loops, loop workingIterative control structures, looping, types of loops, loop working
Iterative control structures, looping, types of loops, loop working
 
Python programing
Python programingPython programing
Python programing
 
06
0606
06
 

Más de Student

Python for Machine Learning
Python for Machine LearningPython for Machine Learning
Python for Machine LearningStudent
 
Decision Trees
Decision TreesDecision Trees
Decision TreesStudent
 
Introducing Technologies for Handling Big Data by Jaseela
Introducing Technologies for Handling Big Data by JaseelaIntroducing Technologies for Handling Big Data by Jaseela
Introducing Technologies for Handling Big Data by JaseelaStudent
 
A review of localization systems for robotic endoscopic
A review of localization systems for robotic endoscopicA review of localization systems for robotic endoscopic
A review of localization systems for robotic endoscopicStudent
 
Signature Free Virus Blocking Method to Detect Software Code Security (Intern...
Signature Free Virus Blocking Method to Detect Software Code Security (Intern...Signature Free Virus Blocking Method to Detect Software Code Security (Intern...
Signature Free Virus Blocking Method to Detect Software Code Security (Intern...Student
 
Sigfree ppt (International Journal of Computer Science and Mobile Computing)
Sigfree ppt (International Journal of Computer Science and Mobile Computing)Sigfree ppt (International Journal of Computer Science and Mobile Computing)
Sigfree ppt (International Journal of Computer Science and Mobile Computing)Student
 
Design and Implementation of Improved Authentication System for Android Smart...
Design and Implementation of Improved Authentication System for Android Smart...Design and Implementation of Improved Authentication System for Android Smart...
Design and Implementation of Improved Authentication System for Android Smart...Student
 
Desgn&imp authentctn.ppt by Jaseela
Desgn&imp authentctn.ppt by JaseelaDesgn&imp authentctn.ppt by Jaseela
Desgn&imp authentctn.ppt by JaseelaStudent
 
Google glass by Jaseela
Google glass by JaseelaGoogle glass by Jaseela
Google glass by JaseelaStudent
 

Más de Student (9)

Python for Machine Learning
Python for Machine LearningPython for Machine Learning
Python for Machine Learning
 
Decision Trees
Decision TreesDecision Trees
Decision Trees
 
Introducing Technologies for Handling Big Data by Jaseela
Introducing Technologies for Handling Big Data by JaseelaIntroducing Technologies for Handling Big Data by Jaseela
Introducing Technologies for Handling Big Data by Jaseela
 
A review of localization systems for robotic endoscopic
A review of localization systems for robotic endoscopicA review of localization systems for robotic endoscopic
A review of localization systems for robotic endoscopic
 
Signature Free Virus Blocking Method to Detect Software Code Security (Intern...
Signature Free Virus Blocking Method to Detect Software Code Security (Intern...Signature Free Virus Blocking Method to Detect Software Code Security (Intern...
Signature Free Virus Blocking Method to Detect Software Code Security (Intern...
 
Sigfree ppt (International Journal of Computer Science and Mobile Computing)
Sigfree ppt (International Journal of Computer Science and Mobile Computing)Sigfree ppt (International Journal of Computer Science and Mobile Computing)
Sigfree ppt (International Journal of Computer Science and Mobile Computing)
 
Design and Implementation of Improved Authentication System for Android Smart...
Design and Implementation of Improved Authentication System for Android Smart...Design and Implementation of Improved Authentication System for Android Smart...
Design and Implementation of Improved Authentication System for Android Smart...
 
Desgn&imp authentctn.ppt by Jaseela
Desgn&imp authentctn.ppt by JaseelaDesgn&imp authentctn.ppt by Jaseela
Desgn&imp authentctn.ppt by Jaseela
 
Google glass by Jaseela
Google glass by JaseelaGoogle glass by Jaseela
Google glass by Jaseela
 

Último

4.16.24 21st Century Movements for Black Lives.pptx
4.16.24 21st Century Movements for Black Lives.pptx4.16.24 21st Century Movements for Black Lives.pptx
4.16.24 21st Century Movements for Black Lives.pptxmary850239
 
Indexing Structures in Database Management system.pdf
Indexing Structures in Database Management system.pdfIndexing Structures in Database Management system.pdf
Indexing Structures in Database Management system.pdfChristalin Nelson
 
Q-Factor HISPOL Quiz-6th April 2024, Quiz Club NITW
Q-Factor HISPOL Quiz-6th April 2024, Quiz Club NITWQ-Factor HISPOL Quiz-6th April 2024, Quiz Club NITW
Q-Factor HISPOL Quiz-6th April 2024, Quiz Club NITWQuiz Club NITW
 
Expanded definition: technical and operational
Expanded definition: technical and operationalExpanded definition: technical and operational
Expanded definition: technical and operationalssuser3e220a
 
Grade Three -ELLNA-REVIEWER-ENGLISH.pptx
Grade Three -ELLNA-REVIEWER-ENGLISH.pptxGrade Three -ELLNA-REVIEWER-ENGLISH.pptx
Grade Three -ELLNA-REVIEWER-ENGLISH.pptxkarenfajardo43
 
ClimART Action | eTwinning Project
ClimART Action    |    eTwinning ProjectClimART Action    |    eTwinning Project
ClimART Action | eTwinning Projectjordimapav
 
Scientific Writing :Research Discourse
Scientific  Writing :Research  DiscourseScientific  Writing :Research  Discourse
Scientific Writing :Research DiscourseAnita GoswamiGiri
 
BIOCHEMISTRY-CARBOHYDRATE METABOLISM CHAPTER 2.pptx
BIOCHEMISTRY-CARBOHYDRATE METABOLISM CHAPTER 2.pptxBIOCHEMISTRY-CARBOHYDRATE METABOLISM CHAPTER 2.pptx
BIOCHEMISTRY-CARBOHYDRATE METABOLISM CHAPTER 2.pptxSayali Powar
 
Active Learning Strategies (in short ALS).pdf
Active Learning Strategies (in short ALS).pdfActive Learning Strategies (in short ALS).pdf
Active Learning Strategies (in short ALS).pdfPatidar M
 
Transaction Management in Database Management System
Transaction Management in Database Management SystemTransaction Management in Database Management System
Transaction Management in Database Management SystemChristalin Nelson
 
4.11.24 Mass Incarceration and the New Jim Crow.pptx
4.11.24 Mass Incarceration and the New Jim Crow.pptx4.11.24 Mass Incarceration and the New Jim Crow.pptx
4.11.24 Mass Incarceration and the New Jim Crow.pptxmary850239
 
Tree View Decoration Attribute in the Odoo 17
Tree View Decoration Attribute in the Odoo 17Tree View Decoration Attribute in the Odoo 17
Tree View Decoration Attribute in the Odoo 17Celine George
 
Using Grammatical Signals Suitable to Patterns of Idea Development
Using Grammatical Signals Suitable to Patterns of Idea DevelopmentUsing Grammatical Signals Suitable to Patterns of Idea Development
Using Grammatical Signals Suitable to Patterns of Idea Developmentchesterberbo7
 
Textual Evidence in Reading and Writing of SHS
Textual Evidence in Reading and Writing of SHSTextual Evidence in Reading and Writing of SHS
Textual Evidence in Reading and Writing of SHSMae Pangan
 
Unraveling Hypertext_ Analyzing Postmodern Elements in Literature.pptx
Unraveling Hypertext_ Analyzing  Postmodern Elements in  Literature.pptxUnraveling Hypertext_ Analyzing  Postmodern Elements in  Literature.pptx
Unraveling Hypertext_ Analyzing Postmodern Elements in Literature.pptxDhatriParmar
 
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdf
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdfGrade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdf
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdfJemuel Francisco
 
4.9.24 School Desegregation in Boston.pptx
4.9.24 School Desegregation in Boston.pptx4.9.24 School Desegregation in Boston.pptx
4.9.24 School Desegregation in Boston.pptxmary850239
 
ICS2208 Lecture6 Notes for SL spaces.pdf
ICS2208 Lecture6 Notes for SL spaces.pdfICS2208 Lecture6 Notes for SL spaces.pdf
ICS2208 Lecture6 Notes for SL spaces.pdfVanessa Camilleri
 
Mythology Quiz-4th April 2024, Quiz Club NITW
Mythology Quiz-4th April 2024, Quiz Club NITWMythology Quiz-4th April 2024, Quiz Club NITW
Mythology Quiz-4th April 2024, Quiz Club NITWQuiz Club NITW
 

Último (20)

4.16.24 21st Century Movements for Black Lives.pptx
4.16.24 21st Century Movements for Black Lives.pptx4.16.24 21st Century Movements for Black Lives.pptx
4.16.24 21st Century Movements for Black Lives.pptx
 
Indexing Structures in Database Management system.pdf
Indexing Structures in Database Management system.pdfIndexing Structures in Database Management system.pdf
Indexing Structures in Database Management system.pdf
 
Mattingly "AI & Prompt Design: Large Language Models"
Mattingly "AI & Prompt Design: Large Language Models"Mattingly "AI & Prompt Design: Large Language Models"
Mattingly "AI & Prompt Design: Large Language Models"
 
Q-Factor HISPOL Quiz-6th April 2024, Quiz Club NITW
Q-Factor HISPOL Quiz-6th April 2024, Quiz Club NITWQ-Factor HISPOL Quiz-6th April 2024, Quiz Club NITW
Q-Factor HISPOL Quiz-6th April 2024, Quiz Club NITW
 
Expanded definition: technical and operational
Expanded definition: technical and operationalExpanded definition: technical and operational
Expanded definition: technical and operational
 
Grade Three -ELLNA-REVIEWER-ENGLISH.pptx
Grade Three -ELLNA-REVIEWER-ENGLISH.pptxGrade Three -ELLNA-REVIEWER-ENGLISH.pptx
Grade Three -ELLNA-REVIEWER-ENGLISH.pptx
 
ClimART Action | eTwinning Project
ClimART Action    |    eTwinning ProjectClimART Action    |    eTwinning Project
ClimART Action | eTwinning Project
 
Scientific Writing :Research Discourse
Scientific  Writing :Research  DiscourseScientific  Writing :Research  Discourse
Scientific Writing :Research Discourse
 
BIOCHEMISTRY-CARBOHYDRATE METABOLISM CHAPTER 2.pptx
BIOCHEMISTRY-CARBOHYDRATE METABOLISM CHAPTER 2.pptxBIOCHEMISTRY-CARBOHYDRATE METABOLISM CHAPTER 2.pptx
BIOCHEMISTRY-CARBOHYDRATE METABOLISM CHAPTER 2.pptx
 
Active Learning Strategies (in short ALS).pdf
Active Learning Strategies (in short ALS).pdfActive Learning Strategies (in short ALS).pdf
Active Learning Strategies (in short ALS).pdf
 
Transaction Management in Database Management System
Transaction Management in Database Management SystemTransaction Management in Database Management System
Transaction Management in Database Management System
 
4.11.24 Mass Incarceration and the New Jim Crow.pptx
4.11.24 Mass Incarceration and the New Jim Crow.pptx4.11.24 Mass Incarceration and the New Jim Crow.pptx
4.11.24 Mass Incarceration and the New Jim Crow.pptx
 
Tree View Decoration Attribute in the Odoo 17
Tree View Decoration Attribute in the Odoo 17Tree View Decoration Attribute in the Odoo 17
Tree View Decoration Attribute in the Odoo 17
 
Using Grammatical Signals Suitable to Patterns of Idea Development
Using Grammatical Signals Suitable to Patterns of Idea DevelopmentUsing Grammatical Signals Suitable to Patterns of Idea Development
Using Grammatical Signals Suitable to Patterns of Idea Development
 
Textual Evidence in Reading and Writing of SHS
Textual Evidence in Reading and Writing of SHSTextual Evidence in Reading and Writing of SHS
Textual Evidence in Reading and Writing of SHS
 
Unraveling Hypertext_ Analyzing Postmodern Elements in Literature.pptx
Unraveling Hypertext_ Analyzing  Postmodern Elements in  Literature.pptxUnraveling Hypertext_ Analyzing  Postmodern Elements in  Literature.pptx
Unraveling Hypertext_ Analyzing Postmodern Elements in Literature.pptx
 
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdf
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdfGrade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdf
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdf
 
4.9.24 School Desegregation in Boston.pptx
4.9.24 School Desegregation in Boston.pptx4.9.24 School Desegregation in Boston.pptx
4.9.24 School Desegregation in Boston.pptx
 
ICS2208 Lecture6 Notes for SL spaces.pdf
ICS2208 Lecture6 Notes for SL spaces.pdfICS2208 Lecture6 Notes for SL spaces.pdf
ICS2208 Lecture6 Notes for SL spaces.pdf
 
Mythology Quiz-4th April 2024, Quiz Club NITW
Mythology Quiz-4th April 2024, Quiz Club NITWMythology Quiz-4th April 2024, Quiz Club NITW
Mythology Quiz-4th April 2024, Quiz Club NITW
 

Python for Machine Learning

  • 1. Python for Machine Learning Course Code : CST283 Category : Minor Offered by : Dept. of Computer Science and Engg. Faculty in charge : Jaseela Beevi S 1 Dept. of CSE, TKMCE
  • 2. Syllabus - Module II Building Python Programs: Control statements - Selection structure (if-else, switch-case), Iteration structure(for, while), Testing the control statements, Lazy evaluation. Functions - Hiding redundancy and complexity, Arguments and return values, Variable scopes and parameter passing, Named arguments, Main function, Working with recursion, Lambda functions. Strings and number systems - String function, Handling numbers in various formats. 2 Dept. of CSE, TKMCE
  • 3. Control Statements 3 Dept. of CSE, TKMCE control statements—statements that allow the computer to select or repeat an action. üSelection structure : statements which determines whether other statements will be executed e.g. if-else, switch-case üIteration structure : statements which determines how many time to execute another statements. e.g. for, while
  • 4. Selection: if and if-else Statements 4 Dept. of CSE, TKMCE • if-else statement: ü It is also called a two-way selection statement, because it directs the computer to make a choice between two alternative courses of action. üIt is often used to check inputs for errors and to respond with error messages if necessary. if <condition>: <sequence of statements–1> else: <sequence of statements–2> ü The condition in the if-else statement must be a Boolean expression—that is, an expression that evaluates to either true or false.
  • 5. if-else Statements 5 Dept. of CSE, TKMCE e.g., prints the maximum and minimum of two input numbers.
  • 6. Selection: if and if-else Statements 6 Dept. of CSE, TKMCE • if statement: üThe simplest form of selection is the if statement. This type of control statement is also called a one-way selection statement. üit consists of a condition and just a single sequence of statements. If the condition is True, the sequence of statements is run. Otherwise, control proceeds to the next statement following the entire selection statement. if <condition>: <sequence of statements>
  • 7. 7 Dept. of CSE, TKMCE if x < 0: x = –x if - Statements
  • 8. 8 Dept. of CSE, TKMCE The process of testing several conditions and responding accordingly can be described in code by a multi-way selection statement. if <condition-1>: <sequence of statements-1> . . . elif <condition-n>: <sequence of statements-n> else: <default sequence of statements>
  • 10. Introduction to Python Switch Statement 10 Dept. of CSE, TKMCE • A switch statement is a very useful and powerful programming feature. It is an alternate to if-else-if ladder statement and provides better performance and more manageable code than an if-else-if ladder statement. • Python language does not provide any inbuilt switch statements. We can implement this feature with the same flow and functionality but with different syntax and implementation using Python Dictionary.
  • 12. Syntax of Switch Statement 12 Dept. of CSE, TKMCE 1. Switch in Other Languages (c, Java,..) switch(N) { case 1: Statement if N = 1; break; case 2: Statement if N = 2; break; :: case n: Statement if N = n; break; default: Statement if N doesn't match any }
  • 13. 13 Dept. of CSE, TKMCE 2. Switch Implementation in Python switcher= { key_1: value_1/method_1(), key_2: value_2/method_2(), key_3: value_3/method_3(), :: key_n: value_n/method_n(), } key = N value = switcher.get(key, "default")
  • 14. Implementation of Switch statement in Python 14 Dept. of CSE, TKMCE def get_week_day(argument): switcher = { 0: "Sunday", 1: "Monday", 2: "Tuesday", 3: "Wednesday", 4: "Thursday", 5: "Friday", 6: "Saturday" } return switcher.get(argument, "Invalid day") print (get_week_day(6)) print (get_week_day(8)) print (get_week_day(0))
  • 15. The Boolean Type, Comparisons, and Boolean Expressions 15 Dept. of CSE, TKMCE Boolean Data Type: The Boolean data type consists of only two data values—true and false. Simple Boolean expressions consist of the Boolean values True or False, variables bound to those values, function calls that return Boolean values, or comparisons.
  • 16. Logical Operators and Compound Boolean Expressions 16 Dept. of CSE, TKMCE number = int(input("Enter the numeric grade: ")) if number > 100: print("Error: grade must be between 100 and 0") elif number < 0: print("Error: grade must be between 100 and 0") else: # The code to compute and print the result goes here • The two conditions can be combined in a Boolean expression that uses the logical operator or. The resulting expression is compound Boolean expression number = int(input("Enter the numeric grade: ")) if number > 100 or number < 0: print("Error: grade must be between 100 and 0") else: # The code to compute and print the result goes here
  • 17. 17 Dept. of CSE, TKMCE • Another way to describe this situation using and operator is number = int(input("Enter the numeric grade: ")) if number >= 0 and number <= 100: # The code to compute and print the result goes here else: print("Error: grade must be between 100 and 0")
  • 18. Precedence of Logical Operators 18 Dept. of CSE, TKMCE >>> A = True >>> B = False >>> A and B False >>> A or B True >>> not A False The logical operators are evaluated after comparisons but before the assignment operator. The not operator has a higher precedence than the and operator, which has a higher precedence than the or operator. Thus, in our example, not A and B evaluates to False, whereas not (A and B) evaluates to True.
  • 20. Short-Circuit Evaluation 20 Dept. of CSE, TKMCE The Python virtual machine sometimes knows the value of a Boolean expression before it has evaluated all of its operands. For instance, in the expression A and B, if A is false, there is no need to evaluate B. Likewise, in the expression A or B, if A is true, there is no need to evaluate B. This approach, in which evaluation stops as soon as possible, is called short-circuit evaluation. count = int(input("Enter the count: ")) sum = int(input("Enter the sum: ")) if count > 0 and sum // count > 10: print("average > 10") else: print("count = 0 or average <= 10")
  • 21. 21 Dept. of CSE, TKMCE Exercises: • 1. Assume that x is 3 and y is 5. Write the values of the following expressions: a) x == y b) x > y – 3 c) x <= y – 2 d) x == y or x > 2 e) x != 6 and y > 10 f) x > 0 and x < 100 2. Assume that x refers to a number. Write a code segment that prints the number’s absolute value without using Python’s abs function.
  • 22. Definite Iteration: The for Loop 22 Dept. of CSE, TKMCE Loops - They are the control statements with repetition statements, also known as loops, which repeat an action. Each repetition of the action is known as a pass or an iteration. There are two types of loops— üthose that repeat an action a predefined number of times (definite iteration) üthose that perform the action until the program determines that it needs to stop (indefinite iteration).
  • 24. 24 Dept. of CSE, TKMCE Syntax: for <variable> in range(<an integer expression>): <statement-1> . . <statement-n> • The first line of code in a loop is sometimes called the loop header. • The integer value or expression in the range() tells the loop how many times to call the below statements. • The loop body comprises the statements in the remaining lines of code, below the header. These statements are executed in sequence on each pass through the loop.
  • 25. 25 Dept. of CSE, TKMCE • Implementation of exponentiation operator using for loop #computation of 23 >>> number = 2 >>> exponent = 3 >>> product = 1 >>> for eachPass in range(exponent): product = product * number print(product, end = " ") 2 4 8 >>> product 8
  • 26. Count-Controlled Loops 26 Dept. of CSE, TKMCE üLoops that count through a range of numbers are also called count- controlled loops. üIt counts from 0 to the value of the header’s integer expression minus 1. üOn each pass through the loop, the header’s variable is bound to the current value of this count. e.g., >>> for count in range(4): print(count, end = " ") 0 1 2 3 üTo count from an explicit lower bound, the programmer can supply a second integer expression in the loop header. When two arguments are supplied to range, the count ranges from the first argument to the second argument minus 1.
  • 27. 27 Dept. of CSE, TKMCE # method - 2 : computation of factorial of 4 >>> product = 1 >>> for count in range(1, 5): product = product * count >>> product 24 Syntax: for <variable> in range(<lower bound>, <upper bound + 1>): <loop body> # method - 1 : computation of factorial of 4 >>> product = 1 >>> for count in range(4): product = product * (count + 1) >>> product 24
  • 28. 28 Dept. of CSE, TKMCE # program to find the sum of first 10 integer numbers: >>> lower = int(input("Enter the lower bound: ")) Enter the lower bound: 1 >>> upper = int(input("Enter the upper bound: ")) Enter the upper bound: 10 >>> >>>theSum = 0 >>>for number in range(lower, upper+1): theSum = theSum + number >>>print(theSum) 55
  • 29. Augmented Assignment 29 Dept. of CSE, TKMCE • The assignment symbol can be combined with the arithmetic and concatenation operators to provide augmented assignment operations. üa = 17 üs = "hi" üa += 3 # Equivalent to a = a + 3 üa -= 3 # Equivalent to a = a - 3 üa *= 3 # Equivalent to a = a * 3 üa /= 3 # Equivalent to a = a / 3 üa %= 3 # Equivalent to a = a % 3 üs += " there" # Equivalent to s = s + " there" All these examples have the format <variable> <operator>= <expression>
  • 30. Loop Errors: Off-by-One Error 30 Dept. of CSE, TKMCE • Once we get the syntax correct, we need to be concerned about only one other possible error: The loop fails to perform the expected number of iterations. Because this number is typically off by one, the error is called an off-by-one error. • For the most part, off-by-one errors result when the programmer incorrectly specifies the upper bound of the loop. • # Count from 1 through 4, we think • >>> for count in range(1,4): • print(count) • • 1 2 3
  • 31. Traversing the Contents of a Data Sequence 31 Dept. of CSE, TKMCE • The for loop itself visits each number in a sequence of numbers generated by the range function. • The sequence of numbers generated by the function range is fed to Python’s list function, which returns a special type of sequence called a list. >>> list(range(4)) [0, 1, 2, 3] >>> list(range(1, 5)) [1, 2, 3, 4] Strings are also sequences of characters. The values contained in any sequence can be visited by running a for loop, as follows: for <variable> in <sequence>: <do something with variable>
  • 32. Specifying the Steps in the Range 32 Dept. of CSE, TKMCE • In some programs we might want a loop to skip some numbers. • A variant of Python’s range function expects a third argument that allows you to nicely skip some numbers. The third argument specifies a step value, or the interval between the numbers used in the range. >>> list(range(1, 6, 1)) # Same as using two arguments [1, 2, 3, 4, 5] >>> list(range(1, 6, 2)) # Use every other number [1, 3, 5] >>> list(range(1, 6, 3)) # Use every third number [1, 4]
  • 33. 33 Dept. of CSE, TKMCE Suppose you had to compute the sum of the even numbers between 1 and 10. >>> theSum = 0 >>> for count in range(2, 11, 2): theSum += count >>> theSum 30 Loops that Count Down When the step argument is a negative number, the range function generates a sequence of numbers from the first argument down to the second argument plus 1. Thus, in this case, the first argument should express the upper bound, and the second argument should express the lower bound minus 1.
  • 34. 34 Dept. of CSE, TKMCE Exercises 1. Write the outputs of the following loops: a. for count in range(5): print(count + 1, end = " ") b. for count in range(1, 4): print(count, end = " ") c. for count in range(1, 6, 2): print(count, end = " ") d. for count in range(6, 1, –1): print(count, end = " ")
  • 35. Formatting Text for Output 35 Dept. of CSE, TKMCE To maintain the margins between columns of data, left-justification requires the addition of spaces to the right of the datum, whereas right-justification requires adding spaces to the left of the datum. A column of data is centered if there are an equal number of spaces on either side of the data within that column. The total number of data characters and additional spaces for a given datum in a formatted string is called its field width. how to right-justify and left-justify the string "four" within a field width of 6: >>> "%6s" % "four" # Right justify ' four' >>> "%-6s" % "four" # Left justify 'four '
  • 36. 36 Dept. of CSE, TKMCE The simplest form of this operation is the following: <format string> % <datum> üThe format string can contain string data and other information about the format of the datum. ü When the field width is positive, the datum is right-justified; when the field width is negative, you get left-justification. üIf the field width is less than or equal to the datum’s print length in characters, no justification is added. ü To format a sequence of data values, <format string> % (<datum–1>, ..., <datum–n>)
  • 37. 37 Dept. of CSE, TKMCE The format information for a data value of type float has the form <%field width> . <precision>f >>> salary = 100.00 >>> print("Your salary is $" + str(salary)) Your salary is $100.0 >>> print("Your salary is $%0.2f" % salary) Your salary is $100.00 To use a field width of 6 and a precision of 3 to format the float value 3.14: >>> "%6.3f" % 3.14 ' 3.140'
  • 38. 38 Dept. of CSE, TKMCE Exercises: • 1. Assume that the variable amount refers to 24.325. Write the outputs of the following statements: a. print("Your salary is $%0.2f" % amount) b. print("The area is %0.1f" % amount) 2. Write a code segment that displays the values of the integers x, y, and z on a single line, such that each value is right-justified with a field width of 6. 3. Write a format operation that builds a string for the float variable amount that has exactly two digits of precision and a field width of zero. 4. Write a loop that outputs the numbers in a list named salaries. The outputs should be formatted in a column that is right-justified, with a field width of 12 and a precision of 2.
  • 39. Conditional Iteration: The while Loop 39 Dept. of CSE, TKMCE In many situations, however, the number of iterations in a loop is unpredictable. The loop eventually completes its work, but only when a condition changes. The process continues to repeat as long as a condition remains true. This type of process is called conditional iteration, meaning that the process continues to repeat as long as a condition remains true. Syntax: while <condition>: <sequence of statements>
  • 40. The Structure and Behavior of the Loop 40 Dept. of CSE, TKMCE • The form of this statement is almost identical to that of the one-way selection statement. • The use of the reserved word while instead of if indicates that the sequence of statements might be executed many times, as long as the condition remains true. • Clearly, something eventually has to happen within the body of the loop to make the loop’s continuation condition become false. Otherwise, the loop will continue forever, an error known as an infinite loop. • At least one statement in the body of the loop must update a variable that affects the value of the condition.
  • 41. 41 Dept. of CSE, TKMCE Consider the pseudocode given below: set the sum to 0.0 input a string while the string is not the empty string convert the string to a float add the float to the sum input a string print the sum • The first input statement initializes a variable to a value that the loop condition can test. This variable is also called the loop control variable. • The second input statement obtains the other input values, including one that will terminate the loop.
  • 42. 42 Dept. of CSE, TKMCE theSum = 0.0 data = input("Enter a number or just enter to quit: ") while data != "": number = float(data) theSum += number data = input("Enter a number or just enter to quit: ") print("The sum is", theSum) Output: Enter a number or just enter to quit: 3 Enter a number or just enter to quit: 4 Enter a number or just enter to quit: 5 Enter a number or just enter to quit: The sum is 12.0 The while loop is also called an entry- control loop, because its condition is tested at the top of the loop. This implies that the statements within the loop can execute zero or more times.
  • 43. Count Control with a while Loop 43 Dept. of CSE, TKMCE # Summation with a for loop theSum = 0 for count in range(1, 11): theSum += count print(theSum) It includes a Boolean expression and two extra statements that refer to the count variable. This loop control variable must be explicitly initialized before the loop header and incremented in the loop body. * # Summation with a while loop theSum = 0 count = 1 while count <= 10: theSum += count count += 1 print(theSum)
  • 44. The while True Loop and the break Statement 44 Dept. of CSE, TKMCE #Computation of sum using break within while loop theSum = 0.0 while True: data = input("Enter a number or just enter to quit: ") if data == "": break number = float(data) theSum += number print("The sum is", theSum) Within this body, the input datum is received. It is then tested for the loop’s termination condition in a one-way selection statement. If the user wants to quit, the input will equal the empty string, and the break statement will cause an exit from the loop. Otherwise, control continues beyond the selection statement to the next two statements that process the input.
  • 47. Random Numbers 47 Dept. of CSE, TKMCE üTo simulate randomness in computer applications, programming languages include resources for generating random numbers. üThe function random.randint returns a random number from among the numbers between the two arguments and including those numbers. #Code to simulate the role of a die 10 times. >>> import random >>> for roll in range(10): print(random.randint(1, 6), end = " ") 2 4 6 4 3 2 3 6 2 2
  • 48. 48 Dept. of CSE, TKMCE Develop a simple guessing game using random.randint At start-up, the user enters the smallest number and the largest number in the range. The computer then selects a number from this range. On each pass through the loop, the user enters a number to attempt to guess the number selected by the computer. The program responds by saying “You’ve got it,” “Too large, try again,” or “Too small, try again.” When the user finally guesses the correct number, the program congratulates him and tells him the total number of guesses.
  • 49. 49 Dept. of CSE, TKMCE import random smaller = int(input("Enter the smaller number: ")) larger = int(input("Enter the larger number: ")) myNumber = random.randint(smaller, larger) count = 0 while True: count += 1 userNumber = int(input("Enter your guess: ")) if userNumber < myNumber: print("Too small!") elif userNumber > myNumber: print("Too large!") else: print("Congratulations! You've got it in", count,"tries!") break
  • 50. 50 Dept. of CSE, TKMCE Exercises 1. Translate the following for loops to equivalent while loops: a. for count in range(100): print(count) b. for count in range(1, 101): print(count) c. for count in range(100, 0, –1): print(count) 2. The factorial of an integer N is the product of the integers between 1 and N, inclusive. Write a while loop that computes the factorial of a given integer N.
  • 51. 51 Dept. of CSE, TKMCE Exercises 3. Write a program that accepts the lengths of three sides of a triangle as inputs. The program output should indicate whether or not the triangle is an equilateral triangle. 4. Write a program that accepts the lengths of three sides of a triangle as inputs. The program output should indicate whether or not the triangle is a right triangle. Recall from the Pythagorean theorem that in a right triangle, the square of one side equals the sum of the squares of the other two sides.
  • 52. Strings and Number System 52 Dept. of CSE, TKMCE The Structure of Strings: String is a data structure. A string is a sequence of zero or more characters. eg. "Hi there!" empty string “” A string’s length is the number of characters it contains. Python’s len function returns this value when it is passed a string. >>> len("Hi there!") 9 >>> len("") 0
  • 53. 53 Dept. of CSE, TKMCE The positions of a string’s characters are numbered from 0, on the left, to the length of the string minus 1, on the right. The string is an immutable data structure. This means that its internal data elements, the characters, can be accessed, but cannot be replaced, inserted, or removed. The Subscript Operator: a simple for loop can access any of the characters in a string, sometimes you just want to inspect one character at a given position without visiting them all. The subscript operator [] makes this possible. <a string>[<an integer expression>]
  • 54. 54 Dept. of CSE, TKMCE The integer expression is also called an index. >>> name = "Alan Turing" >>> name[0] 'A' >>> name[3] 'n' >>> name[len(name)] Traceback (most recent call last): File "<stdin>", line 1, in <module> IndexError: string index out of range >>> name[len(name) - 1] 'g' >>> name[-l] 'g' >>> name[-2] 'n'
  • 55. 55 Dept. of CSE, TKMCE The next code segment uses a count-controlled loop to display the characters and their positions: >>> data = "apple" >>> for index in range(len(data)): print(index, data[index]) 0 a 1 p 2 p 3 l 4 e Slicing for Substrings You can use Python’s subscript operator to obtain a substring through a process called slicing. To extract a substring, the programmer places a colon (:) in the subscript.
  • 56. 56 Dept. of CSE, TKMCE >>> name = "myfile.txt" # The entire string >>> name[0 : ] 'myfile.txt' >>> name[0 : 1] # The first character 'm' >>> name[ 0 : 2] # The first two characters 'my' >>> name[ : len(name)] # The entire string 'myfile.txt' >>> name[-3 : ] # The last three characters 'txt' >>> name[ 2 : 6] # Drill to extract 'file' 'file'
  • 57. Testing for a Substring with the in Operator 57 Dept. of CSE, TKMCE Python’s in operator : We can search for a substring or a character using this operator. üWhen used with strings, the left operand of in is a target substring, and the right operand is the string to be searched. üThe operator in returns True if the target string is somewhere in the search string, or False otherwise. eg., >>> fileList = ["myfile.txt", "myprogram.exe", "yourfile.txt"] >>> for fileName in fileList: if ".txt" in fileName: print(fileName) o/p - myfile.txt yourfile.txt
  • 58. 58 Dept. of CSE, TKMCE Exercises 1. Assume that the variable data refers to the string "myprogram.exe". Write the values of the following expressions: a. data[2] b. data[-1] c. len(data) d. data[0:8] 2. Assume that the variable data refers to the string "myprogram.exe". Write the expressions that perform the following tasks: a. Extract the substring "gram" from data. b. Truncate the extension ".exe" from data. c. Extract the character at the middle position from data.
  • 59. 59 Dept. of CSE, TKMCE 3. Assume that the variable myString refers to a string. Write a code segment that uses a loop to print the characters of the string in reverse order. 4. Assume that the variable myString refers to a string, and the variable reversedString refers to an empty string. Write a loop that adds the characters from myString to reversedString in reverse order.
  • 60. Number System 60 Dept. of CSE, TKMCE The value of each digit in a number can be determined using − ü The digit ü The position of the digit in the number ü The base of the number system (where the base is defined as the total number of digits available in the number system) 1) decimal number system - base 10 number system 0, 1, 2, 3, 4, 5, 6, 7, 8, and 9 as digits 2) binary number system - base 2 number system binary 0 and 1 3) octal number system - base 8 number system 0, 1, 2, 3, 4, 5, 6, and 7 4) hexadecimal number system - base 16 number system 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, A, B, C, D, E, F
  • 61. 61 Dept. of CSE, TKMCE To identify the system being used, you attach the base as a subscript to the number. 415 in binary notation 1100111112 415 in octal notation 6378 415 in decimal notation 41510 415 in hexadecimal notation 19F16 üThe digits used in each system are counted from 0 to n – 1, where n is the system’s base. üThus, the digits 8 and 9 do not appear in the octal system. üTo represent digits with values larger than 910, systems such as base 16 use letters. Thus, A16 represents the quantity 1010, whereas 1016 represents the quantity 1610
  • 62. The Positional System for Representing Numbers 62 Dept. of CSE, TKMCE positional notation—that is, the value of each digit in a number is determined by the digit’s position in the number. In other words, each digit has a positional value. How to find positional value of a digit in a number? The positional value of a digit is determined by raising the base of the system to the power specified by the position . (baseposition) For an n-digit number, the positions are numbered from n – 1 down to 0, starting with the leftmost digit and moving to the right. eg., the positional values of the three-digit number 41510 are: 100 (10 ) 2 , 10 (10 )1 , and 1 (10 ) 0 , moving from left to right in the number.
  • 63. 63 Dept. of CSE, TKMCE To determine the quantity represented by a number in any system from base 2 through base 10, you multiply each digit (as a decimal number) by its positional value and add the results. The following example shows how this is done for a three-digit number in base 10:
  • 64. Converting Binary to Decimal 64 Dept. of CSE, TKMCE conversion process : Multiply the value of each bit (0 or 1) by its positional value and add the results.
  • 65. 65 Dept. of CSE, TKMCE Converts a string of bits to a decimal integer. bitString = input("Enter a string of bits: ") decimal = 0 exponent = len(bitString) - 1 for digit in bitString: decimal = decimal + int(digit) * 2 ** exponent exponent = exponent - 1 print("The integer value is", decimal) Enter a string of bits: 1111 The integer value is 15
  • 66. Converting Decimal to Binary 66 Dept. of CSE, TKMCE 1) This algorithm repeatedly divides the decimal number by 2. 2) After each division, the remainder (either a 0 or a 1) is placed at the beginning of a string of bits. 3) The quotient becomes the next dividend in the process. 4) The string of bits is initially empty, and the process continues while the decimal number is greater than 0.
  • 67. 67 Dept. of CSE, TKMCE decimal = int(input("Enter a decimal number: ")) if decimal == 0: print(0) else: bitString = "" while decimal > 0: remainder = decimal % 2 decimal = decimal // 2 bitString = str(remainder) + bitString print("The binary representation is", bitString) Enter a decimal number: 156 The binary representation is 00111001
  • 69. 69 Dept. of CSE, TKMCE üNote the rows that contain exact powers of 2 (2, 4, and 8 in decimal). üEach of the corresponding binary numbers in that row contains a 1 followed by a number of zeroes that equal the exponent used to compute that power of 2. üThus, a quick way to compute the decimal value of the number 100002 is 24 or 1610. üThe rows whose binary numbers contain all 1s correspond to decimal numbers that are one less than the next exact power of 2. üFor example, the number 1112 equals 23-1 , or 710. Thus, a quick way to compute the decimal value of the number 111112 is 25 -1 or 3110.
  • 70. Octal Number System 70 Dept. of CSE, TKMCE • It requires only 3 bits to represent value of any digit. • Octal numbers are indicated by the addition of either an 0o prefix or an 8 subscript. • Position of every digit has a weight which is a power of 8. • Numeric value of an octal number is determined by multiplying each digit of the number by its positional value and then adding the products. • The main advantage of using Octal numbers is that it uses less digits than decimal and Hexadecimal number system. So, it has fewer computations and less computational errors.
  • 72. Binary to Octal Conversion 72 Dept. of CSE, TKMCE
  • 73. Octal to Binary Conversion 73 Dept. of CSE, TKMCE
  • 74. Octal to Decimal Conversion 74 Dept. of CSE, TKMCE
  • 75. Decimal to Octal Conversion 75 Dept. of CSE, TKMCE
  • 76. Binary to Octal Conversion 76 Dept. of CSE, TKMCE bitString = input("Enter a string of bits: ") decimal = 0 exponent = len(bitString) - 1 for digit in bitString: decimal = decimal + int(digit) * 2 ** exponent exponent = exponent - 1 i=1 octal=0 while decimal != 0: octal += int(decimal % 8)*i decimal /= 8 i *= 10 print("octal number is: ",octal)
  • 77. Octal to Binary Conversion oc = int(input("Enter the octal number: ")) dec = 0 i = 0 while oc != 0: dec = dec + (oc % 10) * pow(8,i) oc = oc // 10 i = i+1 bi = "" while dec != 0: rem = dec % 2 dec = dec // 2 bi = str(rem) + bi print("binary number is:", bi) 77 Dept. of CSE, TKMCE
  • 78. Hexadecimal Number System • It requires only 4 bits to represent value of any digit. • Hexadecimal numbers are indicated by the addition of either an 0x prefix or an 16 as subscript. • Position of every digit has a weight which is a power of 16. • Numeric value of a hexadecimal number is determined by multiplying each digit of the number by its positional value and then adding the products. • So, it is also a positional (or weighted) number system. 78 Dept. of CSE, TKMCE
  • 79. Binary to Hexadecimal Conversion Factor the bits into groups of four and look up the corresponding hex digits. 79 Dept. of CSE, TKMCE
  • 80. Hexadecimal to Binary Conversion • Each digit in the hexadecimal number is equivalent to four digits in the binary number. • Thus, to convert from hexadecimal to binary, you replace each hexadecimal digit with the corresponding 4-bit binary number. 80 Dept. of CSE, TKMCE
  • 81. String Methods • Python includes a set of string operations called methods. • A method behaves like a function but has a slightly different syntax. • Unlike a function, a method is always called with a given data value called an object, which is placed before the method name in the call. <an object>.<method name>(<argument-1>,..., <argument-n>) Methods can also expect arguments and return values. A method knows about the internal state of the object with which it is called. 81 Dept. of CSE, TKMCE
  • 84. some string methods in action: >> s = "Hi there!" >>> len(s) 9 >>> s.center(11) ' Hi there! ' >>> s.count('e') 2 >>> s.endswith("there!") True >>> s.startswith("Hi") True >>> s.find("the") 3 84 Dept. of CSE, TKMCE >>> s.isalpha() False >>> 'abc'.isalpha() True >>> "326".isdigit() True >>> words = s.split() >>> words ['Hi', 'there!'] >>> " ".join(words) 'Hithere!' >>> " ". join(words) 'Hi there!' >>> s.lower() 'hi there!' >>> s.upper() 'HI THERE!' >>> s.replace('i', 'o') 'Ho there!'
  • 85. • Extracting a filename’s extension using split() method • >>> "myfile.txt". split('.') ['myfile', 'txt'] • >>> "myfile.py". split('.') ['myfile', 'py'] • >>> "myfile.html". split('.') ['myfile', 'html'] To write a general expression for obtaining any filename’s extension, as follows: filename.split('.')[-1] >>> filename="myfile.txt" >>> filename.split('.')[-1] 'txt' 85 Dept. of CSE, TKMCE
  • 86. • Exercises 1. Assume that the variable data refers to the string "Python rules!". Use a string method from perform the following tasks: a. Obtain a list of the words in the string. b. Convert the string to uppercase. c. Locate the position of the string "rules". d. Replace the exclamation point with a question mark. • 2. Using the value of data from Exercise 1, write the values of the following expressions: a. data.endswith('i') b. " totally ". join(data.split()) 86 Dept. of CSE, TKMCE
  • 87. Functions Functions are the primary and most important method of code organization and reuse in Python. Functions can also help make your code more readable by giving a name to a group of Python statements. Functions are declared with the def keyword and returned from with the return key- word: def my_function(x, y, z=1.5): if z > 1: return z * (x + y) else: return z / (x + y) 87 Dept. of CSE, TKMCE
  • 88. There are many built-in functions provided by Python such as dir(), len(), abs(), etc. Users can also build their own functions, which are called user-defined functions. There are many advantages of using functions: a) They reduce duplication of code in a program. b) They break the large complex problems into small parts. c) They help in improving the clarity of code. d) A piece of code can be reused as many times as we want with the help of functions. 88 Dept. of CSE, TKMCE
  • 89. When a function is called, any expressions supplied as arguments are first evaluated. Their values are copied to temporary storage locations named by the parameters in the function’s definition. The parameters play the same role as variables in the code that the function then executes. A function may have one or more return statements, whose purpose is to terminate the execution of the function and return control to its caller. A return statement may be followed by an expression. In that case, Python evaluates the expression and makes its value available to the caller when the function stops execution. 89 Dept. of CSE, TKMCE
  • 90. Built-In Functions • They are already defined in Python Programming Language. • We can directly invoke them to perform a specific task. • Examples of Built-In Functions a) Type Conversion / explicit conversion - int(), float(), char() b) Type Coercion / implicit conversion c) Mathematical Functions - sin(), log(), cos(), cosec(), etc. d) Date and Time e) dir() Function f) help() Function 90 Dept. of CSE, TKMCE
  • 91. ü Functions as Abstraction Mechanisms ü Functions Eliminate Redundancy ü Functions Hide Complexity 91 Dept. of CSE, TKMCE
  • 92. Functions As Abstract Mechanism, An abstraction hides detail and thus allows a person to view many things as just one thing. Functions Eliminate Redundancy The first way that functions serve as abstraction mechanisms is by eliminating redundant, or repetitious, code. def summation(lower, upper): result = 0 while lower <= upper: result += lower lower += 1 return result >>> summation(1,4) #The summation of the numbers 1..4 10 >>> summation(50,100) # The summation of the numbers 50..100 3825 92 Dept. of CSE, TKMCE
  • 93. User Defined Functions • Python also allows users to define their own functions. • To use their own functions in Python, users have to define the function first - Function Definition. • In a function definition, users have to define a name for the new function and also the list of the statements that will execute when the function will be called. Syntax: def functionname(parameters): “function docstring” statement(s) return [expression] 93 Dept. of CSE, TKMCE
  • 94. Parameters and Arguments They are the values or expressions passed to the functions between paranthesis. The value of the argument is always assigned to a variable known as parameter. There can be four types of formal arguments using which a function can be called which are as follows: 1) Required Arguments 2) Keyword Arguments 3) Default Arguments 4) Variable-length arguments 94 Dept. of CSE, TKMCE
  • 95. 1. Required Arguments : When we assign the parameters to a function at the time of function definition, at the time of calling, the arguments should be passed to a function in correct positional order; furthermore, the no: of arguments should match the defined no: of parameters. 2. Keyword Arguments: The caller recognises the arguments by the parameter's names. This type of argument can also be out of order. 3. Default Arguments: We can assign a value to a parameter at the time of function definition. This value is considered the default value to that parameter. If we do not provide a value to the parameter at the time of calling, it will not produce an error. Instead it will pick the default value and use it. 4. Variable Length Arguments: The names for these arguments are not specified in the function definition. Instead we use * before the name of the variable which holds the value for all non-keyword variable arguments. 95 Dept. of CSE, TKMCE
  • 96. Function Calls A function is called using the name with which it was defined earlier, followed by a pair of parantheses. Any input parameters or arguments are to be placed within these calling parantheses. All parameters which are passed in functions are always passed by reference in Python. This means that if the values of the parameters are changed in the function, it will also reflect the change in the calling function. The 'return' Statement The return statement is used to exit a function. If a function returns a value, it is passed back by the return statement as argument to the caller. If it does n't return a value, we simply write return with no arguments. 96 Dept. of CSE, TKMCE
  • 97. Design with Recursive Functions In some cases, you can decompose a complex problem into smaller problems of the same form. In these cases, the subproblems can all be solved by using the same function. This design strategy is called recursive design, and the resulting functions are called recursive functions. Defining a Recursive Function A recursive function is a function that calls itself. To prevent a function from repeating itself indefinitely, it must contain at least one selection statement. This statement examines a condition called a base case to determine whether to stop or to continue with another recursive step. 97 Dept. of CSE, TKMCE
  • 98. def displayRange(lower, upper): """Outputs the numbers from lower through upper.""" while lower <= upper: print(lower) lower = lower + 1 How would we go about converting this function to a recursive one? First, you should note two important facts: 1. The loop’s body continues execution while lower <= upper. 2. When the function executes, lower is incremented by 1, but upper never changes 98 Dept. of CSE, TKMCE
  • 99. def displayRange(lower, upper): """Outputs the numbers from lower through upper.""" if lower <= upper: print(lower) displayRange(lower + 1, upper) 99 Dept. of CSE, TKMCE
  • 100. Using Recursive Definitions to Construct Recursive Functions Define recursive definitions for Fibonacci Series: Fibonacci sequence is a series of values with a recursive definition. The first and second numbers in the Fibonacci sequence are 1. Thereafter, each number in the sequence is the sum of its two predecessors, as follows: 1 1 2 3 5 8 13 . . . A recursive definition of the nth Fibonacci number is the following: Fib(n) = 1, when n = 1 or n = 2 Fib(n) = Fib(n - 1) + Fib(n - 2), for all n > 2 def fib(n): if n < 3: return 1 else: return fib(n - 1) + fib(n - 2) 100 Dept. of CSE, TKMCE
  • 102. Infinite Recursion • When a function continue executing forever, that situation is known as infinite recursion. • Infinite recursion arises when the programmer fails to specify the base case or to reduce the size of the problem in a way that terminates the recursive process. • This will results in stack overflow error. >>> def runForever(n): if n > 0: runForever(n) else: runForever(n - 1) >>> runForever(1) 102 Dept. of CSE, TKMCE
  • 103. The Costs and Benefits of Recursion • At program startup, the PVM reserves an area of memory named a call stack. • For each call of a function, recursive or otherwise, the PVM must allocate on the call stack a small chunk of memory called a stack frame. • In this type of storage, the system places the values of the arguments and the return address for each function call. • Space for the function call’s return value is also reserved in its stack frame. • When a call returns or completes its execution, the return address is used to locate the next instruction in the caller’s code, and the memory for the stack frame is deallocated. 103 Dept. of CSE, TKMCE
  • 104. 104 Dept. of CSE, TKMCE The stack frames for displayRange(1, 3)
  • 105. Higher-Order Functions A function is called Higher Order Function if it contains other functions as a parameter or returns a function as an output. i.e, The functions that operate with another function are known as Higher order Functions. Functions as First-Class Data Objects: In Python, functions can be treated as first-class data objects. This means that - they can be assigned to variables - passed as arguments to other functions - returned as the values of other functions - and stored in data structures such as lists and dictionaries. 105 Dept. of CSE, TKMCE
  • 106. üPassing a function as an argument to another function is no different from passing any other datum. üThe function argument is first evaluated, producing the function itself, and then the parameter name is bound to this value. üThe function can then be applied to its own argument with the usual syntax. eg., >>> def example(functionArg, dataArg): return functionArg(dataArg) >>> example(abs, -4) 4 >>> example(math.sqrt, 2) 1.4142135623730951 106 Dept. of CSE, TKMCE
  • 107. Mapping, Filtering, Reducing Mapping : The first type of useful higher-order function to consider is called a mapping. This process applies a function to each value in a sequence (such as a list, a tuple, or a string) and returns a new sequence of the results. Python includes a map function for this purpose. Suppose we have a list named words that contains strings that represent integers. We want to replace each string with the corresponding integer value. >>> words = ["231", "20", "-45", "99"] >>> map(int, words) # Convert all strings to ints <map object at 0xl4cbd90> >>> words # Original list is not changed ['231', '20', '-45', '99'] >>> words = list(map(int, words)) # Reset variable to change it >>> words [231, 20, -45, 99] 107 Dept. of CSE, TKMCE
  • 108. # Python program to demonstrate working of map. # Return double of n def addition(n): return n + n # We double all numbers using map() numbers = [1, 2, 3, 4] result = map(addition, numbers) print(list(result)) NOTE : The returned value from map() (map object) then can be passed to functions like list() (to create a list), set() (to create a set) . 108 Dept. of CSE, TKMCE Output: [2, 4, 6, 8]
  • 109. Filtering : A second type of higher-order function is called a filtering. In this process, a function called a predicate is applied to each value in a list. If the predicate returns True, the value passes the test and is added to a filter object (similar to a map object). Otherwise, the value is dropped from consideration. Python includes a filter function that is used to produce a list of the odd numbers in another list: >>> def odd(n): return n % 2 == 1 >>> list(filter(odd, range(10))) [1, 3, 5, 7, 9] 109 Dept. of CSE, TKMCE
  • 110. # function that filters vowels def fun(variable): letters = ['a', 'e', 'i', 'o', 'u'] if (variable in letters): return True else: return False sequence = ['g', 'e', 'e', 'j', 'k', 'i', 'p', 'r'] filtered = filter(fun, sequence) print('The filtered letters are:') for s in filtered: print(s) 110 Dept. of CSE, TKMCE Output: The filtered letters are: e e i
  • 111. Reducing: Here we take a list of values and repeatedly apply a function to accumulate a single data value. A summation is a good example of this process. The first value is added to the second value, then the sum is added to the third value, and so on, until the sum of all the values is produced. eg., >>> from functools import reduce >>> def add(x, y): return x + y >>> def multiply(x, y): return x * y >>> data = [1, 2, 3, 4] >>> reduce(add, data) 10 >>> reduce(multiply, data) 24 111 Dept. of CSE, TKMCE
  • 112. Boolean Functions • A Boolean function usually tests its argument for the presence or absence of some property. • The function returns True if the property is present, or False otherwise. def odd(x): """Returns True if x is odd or False otherwise.""" if x % 2 == 1: return True else: return False >>> odd(5) True 112 Dept. of CSE, TKMCE
  • 113. Using lambda to Create Anonymous Functions • Python includes a mechanism called lambda that allows the programmer to create functions in this manner. • A lambda is an anonymous function. It has no name of its own, but it contains the names of its arguments as well as a single expression. • When the lambda is applied to its arguments, its expression is evaluated, and its value is returned. The syntax of a lambda is very tight and restrictive: lambda <argname-1, ..., argname-n>: <expression> >>> mul = lambda a,b : a*b >>> print(mul(12,12)) 144 113 Dept. of CSE, TKMCE
  • 114. Use of range, reduce, and lambda to simplify the definition of the summation function from functools import reduce def summation(lower, upper) if lower > upper: return 0 else: return reduce(lambda x,y : x+y , range(lower,upper+1)) 114 Dept. of CSE, TKMCE
  • 115. # Python code to illustrate filter() with lambda() li = [5, 7, 22, 97, 54, 62, 77, 23, 73, 61] final_list = list(filter(lambda x: (x%2 != 0) , li)) print(final_list) # Python code to illustrate map() with lambda() to get double of a list. li = [5, 7, 22, 97, 54, 62, 77, 23, 73, 61] final_list = list(map(lambda x: x*2, li)) print(final_list) 115 Dept. of CSE, TKMCE Output: [5, 7, 97, 77, 23, 73, 61] Output: [10, 14, 44, 194, 108, 124, 154, 46, 146, 122]