Input and output statements
1.
input()
function
2.
How to read
multiple values from the keyboard id’s single line
3.
Command-line
arguments
4.
Output statement:
print() function
5.
Sep='' attribute
6.
End='' attribute
7.
Printing formatted
string
8.
Replacement operator:{}
Input()function
In python input data always return in string (str)
form only. And any language most common used type is string(str) type.
Ex:
number = input(' enter the number: ') #we asking to user for input.
print(type(number))
print(number)
Result:
enter the number: 10
10 #user enter the number 10, actually, it’s int type but as per Input, the rule is its return as a string(str).
10
Then how to convert to as a int type. Check on below with example.
number = input(' Enter the number: ') #we asking to
user for input.
number = int(number) #it’s convert to int type
print (type(number))
print(number)
Enter the number: 10
<class 'int'>
10
One more type:
number = int(input(' Enter the number: ')) # this
type is most commonly used. and it's directly convert to int type.
print (type(number))
print(number)
Result:
Enter the number: 10
<class 'int'>
10
One more example
number_1 = int(input(' Enter the number: '))
number_2 = int(input(' Enter the number: '))
print (number_1+number_2)
Result:
Enter the number: 10
Enter the number:20
30 # Result
One more example
Emp_Number = int(input('enter the employee No: '))
Emp_Name = input('enter the employee Name: ')
Emp_salary = float(input('enter employee salary:
'))
Emp_address = input('enter the address: ')
Marrige_status = eval(input('enter yes or
no?[Ture|False]:'))
print(Emp_Name)
print(Emp_salary)
print(Emp_address)
print(Marrige_status)
Result:
enter the employee No: 255
enter the employee Name: dfjds
enter employee salary: 73455
enter the address: hdsfjalsd
enter yes or no?[Ture|False]:False
255
dfjds
73455.0
hdsfjalsd
False
Taking
multiple values in single line
Ex:
a,b = [int(x)for x in input('enter the two numbers:
').split()]
print('The sum', a+b)
Result:
enter the two numbers: 20 30
The sum 50
Explaining:
Converting in to small steps
s = input('enter the two numbers: ')
print(s)
vlaue = s.split()
print(vlaue)
a,b =
print(a)
print(b)
c,d = int(a), int(b)
print('the sum',c+d )
Result
enter the two numbers: 20 30
20 30
['20', '30']
20
30
the sum 50
based on this example we can take multiple values in a single line.
Eval() Function
In the input() method whatever we give the value, it’s converted to 'str' type only. But we want to take as a given value type. For example, if we give 10 it’s automatically taken as an int type. this type of conversion
purpose eval() function will help. If we add eval() function it’s automatically converting as a given value type.
if we compare two examples then we can find the eval() function uses.
Ex:
a = input('enter the number:')
print (a)
print(type(a))
Result:
enter the number:10
10
<class 'str'>
Ex:
a = eval(input('enter the number:'))
print (a)
print(type(a))
Result:
enter the number:10
10
<class 'int'>
Post a Comment