Operations with numbers


In [1]:
21 * 21 #Returns int


Out[1]:
441

In [2]:
21 * 21.0 #Returns float


Out[2]:
441.0

In [3]:
21/3 #Returns float (division always returns float)


Out[3]:
7.0

In [4]:
21//3 #Returns int


Out[4]:
7

In [5]:
21//3.0 #Returns float


Out[5]:
7.0

In [6]:
21//3.2 #Returns float whose value is the lower integer bound of 21//3.2


Out[6]:
6.0

In [7]:
21%4 #Returns int (the remainder of dividing 21 by 4)


Out[7]:
1

In [13]:
print(21%4.4) #Returns float (21 - 21//4.4) this is wrong, should be (21 - 21 // 4.4 * 4.4)
print(21 - 21 // 4.4 * 4.4)


3.3999999999999986
3.3999999999999986

In [14]:
21**4 #Returns int


Out[14]:
194481

In [16]:
21**4.0 #Returns float


Out[16]:
194481.0

String operations

Indexing


In [ ]:
x = "Always take a banana to a party!"

In [ ]:
x[3] #Returns the 4th (i+1)th character in x

In [ ]:
x[-1] #Returns the last character in x

In [ ]:
x[-2] #Returns the second last character in x

In [ ]:
x[32] #IndexError. Out of range

In [ ]:
len(x) #Returns the length of the string (an integer)

Slicing


In [ ]:
x[7:11] #Returns the 8th to the 11th character (i.e., 11-7 characters)

In [ ]:
x[7:] #Returns every character starting with location 7 (the 8th character) to the end of the string
#Omitting the endpoint defaults to the "rest of the string"

In [ ]:
x[0::2] #returns a substring starting from 0, going to the end (omitted), 2 characters at a time

In [ ]:
x[::-1] #Start from whatever makes sense as the start, go to whatever makes sense as the end, go backward
# one character at a time
#Here it makes sense to start at the end and go all the way to the beginning (because of the -1)
#Returns a reversed string

Searching


In [ ]:
x.find('to') #Returns the location of the first 'to' found

In [ ]:
x.find('hello') #Returns -1. I.e., the substring was not found in x

Immutability


In [ ]:
x[3] = 'b' #TypeError. Can't change a string

In [ ]:
x = "Hello"
y = x
print(id(x),id(y))
#x and y are the same string

In [ ]:
x="Always take a banana to a party!"
print(id(x),id(y))
#x now points to a different string. y is still the same old string at the same old location!

Concatenation


In [ ]:
#The plus operator concatenates two strings to give rise to a third string
x="Hello"
y="Dolly"
z=x+y
print(x,y,z,id(x),id(y),id(z)) #x, y and z are all different strings

In [ ]:
#Since python doesn't understand that we need a space between Hello and Dolly, we need to add it ourselves
z = x + " " + y
print(z)

booleans


In [ ]:
x=4
y=2
z=(x==y) #False because x and y are not the same
z=(x==x) #True because x and y are the same

In [ ]:
#Comparison is by value
x = "Hello"
y = "Hello"
print(id(x),id(y)) #Different strings

In [ ]:
print(x == y) #But they have the same value

In [ ]:
x=8
print(x,bool(x)) #Non-zero numbers, strings with values are always True

In [ ]:
x='' #Empty string
print(x,bool(x)) #Empty strings, 0 numbers are always False

Logical operators


In [ ]:
x=4
y=5
print(x>2 and y>2) #True because both x and y are greater than 2

In [ ]:
print(x>2 and y<2) #False because one is False

In [ ]:
print(x<2 and y<2) #False because both are False

In [ ]:
print(x>2 or y>2) #True because at least one is True

In [ ]:
print(x<2 or y>2) #True because at least one is True

In [ ]:
print(x<2 or y<2) #False because both are False

In [ ]:
print(not(x>2 or y>2)) #False because x>2 or y>2 is True

In [83]:
print(x or y) #4 because x is True (non-zero) so no need to evaluate y. Value of x is returned
print(0 or y)


11
4

In [85]:
print(x and 0+3) #3 because x is non-zero but need to check the second operand as well. That evaluates to 3
print(0 and x)
print(False and x)


3
0
False

Assignment


In [ ]:
x = p + 10 #NameError! p is not defined

In [ ]:
p = 7
x = p + 10 #This works

In [86]:
x,y = 3,4
print(x,y) #x is 3 and y is 4
x,y = y,x
print(x,y)


3 4
4 3

In [ ]:
x = 7
x += 4 
print(x) #x is 11 because x += 4 is equivalent to x = x+4

In [ ]: