<< Chapter < Page Chapter >> Page >

The modulus operator

And that brings us to the modulus operator (%).

Enter the code shown in Figure 9 . (You don't need to enter the comments. They are there to explain what is going on.)

Figure 9 . Whole number quotient and remainder.
>>>23//4 #get integer quotient 5>>>23%4 #get remainder 3>>>23.0//4 #get quotient 5.0>>>23.0%4 #get remainder 3.0>>>

It is probably safe to say that the purpose of the // division operator is to produce a whole number quotient (no digits to the right of the decimal point even if the decimal point is showing as in the last operation in Figure 8 ) .

The purpose of the modulus operator is to produce the remainder resulting from a division.

As you can see from the example in Figure 9 , both division operations using the // division operator produced the whole number quotient of 5, and bothmodulus operations produced the whole number remainder of 3.

The order of operations

What is the result of evaluating the expression shown in Figure 10 on a hand calculator?

Figure 10 . A simple expression.
3+5*4

Try it on your hand calculator. (You will probably need to use an X instead of an * to indicate multiplication.) My hand calculator gives an answer of 32.

Now try it with Python and you should get the result shown in Figure 11 .

Figure 11 . A simple expression in Python.
>>>3+5*4 23>>>

Oops! This answer doesn't match the answer produced by my hand calculator, and I'll bet that it doesn't match your calculator either unless you are using a fancyscientific calculator.

The answer depends on the order in which you perform the various arithmetic operations. Ordinary hand calculators usually do the arithmetic in the orderthat the terms are fed into the keyboard.

Operator precedence

However, most computer programming systems, including Python, use a precedence system to decide which operations to perform first, which operationsto perform second, etc.

I'm not going to go into the Python precedence system in detail. (If you are interested in the order of precedence of all the operators, you can find aprecedence table in Python Language Reference -- 6.15. Operator precedence .)

Rather, I am going to show you how to group terms using parentheses so that you can control the order of operations without worrying about the precedencesystem.

Grouping terms with parentheses

The Python code fragment in Figure 12 shows how I can produce both results simply by grouping terms using parentheses.

Figure 12 . Grouping terms with parentheses.
>>>(3+5)*4 # do addition first 32>>>3+(5*4) # do multiplication first, default 23>>>

The first expression produces 32 as produced by the hand calculator. The second expression produces 23 as produced by the earlier Python expression.

A Python expression is evaluated by first evaluating each of the sub-expressions inside the parentheses and then using those values to complete the evaluation.

Forcing addition to be performed first

In the first expression, the code in Figure 12 forced Python to perform the addition first by placing the addition inside the parentheses. This produced an intermediate valueof 8 when the sub-expression inside the parentheses was evaluated. The remaining part of the overall expression was then evaluated by multiplying theintermediate value by 4, producing a result of 32.

Questions & Answers

what is mutation
Janga Reply
what is a cell
Sifune Reply
how is urine form
Sifune
what is antagonism?
mahase Reply
classification of plants, gymnosperm features.
Linsy Reply
what is the features of gymnosperm
Linsy
how many types of solid did we have
Samuel Reply
what is an ionic bond
Samuel
What is Atoms
Daprince Reply
what is fallopian tube
Merolyn
what is bladder
Merolyn
what's bulbourethral gland
Eduek Reply
urine is formed in the nephron of the renal medulla in the kidney. It starts from filtration, then selective reabsorption and finally secretion
onuoha Reply
State the evolution relation and relevance between endoplasmic reticulum and cytoskeleton as it relates to cell.
Jeremiah
what is heart
Konadu Reply
how is urine formed in human
Konadu
how is urine formed in human
Rahma
what is the diference between a cavity and a canal
Pelagie Reply
what is the causative agent of malaria
Diamond
malaria is caused by an insect called mosquito.
Naomi
Malaria is cause by female anopheles mosquito
Isaac
Malaria is caused by plasmodium Female anopheles mosquitoe is d carrier
Olalekan
a canal is more needed in a root but a cavity is a bad effect
Commander
what are pathogens
Don Reply
In biology, a pathogen (Greek: πάθος pathos "suffering", "passion" and -γενής -genēs "producer of") in the oldest and broadest sense, is anything that can produce disease. A pathogen may also be referred to as an infectious agent, or simply a germ. The term pathogen came into use in the 1880s.[1][2
Zainab
A virus
Commander
Definition of respiration
Muhsin Reply
respiration is the process in which we breath in oxygen and breath out carbon dioxide
Achor
how are lungs work
Commander
where does digestion begins
Achiri Reply
in the mouth
EZEKIEL
what are the functions of follicle stimulating harmones?
Rashima Reply
stimulates the follicle to release the mature ovum into the oviduct
Davonte
what are the functions of Endocrine and pituitary gland
Chinaza
endocrine secrete hormone and regulate body process
Achor
while pituitary gland is an example of endocrine system and it's found in the Brain
Achor
what's biology?
Egbodo Reply
Biology is the study of living organisms, divided into many specialized field that cover their morphology, physiology,anatomy, behaviour,origin and distribution.
Lisah
biology is the study of life.
Alfreda
Biology is the study of how living organisms live and survive in a specific environment
Sifune
Got questions? Join the online conversation and get instant answers!
Jobilize.com Reply

Get Jobilize Job Search Mobile App in your pocket Now!

Get it on Google Play Download on the App Store Now




Source:  OpenStax, Itse 1359 introduction to scripting languages: python. OpenStax CNX. Jan 22, 2016 Download for free at https://legacy.cnx.org/content/col11713/1.32
Google Play and the Google Play logo are trademarks of Google Inc.

Notification Switch

Would you like to follow the 'Itse 1359 introduction to scripting languages: python' conversation and receive update notifications?

Ask