Monday 27 May 2019

Input and Output Statements

#Example progrm for input and output statement
eno=int(input("enter emp no "))
ename=input("enter employee name ")
esal=float(input("enter emp sal "))
eaddr=input("enter emp address ")
marital=bool(input("employee married?: True/False "))
#displaying the emp data
print("empno=",eno)
print("emp name=",ename)
print("emp salary=",esal)
print("emp address=",eaddr)
print("emp marrie ?",marital)

"""output:
enter emp no 007
enter employee name Rajesh
enter emp sal 35000
enter emp address Kurnul
employee married?: True/False True

Related Video:  https://www.youtube.com/watch?v=q8jc3s7Nmhk&feature=youtu.be

input()
It is a predefined method. The input() reads a line from input, converts into a string and returns it.

#Example program
a=int(input("enter 1 value"))
b=int(input("enter 2nd value"))
print(a,b)
print("product =",a*b)

Output:
enter 2 numbers:10 20
10 20
product = 200

If I dont write type cast into int? i.e a=int(input("enter value"))
Error: can't multiply sequence by non-int of type 'str'

i.e print("product =",a*b)

#how to read multiple values from the keyboard in a single line
a,b=(int(x) for x in input("enter 2 numbers:").split())
print(a,b)
print("product =",a*b)
"""output:
enter 2 numbers:10 20
10 20
product = 200

1Q: Why do use split()?

Ans: There are two reasons:
(1) It leads to error if I give i/ps by giving spaces.
Ex: 10 20 #error
(2) It takes only 1 input, and stores into two inputs like 1 and 0
so we will get a result like the below:
1 0
product=0

so the split() function is mandatory when we want to read more than one i/p from the keyboard


What is split()?
The split() method splits a string into a list.

You can specify the separator, the default separator is any whitespace.


Example:
"2 4".split() #We have to call the split() function w.r.t string
['2', '4']
>>> "1, 2 3 Hyd, Vij Kurnul,Kadapa atp".split(',')
['1', ' 2 3 Hyd', ' Vij Kurnul', 'Kadapa atp']
>>>

Related video https://www.youtube.com/watch?v=523O_gX3a-Y&feature=youtu.be

print(“Hello how are you”)
The print() prints the specified message to the screen. The message can be a string, object, any other type variable.

Ex 1: print() without any argument just it prints new line character

Ex 2:
print("hello ")
print("Hi\nhow are you")
output:
Hi
how are you
print("Hi\thow are you")
Hi  how are you

Ex3: Find ncr and npr:
"""
c= no of distinct combinations
n=total no of objects in the set
r=no of choosing objects from the set
5c2=n!/(n-r)!
Input: Two positive integers as the total no of objects and sample size
Output:A positive integer as the permutation or permutation of 'n' objects
taken at 'r' at a time
"""
import math
#n,r=(int(x)for x in input("enter 2 numbers:").split())
n=int(input("enter n value: ="))
r=int(input("enter r value: ="))
if n>=r:
    print("ncr=",math.factorial(n)/(math.factorial(r)*math.factorial(n-r)))
    print("npr=",math.factorial(n)/(math.factorial(n-r)))
else:
    print("value of n can not be <r")

"""enter n value: =5
enter r value: =2
ncr= 10.0
npr= 20.0
>>>
===================== RESTART: F:\Programs\input\ncr.py =====================
enter n value: =2
enter r value: =5
value of n can not be <r
>>> """

Next Topic: eval() function
Previous Topics:
Operators
Arithmetic Operators
Relational Operator


Special Operators
Bitwise Operators

No comments:

Post a Comment

Files with Exception handling

#Ask the user to take two input integer values var a,b try:     a=int(input("enter any integer value:"))     b=int(input(&qu...