Python Operator Exercises

Let’s check out some exercises that will help understand Python Operators better.

Exercise 18-a: Assignment Operator =

Let's start with the most basic . Assign a list of colors to the variable: ["yellow", "white", "blue"]


Assignment operator is:

=

Assignee (variable) goes to the left of the = sign and value(s) being assigned goes to the right of the = sign.

lst = ["yellow", "white", "blue"]

Exercise 18-b: Division Operator /

Assign division of a to b to the variable.


Division operator which is an arithmetic operator is: /
result=a/b

Exercise 18-c: Addition using +=

Add 100 to the variable using +=.


You can use arithmetic assignment operator +=

vertical_speed+=100

Exercise 18-d: Modulus %

Using modulus operator assign the remainder of 1000/400 to the variable.


You can use the modulus operator to get a division remainder:
%
remainder=1000%400

Exercise 18-e: Power Operator **

Using power operator **, assign the square of 11111 to the variable.


You can use ** power operator as below:

**2
p_result=11111**2

Exercise 18-f: Comparison Operator ==

Write a conditional statement (if else) which checks the first element in a list (index zero) and assign answer_1 to "Yes" if the first element is equal to "strawberry" else it should assign answer_1 to "No".


In conditional statements comparison operator == is used to check if 2 values are equal.

==

lst[0] can be used to access the first element of the list.

if else structure can be used to create a conditional statement.

Please refer to this lesson if you need to refresh your knowledge of conditional statements (if, elif, else): Conditional Statements

if lst[0]=="strawberry":
    answer_1="Yes"
else:
answer_1="No"