#26 Python Programming – Input-Output statements & Operators P02

Python input-output statements and operators

teknonauts

Till now you know the basics of Python programming and able to make simple program. Now lets go little more deeper and understand Input & Output statements

Input and Output statement

The usage of input statement depends on the Python version being used.

Python 3 . x (Input)

  • The input() function is used to get input from STDIN which usually returns String type of data.
  •  In the case of numerical input, perform an additional step of typecasting it using int() or float() function.

Syntax

var=input(“Prompt”)

Example :-

>>> x = input (“Enter X value : “)
Enter X value : 12
>>>x
‘12’ # Note the quotes enclosing the value referring string type data
>>>type(x)
>>>x = int ( input (“Enter X value : “))
Enter X value : 10
>>>x
10 # Numerical data-displayed without quotes
>>>type(x)

Python 2. x (Input)

  •  An older version of Python where in addition to the input () method, we have raw_input() to get input from STDIN.
  •  The input() in Python 2. x is slightly different from that in Python 3. x.
  • Here the input() returns the numerical type of data. Otherwise, syntax and usage are the same.
  •   raw_input() returns string type of data.

Syntax

var=raw_input(“Prompt”)
Example
>>>y = input ( “Enter y : ”)
Enter y : 12
>>>type(y)
>>>x= raw_input (“Enter x : “)
Enter x : hai
>>x
‘hai’
>>>type(x)

Print statement 

  • The usual way of displaying text to the STDOUT is print method. This is very similar to the printf statement in C language.
  •  The usage of format specifier (%) in print method is similar to C style coding.
>>> print(“s”  %(“Hai”))
Hai  # we tell format specifier that 10 characters are expected.
 # Right formatting with white space prefixed.
>>>  print(“% -10s”  %(“Hai”))
Hai       #Left Formatting with trailing white space & width=10
  • Let us make it clearer with another print statement.
>>>  print(“% -10s”  %(“Hai”)), ; print(“All”)
#output 
Hai       All
  •   Usage of comma(,) after a print() method suppresses the newline character.
  •   Recall semicolon(;) used to concatenate multiple statement on single line.
  •   Note the above way of comma usage works only in Python 2.x versions & not in Python 3.x.

format( ) method

  •  The another style of using print() method along with the usage of format().
>>>  x=3; y=5; print ( “The Total of {} and {} is {}”.format(x,y,x+y))
#output
The Total of 3 and 5 is 8

Using sys module

Other way of displaying text to the STDOUT is by using sys module

In Python 3.x, 

>>>import sys
>>>sys.stdout.write(“Ever Inspiring”)
Ever Inspiring14
>>>sys.stdout.write(“Dr.A.P.J.Abdul Kalam”)
Dr.A.P.J.Abdul Kalam20

Here, the write() method returns the text followed by its total length and a newline character.

In Python 2.x,

>>>import sys
>>>sys.stdout.write(“Ever Inspiring”)
Ever Inspiring
>>>sys.stdout.write(“Dr.A.P.J.Abdul Kalam”)
Dr.A.P.J.Abdul Kalam

Here the write() method in sys module works similar to print() except that it does not allow the trailing newline character.

Python operators

Arithmetic operators

  •   Arithmetic operators
  •  Python supports arithmetic operations like addition (+), multiplication (*), division (/), subtractions (), modulus( %), exponent (**) and floor division  (//).

Example:-

>>>x = 30
>>>y = 80
>>>total = x + y
>>>print(total)
110
>>>2**3
8
>>>20//3
6
>>>-20/3
-7

Relational operators

  •   The following are some relational operators used to perform comparisons.
  •    ==, != (or) <>, <, >, <=, >= . These operators return Boolean values ( True / False ).
>>>a= int( input(“Enter A : ”))
Enter A : 12
>>>b= int( input(“Enter B : ”))
Enter B : 3
>>>a != b
True
>>>a <> b
True
>>>a == b
False

Logical operators

  • Logical operators includes andornot
>>> (a != b) and (a>b )
True
>>>not(a <> b)
False

Bitwise operators

  • Performs bit by bit operation

Consider A=5, B=10

A= 0000 0101 (5)

B= 0000 1010 (10)

A&B   = 0000 0000 (0) # copies bit if bit in both the operands are set.

A|B      = 0000 1111 (15)   # copies bit if bit in any of the operands are set.

~A  = 1111 1010 (10) # one’s complement

A<<2> (20)  # Binary left shift

A>>2   = 0000 0001 (1)   # Binary right shift

Python Identity Operators

Identity operators are used to compare the objects, not if they are equal, but if they are actually the same object, with the same memory location:

is –> Returns True if both variables are the same object . Example : x is y

is not–> Returns True if both variables are not the same object. Example : x is not y

Python Membership Operators

Membership operators are used to test if a sequence is presented in an object:

in –> Returns True if a sequence with the specified value is present in the object. Example : x in y

not in –> Returns True if a sequence with the specified value is not present in the object. Example : x not in y

Reference

https://www.python.org/

https://www.anaconda.com/

Continue exploring at Teknonauts.com

Leave a Reply

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