Chapter 4: Functions

Example 4.1, Page number: 91

In [21]:
# Function declaration and result
a = 5
c = 'h'
b = 3.14
def myfunction (x, ch, fl) :
    print ('%d %c %.2f ' % (x, ch, fl))
    
myfunction (a, c, b)
5 h 3.14 

Example 4.2, Page number: 92

In [1]:
# Variable declaration
a = int(raw_input('Enter first number: '))
b = int(raw_input('Enter second number: '))

# Function declaration, calculation and result
def add (a, b) :
   som = a + b
   return som

print ('The sum of %d and %d is %d' % (a, b, (add (a, b))))
Enter first number: 7
Enter second number: 8
The sum of 7 and 8 is 15

Example 4.3, Page number: 99

In [2]:
# Variable declaration
a = int(raw_input('Enter first number: '))
b = int(raw_input('Enter second number: '))

# Function declaration, calculation and result
def greater (a, b) :
   if a > b :
      return a
   else :
      return b

print ('The greater number is %d' % (greater (a, b)))
Enter first number: 78
Enter second number: 190
The greater number is 190

Example 4.4, Page number: 100

In [3]:
# Variable declaration
a = int(raw_input('Enter first number: '))
b = int(raw_input('Enter second number: '))

# Function declaration, calculation and result
def greater (a, b) :
   if a == b :
      print ('Both numbers are equal')
   elif a > b :
      print ('The greater number is %d' % a)
   else :
      print ('The greater number is %d' % b)
   return

greater (a, b)
Enter first number: 20
Enter second number: 20
Both numbers are equal

Example 4.5, Page number: 102

In [4]:
# Variable declaration
a = int(raw_input('Enter first number: '))
b = int(raw_input('Enter second number: '))
c = int(raw_input('Enter third number: '))

# Function declaration, calculation and result
def greatest (a, b, c) :
   if a > b :
      if a > c :
         return a
      else :
         return c
   else :
      if b > c :
         return b
      else :
         return c

print ('The greatest number is %d' % (greatest (a, b, c)))
Enter first number: 45
Enter second number: 128
Enter third number: 190
The greatest number is 190

Example 4.6, Page number: 104

In [5]:
# Variable declaration
n = int(raw_input('Enter a number: '))

# Function declaration, calculation and result
def printnum (n) :
   print ('Counting upto %d' % n)   
   for i in range (1, n+1) :
      print i,

printnum (n)
Enter a number: 7
Counting upto 7
1 2 3 4 5 6 7

Example 4.7, Page number: 105

In [6]:
# Variable declaration
n = int(raw_input('Enter a number: '))

# Function declaration, calculation and result
def printeven (n) :
   print ('Even numbers upto %d' % n)   
   for i in range (2, n+1, 2) :
      print i,

printeven (n)
Enter a number: 7
Even numbers upto 7
2 4 6

Example 4.8, Page number: 107

In [7]:
# Variable declaration
n = int(raw_input('Enter a number: '))

# Function declaration, calculation and result
def printodd (n) :
   print ('Odd numbers upto %d' % n)   
   for i in range (1, n+1, 2) :
      print i,

printodd (n)
Enter a number: 7
Odd numbers upto 7
1 3 5 7

Example 4.9, Page number: 108

In [8]:
# Variable declaration
n = int(raw_input('Enter a number: '))

# Function declaration, calculation and result
def printnum (n) :
   print ('Number line for %d' % n)   
   for i in range (-n, n+1) :
      print i,

printnum (n)
Enter a number: 4
Number line for 4
-4 -3 -2 -1 0 1 2 3 4

Example 4.10, Page number: 110

In [9]:
# Variable declaration
n = int(raw_input('Enter a number: '))

# Function declaration, calculation and result
def summate (n) :
   total = 0
   for i in range (1, n+1) :
      total = total + i
   return total

print('Sum of first %d natural numbers is %d' % (n, summate (n)))
Enter a number: 6
Sum of first 6 natural numbers is 21

Example 4.11, Page number: 111

In [10]:
# Variable declaration
n = int(raw_input('Enter a number: '))

# Function declaration, calculation and result
def factors (n) :
   print ('The factors of %d are' % n)
   for i in range (1, n+1) :
      if n%i == 0 :
         print i,

factors (n)
Enter a number: 28
The factors of 28 are
1 2 4 7 14 28

Example 4.12, Page number: 113

In [11]:
# Variable declaration
n = int(raw_input('Enter a number: '))

# Function declaration, calculation and result
def multiple (n) :
   print ('The first 10 multiples of %d are' % n)
   for i in range (1, 11) :
         print('%d X %d = %d' % (n, i, n*i))

multiple (n)
Enter a number: 6
The first 10 multiples of 6 are
6 X 1 = 6
6 X 2 = 12
6 X 3 = 18
6 X 4 = 24
6 X 5 = 30
6 X 6 = 36
6 X 7 = 42
6 X 8 = 48
6 X 9 = 54
6 X 10 = 60

Example 4.13, Page number: 114

In [12]:
# Variable declaration
n = int(raw_input('Enter a number: '))

# Function declaration, calculation and result
def sumofdigit (n) :
   sum = 0
   while n>0 :
      sum = sum + n%10
      n = n/10
   return sum

print ('The sum of digits of %d is %d' % (n, sumofdigit (n)))
Enter a number: 5214
The sum of digits of 5214 is 12

Example 4.14, Page number: 116

In [13]:
# Variable declaration
n = int(raw_input('Enter a number: '))

# Function declaration, calculation and result
def prime (n) :
   count = 0
   for i in range (2, n) :
      if n%i == 0 :
         count = count + 1
         break

   if count == 0 :
      print ('The number %d is prime' % n)
   else :
      print ('The number %d is composite' % n)

prime (n)
Enter a number: 14
The number 14 is composite

Example 4.15, Page number: 118

In [15]:
# Variable declaration
n = int(raw_input('Enter a number: '))

# Function declaration, calculation and result
print ('%d terms of Fibonacci series are ' % n)

def printfibo (n) :
   first = 0
   second = 1
   count = 3

   print ('%d  %d ' % (first, second)),

   while count<=n :
      third = first + second
      first = second
      second = third
      print (' %d ' % third), 
      count = count + 1

printfibo (n)
Enter a number: 10
10 terms of Fibonacci series are 
0  1   1   2   3   5   8   13   21   34 

Example 4.16, Page number: 120

In [16]:
# Variable declaration
n = int(raw_input('Enter a number: '))

# Function declaration, calculation and result
def sumofdigit (n) :
   sum = n
   while sum>=10 :
      sum = 0
      while n>0 :
         sum = sum + n%10
         n = n/10
      n = sum
   return sum

print ('Sum of digits of %d is %d' % (n, sumofdigit (n)))
Enter a number: 8626
Sum of digits of 8626 is 4

Example 4.17, Page number: 122

In [18]:
# Variable declaration
n = int(raw_input('Enter a number: '))

# Function declaration, calculation and result
print ('Prime numbers upto %d are ' % n)

def printprime (n) :
   for num in range (2, n+1) :
      flag = 1
      for i in range (2, num) :
         if num % i == 0 : 
            flag = 0
      if flag :
         print ('%d ' % num),

printprime (n)
Enter a number: 40
Prime numbers upto 40 are 
2  3  5  7  11  13  17  19  23  29  31  37 

Example 4.18, Page number: 124

In [20]:
# Variable declaration
n = int(raw_input('Enter a number: '))

# Function declaration, calculation and result
def pattern (n) :
   for lin in range (1, n+1) :
      for count in range (1, lin+1) :
         print ('%d ' % count),
      print

pattern (n)
Enter a number: 4
1 
1  2 
1  2  3 
1  2  3  4 

Example 4.19, Page number: 126

In [21]:
# Variable declaration
n = int(raw_input('Enter a number: '))

# Function declaration, calculation and result
def pattern (n) :
   for lin in range (1, n+1) :
      for count in range (lin, 2*lin) :
         print ('%d ' % count),
      print

pattern (n)
Enter a number: 4
1 
2  3 
3  4  5 
4  5  6  7 

Example 4.20, Page number: 128

In [23]:
# Variable declaration
n = int(raw_input('Enter a number: '))

# Function declaration, calculation and result
def pattern (n) :
   for lin in range (1, n+1) :
      for blk in range (1, n-lin+1) :
         print ' ',
      for up in range (1, lin+1) :
         print ('%d' % up),
      down = lin-1
      while down>0 :
         print ('%d' % down),
         down = down-1
      print

pattern (n)
Enter a number: 4
      1
    1 2 1
  1 2 3 2 1
1 2 3 4 3 2 1

Example 4.21, Page number: 131

In [24]:
# Variable declaration
n = int(raw_input('Enter a number: '))

# Function declaration, calculation and result
def pattern (n) :
   for lin in range (1, n+1) :
      for blk in range (1, n-lin+1) :
         print ' ',
      for up in range (lin, 2*lin) :
         print ('%d' % (up%10)),
      down = 2*lin-2
      while down>=lin :
         print ('%d' % (down%10)),
         down = down-1
      print

pattern (n)
Enter a number: 4
      1
    2 3 2
  3 4 5 4 3
4 5 6 7 6 5 4