Arithmetic with Integers and Floats


In [73]:
1.0/2.0


Out[73]:
0.5

* Note that python 2.7 cannot divide integers, only floating point values


In [75]:
1/2


Out[75]:
0

In [2]:
1 * 3


Out[2]:
3

In [3]:
3 ** 4


Out[3]:
81

In [4]:
2 + 3 * 5 + 5


Out[4]:
22

In [5]:
(2 + 3) * (5 + 5)


Out[5]:
50

In [7]:
4 % 2.0


Out[7]:
0.0

In [9]:
5 % 2.0


Out[9]:
1.0

Variables


In [10]:
var = 2

In [11]:
var


Out[11]:
2

In [12]:
x = 2.0
y = 3.0

In [13]:
x + y


Out[13]:
5.0

In [14]:
x = x + x

In [15]:
x


Out[15]:
4.0

In [16]:
x = x + x

In [17]:
x


Out[17]:
8.0

In [18]:
name_of_var = 12

Strings


In [21]:
'single'


Out[21]:
'single'

In [22]:
"double"


Out[22]:
'double'

In [23]:
"h'rothgar"


Out[23]:
"h'rothgar"

In [24]:
x = 'hello'

In [25]:
x


Out[25]:
'hello'

In [26]:
print(x)


hello

String formatting


In [29]:
num = 13
name = 'Ian'

In [34]:
print('my num is {one} and my name is {two}, which {two} {one} cool'.format(one=num,two=name))


my num is 13 and my name is Ian, which Ian 13 cool

Indices: strings, lists, nested lists


In [42]:
s = 'abcdefghijk'

In [46]:
s[3:6]


Out[46]:
'def'

In [76]:
[1, 2, 3]


Out[76]:
[1, 2, 3]

In [50]:
my_list = ['sam', 'i', 'am']

In [52]:
my_list.append('d')

In [53]:
print(my_list)


['sam', 'i', 'am', 'd']

In [54]:
my_list[2:4]


Out[54]:
['am', 'd']

In [58]:
my_list[3] = "mf'ers"

In [59]:
my_list


Out[59]:
['sam', 'i', 'am', "mf'ers"]

In [60]:
nest = 'you', 'goat', 'mouthed', my_list

In [61]:
nest


Out[61]:
('you', 'goat', 'mouthed', ['sam', 'i', 'am', "mf'ers"])

In [68]:
print(nest[0:3], nest[3][3])


(('you', 'goat', 'mouthed'), "mf'ers")

In [69]:
nest = [1,2,3,[4,5,['hell yeah']]]

In [72]:
print(nest[3][2][0])


hell yeah

In [ ]: