12 code cells other cells contain code in the python

This preview shows page 2 - 6 out of 19 pages.

1.2. Code cells Other cells contain code in the Python 3 language. Running a code cell will execute all of the code it contains. To run the code in a code cell, first click on that cell to activate it. It'll be highlighted with a little green or blue rectangle. Next, either press | or hold down the shift key and press return or enter . Try running this cell: In [1]: print("Hello, World!") And this one: In [2]: print(" \N{WAVING HAND SIGN} , \N{EARTH GLOBE ASIA-AUSTRALIA} !") The fundamental building block of Python code is an expression. Cells can contain multiple lines with multiple expressions. When you run a cell, the lines of code are executed in the order in which they appear. Every print expression prints a line. Run the next cell and notice the order of the output. Hello, World! ± , ² !
In [3]: print("First this line is printed,") print("and then this one.") Question 1.2.1. Change the cell above so that it prints out: First this line, then the whole ² , and then this one. Hint: If you're stuck on the Earth symbol for more than a few minutes, try talking to a neighbor or a TA. That's a good idea for any lab problem. 1.3. Writing Jupyter notebooks You can use Jupyter notebooks for your own projects or documents. When you make your own notebook, you'll need to create your own cells for text and code. To add a cell, click the + button in the menu bar. It'll start out as a text cell. You can change it to a code cell by clicking inside it so it's highlighted, clicking the drop-down box next to the restart ( ) button in the menu bar, and choosing "Code". Question 1.3.1. Add a code cell below this one. Write code in it that prints out: A whole new cell! ♪ ² (That musical note symbol is like the Earth symbol. Its long-form name is \N{EIGHTH NOTE} .) Run your cell to verify that it works. First this line is printed, and then this one.
1.4. Errors Python is a language, and like natural human languages, it has rules. It differs from natural language in two important ways: 1. The rules are simple . You can learn most of them in a few weeks and gain reasonable proficiency with the language in a semester. 2. The rules are rigid . If you're proficient in a natural language, you can understand a non-proficient speaker, glossing over small mistakes. A computer running Python code is not smart enough to do that. Whenever you write code, you'll make mistakes. When you run a code cell that has errors, Python will sometimes produce error messages to tell you what you did wrong. Errors are okay; even experienced programmers make many errors. When you make an error, you just have to find the source of the problem, fix it, and move on. We have made an error in the next cell. Run it and see what happens. In [4]: print("This line is missing something." In [ ]: print("This line is missing something.") File "<ipython-input-4-0fbe4427aee1>", line 1 print("This line is missing something." ^ SyntaxError: unexpected EOF while parsing
You should see something like this (minus our annotations): The last line of the error output attempts to tell you what went wrong. The syntax of a language is its structure, and this SyntaxError

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture