Professional Documents
Culture Documents
L12 Usage of Import
L12 Usage of Import
• >>> int('Hello')
• ValueError: invalid literal for int(): Hello
• >>> float('3.14159')
• 3.14159
• >>> str(3.14159)
• '3.14159'
type functions
• The type function:
• Takes an expression as the argument of the function.
• The result for this function, is the type of the argument
• Ex1:
• >>> type(32)
• <type 'int'>
Math functions
Filename: importsum4
def linecount(filename):
count = 0
fo = open(filename)
for line in fo:
count += 1
fo.close()
return count
print (linecount('hello.txt'))
print (linecount(‘lc.py'))
Note: the file name of above program is lc.py. So it reads itself and prints the number of
lines in the file, which is 9.
Importing our own modules
def linecount(filename):
import lc
count = 0 print (lc)
fo = open(filename) print (lc.linecount("lc.py"))
for line in fo:
count += 1 output
fo.close()
return count <module 'lc' from
'C:/Users/Mahe/Desktop/Python
Programming/Python 3 Programs 2018\\lc.py'>
9
Working in IDLE
• Additionally check in IDLE about our module
• File-> Module Browser which shows
• lc.py
• def linecount(…)
Locating Modules