Download as ppt, pdf, or txt
Download as ppt, pdf, or txt
You are on page 1of 22

Usage of import

Type Conversion functions


• Python provides built-in functions that convert values from one
type to another.
• The int function takes any value and converts it to an integer
• >>> int('32')
• 32

• >>> int('Hello')
• ValueError: invalid literal for int(): Hello

• int can convert floating-point values to integers, but it doesn’t


round off; it chops off the fraction part:
• >>> int(3.99999)
• 3
• >>> int(-2.3)
• -2
Type Conversion functions
• float converts integers and strings to floating-point numbers:
• >>> float(32)
• 32.0

• >>> float('3.14159')
• 3.14159

• str converts its argument to a string:


• >>> str(32)
• '32‘

• >>> 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

• The math module object contains the functions and variables


defined in the module.
• To access functions, specify the name of the module math
and the name of the function, separated by a dot (also known
as a period).
• This format is called dot notation.
• >>> ratio = signal_power / noise_power
• >>> decibels = 10 * math.log10(ratio)
• >>> radians = 0.7
• >>> height = math.sin(radians)

Note: The math module provides log, computes logarithms base e.


trigonometric functions (cos, tan, etc.) take arguments in radians
Math functions

• To convert from degrees to radians, divide by 360 and


multiply by 2* pi
• >>> degrees = 45
• >>> radians = degrees / 360.0 * 2 * math.pi
• >>> math.sin(radians)
• 0.707106781187
• The expression math.pi gets the variable pi from the math
module.
• The value of this variable is an approximation of pi, accurate
to about 15 digits.

Note: find the precedence of operators


Math functions - composition

• x = math.sin(degrees / 360.0 * 2 * math.pi)


• With function calls:
• x = math.exp(math.log(x+1))

• Getting help for math functions:


• >>> import math
• >>> help(math.pow)
• Help on built-in function pow in module math:
• pow(...)
• pow(x,y)
• Return x**y (x to the power of y).
• (END)
Importing math functions
Importing math functions
• Modules refer to a file containing Python statements and
definitions.
• A file containing Python code, for e.g.: ”example.py”, is called
a module and its module name would be ”example”.
• We use modules to break down large programs into small
manageable and organized files.
• Furthermore, modules provide reusability of code.
• We can define our most used functions in a module and
import modules, instead of copying their definitions into
different programs.
Importing math functions

• Python has a math module that provides most of the familiar


mathematical functions.
• A module is a file that contains a collection of related
functions.
• Before we can use the module, we have to import it:
• >>> import math
• This statement creates a module object named math.
• If we print the module object, we get some information about
it:
• >>> print (math)
• <module 'math' (built-in)>
Importing math functions

• Python provides two ways to import modules;


• Method 1:
• >>> import math
• >>> print (math.pi)
• 3.14159265359
• If we import math, we get a module object named math. The
module object contains constants like pi and functions like sin
and exp.

Note: Disadvantage of method 1: accessing pi directly, gives an error.


>>> print (pi)
Traceback (most recent call last): File "<stdin>", line 1, in <module>
NameError: name 'pi' is not defined
Importing with from
• Method 2:
• We can import an object from a module like this:
• >>> from math import pi
• Now we can access pi directly, without dot notation.
• >>> print (pi)
• 3.14159265359
• Or use the star operator to import everything from the module:
• >>> from math import *
• >>> cos(pi)
• -1.0
• The advantage of importing everything from the math module is
that code can be more concise.
• The disadvantage is that there might be conflicts between names
defined in different modules, or between a name from a module
and one of our variables.
Import with renaming

Import with renaming


>>> import math as m
>>>print("The value of pi is", m.pi)
Writing our own modules

• Any file that contains Python code can be


imported as a module.
Problem
• Create a module summation and create a function sum to add
given numbers
• Import summation and show the usage of add function using
following syntax:
1. import <module name>
2. from <module name> import <function/constant>
3. from <module name> import *
4. Import <module name> as m
Import with renaming
>>> import math as m
>>>print("The value of pi is", m.pi)

Note: The from clauses does not use dot notation.


Problem
Filename: summation Filename: importsum1
import summation
def add(a, b): print (summation.add(4,5.5) )
"""This program adds two
numbers and return the Filename: importsum2

result""" import summation as s


print (s.add(4,5.5) )
result = a + b
Filename: importsum3
return result
from summation import *
Output 9.5
print (add(4,5.5) )

Filename: importsum4

from summation import add


print (add(4,5.5) )
Writing our own modules

• Any file that contains Python code can be


imported as a module.
• Ex: suppose a file named lc.py contains the
following code:
Writing our own modules
Filename: lc.py

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

• We can import lc.py like this:


• >>> import lc
• Now we have a module object lc:
• >>> print (lc)
• <module ‘lc' from ‘lc.py'>
• module lc provides a function called linecount:
• >>> lc.linecount(‘lc.py')
• 9
Importing our own modules

Filename: lc.py Filename: importlc.py

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

• When we import a module, the Python interpreter


searches for the module in the following sequences −
1. The current directory.
2. PYTHONPATH (an environment variable with a list of
directory).
3. The installation-dependent default directory.

You might also like