📖
Introduction to programming with Python
  • Introduction to programming with Python 3
  • Preface
    • Common misconceptions about learning to program
    • The Hard truth about learning to program
    • Personal qualities for success
  • 1 - The way of the program
    • Python Programming Language
    • The first program
    • What is debugging?
    • Glossary
    • Exercises
  • 2 - Variables, expressions and statements
    • Values, types and variables
    • Common Built-in data types
    • Expressions
    • Code Format
    • Debugging
    • Glossary
    • Exercises
  • 3 - Functions
    • Python "built-in" Functions
    • Math Functions
    • Composition
    • User Defined Functions
    • PEP 8 Recommendations
    • Debugging
    • Glossary
    • Exercises
  • 4 - Conditionals
    • Boolean expressions
    • Conditional statements
    • PEP 8 Recommendations
  • 5 - Iteration
    • The while statement
    • The for statement
    • break and continue statements
    • Nested Loops
    • PEP 8 Recommendations
    • Debugging
    • Glossary
    • Exercises
  • 6 - A short introduction to testing: Building Reliable Software
  • 7 - A deeper dive into Strings, Lists and Tuples
    • More on Strings
    • More on Lists
    • More on Tuples
    • Debugging
    • Glossary
    • Exercises
  • 8 - A deeper look at Functions in Python
    • Function Preconditions and Postconditions
    • Positional and Keywords arguments
    • Nested Functions
    • Scope of a variable
    • Recursion
    • Functions' side effects
    • Glossary
    • Exercises
  • 9 - Code Documentation
    • Basics of Commenting Code
    • Documenting Code via Python Docstring
  • 10 - Sets and dictionaries
    • Sets
    • Dictionaries
    • Which data structure should I use?
    • Debugging
    • Glossary
    • Exercises
  • 11 - File I/O
    • Read/Write to a file
    • File management
    • Debugging
    • Glossary
    • Exercises
  • 12 - Handling Errors and Exceptions
  • 13 - Writing modules
  • 14 - Classes
    • Classes and Objects
    • Classes and Functions
    • Classes and Methods
    • Pythonic Magic: Understanding and Implementing Dunder Methods
    • Glossary
    • Exercises
  • 15 - Python's Type Hinting
  • Acknowledgements
Powered by GitBook
On this page
  • Operators and operands
  • Expressions
  • Order of operations
  • Modulus operator
  • String operations
Edit on GitHub
  1. 2 - Variables, expressions and statements

Expressions

PreviousCommon Built-in data typesNextCode Format

Last updated 1 year ago

Operators and operands

Operators are special symbols that represent computations like addition and multiplication. The values the operator is applied to are called operands. The operators +, -, *, / and ** perform addition, subtraction, multiplication, division and exponentiation, as in the following examples:

20 + 32 
hour - 1 
hour*60 + minute 
minute/60 
5**2 
(5+9) * (15-7)

When a variable name appears in the place of an operand, it is replaced with its value before the operation is performed.

Python 3, as well as other languages have an integer division //. The integer division operates floor division, that is the operator returns the whole part of the division. The result of the integer division is an integer. If both operands are integer the value returned is a integer. Otherwise, if at least one of the operand is a float, the value returned is a float.

>>> minute = 59 
>>> minute // 60
0
>>> minute = 61 
>>> minute // 60
1 
>>> minute // 60.0
1.0 
>>> minute / 60.0
1.0166666666666666 

Expressions

An expression is a combination of values, variables, and operators. A value all by itself is considered an expression, and so is a variable, so the following are all legal expressions (assuming that the variable x has been assigned a value):

17
x 
x + 17

If you type an expression in interactive mode, the interpreter evaluates it and displays the result:

>>> 1 + 1
2

But in a script, an expression all by itself doesn't do anything! This is a common source of confusion for beginners.

Exercise

Type the following statements in the Python interpreter to see what they do:

5 
x = 5 
x + 1

Now put the same statements into a script and run it. What is the output? Modify the script by transforming each expression into a print statement and then run it again.

You can use a variable in an expression, and you can assign the result of an expression to a variable. In addition, a variable can be used on both side of the assignment operator = like in x = x + 1. In this assignment statement the result of x + 1 is assigned to x. For example, if x has the value 1 before the assignment statement is executed, then x becomes 2 after the statement is executed.

Order of operations

When more than one operator appears in an expression, the order of evaluation depends on the rules of precedence. For mathematical operators, Python follows mathematical convention. The acronym PEMDAS is a useful way to remember the rules:

  1. 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 if it doesn't change the result.

  2. Exponentiation has the next highest precedence, so 2**1+1 is 3, not 4, and 3*1**3 is 3, not 27.

  3. Multiplication and Division have the same precedence, which is higher than Addition and Subtraction, which also have the same precedence. So 2*3-1 is 5, not 4, and 6+4/2 is 8, not 5.

  4. Operators with the same precedence are evaluated from left to right. So in the expression degrees/2*pi, the division happens first and the result is multiplied by pi, therefore the expression is equal to Ī€2degrees\frac{\pi}{2}degrees2Ī€â€‹degrees. To divide by 2Ī€2\pi2Ī€, you can use parentheses (e.g. degrees/(2*pi) or write degrees/2/pi.

Operator
Description

**

Exponentiation (raise to the power)

+, -

Unary plus and minus (method names for the last two are +@ and -@)

*, /, %, //

Multiply, divide, modulo and floor division

+, -

Addition and subtraction

>>, <<

Right and left bitwise shift

<=, <, >, >=

Comparison operators

<>, ==, !=

Equality operators

is, is not

Identity operators

in, not in

Membership operators

not

Logical operator

and

Logical operator

or

Logical operator

= ,%=, /=, //=, -=, +=, *=, **=

Assignment operators

Modulus operator

The modulus operator works on integers and yields the remainder when the first operand is divided by the second. In Python, the modulus operator is a percent sign (%). The syntax is the same as for other operators:

>>> quotient = 7 // 3 
>>> print(quotient)
 2 
>>> remainder = 7 % 3 
>>> print(remainder) 
 1 

So 7 divided by 3 is 2 with 1 left over, or in other words 7=3×2+17=3\times 2 + 17=3×2+1.

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

The modulus operator is very useful in determining if a integer x is an even number or an odd number. If x%2 is equal to 0 then x is an even number, odd otherwise.

String operations

In general, you cannot perform mathematical operations on strings, even if the strings look like numbers, so the following are illegal:

'2'-'1' 
'eggs'/'easy' 
'third'*'a charm'

The + operator works with strings, but it might not do what you expect: it performs concatenation, which means joining the strings by linking them end-to-end. For example:

first = 'throat' 
second = 'warbler' 
print(first + second) 

The output of this program is throatwarbler.

The * operator also works on strings; it performs repetition. For example, 'Spam'*3 is 'SpamSpamSpam'. If one of the operands is a string, the other has to be an integer. This use of + and * __ makes sense by analogy with addition and multiplication. Just as 4*3} is equivalent to 4+4+4, we expect 'Spam'*3 to be the same as 'Spam'+'Spam'+'Spam', and it is.

On the other hand, there is a significant way in which string concatenation is different from integer addition. Concatenation is not commutative, which means that 'spam'+'sandwich' is not the same as 'sandwich'+'spam', whereas 3+4 is the same as 4+3.

In mathematics, x=2∗x+1x = 2 * x + 1x=2∗x+1 denotes an equation (where x=−1x = -1x=−1 is the solution). In python, it is an assignment statement, and if x has the value 1 before the statement, executing the statement would change x to 3.

â„šī¸