Chapter 14: Introduction to Operators
The annual book fair at Sunrise Public School runs for three days. This year Aarav and Neha are in charge of the billing counter. On the first morning it goes smoothly. A student picks up two books. Neha adds the prices on a calculator, looks down the printed sheet to see whether the buyer’s name is on the members list, takes off ten per cent if it is, and writes the final amount in a register.
By the afternoon the queue has reached the corridor. Every single bill needs the same four things. Add the prices. Take off the discount. Decide whether the total has crossed the limit for a free bookmark. Check the members list. The work never changes; only the numbers change. Fig. 14.0-1 shows the counter on that busy afternoon.
Diagram
Fig. 14.0-1 – a school book-fair billing counter with a queue of students; two student volunteers at the table with a calculator, a register and a printed members list; four labelled callout boxes above the table reading “Add the prices”, “Take off the discount”, “Free bookmark or not?”, “Name on the members list?”
Their computer teacher watches for a while and says one thing: a Python program can do all four jobs, and it will fit in a few lines. That evening Aarav opens a laptop. He already knows how to store a price in a variable. What he does not know yet is how to make Python work with the values he has stored.
This chapter will answer all five of these questions.
14.1 Operators, Operands and Expressions
A variable that only holds a value is not much use on its own. If Neha’s laptop stores the price of a book and the number of copies, it has done nothing that the printed price tag had not already done. The counter becomes faster only when the computer starts doing something with those values — multiplying them, comparing them, deciding whether a discount applies.
Think of a calculator. The number keys let you put values in. The +, −, × and ÷ keys are what actually get work done. Python has the same two kinds of things: values, and symbols that act on values.
A symbol that tells Python to do something with one or more values is called an operator. The + in 8 + 5 is an operator. So is the > in a line that checks whether one price is greater than another.
Notice the words “or keyword”. Most operators are symbols, but a few are ordinary English words, such as and, is and in. They are still operators.
What the operator works on
The values an operator works on have a name too. Each one is called an operand. An operand can be a plain value, or a variable holding a value, or even the result of another operation.
In 8 + 5, the operator is + and the operands are 8 and 5. In price * quantity, the operator is * and the operands are the two variables. See Fig. 14.1-1.
Diagram
Fig. 14.1-1 – the code 8 + 5 written large, with a downward arrow from 8 labelled “operand”, an arrow from + labelled “operator”, and an arrow from 5 labelled “operand”; below it a curved bracket under the whole thing labelled “expression”, and to the right an arrow pointing to the value 13 labelled “the value it produces”
Most Python operators take two operands, one on each side. A few take only one — not is written in front of a single operand, with nothing on its left.
Expressions
Put operators and operands together and you get an expression. An expression is any piece of code that produces a single value. Python works out the value of an expression as soon as it meets one, and this working-out is called evaluating the expression.
An expression can be long, like price * quantity - discount. It can also be very short.
The value an expression produces is not always a number. Comparing two prices produces True or False. Joining two names together produces a string. The type of the result depends on the operator used and on the type of the operands.
The six groups of operators
Python has many operators, so they are studied in groups. The syllabus covers six of them, and the rest of this chapter takes them one at a time.
- Arithmetic operators do the calculations — adding, subtracting, multiplying, dividing and raising to a power.
- Logical operators combine two or more conditions, so that a program can check several things at once.
- Assignment operators put a value into a variable, and can update the value already stored there.
- Comparison operators compare two values and answer with
TrueorFalse. - Identity operators check whether two names refer to the very same object in memory.
- Membership operators check whether a value appears inside a sequence such as a list or a string.
Aarav’s billing program will need four of these six groups before it is finished, which is a good sign that none of them is there just for decoration.
Spaces around an operator are optional. 8+5 and 8 + 5 mean exactly the same thing to Python. The spaced-out version is easier for a person to read, so it is worth getting into the habit.
14.2 Arithmetic Operators
Almost every program that deals with real life has to do sums somewhere. A bill needs a total. A report card needs an average. A robot that must reach a wall in four seconds needs its speed worked out from a distance and a time. None of this happens unless the program can calculate.
The operators that do calculations are called arithmetic operators. They are the closest thing Python has to the keys on a calculator, except that you type them into a line of code instead of pressing them one at a time.
There are seven of them, and you already know most of them from mathematics. Table 14.1 lists all seven together so that you can see the whole set before studying each one.
Three of these will look unfamiliar. // and % are two different ways of dividing, and ** raises a number to a power. Each gets its own sub-section below.
The type of the answer
Before starting, one idea is worth fixing in your mind, because it explains results that otherwise look strange.
You met two kinds of numbers earlier: an int, which is a whole number such as 7, and a float, which is a number with a decimal point such as 7.0 or 7.25. The type of the answer an arithmetic operator gives depends on the types of its operands.
The general rule is simple. If both operands are int, the answer is an int. If even one operand is a float, the answer is a float. There is one operator that breaks this rule, /, and 14.2.2 explains why.
You can, of course, put several arithmetic operators in one line. When you do, Python does not simply work from left to right — it follows an order, much like the order you use in mathematics. That order is explained in 14.8, once all the operators have been introduced. Until then, each example here uses one operator at a time.
14.2.1 Addition, Subtraction and Multiplication
Go back to the billing counter for a moment. A student buys three copies of the same book. Neha does not add the price three times; she multiplies. Then she adds ten rupees for a paper bag. Two operations, both of which a program can do far faster than a person with a calculator.
These three operators behave exactly as they do in mathematics.
+ adds its two operands. - subtracts the right operand from the left. * multiplies. The only surprise is the multiplication sign, which is a star and not a cross.
Working out the bill
Before writing any code, say the method out loud in ordinary words. Multiply the price of one book by the number of copies. That gives the cost of the books. Add the bag charge to it. That gives the amount to be paid.
Each of those two steps becomes one line of Python.
Notice that the result of the first calculation is stored in books_cost, and that variable is then used as an operand in the next line. This is what was meant in 14.1 by saying that an operand can itself be the result of an earlier operation.
Changing the program to handle four copies means changing one number. That is the whole reason the counter is faster with a program than with a register.
Whole numbers and decimals
The type rule from 14.2 shows up as soon as a decimal enters the calculation.
The first two lines give the same amount, but not the same type. 40 + 25 has two int operands, so the answer is the int 65. In 40 + 25.0 one operand is a float, so Python gives back the float 65.0.
The third line shows that subtraction is quite happy to give a negative answer. 18 - 25 is -7. Nothing special has to be done for negative results.
If an operand is itself negative, put it in brackets so the line stays readable. 50 - (-6) gives 56, and the brackets make it obvious that the second operand is negative six rather than a typing slip.
14.2.2 Division, Floor Division and Modulus
The book fair leaves forty-seven unsold books, and they have to be packed into cartons that hold six each. Two different answers are needed here. How many full cartons will there be? And how many books will be left over after the last full carton is packed?
Ordinary division does not answer either question properly. Dividing forty-seven by six gives a number with a long decimal tail, and you cannot pack a fraction of a carton. This is why Python has three division operators instead of one.
The / operator
/ is ordinary division, the kind you do in mathematics. It gives the exact answer, decimals and all.
It also does something that surprises everybody at first: it always gives a float, even when the division comes out exactly.
Look at the second line. Six divided by two is three with nothing left over, yet Python prints 3.0. This is deliberate. Python does not check each division to see whether it came out exactly; it simply promises that / always hands back a float. That way you always know the type of the result before the program runs.
The // operator
The third line uses a different operator. // performs floor division: it divides, then rounds the answer down to a whole number. Five divided by two is 2.5, and rounding 2.5 down gives 2.
Rounding down is not the same as chopping off the decimal part, and the difference shows up with negative numbers. -5 // 2 gives -3, because -2.5 rounded down is -3, not -2. This catches people out, so be careful when a floor division might receive a negative operand.
If either operand is a float, // still rounds down but gives a float. 7.0 // 2 gives 3.0.
The % operator
The last line of the program uses %, called the modulus operator. It divides the left operand by the right operand and gives back the remainder.
Seven divided by four is one, with three left over, so 7 % 4 gives 3. It has nothing to do with percentages, even though it borrows the percentage sign.
// and % are a pair. One tells you how many whole groups you can make, the other tells you what is left over. Together they answer the carton question completely.
The program and the hand calculation agree, which is exactly what you should check whenever you write a small piece of arithmetic code.
There is one number that no division operator can handle.
14.2.3 The Exponent Operator
The school wants to tile a square patch of floor in the computer lab and needs to know its area. The patch is 7 metres along each side. Area means side multiplied by side, and you could of course write 7 * 7.
But a cube-shaped storage box would need 7 * 7 * 7, and a calculation with a much larger power would be unbearable to type. Repeated multiplication by the same number deserves an operator of its own.
That operator is **, the exponent operator. It raises the left operand to the power of the right operand. So 2 ** 3 means two multiplied by itself three times, which is 8.
The area of a square is written in mathematics as
\[\text{area} = \text{side}^2\]
where side is the length of one side and area is the space the square covers. The little raised 2 becomes ** 2 in Python.
Both answers match the hand calculation, and both are int values because both operands of ** were whole numbers.
Powers that are not whole numbers
The right operand does not have to be a positive whole number, and this is where ** becomes more useful than plain multiplication.
A power of 0.5 gives the square root. 4 ** 0.5 is 2.0, because two multiplied by itself is four. A negative power gives one divided by that power, so 2 ** -1 is one divided by two, which is 0.5.
14.2.4 Arithmetic Operators on Text and Lists
A printed bill is not only numbers. It has the shop’s name at the top, and a line of dashes under it to separate the heading from the items. Aarav wants his program to print both.
Neither job is arithmetic, and yet two of the operators you have just met do exactly what is needed. + and * behave differently depending on the type of their operands, and on text they do something rather useful.
Joining text with +
When both operands are strings, + does not add — it joins them end to end. Joining two strings this way is called concatenation.
"Sun" + "rise" produces "Sunrise". Nothing is inserted between them, so if you want a space you have to supply it yourself as a string of its own.
Repeating text with *
When one operand is a string and the other is a whole number, * repeats the string that many times. "ab" * 3 gives "ababab".
That is the neat way to draw a dividing line. Instead of typing twenty dashes and hoping you counted right, you write "-" * 20 and let Python count.
The number must be a whole number. "-" * 20.0 is not allowed, because Python cannot repeat something twenty-point-zero times.
Joining lists
A list behaves the same way. + joins two lists into one longer list, and * repeats a list.
Suppose a class has two subjects before the break and two after it, kept in separate lists. One operator puts the whole day together.
The items stay in order, with the second list’s items placed after the first list’s items.
What does not work
Only + and * behave this way. There is no sensible meaning for subtracting one piece of text from another, so "Science" - "ence" is not allowed, and neither is dividing a string. Python stops the program with a TypeError, which is its way of saying that an operator has been given operand types it cannot work with.
Mixing a string with a number in an addition fails for the same reason.
The wider lesson is worth remembering for the whole chapter. An operator does not have one fixed meaning. What it does depends on the types of the operands it is given.
14.3 Logical Operators
At the book fair counter, the free bookmark is not given to everybody. It goes to a buyer whose bill has crossed the limit and who is on the members list. Two conditions, both of which must hold.
Real decisions are usually like this. A school lets a student sit the sports trial if the medical form is signed and the fee is paid. A robot moves forward if the path ahead is clear or the sensor has been switched off for testing. A single yes-or-no fact is rarely enough.
Python stores a yes-or-no fact as one of two special values, True and False. You met these earlier as Boolean values. A condition in a program is simply something that works out to True or False.
The operators that combine such values are called logical operators. There are three of them: and, or and not.
Notice that all three are ordinary English words rather than symbols. They are still operators, exactly as + is, and they are written in lower case. Python will not accept AND or And.
The three sub-sections below take one operator each. In every case the question to keep in mind is the same: given these True and False values coming in, what single True or False value comes out?
14.3.1 The and Operator
The bookmark rule at the counter has two parts, and both have to be satisfied. A bill above the limit is not enough on its own. Being on the members list is not enough on its own either.
and is the operator for exactly this situation. It looks at the value on its left and the value on its right, and gives True only when both of them are True. In every other case it gives False.
That is a strict rule, and it matches the way a careful shopkeeper thinks. One missing condition and the answer is no.
The buyer met one condition out of two, and and still answered False. Change is_member to True and the same line would give True.
There are only four possible combinations of two Boolean values, so the whole behaviour of and fits in a short program.
Table 14.2 sets out the same four results in the form that is easier to look up later.
Only the first row gives True. This is the quickest way to remember and: one True out of four.
14.3.2 The or Operator
The reading room next to the book fair has a different rule. A student may go in if they are a library member, or if they have been given a day pass for the fair. Either one is enough. A student with both is welcome too.
or is the operator for a rule of that shape. It gives True when at least one of its two operands is True. It gives False only in the one case where both are False.
Where and is strict, or is generous.
One condition out of two was enough. Had the same two values been joined with and, the answer would have been False, which is a good reminder that the operator you choose decides the rule.
Table 14.3 shows the same four results together.
Only the last row gives False. That is the mirror image of and, and it is the easiest way to hold the two apart: and needs everything, or needs anything.
14.3.3 The not Operator
Some rules are written the other way round. The office sends a fee reminder to a student whose fee has not been paid. A robot stops when the path ahead is not clear.
In both cases the program already has the fact it needs, but it needs the opposite of it. Turning True into False, and False into True, is the job of not.
not is unusual in taking a single operand. It is written in front of that operand, with nothing on its left.
There are only two values for not to work on, so its complete behaviour fits in Table 14.4.
Where not acts
When not appears in a longer expression, it matters how much of the expression it applies to. not acts on the value immediately to its right, and nothing more.
The two lines contain the same three pieces and give opposite answers. In the first line, not reverses above_limit alone, giving False, and False and False is False. In the second line the brackets are worked out first, so not reverses the combined result False, giving True.
The order in which Python works through an expression like this is set out fully in 14.8. Until then, brackets are the safe way to say exactly what you mean, and they cost nothing.
14.4 Assignment Operators
Every program so far has quietly used one operator without naming it. Lines such as price = 180 and books_cost = price * quantity all contain an equals sign, and that sign is doing real work.
Think about the counter again. Neha keeps a running total on a slip of paper. Each time a book is scanned she crosses out the old total and writes the new one. The slip always holds one value, and that value keeps being replaced.
A variable is that slip of paper, and the assignment operator is the act of writing on it.
The direction matters and it is always right to left. Python first works out whatever is on the right, then puts that result into the variable on the left. This is why books_cost = price * quantity makes sense: the multiplication happens first, and only the finished answer is stored.
Python also has a set of shorter assignment operators that update a variable using the value it already holds. They are handy for exactly the running-total job that the slip of paper does. The two sub-sections below cover the plain assignment operator first, then that shorter family.
14.4.1 The Assignment Operator
The simplest use of = is to put a value into a variable for the first time.
x = 50After this line runs, the variable x holds 50, and the rest of the program can use x wherever it needs that value. The value can be of any type: a whole number, a decimal, a piece of text, a Boolean value, or a list.
The name always goes on the left and the value on the right. Writing 50 = x is not allowed, because Python would be asked to store something into the number fifty.
Giving a variable a new value
A variable does not hold one value for ever. Assign to it again and the new value replaces the old one, which is then gone. See Fig. 14.4-1.
Diagram
Fig. 14.4-1 – a labelled box named total shown twice, side by side; in the first box the value 180, in the second box the value 300 with a small arrow between the two boxes labelled “total = total + 120”; the old value shown faded to indicate it has been replaced
This is what makes a running total possible. The counter’s slip of paper never holds two totals at once.
The middle line is the one worth staring at. In mathematics, total = total + 120 would be nonsense, because no number equals itself plus 120. In Python it is an instruction rather than a statement of fact.
Reading such a line from right to left, out loud, usually clears it up: “take the current total, add 120, and put the answer back into total”.
14.4.2 Augmented Assignment Operators
Look at that running-total line once more.
total = total + 120The name total is typed twice, and it must be spelt the same way both times. In a program that adds twenty books to a bill, that name gets typed forty times, and any one of them could be mistyped.
Python offers a shorter way of writing the same instruction. These shorter forms are called augmented assignment operators. Each one joins an arithmetic operator to the = sign.
total += 120 means exactly what total = total + 120 means: take the value in the variable, add 120 to it, and store the answer back. The variable name is written only once.
There is one for each arithmetic operator, as Table 14.5 shows.
The first line still uses plain =, and it has to. An augmented operator uses the value already in the variable, so the variable must hold something before the first += runs. Starting a total at 0 is the usual way to arrange that.
Because += follows whatever + does, it works on text as well. If line holds a piece of text, line += " paid" adds those characters to the end of it, in the same way that + joined strings in 14.2.4.
14.5 Comparison Operators
The logical operators in 14.3 all needed True and False values to work on, and those values were simply typed in. A real program cannot do that. Nobody sits beside the counter deciding whether the bill has crossed the limit; the program has to find that out for itself.
Finding it out means comparing two values. Is the total greater than the limit? Is the quantity equal to zero? Is today’s date the same as the last date of the fair?
A comparison operator compares its two operands and answers with a single Boolean value.
There are six of them, listed in Table 14.6. Four will be familiar from mathematics; the other two are written differently from what you may expect.
This is what makes comparison and logical operators natural partners. A comparison produces a Boolean value, and a logical operator combines Boolean values. Together they let a program work out a rule as complicated as the bookmark rule from a set of plain facts.
14.5.1 Equal To and Not Equal To
Two operators in this group cause more confusion than all the others together, and both of them involve an equals sign.
= is an order. It tells Python to store a value in a variable.
== is a question. It asks whether two values are the same, and the answer comes back as True or False. Nothing is stored and nothing is changed.
The double sign exists precisely because the single one was already taken.
!= asks the opposite question: are these two values not equal? The ! in front of the = is Python’s way of writing “not”, and this is the only place it is used. For any pair of values, == and != always give opposite answers.
Comparing values of different kinds
Equality compares the values, not the way they are written. A whole number and a decimal that stand for the same amount are counted as equal.
Text is compared character by character, and capital letters are not the same as small ones.
This matters whenever a program compares something a person has typed. A student who enters science instead of Science has, as far as == is concerned, entered a different word altogether.
14.5.2 Greater Than and Less Than
The bookmark rule has a number in it. The bill must cross ₹500. Equality is no use here, because a bill of ₹620 should qualify even though it is not equal to anything in particular.
Four operators handle questions of size. > asks whether the left value is greater than the right, and < asks whether it is smaller. Both are written exactly as in mathematics.
The other two allow the two values to be equal as well. >= asks whether the left value is greater than or equal to the right, and <= asks whether it is less than or equal. The equals sign always comes second; => is not a Python operator.
Writing the bookmark rule at last
With comparison and logical operators together, the rule from the chapter opener can finally be written as it actually is.
Splitting the rule across two lines is not necessary — both could be written as one long expression — but a program that names its middle results is far easier to check when something goes wrong.
The value on the boundary
The difference between > and >= shows up in exactly one place: when the two values are equal.
So the choice of operator decides a real question about the offer. Does a bill of exactly ₹500 earn the bookmark or not? If the notice says “above ₹500”, the program should use >. If it says “₹500 or more”, it should use >=.
14.5.3 Comparing Different Data Types
Comparison operators are not only for numbers. A program that arranges a class list needs to know whether one name comes before another, and that is a comparison too.
Numbers of different types compare without any trouble. A whole number and a decimal both stand for an amount, so Python simply compares the amounts. 7 < 7.5 is True.
Text is compared in dictionary order, one character at a time. Python looks at the first character of each string; if they differ, that settles the answer. If they are the same, it moves on to the second character, and so on.
Each character has a number code stored inside the computer, and the comparison really works on those codes. The codes run in alphabetical order, which is why dictionary order comes out correctly. But all the capital letters have smaller codes than all the small letters, and that produces one result people do not expect.
The second line is ordinary alphabetical order: A comes before B. The third line says that "Zebra" comes before "apple", which no dictionary would agree with. It happens because a capital Z has a smaller code than a small a, and Python never got past the first character.
When the types do not fit
Not every pair of values can be compared. Asking whether a piece of text is greater than a number has no meaning, so "5" > 3 stops the program with a TypeError.
Equality is more forgiving. It does not fail; it simply answers False.
The two are of different types, and no amount of similarity on the screen changes that. This is the same trap as the one in 14.2.4: a value that came from input() is text until it is converted, so comparing it with a number will quietly answer False every time.
14.6 Identity Operators
Two students in the same class own the same novel. The books have the same cover, the same pages, the same words. They are equal in every way that matters to a reader, but they are still two separate books. If one student writes a note in the margin, the other student’s copy is unaffected.
Now imagine instead that one book is shared, and both students have written their names in the same copy. That is a different situation altogether. A note written by one is seen by the other, because there is only one book.
Python keeps track of the same difference. == asks whether two values are equal. An identity operator asks something stricter: whether two names refer to the very same object stored in memory.
There are two of them. is gives True when both operands refer to the same object. is not gives True when they do not.
The first line says the two lists hold the same items, which they do. The second line says they are not the same object, which is also correct: two separate lists were built, one on each line, and they happen to match.
The third line is different. No new list was built there. list_c = list_a gave a second name to the list that already existed, so both names refer to one object. See Fig. 14.6-1.
Diagram
Fig. 14.6-1 – three labelled name tags on the left, list_a, list_c and list_b; arrows from list_a and list_c both pointing to a single box containing 1, 2, 3; a separate arrow from list_b pointing to a second, identical box containing 1, 2, 3
This is not a fine point of no consequence. Because list_a and list_c refer to one list, adding an item through either name changes what both names show. list_b would stay as it was.
14.7 Membership Operators
The last thing Neha does for every buyer is run her finger down the members list, looking for the name. It is the slowest part of the sale, and it is the part a computer is best at.
You could write this check with the operators you already have: compare the name with the first entry, then with the second, then with the third. For a list of two hundred members that is two hundred comparisons, and you would have to write a loop to do it.
Python has an operator that does the whole search in one go. It is called a membership operator, because it asks whether a value is a member of a sequence.
There are two. in gives True when the value is found. not in gives True when it is not.
One short line replaced the whole finger-down-the-list search, and it works just as well for four names as for four hundred.
Membership in text
A string is a sequence of characters, so in works on text as well. There it asks whether one piece of text appears inside another.
The second line is False for the same reason as in 14.5.1: a capital S and a small s are different characters. Membership is exact.
The same operator works on a tuple, which is a sequence like a list but one whose items cannot be changed after it is created.
14.8 Operator Precedence
In 14.2.1 the bill was worked out in two separate lines, one for the books and one for the bag charge. Written as a single line it would look like this:
total = 180 * 3 + 10Two operators, one line. Python has to decide which one to do first, and the two choices give very different bills. Multiplying first gives 550. Adding first gives 2340.
The order in which Python works through the operators in an expression is called operator precedence. It is not a matter of guessing; the order is fixed, and it is the same order you already use in arithmetic.
The first line follows the normal precedence: multiplication is done before addition, so 540 is worked out first and 10 is added to it. In the second line the brackets force the addition to happen first, and 180 is then multiplied by 13.
Brackets sit at the top of the order. Anything inside them is worked out before it is used anywhere else.
The full order
Table 14.8 lists the operators of this chapter from the one Python does first to the one it does last.
Two things in the table are worth noticing. All the comparison, identity and membership operators sit together on one level, and all of them come before the logical operators. That is exactly what a condition such as total > limit and is_member needs: both comparisons finish first, and and then combines their answers.
The three logical operators are not on one level either. not acts first, then and, then or. This explains the two lines from 14.3.3, and it shows up again here.
In the first line, False and False is worked out first, giving False, and True or False then gives True. The brackets in the second line reverse the order, and the answer flips.
When two operators are on the same level, Python works through them from left to right. So 20 - 8 - 5 gives 7, because the first subtraction is done first.
14.9 Kinds of Statements in Python
Everything in this chapter has been built out of expressions — pieces of code that produce a value. But an expression on its own is not yet a program. 180 * 3 produces 540 and then, unless something is done with it, that 540 goes nowhere.
What a program is really made of is statements. A statement is one complete instruction that Python can carry out.
An expression is usually a part of a statement. In total = 180 * 3, the expression is 180 * 3 and the statement is the whole line, which works out that expression and stores the result.
The kinds you will meet
Python has several kinds of statements, and Table 14.9 sets out the ones that make up a Class 9 program. Two of them you have already been writing all through this chapter. The rest are taken up in the chapters that follow.
The last three are different in shape from the first three. Each has a header line that ends in a colon, followed by an indented block of lines underneath it. A statement built this way is often called a compound statement, because it contains other statements inside it. Conditional and loop statements are covered in the next chapter, and function definitions in the one after that.
A line that does nothing
An expression can stand alone as a statement. Python will work it out — and then throw the answer away.
Both lines were carried out. The first one added the numbers and discarded the result, so nothing appeared. Only the second line, which passes the value to print(), produced anything on the screen.
Comments are the one thing in a program file that is not a statement at all. Anything after a # on a line is ignored by Python entirely; it is there for the person reading the program.
Every statement in Table 14.9 leans on the operators from this chapter. An assignment needs =. A conditional needs a comparison. A loop usually needs both a comparison and an augmented assignment to move it along. That is why operators come first, and why the next chapter can move straight on to making decisions.
14.10 Summary
- An operator is a symbol or keyword that tells Python to perform an operation. The values it works on are called operands, and a combination of values, variables and operators that produces a single value is called an expression.
- Arithmetic operators do calculations. Python has seven of them:
+,-,*,/,//,%and**. - If both operands of an arithmetic operator are whole numbers, the answer is a whole number. If either operand is a decimal, the answer is a decimal.
/is ordinary division and always gives a decimal answer.//is floor division: it divides and rounds down to a whole number.%gives the remainder after division. Dividing by zero stops the program.**raises a number to a power. A power of0.5gives a square root, and a negative power gives a decimal answer.- The meaning of an operator depends on the types of its operands. On text and lists,
+joins and*repeats. - Logical operators combine yes-or-no values.
andgivesTrueonly when both operands areTrue;orgivesTruewhen at least one is;notreverses a single value. - The assignment operator
=stores the value on its right into the variable on its left. The right side is always worked out first. - Augmented assignment operators such as
+=and*=update a variable using the value it already holds, writing the variable’s name only once. - Comparison operators compare two values and always answer
TrueorFalse. There are six:==,!=,>,<,>=and<=. =stores a value;==asks whether two values are equal. Comparisons on text are exact, so capital and small letters are treated as different.- Identity operators,
isandis not, ask whether two names refer to the same object in memory. Two separate objects can be equal without being identical. - Membership operators,
inandnot in, check whether a value appears inside a sequence such as a list, a tuple or a string. - Operator precedence is the fixed order in which Python works out an expression: brackets first, then
**, then multiplication and division, then addition and subtraction, then the comparison, identity and membership operators, thennot,andandor. Brackets make the intended order plain. - A statement is one complete instruction. Assignment, augmented assignment and expression statements were used throughout this chapter; conditional statements, loops and function definitions are built the same way and come later.
14.11 Key Terms
- Arithmetic operator: an operator that performs a calculation, such as
+,-,*,/,//,%or**. - Assignment operator: the
=sign, which stores the value on its right into the variable named on its left. - Augmented assignment: a shorter assignment operator such as
+=or*=that updates a variable using the value it already holds. - Comparison operator: an operator that compares two values and gives back
TrueorFalse. - Concatenation: joining two pieces of text end to end using the
+operator. - Exponent operator: the
**operator, which raises the left operand to the power of the right operand. - Expression: a combination of values, variables and operators that Python evaluates to produce a single value.
- Floor division: division using
//, which divides and then rounds the answer down to a whole number. - Identity operator:
isoris not, which checks whether two operands refer to the same object in memory. - Logical operator:
and,orornot, which combines or reversesTrueandFalsevalues. - Membership operator:
inornot in, which checks whether a value appears inside a sequence such as a list, a tuple or a string. - Modulus: the
%operator, which gives the remainder left after dividing the left operand by the right operand. - Operand: a value or variable that an operator works on.
- Operator: a symbol or keyword that tells Python to perform an operation on one or more operands.
- Operator precedence: the fixed order in which Python works out the operators in an expression when more than one is present.
- Statement: one complete instruction that Python can execute, such as storing a value, printing something or making a decision.
14.12 Objective Questions
(What is the value of
7 // 2in Python?)- 3.5
- 3
- 4
- 3.0
(Fill in the blank: The operator that gives the remainder left after a division is called the ________ operator.)
(State whether true or false: In Python, the expression
5 == 5.0givesTrue.)(Which operator checks whether a value appears inside a list?)
isin==and
(Match each operator in Column A with the correct description in Column B.)
Column A Column B a. **i. Reverses a TrueorFalsevalueb. !=ii. Updates a variable using the value it already holds c. +=iii. Raises a number to a power d. notiv. Checks whether two values are different (What will the following line display?
print("ab" * 3))(What will the following line display?
print(2 + 3 * 4))
14.13 Concept Questions
(Define the terms operand and expression. Give one example of each.)
(Differentiate between
=and==, using one example of each.)(Explain the difference between the
/operator and the//operator. Give one example that shows how their results differ.)(Give a reason why
4 ** 0.5gives2.0and not2.)(Differentiate between
==andis. Describe one situation in which two values are equal but not identical.)(What would happen if the line
total = total + 120were run before any value had been stored intotal? Name the error Python would report and state how it can be avoided.)(Write a Python program that stores the price of one notebook and the number of notebooks bought, adds a packing charge of ₹15, and prints the amount to be paid.)
14.14 Case-Based Questions
(The canteen at a school has started a token counter run by two students of Class 9. The prices are given in the table below.)
Item Price Samosa ₹15 Sandwich ₹40 Juice ₹25 (A student buys two samosas and one juice. The canteen gives a free bottle of water to anyone whose bill comes to ₹100 or more.)
- Write a Python statement that uses arithmetic operators to work out this student’s total and store it in a variable named
total. - Write the condition the program should use to decide whether the free water bottle is given, and state which comparison operator you have chosen and why. Does this student qualify?
- The number of samosas is read from the user with
input()and used in the calculation without being converted. Explain what goes wrong, and give the correction.
- Write a Python statement that uses arithmetic operators to work out this student’s total and store it in a variable named
(The school library keeps a list of the students who have taken membership this term.)
members = ["Anita", "Rohan", "Farida", "Meera"](A reader is allowed to keep a book for an extra week if the reader is a member and has no pending fine.)
- Write a Python statement that uses a membership operator to check whether a reader named
readeris on the list. - The variable
has_fineholdsTruewhen a fine is pending. Write the condition that decides whether the extra week is allowed, and name the logical operators you have used. - A genuine member types her name as
anitaat the counter, and the program reports that she is not a member. Explain why this happens, and suggest how the program should handle names typed by a person.