CoCalc Shared FilesATMS-305-Content / Week-6 / Week 6 Handouts / Basics.ipynbOpen in CoCalc with one click!
Authors: Andrew Huang, Anna Nesbitt
Views : 6
Description: Jupyter notebook ATMS-305-Content/Week-6/Week 6 Handouts/Basics.ipynb

Names first

In [35]:
import numpy as np import pandas as pd

What's a variable?

In [7]:
x = 'hello' print('hello')
hello
In [13]:
x
'hello'

How to print in Jupyter?

In [25]:
print(x) x
hello
'hello'

Basic types: String? Integer? Float?

In [50]:
astr = 'yellow' another_str = "yellow" aint = 4 another2_str = str(aint) multiple_line_str = """ asgashdsj sdajfdfsjfdjjsfjdsjfsdj fsjfdsjsdfj """ afloat = 10.2
In [49]:
a = 10. b = '45' c = 1337

Higher level types: Tuple? List? Dictionary?

In [9]:
atup = (1, 2) atup2 = 1, 2 atup3 = tuple(atup) alist = [5, 5, 6, 7] adict = {'key1': [6, 3], 'key2': [5, 4]} mypairs = ((1,"Hi there"), (2, "Good-bye")) adict2 = dict(mypairs) adict2
{1: 'Hi there', 2: 'Good-bye'}
In [16]:
atup_of_lists = ([1, 2], (1, 2), adict2) alist_of_tups = [(1, 2), [1, 2]] adict_of_lists_and_tups = {'x': alist, 'y': atup} # NOT ABLE TO SORT

How to check the type? And length!

In [25]:
print(type(atup)) print(type(atup[0])) alist = [(5, 4, [3, 4, 5, 6, 7, 8, 8], 2, 1), 5, 6, 7] print(alist[0][2])
<class 'tuple'> <class 'int'> [3, 4, 5, 6, 7, 8, 8]

Subscript/slice/subselect/reverse

In [104]:
alist = [1, 2, 3, 4, 5] alist_rev = alist[::-1] alist = np.array([[1, 2, 3], [4, 5, 6]]) print(np.shape(alist)) # get dimensions print(alist[1][0]) print(alist[1, 0]) # only works if np array atup_sub = atup[0] alist_sub = alist[0:2] adict_sub = adict['key1'] alist_sub
(2, 3) 4 4
array([[1, 2, 3], [4, 5, 6]])

Other types: np.array?, pd.DataFrame?

In [43]:
nparr = np.array(alist) pddf = pd.DataFrame(adict)

One big difference between np.array and lists

In [47]:
alist = [1, 2, 3, 4] aint = 2 alist_aint = alist * aint nparr_aint = nparr * aint alist_aint, nparr_aint
([1, 2, 3, 4, 1, 2, 3, 4], array([[ 2, 4, 6], [ 8, 10, 12]]))

Format a string!

In [89]:
afloat_fmt = '{0:0.2f} hi {1}'.format(afloat, aint) aint_fmt = '{0:5d}'.format(aint) astr_fmt = '{0}'.format(astr) test_fmt = '{0:50.24f}'.format(np.pi) test_fmt2 = '{0:<050f}'.format(15.2152151251) test_fmt3 = '{0:25d}'.format(15) # order number, colon (syntax), 0 = fill empty with zeros, number after decimal number of decimal places # name format~~~ otherway_fmt = '{anumber:50.24f}, {anumber:25.24f}'.format(anumber=afloat) afloat_fmt # test_fmt, test_fmt2, test_fmt3, otherway_fmt
'10.20 hi 4'
In [96]:
aint = 2 afloat = 3.5 astr = 'hyoelllo' 'random something 2 3.5 hyo' pleasedonthardcode = 'random something 2 3.5 hello' afloat_aint_astr_fmt = 'random something {0} {1} {2}'.format(aint, afloat, astr) afloat_aint_astr_fmt afloat_aint_astr_fmt = \ 'random something {aint} {afloat} {astr}'.format( aint=aint, afloat=afloat, astr=astr) afloat_aint_astr_fmt
'random something 2 3.5 hyoelllo'
In [101]:
'{0:04.2f}'.format(np.pi), '{0:0.2f}'.format(np.pi), \ '{0:07.2f}'.format(np.pi), '{0:0.6f}'.format(np.pi)
('3.14', '3.14', '0003.14', '3.141593')
In [ ]:
In [ ]:
In [ ]:
In [ ]:
In [ ]:
In [ ]:
In [ ]:
In [ ]:
In [ ]: