Problem Solving and Python Programming: UNIT III: Control Flow, Functions, Strings

Input and Output

Syntax, Example | Python Programming

1. How to Input the Data through Keyboard ?, 2. How to Display Output on Console using Format ?

Input and Output


1. How to Input the Data through Keyboard ?

• In python it is possible to input the data using keyboard.

• For that purpose, the function input() is used.

• Syntax

input([prompt])

where prompt is the string we wish to display on the screen. It is optional.

 

Example 3.3.1 In the following screenshot, the input method is used to get the data.


 

Example 3.3.2 Write a python program to perform addition of two numbers. Accept the two numbers using keyboard,

Solution :

addition.py

Output

For getting the output click on Run-> Module or press F5 key, following shell window will appear-


Program Explanation :

• In above program, we have used input() function to get the input through keyboard. But this input will be accepted in the form of string.

• For performing addition of two numbers we need numerical values and not the strings. Hence we use int() function to which the input() function is passed as parameter. Due to which whatever we accept through keyboard will be converted to integer.

Finally the addition of two numbers as a result will be displayed.

• The above program is run using F5 and on the shell window the messages for entering first and second numbers will be displayed so that user can enter the numbers.

 

Example 3.3.3 Write a Python program to find the square root of a given number

Solution :

SqrtDemo.py

print("Enter the number:")

num = float(input())

result=num**0.5

print("The sqaure root of",num," is ",result)

Output

Enter the number:

25

The sqaure root of 25.0 is 5.0

>>> 

 

Example 3.3.4 Write a program in Python to obtain principle amount, rate of interest and time from user and compute simple interest.

Solution :

Interest.py

print("Enter principal amount: ")

p = float(input())

print("Enter rate of interest: ")

r = float(input())

print("Enter number of years: ")

n = float(input())

I = (p*n*r)/100

print("Simple Interest is: ",I)

Here we are reading the values through keyboard. Note we are reading the values as float

# Output will be displayed on console

 

2.  How to Display Output on Console using Format ?

Using .format the data can be displayed on the console. For that purpose { } and .format is used. For example

Example 1

n = 10

print("There are {} numbers".format(n))

Output

There are 10 numbers

We can also display the data along with some space. For that purpose, we have to use {:n}. For example

Example 2 :

n=10

print("There are {:10d} numbers".format(n))

Output

There are   10 numbers

Example 3 :

a = 10

b = 20

c = 30

print("There are three numbers and those are {} {} {} numbers".format(a,b,c))

Output

There are three numbers and those are 10 20 30 numbers

Example 3.3.5. Write a program to swap two numbers.

Solution :

a = input('Enter value of a: ').

b = input('Enter value of b: ')

temp = a

a = b

b = temp

print('After swapping a: {}'.format(a))

print('After swapping b: {}'.format(b))

Output


 

Problem Solving and Python Programming: UNIT III: Control Flow, Functions, Strings : Tag: Engineering Python : Syntax, Example | Python Programming - Input and Output