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

Key Takeaways

  1. An Expression is a combination of values (or variable, operators, calls to functions - you will learn about them soon) which evaluates to a certain value, e.g., 1 + 2 
  2.  Operators are special symbols or keywords which are able to operate on the values and perform (mathematical) operations, e.g., the * operator multiplies tow values: x * y
  3.  Arithmetic operators in Python: + (addition), - (subtraction), * (multiplication), / (classic division - always returns a float), % (modulus - divides left operand by right operand and returns the remainder of the operation, e.g., 5 % 2 = 1 ), ** (exponentiation - left operand raised to the power of right operand, e.g.d, 2 ** 3 = 2 * 2 * 2 = 8). // (floor/integer division - returns a number resulting from division - returns a number resulting from division, but rounded down to the nearest whole number, e.g., 3 ?? 2.0 = 1.0)
  4. unary operator is an operator with only one operand, e.g., -1, or +3
  5. binary operator is an operator with tow operands, e.g., 4 + 5, or 12 % 5
  6. Some operators act before others - the hierarchy of priorities:
    • ​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​the ** operator (exponentiation) has the highest priority;
    • then the unary + and - (note: a unary operator to the right of the exponentiation operator binds more strongly, for example: 4 ** -1 equals 0.25
    • then *, /, //, and %
    • and, finally the lowest priority: the binary + and -
  7. Subexpressions in parentheses are always calculated first, e.g., 15 -1 * (5 * (1 + 2)) = 0
  8. The exponentiation operator uses right-sided binding, e.g., 2 ** 2 ** 3 = 256

​​​​​​​Exercise 1

What is the output of the following snippet? 

print((2 ** 4), (2 * 4.), (2 * 4))

16 8.0 8

Exercise 2

What is the output of the following snippet? 

print((-2 /4), (2 / 4), (2 // 4), (-2 //4))

-0.5 0.5​ 0 -1

Exercise 3

What is the output of this snippet?

print((2 % -4), (2 % 4), (2 ** 3 ** 2))

​​​​​-2 2 512

What are variables? 

It seems fairly obvious that Python should allow you encode literals carrying number and text values.

You already know that you can do some arithmetic operations with these numbers: add, subtract, etc. You'll be doing that many times.

But it's quite a normal question to ask how to store the results of these operations, in order to use them in other operations, and so on.

How do you save the intermediate results, and use them again to produce subsequent ones? 

Python will help you with that. It offers special "boxes" (containers) for that purpose, and these boxes are called variables - the name itself suggests that the content of these containers can be varied in (almost) any way. 

What does every Python variable have? 

  • a name
  • a value (the content of the container)

Let's start with the issues related to a variable's name. 

Variables do not appear in a program automatically. As developer, you must decide how many and which variables to use in your programs. 

You must also name them. 

If you want to give a name to a variable,  you must follow some strict rules:

  • the name of the variable must be composed of upper-case or lower-case letters, digits, and the character _ (underscore)
  • the name of the variable must begin with a letter
  • the underscore character is a letter
  • upper- and lower-case letters are treated as different (a little differently than in the real world - Alice and ALICE are the same first names, but in Python they are two different variable names, and consequently, two different variables
  • the name of the variable must not be any of Python's reserved words (the keywords - we'll explain more about this soon).