# Python things can be combined using arithmetic operators
1 + 1
# The result of arithmetic operations can be assigned to variables
x = 10. - 5.3
y=5
z=x+y
z
# The result of an arithmetic operation WILL NOT CHANGE the variable contents
x*2
# as you can see here
x
# To change the value of a variable, we assign to it a new value
x = x * 2
x
# An example of floating point division
4.3 / 2.0
# An example of division of integers
16 / 6
# Integer division
16 // 6
# An example of "modulus" or "mod" or "finding the remainder"
4 % 3
# What is the answer?
8 % 3
# How about now?
10 % 4
# How about now?
10 % 2
# And now?
143 % 5
x = 1
x = x + 1
x
x = 1
x += 1 # This is equivalent to "x = x + 1"
x
x = 10
x -= 5
x
x = 60
x *= 50
x
x = 100
x /= 10
x
type(x)
x = 100
x //= 10
x
=
and ==
¶x = 5
: Assign the value 5
to the variable x
x==5
: Is True
if x
equals to 5
already and is False
otherwise
x = 5
x == 5
x == 7
x = 7
x == 7
7 == x
7 = x
# Order of operations matters!
# *, /, % happen first, in the order they appear
1 + 3 * 20 % 7
# Booleans can be combined using logical ands
# The result of logical and is True if and only if both sides are True
True and False
#Is True==True and False==True?
# Booleans can be combined using logical ors
# The result of logical or is True if either side is True
True or False
#is True==True or False==True?
# You can put operations that result in booleans and combine them
(1 == 1) and (2 == 2)
# Logical negation changes True to False
not True
# and False to True
not False
not (1 == 1)
# Adding strings is called "concatenation"
'Hello' ' '+ 'world!'
# Aside: can you have a 1-element list?
len([27])
# Aside: can you have a 0-element list?
len([])
# Adding list is also called "concatenation"
[0] + [1]
x = [0]
y = [1]
x + y
# Errors occur when something you do that isn't allowed
# Remember: True is supposed to be capitalized!
true
# Variables must be assigned before being used
print(boaz)
# Once you assign a variable you can use it
boaz = 7
print(boaz)
# Dividing by 0 is undefined and thus an error in Python
1 / 0
Different types of parenthesis (
,)
.. [
,]
... {
,}
L = [1,2,3] # list
L[1]
(2+3)*4 , 2+3*4
print("hello")