2 – Input and Output Statements, Comment and Command line arguments

PRINT STATEMENT:

Python is used to display the message in terminal or a Serial port or a Output Device. Few examples of using print are given below.

To print a String

1
2
3
4
5
6
>>> print "Welcome to mybtechprojects"
Welcome to mybtechprojects
 
>>> name="Mybtechprojects"
>>> print "Name = %s " %name
Name = Mybtechprojects

To print a Number

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
>>> num=10
>>> print num
10
 
>>> print 1,2,3
1 2 3
 
>>> print 1+2+3
6
 
>>> print "number = ",num
number =  10
 
>>> print "number = %d " %num
number = 10

Difference between comma and +

1
2
3
4
5
>>> print "1","2","3"
1 2 3
 
>>> print "1"+"2"+"3"
123

Using format

1
2
3
4
5
>>> print('{0} and {1}'.format('Python', 'Java'))
Python and Java
 
>>> print('{1} and {0}'.format('Python', 'Java'))
Java and Python

Using Linespace

1
2
3
4
>>> print "1\n","2\n","3"
1
2
3

Using tap Space

1
2
>>> print "1\t2"
1	2

Using Precesion

1
2
3
>>> print "%.2f" % 4.6466
4.65
>>>

INPUT STATEMENTS:

Input statements are used to get input via keyboard. The two commonly used input statements are

  • input()
  • raw_input()

Using input():

  • The value entered by the user will be interpreted and the value returned will depend on the type entered.
  • Suppose a user  inputs a String the return type will be String and if a user inputs a Integer the return type will be Integer
1
2
3
4
5
6
7
8
9
>>> a=input("Enter input ")
Enter input "Mybtechprojects"
>>> type(a)
<type 'str'>
 
>>> a=input("Enter input ")
Enter input 12
>>> type(a)
<type 'int'>

Using raw_input():

  • The value entered by the user will not be interpreted and the value returned will not depend on the type entered.
  • Suppose a user  inputs a String the return type will be String and if a user inputs a Integer the return type will be String
1
2
3
4
5
6
7
8
9
>>> a=raw_input("Enter raw_input ")
Enter raw_input Mybtechprojects
>>> type(a)
<type 'str'>
 
>>> a=raw_input("Enter raw_input ")
Enter raw_input 12
>>> type(a)
<type 'str'>

COMMENT

  • # is used as a Single Line Comment
  • ”’ ”’ (Triple Quotes) is used as Multi Line Comments
1
2
3
4
5
6
7
8
#This is a Single line Comment"
'''
This 
is
a
MultiLine
Comment
'''

COMMAND LINE ARGUMENTS

  • Command line arguments is used to pass arguments to a program when they are called
  • It is usefull when a program is called by another program
  • sys.argv returns the list of input command lne arguments
1
2
3
4
5
6
7
8
#Save this program as command_line.py
import sys
program_name = sys.argv[0]
print "program_name ",program_name
arguments = sys.argv[1:]
print "arguments ",arguments
count = len(arguments)
print "count ",count

OUTPUT in Windows

1
2
3
4
C:\Users\Admin>python C:/Python27/command_line.py 324 324
program_name  C:/Python27/command_line.py
arguments  ['324', '324']
count  2
SHARE THIS!!!

Gowtham S

Gowtham is a programming enthusiast. His field of interest includes Arduino, NodeMCU, Raspberry Pi, and Python. To know more about him visit https://mybtechprojects.tech/about-us/.

3 Comments

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.