In [41]:
from IPython.core.display import HTML
def css_styling():
styles = open("styles/custom.css", "r").read()
return HTML(styles)
css_styling()
Out[41]:
In [42]:
123+321
Out[42]:
In [43]:
4*5.5
Out[43]:
In [44]:
2**5
Out[44]:
In [45]:
3**4
Out[45]:
In [46]:
81**0.5
Out[46]:
In [47]:
3+8*8
Out[47]:
In [48]:
str(4**4)
Out[48]:
In [49]:
len(str(4**4))
Out[49]:
Division: be careful dividing integers - the result is an integer
In [50]:
# This rounds down (floor function)
float(20)/8
Out[50]:
In [51]:
20 // 8
Out[51]:
In [52]:
from __future__ import division
print 20 / 8
print 20 // 8
Warning: integer division gives back intergers. This may not be what you want.
In [53]:
20/8.0
Out[53]:
In [54]:
20/8.0
Out[54]:
In [55]:
20//8.0
Out[55]:
In [56]:
20 % 8.0
Out[56]:
In [57]:
divmod(20,8.0)
Out[57]:
In [58]:
from math import *
print pi
In [59]:
who
In [60]:
import math
pi=math.pi
print pi
print math.pi
In [61]:
pi=float('{:.5e}'.format(math.pi))
pi
Out[61]:
In [62]:
print('{:.5e}'.format(math.pi))
In [63]:
math.sqrt(81.0)
Out[63]:
In [64]:
import random
random.random()
Out[64]:
In [65]:
random.choice(range(1,11))
Out[65]:
In [66]:
x=10
print(bin(x))
print x.bit_length()
In [67]:
x=3.5
print x.as_integer_ratio()
In [68]:
print int(x)
In [69]:
y=float(42)
print y
In [70]:
y.is_integer()
Out[70]:
In [71]:
y=5.5
y.is_integer()
Out[71]:
In [72]:
y.hex()
Out[72]:
In [73]:
# generate a list of 5 random numbers fom 0 to 99
random.sample(xrange(0,100),5)
Out[73]:
In [74]:
[random.random() for _ in range(0, 10)]
Out[74]:
In [75]:
x = [random.randint(0,1000) for _ in range(10)]
print x
In [75]:
In [76]:
random.choice(x)
Out[76]:
In [77]:
print range(10)
print range(0,10)
print range(5,16)
In [78]:
# this is a complex number - notice the number before the the imaginary number j
a = 1 + 1j
In [79]:
# this will not work since j is not defined.
a = 1 + j
In [82]:
b=1-2j
In [83]:
c=complex(1,3)
print c
In [84]:
a*b
Out[84]:
In [85]:
a-3*b
Out[85]:
In [86]:
a/b
Out[86]:
In [87]:
print c
print c.conjugate()
In [88]:
c.real
Out[88]:
In [89]:
c.imag
Out[89]:
A Python list is a container that stores sequence of elements. These elements need not be the same type. You can have strings stored alongside integers and other lists and data types
Note: Python is zero-based indexing: it starts indexing (lists, strings, etc) at zero just like C/C++/Java
In [90]:
# declare a list without initilization
my_list = range(5,15)
print my_list
In [91]:
# list slicing - start from index 1 to index 3 inclusive
my_list[1:4]
Out[91]:
In [92]:
# empty list declaration so you can add elements to it
my_list = []
In [93]:
len(my_list)
Out[93]:
In [94]:
my_list.append(10)
my_list.append(20)
print my_list
In [95]:
my_list.append([1,2,3])
print my_list
In [96]:
print my_list[2][1]
In [97]:
print my_list[3]
In [98]:
# get the length of the list
print len(my_list)
In [99]:
my_list.insert(2,5)
print my_list
In [100]:
# insert 7 before index 9. Since the list is not big enough, 7 gets appended
# to the end of the list
my_list.insert(9,7)
print my_list
In [101]:
# insert 9 before index 3
my_list.insert(3,9)
print my_list
In [102]:
print len(my_list)
In [103]:
# access list elements directly by indexing the list
print my_list[0]
print my_list[1]
print my_list[2]
In [104]:
# remove the element at index 3 from the list
my_list.pop(3)
print my_list
In [105]:
# remove the first occurence of 5 from the listmy_list.remove(5)
print my_list
In [106]:
# get the last element of the list
print my_list[-1]
# or
print my_list[len(my_list)-1]
In [107]:
# sorting a list
my_list=['x', '1', 3, 'z', 'a']
my_list.sort()
print my_list
my_list.reverse()
print my_list
In [108]:
# lists containing other lists
my_list1 = range(0,5)
my_list2 = range(6,10)
my_list3 = [my_list1, my_list2]
print my_list3
print len(my_list3)
In [109]:
print len(my_list3[1])
In [110]:
my_list3[0]
Out[110]:
In [111]:
print len(my_list3[0])
In [112]:
my_list3[1][2]
Out[112]:
In [145]:
M=[[1,2,3],
[4,5,6],
[7,8,9]]
print M
In [114]:
# put the 2nd column of M in a list
column = []
for row in M:
column.append(row[1])
print column
In [116]:
# list comprehension - another way of extracting the 2nd column of M
column = [row[1] for row in M]
print column
In [117]:
# compute the transpose of the matrix M
[[row[i] for row in M] for i in range(3)]
Out[117]:
In [122]:
# get the diagonal elements of M
diag = [M[i][i] for i in [0, 1, 2]]
print diag
In [125]:
# we can do operations on the new list elements before populating the list
repeat_char = [c * 2 for c in 'spam']
print repeat_char
print ''.join(repeat_char)
In [126]:
# build a list with another list as elements
[[x ** 2, x ** 3] for x in range(4)]
Out[126]:
In [144]:
# build a list with an if statement
[[x, x/2, x*2] for x in range(-6, 7, 2) if x > 0]
Out[144]:
In [131]:
# does the same thing as above but more
big_list = []
for x in range(-6,7,2):
if x > 0:
big_list.append([x, x/2, x*2])
print big_list
In [130]:
# does the same as above but lots of code
big_list = []
for x in range(-6,7,2):
lil_list = []
if x > 0:
lil_list.append(x)
lil_list.append(x/2)
lil_list.append(x*2)
big_list.append(lil_list)
print big_list
In [139]:
range(-6, 7, 2)
Out[139]:
In [133]:
# this is a generator - we'll talk more about this in another lecture
G = (sum(row) for row in M)
print next(G)
print next(G)
print next(G)
In [134]:
map(sum, M)
Out[134]:
In [ ]:
In [ ]:
In [118]:
for rand_int in [random.random() for _ in range(0, 10)]:
print rand_int
In [119]:
random_integers = [random.random() for _ in range(0, 10)]
for random_int in random_integers:
print random_int
Note: for large lists, use xrange for iterating though the list and use range for creating the list.
In [120]:
random.sample(range(0,100),10)
Out[120]:
In [121]:
# generate a list of integers from -5 to 9 by 2
range(-5,10,2)
Out[121]:
In [135]:
list1 = random.sample(range(0,100),10)
In [136]:
list1
Out[136]:
In [137]:
sum(list1)/10
Out[137]:
In [138]:
range(-6, 7, 2)
Out[138]:
In [ ]: