This section explains how to use basic operators in Python.
Just as any other programming languages, the addition, subtraction, multiplication, and division operators can be used with numbers.
number = 1 + 2 * 3 / 4.0
Try to predict what the answer will be. Does python follow order of operations?
Another operator available is the modulo (%) operator, which returns the integer remainder of the division. dividend % divisor = remainder.
remainder = 11 % 3
Using two multiplication symbols makes a power relationship.
squared = 7 ** 2 cubed = 2 ** 3
Using Operators with Strings
Python supports concatenating strings using the addition operator:
helloworld = "hello" + " " + "world"
Python also supports multiplying strings to form a string with a repeating sequence:
lotsofhellos = "hello" * 10
Using Operators with Lists
Lists can be joined with the addition operators:
even_numbers = [2,4,6,8] odd_numbers = [1,3,5,7] all_numbers = odd_numbers + even_numbers
Just as in strings, Python supports forming new lists with a repeating sequence using the multiplication operator:
print [1,2,3] * 3
The target of this exercise is to create two lists called
which contain 10 instances of the variables
You are also required to create a list called
big_list, which contains
y, 10 times each, by concatenating the two lists you have created.