lab00.pdf - lab00.juliana 1 Stat89A Lab00 1.0.1 Header to import packages \u2022 Note This header is usually NOT commented out We comment it out in this

lab00.pdf - lab00.juliana 1 Stat89A Lab00 1.0.1 Header to...

This preview shows page 1 - 5 out of 28 pages.

lab00.juliana January 25, 2019 1 Stat89A Lab00 1.0.1 Header to import packages • Note: This header is usually NOT commented out. We comment it out in this introductory notebook to illustrate a few things. In [17]: # Don ' t change this cell; just run it. # from datascience import * # import numpy as np # import math # import matplotlib.pyplot as plt # %matplotlib inline # plt.style.use( ' fivethirtyeight ' ) 2 Some Basic Commands In [18]: ## Hello world print ( ' Hello world ' ) Hello world In [19]: ## Run, then uncomment out the line below, then rerun. print ( ' Hello world ' ) Hello world In [20]: ## Ignore this, but if you want to clear variables, then you can run it. % reset Once deleted, variables cannot be recovered. Proceed (y/[n])? Nothing done. 1
Image of page 1
In [21]: ## Three plus two 3+2 Out[21]: 5 In [22]: ## Note that only the last thing is printed out. ## In Jupyter, the default output of a cell is whatever ## the last line in the cell evaluates to, unless it ## evaluates to ` None ` , in which case nothing is printed. 2+2 3+3 Out[22]: 6 In [23]: ## Use print to get around this. ## You can use print to print things ## in addition to the default output of ## whatever the line evaluates to. ## Note that in this case, the last line, print(3+3), ## evaluates to ` None ` , so the output is only from ## the two print statements. print ( 2+2 ) print ( 3+3 ) 4 6 In [24]: ## Assign variables a = 2 b = a + a print (a) print (b) 2 4 3 Different Types of Variables 3.0.1 Numbers In [25]: ## Integers and Floats - roughly mathematical integers and real numbers 2
Image of page 2
a = 2 b = a + a c = a +2.5 print ( ' a: ' , a) print ( ' b: ' , b) print ( ' c: ' , c) a: 2 b: 4 c: 4.5 In [26]: ## You can also add numbers (as you would expect) print ( ' a+b: ' , a + b) print ( ' a+c: ' , a + c) a+b: 6 a+c: 6.5 In [27]: ## You can see what type a variable is, which is often helpful to diagnose problems. print ( ' a is of type ' , type (a)) print ( ' b is of type ' , type (b)) print ( ' c is of type ' , type (c)) a is of type <class ' int ' > b is of type <class ' int ' > c is of type <class ' float ' > 3.0.2 Strings In [28]: ## Strings are text things my_name = ' Michael Mahoney ' print ( ' My name is ' , my_name) print ( ' my_name is of type ' , type (my_name)) My name is Michael Mahoney my_name is of type <class ' str ' > In [29]: ## The + operator will concatenate two strings my_name = ' Michael ' print ( ' Just first name: ' , my_name) my_name += ' Mahoney ' print ( ' Concatenated: ' , my_name) 3
Image of page 3
Just first name: Michael Concatenated: MichaelMahoney In [30]: ## We can also use Python ' s slicing operator on strings print ( ' Index 1 of my_name is ' , my_name[ 1 ]) print ( ' The first 2 characters of my_name are ' , my_name[: 2 ]) print ( ' The last 3 characters of my_name are ' , my_name[ -3 :]) Index 1 of my_name is i The first 2 characters of my_name are Mi The last 3 characters of my_name are ney 3.0.3 Lists In [31]: ## Lists are just a list of things that usually can be operated on in limited ways. ## I.e., they are NOT mathematical vectors in a vector space ## We will see later how we can create data types backed by lists, but contain ## additional operations, that are a much better representation for a vector.
Image of page 4
Image of page 5

You've reached the end of your free preview.

Want to read all 28 pages?

  • Fall '18

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern

Stuck? We have tutors online 24/7 who can help you get unstuck.
A+ icon
Ask Expert Tutors You can ask You can ask You can ask (will expire )
Answers in as fast as 15 minutes
A+ icon
Ask Expert Tutors