0% found this document useful (0 votes)
12 views

Module 2

Double integral

Uploaded by

Saket Pentapati
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views

Module 2

Double integral

Uploaded by

Saket Pentapati
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 38

CSE1012 - Problem Solving and Programming

Session - 6

L.Pavithra
Assistant Professor
SCOPE
VIT -AP

1
Operators and Expressions
in Python

2
Python - Basic Operators
Python language supports following type of
operators.
• Arithmetic Operators
• Comparison Operators
• Logical (or Relational) Operators
• Assignment Operators
• Bitwise Operators
• Membership Operators
• Identity Operators
3
Python Arithmetic Operators:
Operator Description Example (a=10,b=20)
+ Addition - Adds values on either side of the a + b will give 30
operator
- Subtraction - Subtracts right hand operand a - b will give -10
from left hand operand
* Multiplication - Multiplies values on either a * b will give 200
side of the operator
/ Division - Divides left hand operand by right b / a will give 2
hand operand
% Modulus - Divides left hand operand by right b % a will give 0
hand operand and returns remainder
** Exponent - Performs exponential (power) a**b will give 10 to the
calculation on operators power 20
// Floor Division - The division of operands 9//2 is equal to 4 and
where the result is the quotient in which the 9.0//2.0 is equal to 4.0
digits after the decimal point are removed.
4
Python Comparison Operators:
Operator Description Example(a=10,b=20)
== Equal -Checks if the value of two operands are equal or (a == b) is not true.
not, if yes then condition becomes true.
!= Not equal - Checks if the value of two operands are equal (a != b) is true.
or not, if values are not equal then condition becomes true.
<> Not equal - Checks if the value of two operands are equal (a <> b) is true. This is
or not, if values are not equal then condition becomes true. similar to != operator.
> Greater than - Checks if the value of left operand is (a > b) is not true.
greater than the value of right operand, if yes then condition
becomes true.
< Less than -Checks if the value of left operand is less than (a < b) is true.
the value of right operand, if yes then condition becomes
true.
>= Greater than or equal to - Checks if the value of left (a >= b) is not true.
operand is greater than or equal to the value of right
operand, if yes then condition becomes true.
<= Less than or equal to - Checks if the value of left operand (a <= b) is true.
is less than or equal to the value of right operand, if yes
then condition becomes true.

5
Python Assignment Operators:
Operator Description Example
= Simple assignment operator, Assigns values from right side c = a + b will assigne
operands to left side operand value of a + b into c

+= Add AND assignment operator, It adds right operand to the left c += a is equivalent to
operand and assign the result to left operand c=c+a
-= Subtract AND assignment operator, It subtracts right operand c -= a is equivalent to
from the left operand and assign the result to left operand c=c-a

*= Multiply AND assignment operator, It multiplies right operand c *= a is equivalent to


with the left operand and assign the result to left operand c=c*a

/= Divide AND assignment operator, It divides left operand with c /= a is equivalent to


the right operand and assign the result to left operand c=c/a

%= Modulus AND assignment operator, It takes modulus using c %= a is equivalent to


two operands and assign the result to left operand c=c%a
**= Exponent AND assignment operator, Performs exponential c **= a is equivalent
(power) calculation on operators and assign value to the left to c = c ** a
operand
//= Floor Division and assigns a value, Performs floor division on c //= a is equivalent to
operators and assign value to the left operand c = c // a 6
Python Bitwise Operators:
Operator Description Example
& Binary AND Operator copies a bit to the result (a & b) will give 12 which
if it exists in both operands. is 0000 1100
| Binary OR Operator copies a bit if it exists in (a | b) will give 61 which is
either operand. 0011 1101
^ Binary XOR Operator copies the bit if it is set (a ^ b) will give 49 which
in one operand but not both. is 0011 0001
~ Binary Ones Complement Operator is unary (~a ) will give -60 which is
and has the effect of 'flipping' bits. 1100 0011
<< Binary Left Shift Operator. The left operands a << 2 will give 240 which
value is moved left by the number of bits is 1111 0000
specified by the right operand.

>> Binary Right Shift Operator. The left operands a >> 2 will give 15 which
value is moved right by the number of bits is 0000 1111
specified by the right operand.

7
Python Bitwise Assignment Operators:
Operator Example (a=5)
&= a&=3 will give 1

|= a|=3 will give 7

^= a ^= 3 will give 6

<<= a <<= 3 will give 40

>>= a >>= 3 will give 0

8
Python Logical Operators:
Operator Description Example
and Called Logical AND operator. If (a and b) is true.
both the operands are true then
then condition becomes true.
or Called Logical OR Operator. If (a or b) is true.
any of the two operands are non
zero then then condition becomes
true.
not Called Logical NOT Operator. not(a and b) is
Use to reverses the logical state false.
of its operand. If a condition is
true then Logical NOT operator
will make false.
9
Python Membership Operators:
In addition to the operators discussed previously, Python has membership
operators, which test for membership in a sequence, such as strings, lists,
or tuples.

Operator Description Example

in Evaluates to true if it finds a x in y, here in results in


variable in the specified a 1 if x is a member of
sequence and false otherwise. sequence y.

not in Evaluates to true if it does not x not in y, here not in


finds a variable in the specified results in a 1 if x is a
sequence and false otherwise. member of sequence y.

10
Python Identity Operators:
Identity operators compare the memory locations of two objects. There are
two Identity operators explained below

Operator Description Example

Evaluates to true if the variables x is y, here is results


on either side of the operator in 1 if id(x) equals
is point to the same object and false id(y).
otherwise.
Evaluates to false if the x is not y, here is not
variables on either side of the results in 1 if id(x) is
is not operator point to the same object not equal to id(y)
and true otherwise.
11
Order of Operations
Remember that thing called order of operations that
they taught in maths? Well, it applies in Python, too.
Here it is, if you need reminding:
1.parentheses ()
2.exponents **
3.multiplication *, division \, and remainder %
4.addition + and subtraction -
12
Order of Operations
Operator Operation Precedence
() parentheses 0
** exponentiation 1
* multiplication 2
/ division 2
// int division 2
% remainder 2
+ addition 3
- subtraction 3

13
Order of Operations

14
The computer scans the expression from left to right,
• first clearing parentheses,

• second, evaluating exponentiations from left to right in the order


they are encountered
• third, evaluating *, /, //, % from left to right in the order they are
encountered,
• fourth, evaluating +, - from left to right in the order they are
encountered

15
2**3+2*(2+3)

• 8+2*5
• 8+10
• 18

16
Example 1 – Order of operations
>>> 1 + 2 * 3
7
>>> (1 + 2) * 3
9
• In the first example, the computer calculates 2 * 3 first, then adds 1 to it.
This is because multiplication has the higher priority (at 3) and addition is
below that (at a lowly 4).
• In the second example, the computer calculates 1 + 2 first, then multiplies
it by 3. This is because parentheses have the higher priority (at 1), and
addition comes in later than that.
17
Example 2 – Order of operations
Also remember that the math is calculated from left to right, unless you put in
parentheses. The innermost parentheses are calculated first. Watch these
examples:

>>> 4 - 40 - 3

-39

>>> 4 - (40 - 3)

-33
• In the first example, 4 - 40 is calculated, then - 3 is done.
• In the second example, 40 - 3 is calculated, then it is subtracted from 4.

18
Quotation in Python
• Python accepts single ('), double (") and triple (''' or """)
quotes to denote string literals, as long as the same type of
quote starts and ends the string.
• The triple quotes are used to span the string across multiple
lines. For example, all the following are legal −
• word = 'word'
• sentence = "This is a sentence."
• paragraph = """This is a paragraph. It is made up of multiple
lines and sentences."""
19
Built-in format Function
• Because floating-point values may contain an arbitrary
number of decimal places, the built-in format function can
be used to produce a numeric string version of the value
containing a specific number of decimal places.

• In these examples, format specifier '.2f' rounds the result to


two decimal places of accuracy in the string produced.

20
format Function

21
• For very large (or very small) values 'e' can be
used as a format specifier,

22
23
Python is a Dynamic Type language
• Same variable can be associated with values of
different type during program execution, as
indicated below.
• It's also very dynamic as it rarely uses what it
knows to limit variable usage

24
25
Bitwise Operations
• This includes operators that treat integers as
strings of binary bits, and can come in handy if
your Python code must deal with things like
network packets, serial ports, or packed binary
data
• >>> x = 1 # 1 decimal is 0001 in bits
>>> x << 2 # Shift left 2 bits: 0100
• 4

26
• >>> x | 2 # Bitwise OR (either bit=1): 0011
• 3
• >>> x & 1 # Bitwise AND (both bits=1): 0001
• 1
• In the first expression, a binary 1 (in base 2, 0001) is
shifted left two slots to create a binary 4 (0100).
• The last two operations perform a binary OR to
combine bits (0001| 0010 = 0011) and a binary AND
to select common bits (0001&0001 = 0001).

27
• To print in binary format use bin function:
• >>> X = 0b0001 # Binary literals
• >>> X << 2 # Shift left 4
• >>> bin(X << 2) # Binary digits string
'0b100‘
• >>> bin(X | 0b010) # Bitwise OR: either
'0b11'
• >>> bin(X & 0b1) # Bitwise AND: both
'0b0'

28
Logical Operators
Assume a = 10 and b = 20
Operator Description Example
and If both the operands (a and b) is true.
are true then
condition becomes
true.
Or If any of the two (a or b) is true.
operands are non-
zero then condition
becomes true.
not Used to reverse the Not(a and b) is
logical state of its false.
operand.

29
Python is a Strongly Typed language

• interpreter keeps track of all variable types


• Check type compatibility while expressions
are evaluated
• >>> 2+3 # right
• >>>”two”+1 # Wrong!!

30
31
32
Python features….. lambda
operator
The lambda operator or lambda function is a
way to create small anonymous functions,
i.e. functions without a name.
SYNTAX:
lambda arguments : expression

>>> ftoc =lambda f: (f-32)*5.0/9


>>> ftoc(104)
33
Problem -1
ABC company Ltd. is interested to computerize the pay calculation of their
employee in the form of Basic Pay, Dearness Allowance (DA) and House Rent
Allowance (HRA). DA and HRA are calculated as certain % of Basic pay(For
example, DA is 80% of Basic Pay, and HRA is 30% of Basic pay). They have
the deduction in the salary as PF which is 12% of Basic pay. Propose a
computerized solution for the above said problem.

Input : Basic Pay


Process : Calculate Salary
( Basic Pay + ( Basic Pay * 0.8) + ( Basic Pay * 0.3 - ( Basic Pay * 0.12)
-----------allowances -------------- --- deductions----
Output : Salary

34
Flow chart

35
Python code

#Enter the basic pay


bp=float (input('Enter the basic pay:'))
# net pay calucluation
netpay =bp + (bp*0.8) + (bp*0.3) - (bp*0.12)
# display net salary
print ('Net pay :',netpay)

36
Output

37
38

You might also like