Print() function in python Language with examples

By
The print() function is used for sending the output to the screen.It is used to display the output of any variables like string,list,tuple,set,dictionary,etc.Print() function is a built-in function in python language.
Syntax:-
print(*objects,sep=' ',end='\n',file=sys.stdout,flush=False)

Descriptions:-

  1.) objects:-The object can be Integer,Float,string,list,tuple,set,etc.
e.g.

a=234
b='ram'
list=[1,2,3,4,5,6]
tuple=(1,2,3,4,5,6)
dictionary={'a':'apple','b':'bag','c':'cat','d':'dog'}
set={'a','e','i','o','u'}
print("value of a=",a)
print("value of b=",b)
print("value of list=",list)
print("value of tuple=",tuple)
print("value of dictionary=",dictionary)
print("value of set=",set)
print("value of 12*45=",12*45)
output of the above python program:-
value of a= 234
value of b= ram
value of list= [1, 2, 3, 4, 5, 6]
value of tuple= (1, 2, 3, 4, 5, 6)
value of dictionary= {'a': 'apple', 'b': 'bag', 'c': 'cat', 'd': 'dog'}
value of set= {'o', 'e', 'i', 'a', 'u'}
value of 12*45= 540

  2.) sep=' ' :-This parameter is used to give space between multiple objects.You an use this parameter other than space also.It will be shown in below programs.
e.g.
a=5
b=6
c=24
d=60
e='ram'
f='sita'
print("value of a,b,c,d=",a,b,c,d)
print("value of a,b,c,d=",a,b,c,d,sep='  ')
print("value of a,b,c,d=",a,b,c,d,sep=',')
print("value of a,b,c,d=",a,b,c,d,sep='\n')
print("value of e,f =",e+' '+f)
output of the above python program:-
value of a,b,c,d= 5 6 24 60
value of a,b,c,d=  5  6  24  60
value of a,b,c,d=,5,6,24,60
value of a,b,c,d=
5
6
24
60
value of e,f = ram sita
  3.) end=' \n' :-It means,when we call print () function each time.it will end at a new line.In python by default ,print() function always ends at a new line.You can change it. i will see in the programs.

e.g.
def factor(x):
    for i in range(1, x + 1):
        if (x % i) == 0:
           #print ("value of i=",i,end=" ")
           print ("value of i=",i,end="\n")
           #print ("value of i=",i,end=".")
 
factor(int(input("Enter a number:")))
output of the above python program:-

Enter a number:45
value of i= 1
value of i= 3
value of i= 5
value of i= 9
value of i= 15
value of i= 45
e.g.
for j in range(6):
    if(j>1):
     print(" value of j=",j,end='0')
output of the above python program:-
value of j= 10 value of j= 20 value of j= 30 value of j= 40 value of j= 50

  4.) file=sys.stdout :-It specifies where the print function should send the output.We can change the default value of stdout can change for a external file.
e.g.
import sys
def stars(count):
    if count >= 1:
        i = 1
        while (i <= count):
            x=0
            while(x<i):
                sys.stdout.write('*')
                x = x+1
            print('')
            i=i+1
stars(5)
 output of the above python program:-
*
**
***
****
*****

e.g.
import sys
sys.stdout.write("hello friends"+'\n')
sys.stdout.write(sys.version+ '\n')
print(sys.version)
output of the above python program:-

hello friends
3.8.2 (tags/v3.8.2:7b3ab59, Feb 25 2020, 23:03:10) [MSC v.1916 64 bit (AMD64)]
3.8.2 (tags/v3.8.2:7b3ab59, Feb 25 2020, 23:03:10) [MSC v.1916 64 bit (AMD64)]
5.) flush=False :-It specifies whether flush the output stream or not.By default flush value is false(flush=False) in print () function.You can change flush=True in print() function forcefully.We will see it in below program also. 

e.g.
for i in range(2):
    a=int(input("enter first value:"))
    b=int(input("enter first value:"))
    sum=(a+b)
    print(" sum1=",sum+i,flush=True)
    p=a+b+i
    print(" sum2=",p,flush=False)
output of the above python program:-
enter first value:5
enter first value:6
 sum1= 11
 sum2= 11
enter first value:8
enter first value:9
 sum1= 18
 sum2= 18

Special Notes:-
In this special note ,we will learn about time module in python language.
Python has a new module that called as time .This is used to handle the time related tasks.To use this module,we have to import time first,as show in below:-
import time
for i in range(10):
  print(i, end=" ")
  time.sleep(2)        #couter stop 2 second here to print the output
print()
second=time.time()   # This function returnsthe number of seconds passed
                     #since this period
print ("Local time in seconds=",second)
print(time.ctime())    #It returns a string representing current time/local time

#secod program start:
for i in range(10):
  print(i, end=" ",flush=True)
  time.sleep(2)
print()
print("Local time in seconds=",time.time())
print(time.ctime())
Output of above python program:-
0 1 2 3 4 5 6 7 8 9 
Local time in seconds= 1587019187.354136
Thu Apr 16 12:09:47 2020
0 1 2 3 4 5 6 7 8 9 
Local time in seconds= 1587019207.5175219
Thu Apr 16 12:10:07 2020
Watch complete Lecture video:-

0 comments:

Post a Comment

Powered by Blogger.