Arithmetic Operations, Bitwise Operations and Complex Numbers
Arithmetic Operations, Bitwise Operations and Complex Numbers
areatriangle.py
b=17
h=13
a=1.0/2.0*b*h
print ("Area of triangle is", a)
print ("Base= %d, Height is %d, Area of triangle is %f" %(b,h,a))
Output:
Area of triangle is 110.5
Base= 17, Height is 13, Area of triangle is 110.500000
Output:
Average of three variables is 10.0
Average of three variables is 10.00
Average of three variables is 10
Multiple Assignment Statement
• The basic assignment statement can also
assign multiple variables at one time.
• The rule is that the left and right sides must
have the same number of elements, and the
values will be assigned on a one-to-one basis.
• Example:
p,q,r=10,20,30
sum, avg=p+q+r,(p+q+r)/3
Using Escape Sequences
• Escape sequences are special characters that represent
nonprinting characters such as tabs, newlines, and such.
Escape Character Description
\a Bell (beep)
\b Backspace
\f Form feed
\n Newline
\r Carriage return
\t Tab
\v Vertical tab
\\ Literal backslash
\' Single quote
\" Double quote
• Example:
escapeseq.py
print('Hello World\nIt\'s hot today')
print('Festival Discount\b\b\b\b\b\b\b\b\b Offer')
print("Isn't it?")
print('Isn\'t it?')
print("He said: \"I am going \"")
print('\\Text enclosed in back slashes\\')
print ('Bell sound \a')
print('Name\tEmail Address\tContact Number')
Finding a Data Type
• To find the data type of the specified object,
you use the type() function, which returns
the data type of the object passed to it.
Syntax:
type(x)
Output:
Value of a in decimal is 21
19 in octal is 23 and in hex is 13
19 in octal is 0o23 and in hexa is 0x13
Getting Data
• To get input from the user, you use the input
method
Syntax:
variable=input ('Message')
Output:
Enter length: 9
Enter width: 5
Area of rectangle is 45
• Example
areacircleinput.py
from math import pi
r=int(input("Enter radius: "))
a=pi*r*r
print ("Area of the circle is", a)
print ("Area of the circle is %.2f" %a)
Output:
Enter radius: 5
Area of the circle is 78.53981633974483
Area of the circle is 78.54
Bitwise Operations
• Considering x and y as two operands,
following are the shifting and bitwise
operators:
• x << y (binary shift left)
• x >> y (binary shift right
• x & y (bitwise AND)
• x | y (bitwise AND)
• x ^ y (bitwise exclusive AND)
• ~ x (bitwise inversion)
Example
bitwise.py
a=10
b=7
c=a&b
d=a ^ b
e= a | b
print ('The result of 10 and 7 operation is', c)
print ('The result of 10 exclusive or 7 operation is' , d)
print ('The result of 10 or 7 operation is', e)
g=a<<2
print ('Left shifting - Multiplying 10 by 4 becomes:' , g)
h=a>>1
print ('Right shifting - Dividing 10 by 2 becomes:',h)
Output:
The result of 10 and 7 operation is 2
The result of 10 exclusive or 7 operation is 13
The result of 10 or 7 operation is 15
Left shifting - Multiplying 10 by 4 becomes: 40
Right shifting - Dividing 10 by 2 becomes: 5
Complex Numbers
• The real and imaginary components of a complex object can
be accessed by using its real and imag attributes.
• Example:
complex.py
a = 3.0 + 1.2j
b= -2.0 - 9.0j
print ('The two complex numbers are', a, 'and', b)
c=a+b
print ('The addition of two complex numbers is:', c)
print ('The addition of two real numbers is:', a.real+b.real)
print ('The addition of two imaginary number is:', a.imag+b.imag)
Output:
The two complex numbers are (3+1.2j) and (-2-9j)
The addition of two complex numbers is: (1-7.8j)
The addition of two real numbers is: 1.0
The addition of two imaginary number is: -7.8