Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Download

Jupyter notebook Assignments/Assignment-01/Assignment-01.ipynb

254 views
Kernel: Python 2 (SageMath)

The aim of this first assigment is to get you familiarized with the Jupyter notebook environment and SageMathCloud, and also to refresh your memory about some Python basics. I will indicate the tasks you need to do as part of the homework with the shorthand [ HW ].

Jupyter cells and Markdown

A Jupyter notebook, for us, will be an interface to Python. Notebooks consist of "cells" such as the one that contains the text you are reading right now. Cells can also contain Python code. You can select a cell by clicking on it, and open it by double-clicking on it. Once you open a cell, you can edit it.

You can execute cells by selecting them and pressing the "play" button at the top left:

You can also run a selected cell by clicking on the "Cell" dropdown menu, and selecting "Run Cells".

You can use the same Cell dropdown menu to change the type of a cell, using the "Cell Type" item. "Code" cells contain Python code, and "Markdown" cells contain formatted text. This cell you are reading now is a Markdown cell.

Markdown is a really simple text format used to display titles, bullet lists, etc., in plain text files. If you open a Markdown cell in Jupyter, you can see the Markdown syntax used to produce the formatted content. For example, Mardown allows you to have levels of titles like this:

Subtitle

Sub-subtitle

You can also have bullet-lists, like this:

  • Item 1

  • Item 2

  • Item 3

If you are familiar with LaTeX, you can even add mathematical formulas, such as this: n=1n=112\sum_{n=1}^{\infty} n = - \frac{1}{12} You can also add HTML links like this.

Let's start with a simple task.

[ HW ] Double-click on the text region you are reading right now to open it. This will display the Markdown syntax used to format the text you are reading. After you open this cell, add a few words (e.g., your name) between the horizontal lines below (feel free to play around and add other stuff to familiarize yourself with Markdown syntax; e.g. you can try boldface and italic text).

Add your name between the two lines below


beRARE go¨ktu¨rkgöktürk

Add your name between the two lines above

[ HW ] Create a new cell right underneath this one by using the Insert dropdown menu.

[ HW ] Turn your new cell into a Markdown cell by selecting it, and using the menu item Cell -> Cell Type -> Markdown

[ HW ] Open your Markdown cell and type "Hello!" in it, and execute the cell.

After a little bit of time with Jupyter notebooks, it gets a little boring to use the menu items each time, to open new cells, change cell types, execute cells, etc. Fortunately, there are very useful keyboard shortcuts to do all these things; you can click on Help and select Keyboard Shortcuts to get a list of the available shortcuts. I strongly recommend that you start to learn those; your work will become much more enjoyable that way. Some of the shortcuts I use most frequently are: j and k to select the cells above and below. Shift+Enter to execute the current cell. Alt+Enter to execute the current cell and insert a new one below.

"Hello!"

Simple Python exercises

# Here is a code (Python) cell. Because this cell needs to contain Python code, # we need to use the '#' symbol to comment out the free form text you are # reading. # # [ HW ] Select and execute this cell. print "Python is simple!"
Python is simple!
# Here is another Python cell. # # [ HW ] Write a for loop in this cell that prints all the numbers from 1 to 10 (inclusive) # on separate lines for x in range (1,11): print(x)
1 2 3 4 5 6 7 8 9 10
# [ HW ] Define a Python function that takes one argument, `x`, assumed to # be an integer > 0, and prints the squares of all the integers from 1 # to x, inclusive. def print_up_to(x): i=1 for i in range(1,x+1): print i**2 i= i + 1 print_up_to(11)
1 4 9 16 25 36 49 64 81 100 121
# [ HW ] Define a Python function that takes one argument, `s`, assumed to be a # string, and *returns* (without printing) a string that consists of `s` with an # exclamation mark added at the end def add_exclamation_mark(s): return s + '!' print(add_exclamation_mark("happy"))
happy!
# [ HW ] Define a Python function that takes one argument, `strings`, # which is assumed to be a list of strings, and returns a list of strings # that consists of the strings of the original list with question # marks added at the end of each def add_question_marks(strings): for word in strings.split(): print word + '?' add_question_marks("bunu mu neden bunu")
bunu? mu? neden? bunu?
# [ HW ] Define a function that takes one argument, `x`, assumed to # be an integer > 0, and prints the integers from 1 to x, inclusive, # except it skips the numbers divisable by 5 def skip_multiples_of_five(x): i = 1 while (i < x): if (i % 5 != 0): print i i = i + 1 skip_multiples_of_five(32)
1 2 3 4 6 7 8 9 11 12 13 14 16 17 18 19 21 22 23 24 26 27 28 29 31

Getting familiar with SageMathCloud

I have created two projects for the course, one of these will appear on your account as

[Your Name] - PHYS-48T-Fall-2016

the other will appear as

Shared Project -- PHYS-48T-Fall-2016

The first one, which will have your name in it, is the main course project. You will find the handouts and the assignments (such as this one) there. The second one is our shared "bulletin board" project, where we will have our course chat room, and other files that we want to collaborate on altogether.

[ HW ] Go to the Shared Project. You will see a "chat" file named "PHYS-48T-General-Discussion.sage-chat". Open it, and type a simple hello message. Please use this chat room to ask questions to me and to each other, help others, share relevant links, etc. This is a free space for all of us! If you catch typos in the homeworks or the handouts, please let me know here.

The notification bell at the top right will indicate the number of new messages you have, etc.

** [ HW ] ** Go to the main course project, [Your Name] - PHYS-48T-Fall-2016, and open the folder for this assignment (Assignment-01). In that same folder, create a new Jupyter notebook named "My first notebook". You can do this as follows.

Click on New.

Type in the name of the notebook.

Select Jupyter Notebook as the type of file you want to create.

Your new notebook will be created in a few moments.

[ HW ] Once your new notebook is created, type in a Python cell to print "Hello world!", and run that cell.

[ HW ] Create a Markdown cell under your Python cell, and type "Hello world!" in it, and run the cell (running Markdown cells simply formats them).