User Input Output in Python
In this class, we discuss user input output in python.
For Complete YouTube Video: Click Here
Input in Python
Before we start with input. you should have some basic understanding of operators. Click here.
Let’s take examples and understand user input in python.
a=25
print(a)
The above example a is having value of 25. The output displayed is 25.
How many times we execute the above program. every time it’s displaying output 25.
This is not going to help in real-time situations.
We need to take input from the user. ie from the keyboard.
Example to take input from the keyboard.
a= input(“enter a value”)
print(a)
type(a)
Before understanding the above program. First, discuss some situations where user input is needed.
Take an ATM example.
When the user inserts his debit card. First, it asks to enter his pin.
This pin should be given from the keyboard.
Then it continues its execution and again asks to enter the amount.
There are many situations where input is taken from the keyboard during execution.
In order to take input from the keyboard. Python uses a function called input.
In the input function whatever the string given is displayed to the user.
The user has to understand what he wants to input.
In the above example “enter the value” is displayed to the user. whenever he enters a value, that value is stored in variable a.
type(a) is display str. means string data type.
By default, the input function takes input in string type.
If we need to convert string type to integer we use type casting function int. which we discussed in the previous class.
Example:
a=int(input(“enter an integer”))
print(a)
type(a)
In this example type is displaying int type.
Taking a single character
We don’t have a character type in python. Strings with a single character are considered as a character.
How to take a character from the keyboard.
Example:
Gender = input(“enter your Gender”)[0]
print(Gender)
Execute the code and give input as Female.
How many characters in the input string?
Total 6 Characters present. The first character is given index 0. The second character is given index 1. so on.
We are taking a character from the 0th index position. so F is considered in input.
We discuss strings in our coming classes.
eval Function
eval function is used to do expression evaluation.
Example:
a= eval(input(“enter your expression”)
print(a)
Execute the above code and give input 2+5*2.
The given expression is evaluated and the output is stored in the variable a.
Output in Python
User input and output in python. Help in beautify the output.
After executing the program we have to display the output to the user.
We have to display it in an understandable manner.
How to display the output in a format needed by the user?
Let’s take some examples and understand different ways to display output.
x=5
print(x)
To display the outputs we use the print function in python.
They have given different formats to display our output.
We discuss a few of them.
To display a string.
print(“hello world”)
We use double quotes to display strings.
Display combination of strings and variables.
This can be done in many ways.
Example:
name=”suresh”
salary=25000
print(“The salary of %s is %i”%(name,salary))
%s is a format specifier to display variables that are of type string.
%i is a format specifier to display the variable of type integer.
The above print function will display the string given in the function. and display the variable name where %s format specifier mentioned and display variable salary where %i format specifier mention.
Mention variable names according to the order you needed. and in the same order mention the format specifiers.
The output displayed by the above code is ” The salary of Suresh is 25000″.
Example:
name = “Suresh”
salary = 25000
print(“Salary of %(n)s is %(s)i”%{‘s’:salary,’n’ : name})
This is another way to represent strings and variables.
Here it is given name n and s to our variables. and used before format specifier.
With this, we got the flexibility not to maintain the order of variables.
Example: Zero padding
a=5
print(“zero-padding %05i”%a)
Here we are doing zero padding before integer values.
We mentioned the value 05 means 5 digits.
If the integer is not having 5 digits. left places will be pad with zero’s
The output is Zero padding 00005.
Example: Space padding
a=5
print(“space padding %5i”%a)
Here we did not mention zero in the format specifier. so it gives space.
The output is Space padding 5.
Example: Hexadecimal form
a=15
print(“Hexadecimal form %x”%a)
x is used for hexadecimal.
The output is Hexadecimal form f
Example: Octal form
a=15
print(“Octal form %o”%a)
o is used for Octal.
The output is Octal form 17.
Example: Exponential Form
e is used for exponential form.
The discussion about exponential form is done in numeric data types class.
a=1000
print(“exponential form%e”%a)
The output is exponential form 1.000000e+03
Example: format function
name =”suresh”
salary =25000
print(“salary of {} is {}”.format(name,salary))
This is another way to display strings and variables.
Why we used dot , we understand when we discuss class, object, and method.
We can assign numbers.
print(“salary of {0} is {1}”.format(name, salary))
Another way to display.
print(“salary of {n} is {s}”.format(n=name,s=salary))
Binary using format function.
a=15
print(“binary format {0:b}”.format(a))
The output is 1111.
Hexadecimal using format function.
print(“Hexa format {0:x}”.format(a))
The output is f.
Octal format using format function.
print(“Octal format {0:o}”.format(a))
The output is 17.
Floating value binary format.
a=1.12489
print(“binary format {0:.3f}”.format(a))
.3 shows after floating-point round to three digits.
Example:
name = ” Suresh”
salary = 25000
print(“salary of”,name,”is”,salary)
Give ‘,’ separation between variable and strings.
The output is the salary of Suresh is 25000.