在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
开源软件名称:learn-co-curriculum/dsc-prework-jupyter-notebook开源软件地址:https://github.com/learn-co-curriculum/dsc-prework-jupyter-notebook开源编程语言:Jupyter Notebook 100.0%开源软件介绍:Your First Jupyter Notebook!IntroductionWelcome to your first Code Lesson! Reminder: Code Lessons are mainly asking you to read and execute code, not to write it yourself. They are categorized as "Assignments" due to the naming conventions of Canvas + IllumiDesk, but there is nothing you need to turn in or submit. Just make sure you click the "Mark as done" button in Canvas (so it says "Done") when you're finished ObjectivesYou will be able to:
Jupyter NotebooksThe document you are currently reading is a Jupyter Notebook! Jupyter Notebooks (historically called IPython Notebooks) will be our primary tool for curriculum delivery, and you will use and create many Jupyter Notebooks in the course of this program and in your career as a data scientist. Jupyter is a web application that allows you to create and work with documents that have live code. It's a very popular tool among data scientists, as it allows for both explanations of thinking behind code as well as the code itself. Running Jupyter NotebooksEventually you will set up your local development environment and run the server behind Jupyter Notebook on your personal computer, but for now all you need to worry about is clicking the "Load ... in a new window" button like you did to open this lesson: This creates a copy of the notebook from our curriculum for your personal use, which you are free to edit and modify. Your copied notebook will be stored in IllumiDesk for the duration of the program, with your changes saved. If you want to see a list of all of your copied notebooks, click on the IllumiDesk logo in the upper left corner of the page: Introduction to CellsThe notebook itself consists of cells. Double click on this content in Jupyter Notebook to see what we mean. Once you double click on a cell, you are in edit mode (also known as insert mode). This means that you are able to edit the cells, just as you would if this were a word document. You can tell that you are in insert mode because of the green border around the cell. After entering insert mode for this cell, change some content. Don't worry about what you change as you can always undo it. You can revert the changes to a cell by making sure that you are still in edit mode and by pressing When you're done editing this cell, go ahead and press Command Versus Edit ModeAs we just saw, when you are in a cell writing code or notes, the cell is highlighted in green meaning you are in edit mode (AKA insert mode). Alternatively, if you are not in edit mode, the cell is highlighted in blue inidicating that you are in command mode (AKA escape mode). Edit ModeEdit mode is the standard mode for editing cells, whether it's writing code or notes. To enter edit mode from command mode, you can:
Command ModeIn command mode, you can delete cells, add cells, copy cells, paste cells, change cell types, and more. To enter command mode from enter mode, you can:
You can perform command mode commands with keyboard shortcuts, or in a more cumbersome (and time consuming) manner by using the various headers in the menu bar at top. Keyboard ShortcutsYou can see a full list of shortcuts available in command and edit mode under the help menu. There is (of course) also a shortcut to view shortcuts! Press the key Types of CellsThe current cell and every cell before it in this lesson is a markdown cell, meaning that it allows us to write text and stylize that text. For example, if you surround some text with two asterisks ( The details of markdown are not important here but just know you can use markdown cells to display text. They are really useful for embedding notes and explanations in your Jupyter Notebook. Cells can also contain code. If you are writing in a cell that is for Python code, everything in that cell must be valid Python code or you will see an error. Click on the following cell, then press This is a Code cell without valid Python code so you will see an error Notice that this cell has a little note that says You may also notice that other cells, such as this one, do not have the A cell must either be of type Markdown or of type Code, in which case all of the contents must be valid Python. It cannot be both. Instead of using the dropdown, we can quickly change a cell from markdown to code with some keyboard shortcuts.
Adding CellsYou can add cells with either the graphical menu or keyboard shortcuts. To add a new cell with the graphical menu, click the To add a new cell using keyboard shortcuts:
Anytime you create a new cell, say with the shortcut key Deleting cellsYou can also delete cells with either the graphical menu or keyboard shortcuts. To delete a cell with the graphical menu, make sure it is selected (either command mode or edit mode) then select Edit --> Delete Cells. To delete a cell using keyboard shortcuts:
Of course, you'll want a way to undo your deletion. From command mode, you can press (Note that this is different from Try it out:
Delete this cell Working with Python in JupyterOk, now that you know a little bit about adding and deleting cells, as well as changing cell types from markdown to code, let's focus on working with Python in Jupyter. We'll go into a large amount of detail about working with a Jupyter Notebook in Python, but the main takeaway is this: if you see a Python cell, you should press The major gotcha in working with Python code is that we must execute the cells in order for Python to register the code in them. So for example, just seeing the cell where we define name = 'bob' If we try to reference that variable later on without having executed the cell, Python will tell us that it is not defined. name
To execute or run a cell, we must press age = 14
age As you can see the variable One tricky thing to note is that assignment, the action of assigning a variable, does not have a return a value. So, even though the cell is run, if the last line of cell is the assigning of a variable, nothing is displayed underneath. hometown = 'NYC' Notice, even after pressing shift + enter on the code above, nothing is displayed below. But if we reference the variable hometown
None If you want to print a certain sentence, you need to use the command print('I want to print this sentence about running Python code') As you can see, running the code above printed text to the screen. As we'll cover in more depth later, Importing ModulesThe next thing we're going to do is load in some Python modules that will be part of our toolbox for manipulating and analyzing data. Again, don't worry if you are not sure what a Python module is. For now, just think of modules as collections of specialized tools for performing specific tasks. For the purpose of this example, we'll use the built-in Python Again, press (If you ever get a import random
import math Here's a short example of code that uses imported modules (don't worry about the details of what it's doing or why): # set a seed for reproducibility
random.seed(42)
# get a single random number
random_number = random.random()
random_number # create a list of 5 random floating point numbers
random_numbers = [random.random()*10 for x in range(5)]
random_numbers len(random_numbers) # find the ceilings of the random numbers
# (ceil(x) = the smallest integer >= x)
ceilings = [math.ceil(x) for x in random_numbers]
print(ceilings) Python CommentsNote the green text following the pound sign Anything following a When you create a notebook, you want to use a mixture of markdown cells and Python comments to explain what is happening. In general, markdown cells are useful for headers, longer explanations, and non-technical or business-oriented notes. Python comments are useful for shorter details that are more specifically related to the code implementation. There isn't a hard-and-fast rule of what to write in what format, but in general:
Using Jupyter Notebook to View Module DocumentationOne of the most useful features of Jupyter Notebook is the ability to bring up documentation for a given tool without leaving the notebook. A little more theory and context: When we loaded in modules in a previous cell (using the We demonstrated this when we used the modules above, e.g. This also demonstrates the dot notation in Python, which is how we access built in methods or attributes of a given object. Tab CompletionWhen code is loaded into a Jupyter Notebook, we can also use tab completion to preview methods available in modules or other objects. Try This Out Yourself
math. You can also start typing to subset the list of available commands: math.p Pulling up DocstringsEven better, you can even see how a method works by pulling up its docstring! You can do this by writing Try it out by running the following cell: random.randrange? Alternatively, you can pull up a preview of the docstring by pressing shift+tab within the parentheses of a method Try it out here: random.randrange() #Move your cursor inside the parentheses and press shift+tab VariablesThe other thing that happened in our code example above was that we defined variables. For example, this happened in this line of code:
As we saw, we used the The output of this method was then assigned to the variable
Built-in Python FunctionsWe also used two built in Python functions:
In general, Python has reserved keywords for built in functions like this. Be sure to not name your variables any of these! You can also check what type of object something is using the built in type(ceilings) Practice
These are just suggestions, feel free to explore other methods of As a reminder, this is not an assignment you need to turn in, just make sure you click "Mark as done" on Canvas when you're done reading.
SummaryCongratulations, you got some practice executing cell operations within Jupyter Notebooks and learned how to import Python modules. We will use this format consistently through the rest of the curriculum, as we learn about everything from data visualization to machine learning. Now let's dive into some more Python fundamentals! |
2023-10-27
2022-08-15
2022-08-17
2022-09-23
2022-08-13
请发表评论