Unit 3 (Second) - 1
Unit 3 (Second) - 1
• def sum_odd(N):
• ''' this function is used to return the sum of first N odd numbers...'''
• sum=0
• for i in range(1,N+1,2):
• sum=sum+i
• return sum
• print(sum_odd(7))
• def sum_even(N):
• ''' this function is used to return the sum of first N even numbers...'''
• sum=0
• for i in range(2,N+1,2):
• sum=sum+i
• return sum
• print(sum_even(7))
• ## open new file in IDLE
• Write :
– import sum
– print(sum.sum_even(10)) # call first function
– # to know the help of any file write
• help(sum)
– # to know about specific function write:
• Help(sum.funname)
• Help(sum.sum_even)
• help(sum.sum_odd)
• Help on function sum_odd in module sum:
• sum_odd(N)
• this function is used to return the sum of first N odd
numbers...
ways to call the module
• 1. from <modulename> import <funname>
• #print(<funname()>
• 2. from <modulename> import * # all function called
• 3. import <modulename>
• #call function
• <modulename>.<funname>
• # using Alias name
• Import <modulename> as <aliasname>
• Like : import m1 as m
• Print(M.<funname>)
Predefined
• import calendar
• print(calendar.month(2023,11))
• import keyword
• print(keyword.kwlist)
• max(20,50,30,5)
• 50
• min(50,6,45,0,-1,20)
Random Modules