Loading [MathJax]/jax/output/HTML-CSS/fonts/TeX/fontdata.js

Shortcut operators

It's time for the next set of operators that make a developer's life easier.

Very often, we want to use one and the same variable both to the right and the left sides of the = operator.

For example, if we need to calculate a series of successive values of powers of 2, we may use a piece like this:

x = x * 2

You may use an expression like this if you can't fall asleep and you're trying to deal with it using some good, old-fashioned methods:

sheep = sheep + 1

Python offers you a shortened way of writing operations like these, which can be coded as follows: 

x *= 2

sheep += 1

Let's try to present a general description for these operations. 

If op is a two-argument operator (this is a very important condition) and the operator is used in the following context:

variable = variable op expression

it can be simplified and shown as follows:

variable op= expression

Take a look at the examples below. Make sure you understand them all. 

i = i + 2 * j ⇒ i += 2 * j

var = var / 2 ⇒ var /= 2

rem = rem % 10 ⇒ rem %= 10

j = j - (i + var + rem) ⇒ j -= (i + var + rem)

x = x ** 2 ⇒ x **= 2

LAB

Objectives

  • becoming familiar with the concept of, and working with, variables
  • performing basic computations and conversions 
  • experimenting with Python code

Scenario

Miles and kilometers are units of length or distance. 

Bearing in mind that 1 mile is equal to approximately 1.61 kilometers, complete the program in the editor so that it converts: 

  • miles to kilometers
  • kilometers to miles

Do not change anything in the existing code. Write your code in the places indicated by ### . Test your program with the data we've provided in the source code.

Pay particular attention to what is going inside the print() function. Analyze how we provide multiple arguments to the function, and how we output the expected data. 

Note that some of the arguments inside the print() function are strings (e.g., "miles is", whereas some other are variables (e.g., miles).

TIP

There's one more interesting thing happening there. Can you see another function inside the print() function? It's the round() function. Its job is to round the outputted result to the number of decimal places specified in the parentheses, and return a float (inside the round() function you can find the variable name, a comma, and the number of decimal places we're aiming for). WE're going to talk about functions very soon, so don't worry that everything may not be fully clear yet. We just want to spark your curiosity. 

After completing the lab, open Sandbox, and experiment more. Try to write different converters, e.g., a USD to EUR converter, a temperature converter, etc. - let your imagination fly! Try to output the results by combining strings and variables. Try to use and experiment with the round() function to round your results to one, tow, or three decimal places. Check out what happens if you don't provide any number of digits. Remember to text your programs. 

Experiment, draw conclusions,a dn learn. Be curious.

Expected output

7.38 miles is 11.88 kilometers

12.25 kilometers is 7.61 miles

Shortcut operators

David Khieu
Module by David Khieu, updated more than 1 year ago

Description

Shortcut operators
No tags specified