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 |
>>> 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 |
>>> 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 |
>>> 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 |
>>> 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 |
>>> print "1\n","2\n","3"
1
2
3
Using tap Space
Using Precesion
1
2
3
| >>> print "%.2f" % 4.6466
4.65
>>> |
>>> print "%.2f" % 4.6466
4.65
>>>
INPUT STATEMENTS:
Input statements are used to get input via keyboard. The two commonly used input statements are
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'> |
>>> 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'> |
>>> 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
''' |
#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 |
#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 |
C:\Users\Admin>python C:/Python27/command_line.py 324 324
program_name C:/Python27/command_line.py
arguments ['324', '324']
count 2
3 Comments
Sivaraman Rethinam
(February 12, 2018 - 2:36 am)Thanks for sharing Gowtham.
test1
(February 27, 2018 - 1:40 pm)This is a excellent blog, would you be involved in doing an interview about just how you designed it? If so e-mail me!
KV331
(March 15, 2018 - 10:36 pm)I really enjoy examining on this internet site , it has got great posts .