Introduction to Python Programming Cycle of Python Varibles and Datatypes in Python Input and Output in Python Operators in Python Precedence of Operators in Python Typecasting in Python If Else in Python Loops in Python Break, Continue and Pass in Python Functions in Python Default Arguments and Keyword Arguments in Python Strings in python Lists in Python Tuple in Python Dictionary in Python Sets in Python List Comprehension in Python Unpacking Sequences in Python Higher Order Functions in Python Lambda Functions in Python Sieve of Eratosthenes Algorithm in Python Linear Search in Python Binary Search in Python Selection Sort in Python Bubble Sort in Python Insertion Sort in Python Merge Sort in Python

Input and Output in Python

In any language we have two important things, one is to take input or we can say take data from the user and another is to show output to the user.

Showing output

In python we use 'print()' function to show output to user. Consider the following code for better understanding:

# show string
print("In this way we can show string")
# now we will learn to print empty line
print() 
# "\n" -> this mean print new line
print("\n") 
# show value of variable
s = "string stored in variable"
print(s)
a = 15
print(a)
    
Output:
In this way we can show string



string stored in variable
15
As we have seen above, the print function prints value of the variable or data given in its parenthesis and then prints an empty line. If we do not want to print an empty line at end of the print statement then we can do this in the following way:

print("text 1")
# text 2 will print in new line as print statement insert new line in end
print("text 2")

# In this way we can change new line to anything else
print("text 3",end=" ")
print("text 4")

# One more example
print("Rapid",end="@")
print("coders")
        
Output:
text 1
text 2
text 3 text 4
Rapid@coders

Taking input from user

In python we use 'input()' function to take input from user. Consider the following code for better understanding:

a = input()

print(a)
    
Input:
    Rapid Coders
Output:
    Rapid Coders
Now we will see how to show a message to the user to enter data:

a = input("Enter Your Name : ")

print(a)
    
Input:
    Enter Your Name : Rapid Coders
Output:
    Rapid Coders
Note: In Python, input is always taken in form of a string. Even if you enter an integer or a floating point number, the value will be stored as a string. Later we can typecast this string into float or int. Consider the below code:

a = input("Enter Your name : ")

print(type(a))
print(a)

print()

b = input("Enter Number = ")
print(type(b))
print(b)

print()
# lets typecast b
b = int(b)
print(type(b))
print(b)
    
Input:
    Enter Your name : Rapid Coders
    Enter Number = 123
Output:
    <class 'str'>
    Rapid Coders
    
    <class 'str'>
    123
    
    <class 'int'>
    123