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

Problem Solving Using Python: Assistant Professor, Dept. of CSE, SJCET Palai

This document discusses operators and operands in Python problem solving. It explains that operators like +, -, /, *, ** represent mathematical operations and the order they are evaluated following PEMDAS rules. Strings can be concatenated with + but not other math operations. Comments are recommended to explain programs. Conditionals like if-statements allow branching based on the modulus operator %. The modulus % returns the remainder of a division and can be used to check for divisibility or extract digits of a number.

Uploaded by

Jimmy Jose
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
37 views

Problem Solving Using Python: Assistant Professor, Dept. of CSE, SJCET Palai

This document discusses operators and operands in Python problem solving. It explains that operators like +, -, /, *, ** represent mathematical operations and the order they are evaluated following PEMDAS rules. Strings can be concatenated with + but not other math operations. Comments are recommended to explain programs. Conditionals like if-statements allow branching based on the modulus operator %. The modulus % returns the remainder of a division and can be used to check for divisibility or extract digits of a number.

Uploaded by

Jimmy Jose
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 14

Problem Solving using Python

Prof. Sarju S
Assistant Professor, Dept. of CSE, SJCET Palai

Tuesday, January 05, 2021


Operators and operands

Page 2
Operators and operands
► Operators are special symbols that represent computations like addition and
multiplication. The values the operator uses are called operands.
► The following are all legal Python expressions
► 20+32 hour-1 hour*60+minute minute/60 5**2 (5+9)*(15-7)
► The symbols +, -, and /, and the use of parenthesis for grouping, mean in
Python what they mean in mathematics.
► The asterisk (*) is the symbol for multiplication, and ** is the symbol for
exponentiation

Page 3 Prof. Sarju S, Nodal Officer, SJCET Startup Bootcamp


Order of operations
► When more than one operator appears in an expression, the order of
evaluation depends on the rules of precedence.

► Python follows the same precedence rules for its mathematical operators that
mathematics does.

► The acronym PEMDAS is a useful way to remember the order of operations

Page 4 Prof. Sarju S, Nodal Officer, SJCET Startup Bootcamp


Order of operations
► Parentheses have the highest precedence and can be used to force an
expression to evaluate in the order you want.

► Since expressions in parentheses are evaluated first, 2 * (3-1) is 4, and


(1+1)**(5-2) is 8.

► You can also use parentheses to make an expression easier to read, as in


(minute * 100) / 60, even though it doesn’t change the result.

Page 5 Prof. Sarju S, Nodal Officer, SJCET Startup Bootcamp


Order of operations
► Exponentiation has the next highest precedence
► 2**1+1 is 3 and not 4, and 3*1**3 is 3 and not 27.
► Multiplication and Division have the same precedence, which is higher than
Addition and Subtraction, which also have the same precedence.
► 2*3-1 yields 5 rather than 4, and 2/3-1 is -1, not 1

► Operators with the same precedence are evaluated from left to right.

Page 6 Prof. Sarju S, Nodal Officer, SJCET Startup Bootcamp


Operations on strings
► You cannot perform mathematical operations on strings
► message-1 ’Hello’/123 message*’Hello’ ’15’+2, these are illegal
► Interestingly, the + operator does work with strings, although it does not do
exactly what you might expect.
► For strings, the + operator represents concatenation, which means joining the two
operands by linking them end-to-end.
fruit = ’banana’
bakedGood = ’ nut bread’
print fruit + bakedGood
► The output of this program is banana nut bread.
► The * operator also works on strings; it performs repetition
► For example, ’Fun’*3 is FunFunFun.

Page 7 Prof. Sarju S, Nodal Officer, SJCET Startup Bootcamp


Comments
► As programs get bigger and more complicated, they get more difficult to read.
► It is a good idea to add notes to your programs to explain in natural language
what the program is doing.
# compute the percentage of the hour that has elapsed
percentage = (minute * 100) / 60

Page 8 Prof. Sarju S, Nodal Officer, SJCET Startup Bootcamp


Conditionals

Page 9
The modulus operator
► The modulus operator works on integers (and integer expressions) and yields
the remainder when the first operand is divided by the second.
► In Python, the modulus operator is a percent sign (%).
>>> quotient = 7 / 3
>>> print quotient
2
>>> remainder = 7 % 3
>>> print remainder
1
So 7 divided by 3 is 2 with 1 left over.

Page 10 Prof. Sarju S, Nodal Officer, SJCET Startup Bootcamp


The modulus operator
► The modulus operator turns out to be surprisingly useful
► You can check whether one number is divisible by another
► if x % y is zero, then x is divisible by y.
► You can extract the right-most digit or digits from a number.
► x % 10 yields the right-most digit of x (in base 10).
► Similarly x % 100 yields the last two digits.

Page 11 Prof. Sarju S, Nodal Officer, SJCET Startup Bootcamp


The modulus operator
► The modulus operator turns out to be surprisingly useful
► You can check whether one number is divisible by another
► if x % y is zero, then x is divisible by y.
► You can extract the right-most digit or digits from a number.
► x % 10 yields the right-most digit of x (in base 10).
number = 1234
print(number%10)
The Result will be : 4
► Similarly x % 100 yields the last two digits
number = 1234
print(number%100)
The Result will be : 34

Page 12 Prof. Sarju S, Nodal Officer, SJCET Startup Bootcamp


The modulus operator
► The modulus operator turns out to be surprisingly useful
► You can check whether one number is divisible by another
► if x % y is zero, then x is divisible by y.
► You can extract the right-most digit or digits from a number.
► x % 10 yields the right-most digit of x (in base 10).
number = 1234
print(number%10)
The Result will be : 4
► Similarly x % 100 yields the last two digits
number = 1234
print(number%100)
The Result will be : 34

Page 13 Prof. Sarju S, Nodal Officer, SJCET Startup Bootcamp


#happycoding

Thank You

Prof. Sarju S
Department of Computer Science and Engineering
St. Joseph’s College of Engineering and Technology, Palai
https://www.linkedin.com/in/sarju-s/
Email: [email protected]
Page 14

You might also like