Contact
CoCalc Logo Icon
StoreFeaturesDocsShareSupport News AboutSign UpSign In
| Download

test

Project: Test
Views: 26
Kernel: Python 3 (Anaconda)

This is a Jupyter Notebook – *.ipynb

Jupyter Notebooks are an emerging format for sharing scientific calculations in a standardized document. It is made up of a list of input-cells (usually containing code) and corresponding output cells (containing text and/or graphics). There are also "Markdown" cells, like the one you're reading right now, containing formatted text to explain the content.

Usage

To evaluate a cell, select the input cell (click on it, or put the cursor inside of it), and either press the play-button in the button row or press the Shift+Return keys. Usually, you should be able to go through a full notebook by pressing Shift+Return for evaluating one cell after another. Try it here after reading this introduction!

Each Jupyter Notebook has a corresponding "Kernel". It defines the language and back-end you are using when running code in a cell. Look at the top right, there it tells you what the current one is!

In the menu, you can select the "Kernel" you want to use. For this notebook here, it needs to be Python 3 by Anaconda.

CoCalc supports the following kernels:

CoCalc Jupyter Notebooks

Our Jupyter notebooks are an enhanced version of the legacy implementation. In addition to the standard features, we offer concurrent editing (Google Docs style), integrated chat, detailed revision history, and a faster more scalable interface.

Classical Jupyter Notebook

As an backwards compatible alternative to CoCalc's Jupyter Notebook implementation, there is also an enhanced version of the classical Jupyter Notebook available. It offers all original functionalities, but has a few technical drawbacks. Use it when you require full support of the legacy system.


Read this overview for more information!

Simple calculations

apple = 1.3*2 apple
2.6

Note, the variable apple from above still exists in the following cell. Such a definition holds as long as the session of the kernel is still active.

Restart the kernel (in the menu: Kernel -> Restart) and wait spawning up a new session. You'll see that evaluating this next cell will result in an error, because apple is no longer defined.

NameError: name 'apple' is not defined

2 * apple + 3 * apple

Linear Algebra

Use NumPy for working with two three dimensional vectors x\vec{x} and y\vec{y}:

import numpy as np x = np.array([1, 2, 3]) y = np.array([5, 2.2, -1])
x
y
x + y

Dot-product xy\vec{x} \cdot \vec{y}

x.dot(y)

Plotting

Familiarize yourself with the excellent matplotlib library.

import matplotlib.pyplot as plt

Defining a vector uu for the x-values and calculating a corresponding vector vv for the y-values.

u = np.linspace(0, 3 * np.pi, 1000) v = u * np.sin(u)

First 4 values of u and v as (x, y)-coordinates:

list(zip(u[:4], v[:4]))

Now, we plot a grid and draw a line through all corresponding x and y values.

plt.grid() plt.plot(u, v)

Programming in Python

Procedural programming's main idea is to define functions for encoding – well – procedures. They're a small "recipe" and here is such an example:

def func1(a, b): result = a + 2*b return result

Now, we "call" this function named func1 – containing the recipe to add the first argument to twice the second argument – and show the result

func1(4, 5)

That's 4 + 2 * 5. Follow where a and b are in the function to understand what's going on.

Now, we try another one:

func1(8, 1)

And here we use a nested loop to calculate a whole bunch of them all at once ...

for i in range(3): for j in range(5): z = func1(i, j) print("func1({i}, {j}) = {z}".format(i = i, j = j, z = z))

Great job!

... and don't worry too much about all the details. This tutorial should make you curious about what CoCalc can do for you 😃

Now, let's go back to first-steps.tasks.