Sei sulla pagina 1di 18

Arithmetic operations, bitwise

operations and complex numbers


Performing Arithmetic Operations
Operation Description
x+y Addition
x-y Subtraction
x*y Multiplication
x/y Division
x // y Truncating division
x ** y Exponentiation
x%y Modulo operator
–x Unary minus
+x Unary plus
Division Operator
• The truncating division operator (//), also
known as floor division, truncates the result to
an integer (ignoring the remainder) and works
with both integers and floating-point
numbers.
• The true division operator (/) also truncates
the result to an integer if the operands are
integers.
• Example:

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

• In this program, the true division operator, /, returns an


integer if both the operands are integers.
• To get the correct result, 1/2 is converted into a float,
1.0/2.0, so that the result comes out to be a float.
• The following program calculates the average of
three values and displays the result rounded up to
the desired number of decimal places:
average1.py
p=q=r=10
a=1.0/3.0*(p+q+r)
print ("Average of three variables is", a)
print ("Average of three variables is %.2f" %a)
print ("Average of three variables is %d" %a)

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)

where x is the object whose data type will be


returned
• Example:
typeexample.py Output:
a=10 <class 'int' >
b=15.5 <class 'float'>
c="Hello" <class 'str' >
d=[2,9,4] <class 'list' >
e=('apple', 'mango', 'banana') <class 'tuple'>
f=True <class 'bool' >
print (type(a))
print (type(b))
print (type(c))
print (type(d))
print (type(e))
print (type(f))
Displaying Octal and Hexa Values
• Example
octhex.py
a=0o25
b=0x1af
print ('Value of a in decimal is', a)
c=19
print ('19 in octal is %o and in hex is %x' %(c,c))
d=oct(c)
e=hex(c)
print ('19 in octal is', d, 'and in hexa is', e)

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')

• The data entered by the user is assigned to the


variable in string format.
• If you want the data in another format (integer,
float, etc.), it has to beconverted explicitly
Converting Explicitly into integer Type
• If you do some arithmetic operations on the
operands of the same data type, no auto conversion
or coercion takes place.
• When operands of different data types are
computed, coercion takes place.
• Python converts the operand with the “smaller”
type to the “larger” type.
• For explicit conversion, the functions that you will be
frequently using are int(), float(), and str().
• Example
arearectinput.py
l=input("Enter length: ")
b=input("Enter width: ")
a=int(l)*int(b)
print ("Area of rectangle is",a)

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

Potrebbero piacerti anche