UNIT III CONTROL FLOW, FUNCTIONS

**Conditionals: Boolean values and operators, conditional (if), alternative (if-else), chained conditional (if-elif-else); Iteration: state, while, for, break, continue, pass; Fruitful functions: return values, parameters, local and global scope, function composition, recursion; Strings: string slices, immutability, string functions and methods, string module; Lists as arrays. Illustrative programs: square root, gcd, exponentiation, sum an array of numbers, linear search, binary search**

3.1. CONDITIONALS:

3.1.1 BOOLEAN VALUES AND OPERATORS:

Boolean and Logical operators

1. and Operator:

1. or Operator:

Example 3.1:

A python program to display the National holidays. For example: January 26-Republic day August 15-Indepependance day October 2-Gandhi Jayanthi* If user input is other than the given condition,Error will be displayed as “Invalid input”.

Program: print("Enter Month and Day") month=input() day=int(input()) if(month =="january" and day ==26): print ("republic day") elif(month =="august" and day ==15): print ("independence day") elif(month =="october" and day ==2): print ("Gandhi Jayanthi") else: print ("invalid input")

Output Enter Month and Day august 15 independence day

Example 3.2:

Number of days in a month varies from 30 to 31 days. Write a python program to read the name of the month and display corresponding days. If he input is other than the given month, then display error message as “Invalid”.

Program: import sys month=input("Enter the name of the month") if(month=="may" or month=="july" or month=="august"): print ("31") elif(month=="jun"): print ("30") else: print ("invalid")

Output Enter name of the month may 31

3.1.2. OPERATORS

i. Arithmetic Operators

ii. Comparison (Relational) Operators iii. Assignment Operators iv. Logical Operators v. Membership Operators vi. Identity Operators

i. Arithmetic operators

ii. Comparison Operators

iii. Assignment and Compound Operators

iv. Logical Operators

v. Membership Operators

vi. Identity Opertors

3.1.3. CONDITIONAL STATEMENTS

Available conditional statements in python are

i. if statement

ii. if..else statement

iii. if..elif..else statement

iv. Nested if statement

Rules for conditional statements:

3.1.3.1. ‘if’ STATEMENT (CONDITIONAL STATEMENT)

Example 3.3: Python program to check whether a person is eligible for vote.

Program: print("Enter Your age") n=int(input()) if(n>=18): print("Eligible for voting") Output Enter Your age 21 Eligible for voting

3.1.3.2. ‘if...else’ STATEMENT(ALTERNATIVE CONDITIONAL STATEMENT)

Syntax of if...else

			if (test expression/condition) :
				statement(s)
			else:
				statement(s)

Example 3.4:

Python program to checks if the number is positive or negative

Program num = 3 if num >= 0: print("Positive or Zero") else: print("Negative number")

Output Positive or Zero

Example 3.5:

Program

			print("Enter a number")
			num=int(input())
			if(num%2)==0:
			   print("Even Number")
			else:
			    print("Odd Number")

Output

			Enter a number
			10
			Even Number

3.1.3.3. ‘if...elif...else’(CHAINED CONDITIONAL STATEMENT)

Syntax of if...elif...else

			if (test expression/condition):
				Body of if
			elif (test expression/condition):
				Body of elif
			else:
				Body of else

Example 3.6:

Python program to check if the number is positive or negative or zero

Program num = 3.4 if num > 0: print("Positive number") elif num == 0: print("Zero") else: print("Negative number")

Output

			Negative number

Example 3.7:

Program

			n=int(input("Enter a number between seven and ten"))
			if(n==7):
			    print("heptagon")
			elif(n==8):
			    print("octogon")
			elif(n==9):
			    print("nanogon")
			elif(n==10):
			    print("decagon")
			else:
			    print("input should be from 7 to 10")

Output

			Enter a number between seven and ten
			8
			octogon

3.1.3.4. NESTED CONDITIONAL

Example 3.8:

Python program to check if the number is positive or negative or zero using nested if.

Program

			num = float(input("Enter a number: "))
			if num >= 0:
			    if num == 0:
				print("Zero")
			    else:
				print("Positive number")
			else:
			    print("Negative number")

Output

			Enter a number: 10
			Positive number

3.2. REPETITION STRUCTURE/LOOPING/ITERATIVE STATEMENTS

i. ‘for‘ Statement ii. ‘while’ Statement

3.2.1. ‘for’ LOOP

Syntax of for Loop

			for val in sequence:
				Body of for

Example 3.9:

Python Program to find the sum of all numbers stored in a list

Program #number list numbers = [6, 5, 3, 8, 4, 2, 5, 4, 11] sum = 0 for val in numbers: sum = sum+val print("The sum is", sum) Output The sum is 48

3.2.2. ‘while’ LOOP:

Syntax

			while test_expression:
				Body of while

Example 3.10:

Python program using while Loop to add natural numbers upto n

Program

			n = int(input("Enter a number: "))
			sum = 0
			i= 1
			while i <= n:
			    sum = sum + i
			    i = i+1
			print("The sum is", sum)

Output

			Enter a number: 10
			The sum is 55

In the above program, the test expression will be True as long as our counter variable i is less than or equal to n (10 in our program).

while loop with else

Example 3.11:

Python program to illustrate the use of else statement with the while loop

Program

			counter = 0
			while counter < 3:
				print("Inside loop")
				counter = counter + 1
			else:
				print("Inside else")

Output

			Inside loop
			Inside loop
			Inside loop
			Inside else

Difference between while and for loop

3.2.3. UNCONDITIONAL STATEMENTS

i. Break

ii. Continue

iii. Pass

i. Python break statement

Syntax of break

			break

Example 3.12:

Python program to illustrate break statement inside loop

Program

			for val in "string":
				if val == "i":
						break
				print(val)
			print("The end")

Output

			s
			t
			r
			The end

Example 3.13:

Python program to demonstrate break

Program

			i=1
			while i<=10:
			    if(i==5):
				break
			    print(i)
			    i = i + 1
			print("completed")

Output

			1
			2
			3
			4
			completed

ii. Python continue statement

-[] The continue statement is used to skip the rest of the code inside a loop for the current iteration only. -[] Loop does not terminate but continues on with the next iteration.

Syntax of Continue

			continue

Example 3.14:

Python Program to show the use of continue statement inside loops

Program

			for val in "string":
				if val == "i":
					continue
				print(val)
			print("The end")

Output

			s
			t
			r
			n
			g
			The end

Example 3.15:

Python Program to show the use of continue statement inside loops

Program

			n=int(input("Enter total number of numbers\n"))
			for i in range(0,n):
				a=int(input("Enter the number:"))
				if(a<0):
					continue
				print("Number is "+str(a))

Output

			Enter total number of numbers
			5
			Enter the number:12
			Number is 12
			Enter the number:14
			Number is 14
			Enter the number:16
			Number is 16
			Enter the number:-1
			Enter the number:23
			Number is 23

iii. Pass STATEMENT

Syntax of pass

		pass

Example 3.16:

Python program to illustrate pass

Program

			sequence = {'p', 'a', 's', 's'}
			for val in sequence:
			    pass
			print("pass is just a placeholder for functionality to be added later.")

Output

			pass is just a placeholder for functionality to be added later.

Example 3.17:

Program

			for num in [20, 11, 9, 66, 4, 89, 44]:
				if num%2 == 0:
					pass
				else:
					print(num)

Output

			11
			9
			89

3.3. FRUITFUL FUNCTIONS: RETURN VALUES, PARAMETERS, LOCAL AND GLOBAL SCOPE, FUNCTION COMPOSITION, RECURSION

3.3.1. Functions

i. Creating a Function

Syntax

			def functionname( parameters ):
				"function_docstring"
				function body
				return [expression]

Example 3.18:

			def my_function():
				print("Hello from a function")

ii. Calling a Function To call a function, use the function name followed by parenthesis.

Example 3.19:

Program

			def my_function():
				print("Hello from a function")
			my_function()

Output

			Hello from a function

iii. Parameters

Example 3.20:

Program

			def my_function(fname):
				print(fname + " Refsnes")

			#main function
			my_function("Emil")
			my_function("Tobias")
			my_function("Linus")

Output

			Emil Refsnes
			Tobias Refsnes
			Linus Refsnes

iv. Default Parameter Value

Example 3.21:

Program

			def my_function(country = "Norway"):
				print("I am from " + country)

			#main function
			my_function("Sweden")
			my_function("India")
			my_function()
			my_function("Brazil")

Output

			I am from Sweden
			I am from India
			I am from Norway
			I am from Brazil

3.3.2 Return Values

Example 3.22:

Program

			def my_function(x):
				return 5 * x

			#function calling statements
			print(my_function(3))
			print(my_function(5))
			print(my_function(9))

Output

			15
			25
			45

3.3.3 Function Arguments

The types of formal arguments are:

  i.	Required arguments

 ii.	Keyword arguments

iii.	Default arguments

 iv.	Variable-length arguments

i. Required arguments

Example 3.23:

Program

			#Function definition where str is the required argument
			def display(str):
			    print(str)

			#main script
			str="hello"
			display(str)

Output

			hello

ii. Keyword arguments

Example 3.24:

Program

			def display(name,number,fraction):
			    print("The string is:",name)
			    print("The integer is:",number)
			    print("The float is:",fraction)

			#Main script-Fucntion calling with keyword arguments
			display(fraction=58.62,name="hello",number=28)

Output

			The string is: hello
			The integer is: 28
			The float is: 58.62

iii. Default arguments

Example 3.25:

Program

			def printinfo( name, age = 35 ):
				'''This prints a passed info into this function'''
				print("Name: ",name)
				print("Age ",age)
				return

				#Calling printinfo function
				printinfo(age=50, name="miki" )
				printinfo(name="miki")

Output

			Name:  miki
			Age  50
			Name:  miki
			Age  35

iv. Variable-length arguments

Syntax:

			def functionname([formal_args,] *var_args_tuple ):
				"function_docstring"
				function body
				return [expression]

An asterisk is placed before the variable name that holds the values of all non keyword variable arguments. This tuple remains empty if no additional arguments are specified during the function call.

Example 3.26:

Program

			def printinfo( arg1, *vartuple ):
				'''This prints a variable passed arguments'''
				print("Output is:")
				print(arg1)
				for var in vartuple:
					print(var)
				return

			#Calling printinfo function
			printinfo( 10 )
			printinfo( 70, 60, 50 )

Output

			Output is:
			10
			Output is:
			70
			60
			50

3.3.4. Scope of Variables

Global vs. Local variables

Example 3.27:

Program

			total = 0; # This is global variable.
			def sum(arg1, arg2):
			# Add both the parameters and return them."
			    total = arg1 + arg2; # Here total is local variable.
			    print("Inside the function local total : ", total)
			    return total;

			#Calling sum function
			sum( 10, 20 );
			print("Outside the function global total : ", total)

Output

			Inside the function local total :  30
			Outside the function global total : 0

3.3.5. Function composition or Anonymous Functions

Syntax

			lambda [arg1 [,arg2,.....argn]]:expression

Example 3.28:

Program

			# Function definition is here
			sum = lambda arg1, arg2: arg1 + arg2;
			# Now you can call sum as a function
			print("Value of total : ", sum( 10, 20 ))
			print("Value of total : ", sum( 20, 20 ))

Output

			Value of total :  30
			Value of total :  40

3.3.6. Recursive Function:

Factorial of a number is the product of all the integers from 1 to that number. For example, the factorial of 6 (denoted as 6!) is 12345*6 = 720.

Example 3.29:

Program def calc_factorial(x): '''This is a recursive function to find the factorial of an integer''' if(x == 1): return 1 else: return (x * calc_factorial(x-1))

			#Main Script
			num = 4
			print("The factorial of", num, "is",calc_factorial(num))

Output

			The factorial of 4 is 24

calc_factorial(4) # 1st call with 4

  • calc_factorial(3) # 2nd call with 3

  • 3 * calc_factorial(2) # 3rd call with 2

  • 3 _ 2 _ calc_factorial(1) # 4th call with 1

  • 3 _ 2 _ 1 # return from 4th call as number=1

  • 3 * 2 # return from 3rd call

  • 6 # return from 2nd call # return from 1st call

Advantages of recursion

  • Recursive functions make the code look clean and elegant.

  • A complex task can be broken down into simpler sub-problems using recursion.

  • Sequence generation is easier with recursion than using some nested iteration.

Disadvantages of recursion

  • Sometimes the logic behind recursion is hard to follow through.

  • Recursive calls are expensive (inefficient) as they take up a lot of memory and time.

  • Recursive functions are hard to debug.

3.4. STRINGS

A string is a sequence of characters. You can access the characters one at a time with the bracket operator:

Example 3.30:

Program

			fruit = 'banana'
			letter = fruit[1]
			print("The letter present at index 1 is:",letter)

Output The letter present at index 1 is: a The second statement selects character number 1 from fruit and assigns it to letter. The expression in brackets is called an index. The index indicates which character in the sequence you want (hence the name). Always index starts from 0. The value of the index has to be an integer. Otherwise you get:

Example 3.31:

Program fruit = 'banana' letter = fruit[1.5] print("The letter present at index 1 is:",letter) Output Traceback (most recent call last): File "c:\users\administrator\mu_code\or_op.py", line 2, in letter = fruit[1.5] TypeError: string indices must be integers

3.4.1. String Lengths

len() is a built-in function that returns the number of characters in a string.

Example 3.32:

Program

			fruit = 'banana'
			length = len(fruit)
			print("The length of the strength is:",str(length))

Output

			The length of the strength is: 6

To get the last letter of a string, you might be tempted to try something like this:

Example 3.33:

Program fruit = "banana" length = len(fruit) last = fruit[length] print("The last letter in fruit is:",last) Output

			Traceback (most recent call last):
			  File "c:\users\administrator\mu_code\or_op.py", line 3, in <module>
			    last = fruit[length]
			IndexError: string index out of range

The reason for the IndexError is that there is no letter in 'banana' with the index 6. Since we started counting at zero, the six letters are numbered 0 to 5. To get the last character, you have to subtract 1 from length:

			last = fruit[length-1]
			print(last)  #dispalys a

Alternatively, you can use negative indices, which count backward from the end of the string. The expression fruit[-1] yields the last letter, fruit[-2] yields the second to last, and so on.

3.4.2. String slices

A segment of a string is called a slice. Selecting a slice is similar to selecting a character:

			str = 'Monty Python'
			print(str[0:5])      #Prints Monty
			print(str[6:12]      #Prints Python

The operator [n:m] returns the part of the string from the “n-eth” character to the “m-eth” character, including the first but excluding the last. If you omit the first index (before the colon), the slice starts at the beginning of the string. If you omit the second index, the slice goes to the end of the string: fruit = 'banana' print(fruit[:3]) #Prints 'ban' print(fruit[3:] #Prints 'ana' If the first index is greater than or equal to the second the result is an empty string, represented by two quotation marks:

			fruit = 'banana'
			print(fruit[3:3]) #Prints ' '

An empty string contains no characters and has length 0, but other than that, it is the same as any other string.

3.4.3. Strings are immutable

It is tempting to use the [] operator on the left side of an assignment, with the intention of changing a character in a string. For example:

			greeting = 'Hello, world!'
			greeting[0] = 'J'
			#Causes following output
TypeError: 'str' object does not support item assignment

The “object” in this case is the string and the “item” is the character you tried to assign. For now, an object is the same thing as a value, but we will refine that definition later. An item is one of the values in a sequence. The reason for the error is that strings are immutable, which means you can’t change an existing string. The best you can do is create a new string that is a variation on the original:

			greeting = 'Hello, world!'
			new_greeting = 'J' + greeting[1:]
			print(new_greeting)       #Prints Jello, world!

This example concatenates a new first letter onto a slice of greeting. It has no effect on the original string.

3.4.4. String methods

A method is similar to a function—it takes arguments and returns a value—but the syntax is different. For example, the method upper takes a string and returns a new string with all uppercase letters: Instead of the function syntax upper(word), it uses the method syntax word.upper(). word = 'banana' new_word = word.upper() print(new_word) #Prints BANANA

This form of dot notation specifies the name of the method, upper, and the name of the string to apply the method to, word. The empty parentheses indicate that this method takes no argument. A method call is called an invocation; in this case, we would say that we are invoking upper on the word. As it turns out, there is a string method named find that is remarkably similar to the function we wrote:

			 word = 'banana'
			 index = word.find('a')
			 print(index)    #Prints 1

In this example, we invoke find on word and pass the letter we are looking for as a parameter.

i. find method

Actually, the find method is more general than our function; it can find substrings, not just characters: word = 'banana' print(word.find('na')) #Prints 2

It can take as a second argument the index where it should start: word = 'banana' print(word.find('na', 3)) # Prints 4

And as a third argument the index where it should stop:

			name = 'bob'
			print(name.find('b', 1, 2))   #Prints -1

This search fails because b does not appear in the index range from 1 to 2 (not including 2).

ii. String comparison

The relational operators work on strings. To see if two strings are equal: if(word == 'banana'): print('All right, bananas.') Other relational operations are useful for putting words in alphabetical order:

			if(word < 'banana'):
				print('Your word,' + word + ', comes before banana.')
			elif(word > 'banana'):
				print('Your word,' + word + ', comes after banana.')
			else:
				print('All right, bananas.')

Python does not handle uppercase and lowercase letters the same way that people do. All the uppercase letters come before all the lowercase letters, so:Your word, Pineapple, comes before banana. A common way to address this problem is to convert strings to a standard format, such as all lowercase, before performing the comparison. Keep that in mind in case you have to defend yourself against a man armed with a Pineapple.

iii. Indexing

Individual characters in a string can be accessed using subscript([ ]) operator. The expression in bracket is called as Index.

iv. Traversing a String

A string can be traversed by accessing characters from one index to another.

3.4.4 . String module

-[] The string module consists of number of useful constants,classes and functions -[] These functions are used to manipulate strings -[] Some constants are defined in string module are: + string.ascii_lowercase - Refers all lower case letters. Example: a-z + string.ascii_uppercase - Refers all upper case letters. Example: A-Z + string.digits - Refer digits from 0-9 + string.uppercase - A string that has all the characters that are considered upper case letters Example: A-Z + string.whitespace - A string that has all characters that are considered white space like space,tab etc

Example: Hello world Hai Ajay Output: HelloworldHaiAjay

Example 3.34: Program that use different string methods

Program

			str="welcome to the world of python"
			print("uppercase=",str.upper( ))
			print("lowercase=",str.lower( ))
			print("split=",str.split( ))
			print("join=",'-'.join(str.split( )))
			print("replace=",str.replace("python","java" ))
			print("count of o=",str.count('o' ))
			print("find of =",str.find("of"))

Output

			uppercase= WELCOME TO THE WORLD OF PYTHON
			lowercase= welcome to the world of python
			split= ['welcome', 'to', 'the', 'world', 'of', 'python']
			join= welcome-to-the-world-of-python
			replace= welcome to the world of java
			count of o= 5
			find of = 21

3.4.5. Example Programs:**

1. Traversing a string

Program message="hello!" index=0 for i in message: print("message[",index,"]=",i) index+=1 Output

			message[ 0 ]= h
			message[ 1 ]= e
			message[ 2 ]= l
			message[ 3 ]= l
			message[ 4 ]= o
			message[ 5 ]= !

2. String operations

Program

			str="Hello This is python"
			i=2
			print("Character at index 2: ",str[i])
			print("Character at index i*3+1: ",str[i*3+1])

Output

			Character at index 2:  l
			Character at index i*3+1:  h

3. Concatenate two strings

Program

			str1="hello"
			str2="world"
			str3=str1+str2
			print("The string is:",str3)

Output

			The string is: helloworld

4. Append a string

Program

			Greet="Hello"
			n=input("Enter the String to Concatenate:")
			Greet+=n;
			print(Greet))

Output Enter the String to Concatenate:World HelloWorld 5. String slices

-[] A substring of a string is called a slice -[] A slice operation is used to refer the subpart of strings -[] The subset of a string can be taken from string by using [ ] operator

Example 1: Positive Indexing/Slicing

Program

			str="python"
			print("str[1:5]=",str[1:5])
			print("str[:6]=",str[:6])
			print("str[1:]=",str[1:])
			print("str[:]=",str[:])
			print("str[1:20]=",str[1:20])

Output

			str[1:5]= ytho
			str[:6]= python
			str[1:]= ython
			str[:]= python
			str[1:20]= ython

Example 2: Negative Indexing/Slicing

Program str="python" print("str[-1]=",str[-1]) print("str[-6]=",str[-6]) print("str[-2:]=",str[-2:]) print("str[:-2]=",str[:-2]) print("str[-5:-2]=",str[-5:-2]) Output

			str[-1]= n
			str[-6]= p
			str[-2:]= on
			str[:-2]= pyth
			str[-5:-2]= yth

Example:3 Program

			str="welcome to the world of python"
			print("str[2:10]=",str[2:10])
			print("str[2:10:1]=",str[2:10:1])
			print("str[2:10:2]=",str[2:10:2])
			print("str[2:13:4]=",str[2:13:4])
			print("str[::3]=",str[::3])
			print("str[::-1]=",str[::-1])
			print("str[::-3]=",str[::-3])

Output

			str[2:10]= lcome to
			str[2:10:1]= lcome to
			str[2:10:2]= loet
			str[2:13:4]= le
			str[::3]= wceohwloph
			str[::-1]= nohtyp fo dlrow eht ot emoclew
			str[::-3]= nt  r ttml

3.4.6. String functions and methods

-[] Python supports many build-in methods to manipulate strings -[] A method is like a function

Last updated