0% found this document useful (0 votes)
25 views6 pages

Day 3

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

Day 3

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

Reading data from keyboard:

 input() function is used to read data from keyboard.


 input() function returns the inputed value in the form of a string.
 To convert the inputed string data into its revalent type then we can have to use
typecasting or eval() function.
Ex1:
age=input("Enter ur
age:")print(age)
print(type(age))

output:
Enter ur
age:1212
<class 'str'>

Ex2: using typecasting for converting the inputted data.


age=int(input("Enter ur
age:"))print(age)
print(type(age))

output:
Enter ur
age:1010
<class 'int'>

Ex3: using eval() for converting the inputed data.


price=eval(input("Enter price of the product:"))
print(price)
print(type(price))

Note:
eval() function will work with int and float datatype both, when input type is not
knownin advance then, eval() function is recommended.

Different between Python 2 & Python 3 input() function:


In Python 2 there are two versions of functions input() and raw_input().

input():
The input function treats the inputed data as string if the input is included in " "
quotes.Otherwise the data is taken as it is, ie numbers as numbers and strings as
strings.

raw_input():
Always the inputed data is treated as string weather it is enclosed in quotes or not.
Note: deprecated in python 3.

python 3:
input():
Always treats the inputed data as string type.

Using eval() function to evaluate expression:

res=eval(input("Enter an expression
:"))print(res)

output:
Enter an expression
:2+5+10-512
>>>

How to get History in IDLE ie using Previous commands using Up Arrow:


IDLE  Options  Configure IDLE  Keys  History-Previous  Get New Key for
Selection  UP Arrow  Apply  OK

Examples:
a,b,c=20,22.45,"python"
print(a,b,c)

a=b=c=100
print(a,b,c)
print(id(a))
print(id(b))
print(id(c))

age=input("Enter Your Age..")


print(age)
print("My Age is ",age)
print(type(age))

a=input("Enter the Value for a")


b=input("Enter the Value for b")
c=a+b;
print(c);
print("The addition of a and b is ",c)
print("The addition of",a," and ",b,"is",c)

x=input("Enter the Value for a")


y=input("Enter the Value for b")
a=int(x)
b=int(y)
c=a+b;
print(c);
print("The addition of a and b is ",c)
print("The addition of",a," and ",b,"is",c)

a=int(input("Enter the Value for a"))


b=int(input("Enter the Value for b"))
c=a+b;
print(c);
print("The addition of a and b is ",c)
print("The addition of",a," and ",b,"is",c)
print(type(a))
print(type(b))
pirnt(type(c))

print("The Sum of",int(input('Enter First No'))+int(input('Enter Second number')))

age=int(input("Enter Your Age.."))


print(age)
print("My Age is ",age)
print(type(age))

a=int(input("Enter the Value for a"))


b=int(input("Enter the Value for b"))
c=a+b;
print(c);
print("The addition of a and b is ",c)
print("The addition of",a," and ",b,"is",c)

Output:
Enter the value for a :12.56
Error:-Invalid Literal....

a=float(input("Enter the Value for a"))


b=float(input("Enter the Value for b"))
c=a+b;
print(c);
print("The addition of a and b is ",c)
print("The addition of",a," and ",b,"is",c)
print(type(a))
print(type(b))
print(type(c))

a=float(input("Enter the Value for a"))


b=float(input("Enter the Value for b"))
c=a+b;
print(c);
print("The addition of a and b is ",c)
print("The addition of",a," and ",b,"is",c)
print(type(a))
print(type(b))
print(type(c))

//program to read and print employee data


Eno=int(input("Enter the Employee Number"))
Ename=input("Enter the Employee Name")
Salary=float(input("Enter the Employee Salary"))
Adrs=input("Enter the Employee Address")
M_Status=bool(input("Enter the Marital Status[True/False]"))
(In bool() we provide any information then it returns True, if it is empty then int
returns False)
print(Eno)
print(Ename)
print(Salary)
print(Adrs)
print(M_Status)

Eno=int(input("Enter the Employee Number"))


Ename=input("Enter the Employee Name")
Salary=float(input("Enter the Employee Salary"))
Adrs=input("Enter the Employee Address")
M_Status=eval(input("Enter the Marital Status[True/False]"))
print(Eno)
print(Ename)
print(Salary)
print(Adrs)
print(M_Status)

a,b =[int(x)for x in input("enter 2 values).split()]


print("Sum is ",a+b)

a,b =[int(x) for x in input("enter 2 values").split(',')]


print("Sum is ",a+b)
a,b,c =[float(x) for x in input("enter 3 float values").split(',')]
print("Sum is ",a+b+c)

x=eval("10+20+30")
print(x,type(x))

a=eval(input("Enter the Value for a"))


b=eval(input("Enter the Value for b"))
c=a+b;
print(c);
print("The addition of a and b is ",c)
print("The addition of",a," and ",b,"is",c)
print(type(a))
print(type(b))
print(type(c))

res=int(input("Enter an Expression:"))
print(res)
Error:

res=eval(input("Enter an Expression:"))
print(res)

x=raw_input('enter a number')
print(x)
Error:Because it Deprecated in Vertion 3

a,b,c,d=10,20,30,40
print(a,b,c,d)
print(a,b,c,d,sep=":")

//place Holders
a,b,c,d=0,20,30,40
print("a={},b={},c={},d={}".format(a,b,c,d)

name="Rama Krishna"
course="Python"
Inst='Datapro'
fees=6000
print("Hello {} Your Learing {} course from {} institute and fee is {}".format(name,course,Inst,fees))

name="Rama Krishna"
course="Python"
Inst='Datapro'
fees=6000
print("Hello {0} Your Learing {1} course from {2} institute and fee is
{3}".format(name,course,Inst,fees))
print("Hello {0} Your Learing {2} course from {2} institute and fee is
{0}".format(name,course,Inst,fees))

name="Rama Krishna"
course="Python"
Inst='Datapro'
fees=6000
print("Hello {0} Your Learing {1} course from {2} institute and fee is
{3}".format(name,course,Inst,fees))
print("Hello {n} Your Learing {c} course from {I} institute and fee is
{f}".format(n=name,c=course,I=Inst,f=fees))

//formatted String

rno=1001
name="Rama Krishna"
course="Python"
Inst='Datapro'
fees=6000.9023

print("Roll number=%d" %rno)


print("Student Name=%s" %name)
print("Course is %s" %course)
print("Institute is %s" %Inst)
print("Fees is %f" %fees)
print("Fees is %.2f" %fees)
print("Fees is {}".format(fees))
//by using above function we cant truncat the no of decimals

You might also like