Jupyter notebooks
- [Instructor] We're going to be using Jupyter Notebooks for most of this course, and after all the work of installing it, you might be thinking, "What's this good for? "Why are we doing this?" Well, I'll tell you. Project Jupyter is a nonprofit organization that makes Python tools for programming and data science, and one of their best known tools is Jupyter Notebook, a web application that allows you to write and run Python programs in your browser rather than in the command line. When you run Jupyter Notebook from the command line, what you're really doing is starting a web application server running on your computer. So you start it, wait a second, it starts up and opens automatically in the browser. If the webpage doesn't open automatically for whatever reason, you can copy paste this URL here or this one, these are actually the same URL, and just open it up in your browser. So here we see a list of files. Anything ending in ipynb is an IPython notebook file, or a notebook. These are the things that Jupyter Notebook helps you create and edit. You can also click on new, Python three to create new notebook, or let's just click and open this one here. These notebooks are really popular in the data science community because it's easy to present your findings and create plots and charts in an easily shareable way. You can also export them as many different types of formats here. I actually use notebooks pretty frequently if I want to try out some experiment, if I'm making a report for my boss that involves code or, like right now, if I'm teaching Python and want to lay the small pieces of code out in a fast and organized way. So each notebook has a series of cells, and to make new cells, I can just hold down shift and hit enter. Shift, enter, enter, enter, enter. It makes all the new cells I want. To delete cells, all I do is click outside the cell in the margin here, so that margin turns blue, and then type dd. Dd, dd, dd. So when I click outside the cell, you can see that this border turns blue, and that's called command mode. So this cell is in command mode. When I click inside the cell, now we're in edit mode, and I can enter some Python code. Print, doing some data science. That's what I use these for a lot. To run code inside the cell, all you have to do is hold down that shift again, press enter, and it runs the code. Now up here, if I click, I'm in command mode again, I press A and I can create a cell above that one. If I press B, I create a cell below that one. Let's take the cell up at the very top, and let's say we want to make a title for this document. So not Python code, just some human readable text. So I'm going to go into command mode by clicking outside the cell and press M. Okay, now we're in markdown mode. Another way to get to markdown mode is to go to cell type, markdown, and you see the little M next to that? That gives you the hint that the keyboard shortcut is the letter M. So now the cell is in markdown mode, and we can write some text. Shift and then enter to run it. I can also make a title by doing a hash, and you see that makes a title. If I double click again, I can make a smaller title. Smaller title. There are tons of options for markdown. We can do bullet points, we can do italic text. You can actually see over in Jupyter Notebooks, there are a lot of options here you can use. Here I want to show you, there's even options for math equations, so you can write all the math equations you want in here. If I just copy this, M for markdown mode, paste that and we get a nice little math equation. So these notebooks are nice also because they're widely supported. They get displayed nicely in GitHub, like you can see here. This is in the Python essential training repo, and you can see it's really pretty. You can't edit it from here, but if you do want to edit it, you can actually open it in an IDE such as Visual Studio Code. So over here, I've opened the same file in Visual Studio Code, and you can see that you can edit it, run it with the same hot keys, and you can actually do everything that you can in the web browser inside Visual Studio, which is nice. So if you run into problems installing Jupyter from the command line, you might try installing Visual Studio and see if that works. I know we've covered a lot of content and a lot of keyboard shortcuts here. Don't worry about learning every hot key and memorizing every feature all at once. If you just focus on shift, enter, shift, enter, shift, enter to run things, pick up new hot keys as you need them, you'll be a notebook expert in no time.
随堂练习,边学边练
下载课堂讲义。学练结合,紧跟进度,轻松巩固知识。