Operators

Hands On Exercise

Explore Python operators through practical exercises that reinforce your understanding of operator types, precedence, and bitwise operations. Test and deepen your coding skills by predicting outputs and analyzing code behavior.

Congratulations on completing this lesson on Python operators. Test your knowledge through these short snippets and get one step closer to mastering Python!

Exercise 1

Guess the results of performing the following operations and then run the code below to see the actual output!

a = 10 a += 5 print(a) b = 20 b -= a print(b) print (a < b)

Exercise 2

Question: What will the result of performing a single left shift on 0101010 be?

The bits will be shifted by one position to the left, and a zero will be appended at the end of the sequence. Therefore, the left shift on “0101010” will result in “1010100”.

Exercise 3

Question: What is the difference between the = and the == operators?

The = operator is the assignment operator, and it equates the value on the right side to the variable at the left side. For instance, the expression (a = 10) assigns a value of 10 to a. Conversely, the == operator is a comparison operator which returns True only if the values on both sides are equal.

Exercise 4

Keeping in mind the precedence rules, dry run the expression below. You can find the expression’s answer by running the code and comparing it to your own!

​​mathematical_expression = 2 + (20 - 5) // 5 print(mathematical_expression)
On this page

    In This Module