Breakout: Simple Math in Python

The purpose of this breakout is to help you familiarize yourself with executing Python code in the IPython notebook, and along the way familiarize yourself with some of the mathematical functionality available in Python.

As with many breakouts in this course, you should do the following with a partner. One of you should have your fingers on the keyboard, while the other watches and helps decide what to type.

Some Basic Notebook Navigation

IPython notebooks combine text and executable code: a code cell looks like this, and you can run it by pressing enter/return while holding the shift key. Try it out here:


In [1]:
1 + 1


Out[1]:
2

In [2]:
2 + 3


Out[2]:
5

In [3]:
4 * 5


Out[3]:
20

In [4]:
5 / 7


Out[4]:
0.7142857142857143

Additionally, you'll sometimes want to insert a new cell to add more code. You can do this by using the [+] button which is second from the left at the top of the notebook page. Before you continue, insert a new cell below this one to make sure you see how it works.

Exploring Arithmetic Operators in Python

Python can be used quite straightforwardly as a calculator. Among other commands, it recognizes +, -, *, /, and () with their typical mathematical meanings. With this in mind, we're going to quickly practice using Python as a calculator.

Use Python to solve the following arithmetic problems:

What is $4235 + 3154$?


In [5]:
4235 + 3154


Out[5]:
7389

What is $4235 - 3154$?


In [6]:
4235 - 3154


Out[6]:
1081

What is $4235 \times 3154$?


In [7]:
4235 * 3154


Out[7]:
13357190

What is $\frac{4235}{3154}$?


In [8]:
4235 / 3154


Out[8]:
1.342739378566899

What is $\frac{4235 - 3154}{2752 + 5013}$?


In [9]:
(4235 - 3154) / (2752 + 5013)


Out[9]:
0.13921442369607212

What is $\frac{-(4315 + 378)}{2(65 - 328)}$


In [10]:
- (4315 - 378) / (2 * (65 - 328))


Out[10]:
7.484790874524715

Exploring More Specialized Arithmetic

Some other useful operators are //, %, and **.

What is 4235 // 3154?


In [11]:
4235 // 3154


Out[11]:
1

Try the // operator with some other inputs: can you figure out its meaning?


In [12]:
5 // 3

# This operator is "floor division": it's division while dropping the remainder
# 3 goes into 5 _once_ (with a remainder of two)


Out[12]:
1

What is 4235 % 3154?


In [13]:
4235 % 3154


Out[13]:
1081

Try the % operator with some other inputs: can you figure out its meaning?


In [14]:
5 % 3

# This operator is the "modulus": it computes the remainder after division
# Here it returns the remainder of two


Out[14]:
2

What is 4235 ** 3154?


In [15]:
4235 ** 3154


Out[15]:
1246435640955130144792530912237913145067719374732687133723902482946782520248424287166959029670070195259506533145213002436802807902491168290309313860698516130394664756380857000117465337518240431783860357037295484878151452764254499677265552532183976742554977945915464434418345028864319843367583996565670700304804702433571496408431419478240091003732226943454669343659574634334604981692251531454123547270497587704794664467444323911971825175442832409395371143115654787205941852231980683988501828867075765183154073974083763153911739486958831029872677336665087013948361471146492417887536674964527280598335962645172112864863066819821819082066432023002067568658795326039697217518136881090039902960146268978592249056416898327863966627298569160594948462060650752665247428308885426039666719454504782398107061787152233720684488462440566863590228651250548478756476110257047200246785217178826184172890183100199960763585567601879528678073168030986273035114003664520655017156393127747052661898253342502947473355934541476430148993505088166263929403350889329081470107432542522876303565560808731371448578479245027968993412169103260967466290521440312040777547841244903420213334781745760136067889399293565394540874251054128630352256420741518437188662681915194775589417881755109644088691974378134109456170813900089833159541208630574019117583693917886795343792446593151137663950608171065782490243052064620915385352062571984633685974308864673521486367938935113299935346166000346382583251060703133799227087940690817680624909896154978289101991955026963478874476928459844694500926520810119727143651288691028890271363480138616790043419720171330661885151160661370650552013262078435554282455130922619888660262660763221876479988747990064230961555777820308842127025390269936384120424354448895665039303567782210671895900401198060631988174757455253432225251382909756856804208990889164705836543140083122158644810804064725902668786996743613204428369872387725009874759559088156112160726914242672324890524037745577640917050950375381863654394368155805360306226122755844016063269229446329620819909317050523396756600268668397593173554526594391015404567717486676025614908076001926056605021106231952616968043081324869630506981969720873593620126998269130992453453605648387322456781524861229344103752243037270396834836734484452454432424666379178253209739142842682029129378620433320072860154473865146860957716697909188908880759972322716699031827864695824418055166388041370893748991712413120130753958239097139734871868923768118674016334765896648796121230556313650805467452716675188031862115018505790615774779579143695748244800185267907058152013149073823234834257576052104711864791668938409468847326166282993290802663067192298602569226959212001250957281119305952740417425839435813009725091919458323820128550280310527998383221601953889082435023202341445438035565292699669457575980228324976324142978496674369085935764904684538764552180359207599211405125669757435257899331020329503260046246605529644486531065904073339781669643020024732263448064791082758771776312463358704042857093157453640902343889814532804973932117677624212554344325182602874237571064160113240141909302668194823557980758555671195744515392035872182374864729026167720361114316929652162132598079113688718415324928456696973225503280268561650689858246985570458434175598310831818599827554712853068995250443734654862542560181422259390305771595418312756943011085553956635177737244232997787950644613015213823233702750104184834160002513557088151308913284461441601522988387520393075941491899150921959511662929603724041568598976607113401059666968589582737344667632551392758690087517061435020231873953951784632020362977558598641509115717644015330651960307377522049257988953822943639254432917879157336371782760997986409435922813468054813794716747657933394333978510796827353353998252747519523850942792271630952024065398698489606313193342723405484989278478750002801812110157706048689113190863176956713652521020459607421852379158130722325104156612979745174803576278095375751388917125859175884980542489858387365317448589218789342067172104909321259857523935708226186492024429864559016442401435841717302791863683177038438775767526374819481957233362306614923267314566196292078402641785975274249901848129643447772517666147798980757125041832475862351427457567730999903744485010216985633146667606398863318390016299384490756725354228535400186940411663668978024445614920562078024820970343600120028453166126152377197933377787034462183125886220308045239057841470335199778846219413091510615569863561117283390782569656725268417848088935818517538639042204232892955394259949728471031742888599387024217867998416216050846326087908138072276574551622295305363297303796475236743407033872050505505258316033485672633919832171293734510922378996431364040511750290439960632310001714762650141219586338533659661145207832812153196905584324233880434411008935968210248120254592219143499663331026035940967857702184249516470140726983300038250516655102481461634312264361917131333692746997337610373933539126072670553239905876923261210259653157542326092232224506095372652626647048947514279934096243998980486014276062413465108173130546321103636140176741947117657559372539407539959748554826955685805657656559365361140773364006354066965902979132274764391682954582545362477960998655750720751776362129898196518672093783885027894490660960347861375467276604570485308324611811336766097740639800495311866432755595066972193206345887644490109327582447963655459924979223870585007368535057272969543247494138045327770371999165259358212909844053017843119391314064916023388479434315245272895516572822495957093504029809025062682804545309127614630300097220934388698225884675372111513043529145458035264473295702577951302743212331762750376809322908082075472330484814261413664874904636054732568442465031290739303262529217400235311733657999606249807860091653780387010694493970119861337001762851188726035634798561986426157419985443148723087324423948806538537183579380048054150539466774142134781073287710992297832180184523856354790289437405829653517971562451742998868939025911919143462509191458513987476103370455837358677165931186011633739743633443782132459240943029181265629732922402496957548667917018735985208851014808141148904668182575854119987415096935136105547135982727923512403557557184285707860531780861515210047782102746744122080284793322406595355595590740705680644528302246504224233949929061866715744265547306300244617964411826737107647519308483996492827007417302230921887192843777072523085075723106378152917524698140056836190034306636769111270300982910095368610127328392164044486059453414446615186539598176203239177645886646272086499916965552813205921573828871221213389669467861327914438474749156429382395550505955295038814681910504969463122155314773154484075196481937747604031913723878007346759642740930012793763838094534370853062669975330506017014363450430329459053656520345349465359958870457951562664703486893647474267193403456260368682913189810656014241230426001704144378333173772185759957925612826743656015082399089799112996800393210706977154985415950565861734398414965226431808528718255097967039286533193115038473666847255752726105646678349563384983637665601363442337238914255489018923412164720358660695989957237826011867982564694262585101963120603030954115728922969000608657199523715615191484311935163829613487718992668593251269507869634245843489057041475254487078090892262970574745024734494817131336507663637262844095398225445459013926065679177207638631314593684169917525836038288539530977069426644689437551160969649867225333585097037005980946825128595352787160349265590683162697187546326412927146175361156814831105097468679389859326156658836766067489217852054637464700837816916470841593174480923454775158143708125891408169679973259249740433645511023765843793213174781031035042432742646494396164595451818878694038676201736916225341612931731199046185040498290715097460652855006548654867455425744794872526748048950478266832529955082630318221652082219112472435180206712895074559984737192344601501346072104197809905788027419693483776078004125855445111904372691320692924190418724764782813887206159278485301082773875393841990732871968673090511276949995459940685128780683728611463741035076345172995624400064684881898377983032385624518805345839216210655454157296984583910609321722425602681844490730769445745414847423569694552016545474813298415764706851658332487823989153313176740868686662304341579040727934105840018449501299318083875542278230928177069014069158724456481359557901867322959660961809149965898180045162804264797348457409095395044273891532359013047585664471999554328490900800825991567430863512697987556520795279346092063219053727191438622790137960695235799990894449207569561054059640431009899118916083028001538935580742555496195531604434806336545414356457083981371097808427075941638477819301354681352581970675112631009586557901956182032167482350638217400137734684313460439294886882010441499541047564157593918807952784272303933020201938317171720903702846432528985117478287169208562946224501060500713738206032325233712496339954551196669312159831074968760297607781080497173810583528888976406909935277634190957194488365776613492572808223510792056921325132883547817090159442359191388147115254140116256330940096045863380674182268130700859092708199076687281406044664485534920369051462190577103959796494535587993399084142991368054391504929621372781447548640573937103688549701972223790404780396946439015442147542651908410761981978139980137756769520950003252812640267412173114552524730496923686373962659143152134948989574640189183198212535857640486803997421702879853435065111512148640293188545260705389801880102102519196916738410917034500294461655395405866549649977678966865769324428030025432826443438364605324721451826323813446424652018067355912327760028314641018011419983440246355161209321725114152226577756960788372254209044482552599104922696281000990163197043625999543088957298962127486413395135977695590336066093069595731234026504916532104615781921080254485508765326587879158175692766296839555008661301114729722435959847859563631647331907652106293562662793467664061523719158632684279040102434931585656213376773371343915970828139343914666370506956312804750298900713255657443726015815954838485337798218669785760779351125358623248366536573428973009662234221988401962754917388884746901655921771453897678198837730151489274083267416068332490489007221717040571325046075534155022066234342794157017153713578965326779602535795369984278341049510289894998779593049540053733668684889371561145656152141426602492799017673811657999701646639767591904364622892805694980443549577791153808527980322115033251886308229483622339410216552492284161100919885059763590398154901803120869975234713966982570378733889810348932779987660915993677167575189715029140113080237926386318280689326889735155489700859224877532686670538610258038911885530733524809466214365663253536942645972389099436018983845589705574352119431142773316945665305542576830908767462249113931208634549246890706895727988019559839208899785264922490269432860757072367905051992327122092448364531855238230023181641596366427614209145659732064343066076473696523907653091570103481136362734140032026866918237545543639985743923976925596185538548224374870858672983375743546885757843578934100211454945556586381126532278495531899316376924680512836044064756362691327570072466343880595823396594951602582830155219431127944330289493145617197639685150759015228297084170997669449584168982609677739735261016473879904152308553157146864265314434305764734745025634765625

Whoah!!! I should add that this is something Python does easily that other languages choke on: it has variable precision integers that can expand to many, many digits

Try the ** operator with some other inputs: can you figure out its meaning?


In [16]:
2 ** 3

# this is how you express exponentiation in Python
# 2 raised to the power of 3 is 8


Out[16]:
8

Exploring other Operators

There's another class of operators recognized by Python: &, |, ^, and ~.

Play around with these four operators: can you discover their meaning? (Don't worry if you can't: these are quite a bit more obscure than the ones above!).


In [17]:
1 & 2


Out[17]:
0

In [18]:
1 | 2


Out[18]:
3

These are bitwise operators. To understand what they do, think about the numbers in binary:

$1 = 0 * 2^2 + 0 * 2^1 + 1 * 2^0$ = b001

$2 = 0 * 2^2 + 1 * 2^1 + 0 * 2^0$ = b010

$3 = 0 * 2^2 + 1 * 2^1 + 1 * 2^0$ = b011

The & operator is a binary AND: it returns all bits for which both numbers have a 1:


In [19]:
1 & 3


Out[19]:
1

The | operator is a binary OR: it returns all bits for which either number has a 1:


In [20]:
1 | 3


Out[20]:
3

The ^ operator is a binary XOR: it returns all bits for which one but not both has a 1:


In [21]:
1 ^ 3


Out[21]:
2

The ~ operator is a binary NOT: it flips all bits in the number (including the sign!)


In [22]:
~2


Out[22]:
-3

The math Module

Please switch roles now, so the second person has fingers-on-keyboard

Along with the basic arithmetic operators, Python has some more sophisticated mathematical operations in the math module. A module in Python is a collection of functionality that can be imported and used in your script. Importing functionality from modules is extremely easy in Python compared to some other languages, and it has an incredible array of standard functionality available through these imports.

A popular XKCD comic draws from this idea:

The way you import a module, like in this xkcd comic, is to use the import statement. For example:


In [23]:
import antigravity

In [24]:
import math

With this done, we can now access elements of the math module using math.XXX, where we replace XXX with the name of the variable, function, or other object we're interested in.

For example:


In [25]:
# variable containing the value of π
math.pi


Out[25]:
3.141592653589793

In [26]:
# function to compute the square root of a number
math.sqrt(2)


Out[26]:
1.4142135623730951

Use functions in the math module to compute the following:

What is $\sin(2)$?


In [27]:
math.sin(2)


Out[27]:
0.9092974268256817

What is $\cos(2)$?


In [28]:
math.cos(2)


Out[28]:
-0.4161468365471424

What is $\exp(2)$ (otherwise known as $e^2$)?


In [29]:
math.exp(2)


Out[29]:
7.38905609893065

What is $\log(2)$?


In [30]:
math.log(2) # log base e


Out[30]:
0.6931471805599453

In [31]:
math.log10(2) # log base 10


Out[31]:
0.3010299956639812

In [32]:
math.log2(2)  # log base 2


Out[32]:
1.0

What is $\sin^2(2) + \cos^2(2)$?


In [33]:
math.sin(2) ** 2 + math.cos(2) ** 2


Out[33]:
1.0

What is $\sin(2\pi)$? (do you notice anything strange?)


In [34]:
math.sin(2 * math.pi)

# It's close to zero, but not exact as we might expect!
# This is an issue of floating point roundoff error.


Out[34]:
-2.4492935982947064e-16

Exploring Packages with IPython

The IPython notebook includes a very convenient way to explore the contents of a module.

  • If you type math. followed by the tab key, it will give you a list of valid completions, which in this case consists of a list of all functions and variables in the math module.

  • If you type any function followed by a question mark (e.g. math.cos?) then shift-enter, it will give you a documentation string

Use these two convenience features to explore the math module.

Can you find a function that computes $\sqrt{x^2 + y^2}$ efficiently in a single command?


In [35]:
help(math.hypot)


Help on built-in function hypot in module math:

hypot(...)
    hypot(x, y)
    
    Return the Euclidean distance, sqrt(x*x + y*y).

What is $\sqrt{4235^2 + 3154^2}$?


In [36]:
math.hypot(4235, 3154)


Out[36]:
5280.430001429808

What is $e^x - 1$ for $x = 10^{-20}$? (Hint: it's not zero!) Can you find a function in the math module that computes this function accurately?


In [37]:
math.expm1(10 ** -20)


Out[37]:
1e-20

While you are waiting for others to finish, continue exploring the math module to see what functionality is available.

(Recall: you can type math. followed by the TAB key to see what's available, and you can put a question mark after any Python object to get its documentation string)


In [ ]: