0% found this document useful (0 votes)
7 views41 pages

Python_Lecture

This document covers Python's input function and math module, detailing how to accept user input, convert data types, and perform mathematical operations. It explains various ways to import modules and provides examples of accepting multiple values in a single line. Exercises are included to reinforce the concepts, such as calculating the area of a circle and summing two numbers.

Uploaded by

music93939646
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views41 pages

Python_Lecture

This document covers Python's input function and math module, detailing how to accept user input, convert data types, and perform mathematical operations. It explains various ways to import modules and provides examples of accepting multiple values in a single line. Exercises are included to reinforce the concepts, such as calculating the area of a circle and summing two numbers.

Uploaded by

music93939646
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 41

PYTHON

LECTURE 14
Today’s Agenda

• Input Function And Math Module


In Python

• Using the input( ) Function

• Using the math module

• Different ways of importing a module

• Accepting multiple values in single line


Accepting Input In Python

 To accept user input , Python provides us a


function called input ( )

 Syntax:

 input([prompt])

 The input() function takes a single optional


argument , which is the string to be displayed on
console.
Return Value Of input( )

 The input() function reads a line from keyboard


, converts the line into a string by removing the
trailing newline, and returns it.
Example 1
(Using input() without message)

print("enter your name")


name=input()
print("Hello",name)

Output:
Example 2
(Using input( ) With Message)

name=input("enter your name")


print("Hello",name)

 Output:
Example 3
(Using input( ) With Message)

name=input(“Enter your full name:”)


print("Hello",name)

 Output:
Accepting Integer Input

 By default the function input( ) returns the


inputted value as a string

 So , even if we input a numeric value , still Python


considers it to be string
Accepting Integer Input

 To understand this behavior , consider the


following code:

a=input("enter a number\n")
b=a+1
print(b)

 Output:
Accepting Integer Input

 To solve this , we can use Type Conversion


Functions in Python , for converting a given value
from string to other type.

 For example , in the previous code , we can use the


function int( ) to convert string value to
integer
Accepting Integer Input

a=input("enter a number\n")
b=int(a)+1
print(b)
OR
a=int(input("enter a number\n")
b=a+1
print(b)
Accepting Float And Bool

 For converting input values to float and boolean


we can call float( ) and bool( ) functions

 Example:
s=input("enter your percentage\n")
per=float(s)
print(per)
OR
s=input(“Delete the file ?(yes-True,no-False)")
ans=bool(s)
print(ans)
Exercise

 WAP to accept two numbers from the user


and display their sum
Code:
a=int(input("Enter first num:"))
b=int(input("Enter secnd num:"))
c=a+b
print("Nos are",a,"and",b)
print("Their sum is",c)
Exercise

 Can you write the previous code in one line


only ?
Code:
print("Their sum is",int(input("Enter first
num:"))+int(input("Enter secnd num:")))
Exercise

 WAP to accept radius of a Circle from the user


and calculate area and circumference.
Code:
radius=float(input("Enter radius:"))
area=3.14*radius**2
circum=2*3.14*radius
print("Area is",area)
print("Circumference is",circum)
Exploring More About
math Module

 We have already discussed that Python has a


module called math .

 This module helps us perform mathematical


calculations

 It contains several mathematical constants and


functions
Exploring More About
math Module

 Following are some important functions :


 math.factorial(x)
 math.floor(x)
 math.ceil(x)
 math.gcd(a, b)
 math.pow(x,y)
 math.sqrt(x)

 Following are it’s important mathematical constants:


 math.pi : The mathematical constant π =
3.141592…
 math.e: The mathematical constant e = 2.718281…,
 math.tau: Tau is a circle constant equal to 2π
Modified Version Of Previous
Code Using math Module

Code:
import math
radius=float(input("Enter radius:"))
area=math.pi*math.pow(radius,2)
circum=math.tau*radius
print("Area is",area)
print("Circumference is",circum)
Second Way To Import
A Module

 We can use aliasing for module names

 To do this , Python provides us as keyword

 Syntax:
 import modname as newname

 This helps us to use short names for modules and


make them more easy to use
Using as Keyword

Code
import platform as p
print(p.system())
Second Way Of Writing Previous
Code Using math Module

Code:
import math as m
radius=float(input("Enter radius:"))
area=m.pi*m.pow(radius,2)
circum=m.tau*radius
print("Area is",area)
print("Circumference is",circum)
Third Way To Import A Module

 We can also import specific members of a module

 To do this , Python provides us from keyword

 Syntax:
 from modname import name1[, name2[, ... nameN]]

 In this way we will not have to prefix the module


name before the member name while accessing it
Using from Keyword

Code
from sys import getsizeof
a=10
b=“hello”
print(getsizeof(a))
print(getsizeof(b))
Third Way Of Writing Previous
Code Using math Module

Code:
from math import pi,tau,pow
radius=float(input("Enter radius:"))
area=pi*pow(radius,2)
circum=tau*radius
print("Area is",area)
print("Circumference is",circum)
Fourth Way To Import A Module

 It is also possible to import all names from a


module into the current file by using the wildcard
character *

 Syntax:
 from modname import *

 This provides an easy way to import all the


members from a module into the current file
Using WildCard Character

Code
from sys import *
a=10
b=“hello”
print(getsizeof(a))
print(getsizeof(b))
Fourth Way Of Writing Previous
Code Using math Module

Code:
from math import *
radius=float(input("Enter radius:"))
area=pi*pow(radius,2)
circum=tau*radius
print("Area is",area)
print("Circumference is",circum)
How To List
All Members Of A Module

 In Python , we can print members of a module in


the Python Shell window

 This can be done in 2 ways:

 By calling the dir( ) function passing it the


module name

 By calling the help( ) function passing it the


module name
Using dir( )

 The dir( ) function accepts the name of a module


as argument and returns a list of all it’s
members.

 However the module must be imported before


passing it to the dir( ) function
Using help( )

 The help( ) function accepts the name of a


module as argument and displays complete
documentation of all the members of the
module

 Here also , module must be imported before using


it.
Accepting Different Values

 WAP to accept roll number , grade and


percentage as input from the user and display
it back
Code
roll=int(input("Enter roll no:"))
name=input("Enter name:");
per=float(input("Enter per:"))
print("Roll no is",roll)
print("Name is",name)
print("Per is",per)
Exercise

 Write a program that asks the user to enter


his/her name and age. Print out a message ,
displaying the user’s name along with the
year in which they will turn 100 years old.

 Hint: Use the module datetime to get the current year


Accepting Multiple Values
In One Line

 In Python , the input( ) function can read and


return a complete line of input as a string.

 However , we can split this input string into


individual values by using the function split( )
available in the class str

 The function split( ) , breaks a string into


multiple strings by using space as a separator
Accepting Multiple Values
In One Line

 To understand , working of split( ) , consider the


following example:

text=“I Love Python”


word1,word2,word3=text.split()
print(word1)
print(word2)
print(word3)
Output:
I
Love
Python
Accepting Multiple
Values In One Line

text=input(“Type a 3 word message”)


word1,word2,word3=text.split()
print(“First word”,word1)
print(“Secnd word”,word2)
print(“Third word”,word3)
Output:
An Important Point!

 The number of variables on left of assignment


operator and number of values generated by
split() must be the same
Exercise

 Write a program that asks the user to input 2


integers and adds them . Accept both the
numbers in a single line only
Solution

Code:
s=input("Enter 2 numbers:")
a,b=s.split()
print("First number is",a);
print("Second number is",b)
c=int(a)+int(b)
print("Their sum is",c)
Accepting Multiple
Values Separated With ,

 By default split( ) function considers , space as a


separator

 However , we can use any other symbol also as a


separator if we pass that symbol as argument
to split( ) function

 For example , if we use comma , as a separator


then we can provide comma separated input
Example

Code:
s=input("Enter 2 numbers separated with
comma:")
a,b=s.split(",")
print("First number is",a);
print("Second number is",b)
c=int(a)+int(b)
print("Their sum is",c)
Accepting Different Values
In One Line

Code:
s=input("Enter roll no,name and per:")
roll,name,per=s.split()
print("Roll no is",roll)
print("Name is",name)
print("Per is",per)

You might also like