{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "#### Basic datatypes" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "x=1\n", "y=2" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "1\n" ] } ], "source": [ "print(x)" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [], "source": [ "x = 16 # integer\n", "y = 2.6 # float\n", "z = 'abc' # string\n", "# this is a comment" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "abc\n" ] } ], "source": [ "print(z)" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [], "source": [ "result = x+y" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "18.6\n" ] } ], "source": [ "print(result)" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "3650" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "365*10" ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "8760" ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "365*24" ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [], "source": [ "a = True" ] }, { "cell_type": "code", "execution_count": 12, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "False" ] }, "execution_count": 12, "metadata": {}, "output_type": "execute_result" } ], "source": [ "not a" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Other useful python types" ] }, { "cell_type": "code", "execution_count": 13, "metadata": {}, "outputs": [], "source": [ "# list\n", "l = [1, 2, 3, 'abc', 3.5, True, [4,5,6]]" ] }, { "cell_type": "code", "execution_count": 14, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'abc'" ] }, "execution_count": 14, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# indexing starts at 0!\n", "l[3]" ] }, { "cell_type": "code", "execution_count": 15, "metadata": {}, "outputs": [], "source": [ "# tuple\n", "t = (1,2,3)" ] }, { "cell_type": "code", "execution_count": 16, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "2" ] }, "execution_count": 16, "metadata": {}, "output_type": "execute_result" } ], "source": [ "t[1]" ] }, { "cell_type": "code", "execution_count": 17, "metadata": {}, "outputs": [], "source": [ "# tuples are like lists, but lists are more flexible\n", "l.append(89.7)" ] }, { "cell_type": "code", "execution_count": 18, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[1, 2, 3, 'abc', 3.5, True, [4, 5, 6], 89.7]" ] }, "execution_count": 18, "metadata": {}, "output_type": "execute_result" } ], "source": [ "l" ] }, { "cell_type": "code", "execution_count": 19, "metadata": {}, "outputs": [], "source": [ "# the syntax object.method() is very common\n", "l.reverse()" ] }, { "cell_type": "code", "execution_count": 20, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[89.7, [4, 5, 6], True, 3.5, 'abc', 3, 2, 1]" ] }, "execution_count": 20, "metadata": {}, "output_type": "execute_result" } ], "source": [ "l" ] }, { "cell_type": "code", "execution_count": 21, "metadata": {}, "outputs": [], "source": [ "# dictionary\n", "d = {'a': 1, 'b': 2}" ] }, { "cell_type": "code", "execution_count": 22, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "1" ] }, "execution_count": 22, "metadata": {}, "output_type": "execute_result" } ], "source": [ "d['a']" ] }, { "cell_type": "code", "execution_count": 23, "metadata": {}, "outputs": [], "source": [ "colors = ['red', 'green', 'blue', 'black']\n", "d['red'] = (255, 0, 0)\n", "d['blue'] = (0,0,255)" ] }, { "cell_type": "code", "execution_count": 24, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "(255, 0, 0)" ] }, "execution_count": 24, "metadata": {}, "output_type": "execute_result" } ], "source": [ "d['red']" ] }, { "cell_type": "code", "execution_count": 25, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{'a': 1, 'b': 2, 'red': (255, 0, 0), 'blue': (0, 0, 255)}" ] }, "execution_count": 25, "metadata": {}, "output_type": "execute_result" } ], "source": [ "d" ] }, { "cell_type": "code", "execution_count": 26, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "['a', 'b', 'red', 'blue']" ] }, "execution_count": 26, "metadata": {}, "output_type": "execute_result" } ], "source": [ "list(d.keys())" ] }, { "cell_type": "code", "execution_count": 27, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[1, 2, (255, 0, 0), (0, 0, 255)]" ] }, "execution_count": 27, "metadata": {}, "output_type": "execute_result" } ], "source": [ "list(d.values())" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Numpy" ] }, { "cell_type": "code", "execution_count": 28, "metadata": {}, "outputs": [], "source": [ "import numpy as np" ] }, { "cell_type": "code", "execution_count": 31, "metadata": {}, "outputs": [], "source": [ "a = np.array([0,1,2,3,4,5])" ] }, { "cell_type": "code", "execution_count": 38, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "5" ] }, "execution_count": 38, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# an example of method access\n", "a.max()" ] }, { "cell_type": "code", "execution_count": 39, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "1" ] }, "execution_count": 39, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# ndim is one of the attributes of a numpy array.\n", "a.ndim" ] }, { "cell_type": "code", "execution_count": 40, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "(6,)" ] }, "execution_count": 40, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a.shape" ] }, { "cell_type": "code", "execution_count": 41, "metadata": {}, "outputs": [], "source": [ "b = np.array([[0,1,2,3,4], [5,6,7,8,9]])" ] }, { "cell_type": "code", "execution_count": 42, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "2" ] }, "execution_count": 42, "metadata": {}, "output_type": "execute_result" } ], "source": [ "b.ndim" ] }, { "cell_type": "code", "execution_count": 44, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[0, 1, 2, 3, 4],\n", " [5, 6, 7, 8, 9]])" ] }, "execution_count": 44, "metadata": {}, "output_type": "execute_result" } ], "source": [ "b" ] }, { "cell_type": "code", "execution_count": 45, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "(2, 5)" ] }, "execution_count": 45, "metadata": {}, "output_type": "execute_result" } ], "source": [ "b.shape" ] }, { "cell_type": "code", "execution_count": 46, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "7" ] }, "execution_count": 46, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# The vertical axis is always the first\n", "# This is second row, third column\n", "b[1,2]" ] }, { "cell_type": "code", "execution_count": 47, "metadata": {}, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ ":1: VisibleDeprecationWarning: Creating an ndarray from ragged nested sequences (which is a list-or-tuple of lists-or-tuples-or ndarrays with different lengths or shapes) is deprecated. If you meant to do this, you must specify 'dtype=object' when creating the ndarray\n", " b = np.array([[0,1,2,3,4], [5,6,7,8]])\n" ] } ], "source": [ "# This is a mistake\n", "b = np.array([[0,1,2,3,4], [5,6,7,8]])" ] }, { "cell_type": "code", "execution_count": 50, "metadata": {}, "outputs": [], "source": [ "my_zeros = np.zeros((2000,2000))" ] }, { "cell_type": "code", "execution_count": 53, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[1., 0., 0., ..., 0., 0., 0.],\n", " [0., 0., 0., ..., 0., 0., 0.],\n", " [0., 0., 0., ..., 0., 0., 0.],\n", " ...,\n", " [0., 0., 0., ..., 0., 0., 0.],\n", " [0., 0., 0., ..., 0., 0., 0.],\n", " [0., 0., 0., ..., 0., 0., 0.]])" ] }, "execution_count": 53, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# top corner\n", "my_zeros[0,0] = 1\n", "my_zeros" ] }, { "cell_type": "code", "execution_count": 54, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[1., 1., 1., ..., 1., 1., 1.],\n", " [0., 0., 0., ..., 0., 0., 0.],\n", " [0., 0., 0., ..., 0., 0., 0.],\n", " ...,\n", " [0., 0., 0., ..., 0., 0., 0.],\n", " [0., 0., 0., ..., 0., 0., 0.],\n", " [0., 0., 0., ..., 0., 0., 0.]])" ] }, "execution_count": 54, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# set first row to 1\n", "my_zeros[0,:] = 1\n", "my_zeros" ] }, { "cell_type": "code", "execution_count": 55, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[1., 1., 1., ..., 1., 1., 1.],\n", " [2., 2., 2., ..., 0., 0., 0.],\n", " [0., 0., 0., ..., 0., 0., 0.],\n", " ...,\n", " [0., 0., 0., ..., 0., 0., 0.],\n", " [0., 0., 0., ..., 0., 0., 0.],\n", " [0., 0., 0., ..., 0., 0., 0.]])" ] }, "execution_count": 55, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# set 3 first elements of second row to 2\n", "my_zeros[1,0:3] = 2\n", "# equivalent is my_zeros[1,:3]\n", "my_zeros" ] }, { "cell_type": "code", "execution_count": 56, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[1., 1., 1., ..., 1., 1., 1.],\n", " [2., 2., 2., ..., 0., 0., 0.],\n", " [0., 0., 0., ..., 0., 0., 0.],\n", " ...,\n", " [0., 0., 0., ..., 0., 0., 0.],\n", " [6., 0., 0., ..., 0., 0., 0.],\n", " [6., 0., 0., ..., 0., 0., 0.]])" ] }, "execution_count": 56, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# set last 2 elements of first column to 6\n", "my_zeros[-2:, 0] = 6\n", "my_zeros" ] }, { "cell_type": "code", "execution_count": 58, "metadata": {}, "outputs": [], "source": [ "# set a square 100x100 with top corner at (350, 450) to 10.\n", "my_zeros[350:450, 450:550] = 10" ] }, { "cell_type": "code", "execution_count": 60, "metadata": {}, "outputs": [], "source": [ "my_piece_of_my_zeros = my_zeros[:2,:2]" ] }, { "cell_type": "code", "execution_count": 61, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[1., 1.],\n", " [2., 2.]])" ] }, "execution_count": 61, "metadata": {}, "output_type": "execute_result" } ], "source": [ "my_piece_of_my_zeros\n", "# Tricky: this is a \"view\" on the original array, not a copy" ] }, { "cell_type": "code", "execution_count": 62, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[5., 1.],\n", " [2., 2.]])" ] }, "execution_count": 62, "metadata": {}, "output_type": "execute_result" } ], "source": [ "my_piece_of_my_zeros[0,0] = 5\n", "my_piece_of_my_zeros" ] }, { "cell_type": "code", "execution_count": 63, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[5., 1., 1., ..., 1., 1., 1.],\n", " [2., 2., 2., ..., 0., 0., 0.],\n", " [0., 0., 0., ..., 0., 0., 0.],\n", " ...,\n", " [0., 0., 0., ..., 0., 0., 0.],\n", " [6., 0., 0., ..., 0., 0., 0.],\n", " [6., 0., 0., ..., 0., 0., 0.]])" ] }, "execution_count": 63, "metadata": {}, "output_type": "execute_result" } ], "source": [ "my_zeros" ] }, { "cell_type": "code", "execution_count": 82, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "dtype('float64')" ] }, "execution_count": 82, "metadata": {}, "output_type": "execute_result" } ], "source": [ "my_zeros.dtype # float64 - also known as double-precision float (8 byte = 64 bit)" ] }, { "cell_type": "code", "execution_count": 64, "metadata": {}, "outputs": [], "source": [ "# Typical mistake:\n", "first = np.zeros((100,100))\n", "second = first[:40,:50]\n", "first[0,0] = 1\n", "# This would modify 'second' as well!" ] }, { "cell_type": "code", "execution_count": 65, "metadata": {}, "outputs": [], "source": [ "# solution\n", "first = np.zeros((100,100))\n", "second = first[:40, :50].copy()\n", "# OR\n", "second = first[:40, :50] + 0." ] }, { "cell_type": "code", "execution_count": 66, "metadata": {}, "outputs": [], "source": [ "# operations on arrays\n", "first = np.zeros((100,100))\n", "second = np.ones((100,100))" ] }, { "cell_type": "code", "execution_count": 69, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[0., 0., 0., ..., 0., 0., 0.],\n", " [0., 0., 0., ..., 0., 0., 0.],\n", " [0., 0., 0., ..., 0., 0., 0.],\n", " ...,\n", " [0., 0., 0., ..., 0., 0., 0.],\n", " [0., 0., 0., ..., 0., 0., 0.],\n", " [0., 0., 0., ..., 0., 0., 0.]])" ] }, "execution_count": 69, "metadata": {}, "output_type": "execute_result" } ], "source": [ "first*second" ] }, { "cell_type": "code", "execution_count": 70, "metadata": {}, "outputs": [], "source": [ "i, j = np.indices((100,100))" ] }, { "cell_type": "code", "execution_count": 71, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[ 0, 0, 0, ..., 0, 0, 0],\n", " [ 1, 1, 1, ..., 1, 1, 1],\n", " [ 2, 2, 2, ..., 2, 2, 2],\n", " ...,\n", " [97, 97, 97, ..., 97, 97, 97],\n", " [98, 98, 98, ..., 98, 98, 98],\n", " [99, 99, 99, ..., 99, 99, 99]])" ] }, "execution_count": 71, "metadata": {}, "output_type": "execute_result" } ], "source": [ "i" ] }, { "cell_type": "code", "execution_count": 72, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[ 0, 1, 2, ..., 97, 98, 99],\n", " [ 0, 1, 2, ..., 97, 98, 99],\n", " [ 0, 1, 2, ..., 97, 98, 99],\n", " ...,\n", " [ 0, 1, 2, ..., 97, 98, 99],\n", " [ 0, 1, 2, ..., 97, 98, 99],\n", " [ 0, 1, 2, ..., 97, 98, 99]])" ] }, "execution_count": 72, "metadata": {}, "output_type": "execute_result" } ], "source": [ "j" ] }, { "cell_type": "code", "execution_count": 73, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "34" ] }, "execution_count": 73, "metadata": {}, "output_type": "execute_result" } ], "source": [ "i[34, 78]" ] }, { "cell_type": "code", "execution_count": 74, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "78" ] }, "execution_count": 74, "metadata": {}, "output_type": "execute_result" } ], "source": [ "j[34,78]" ] }, { "cell_type": "code", "execution_count": 75, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[-50, -50, -50, ..., -50, -50, -50],\n", " [-49, -49, -49, ..., -49, -49, -49],\n", " [-48, -48, -48, ..., -48, -48, -48],\n", " ...,\n", " [ 47, 47, 47, ..., 47, 47, 47],\n", " [ 48, 48, 48, ..., 48, 48, 48],\n", " [ 49, 49, 49, ..., 49, 49, 49]])" ] }, "execution_count": 75, "metadata": {}, "output_type": "execute_result" } ], "source": [ "(i - 50)\n", "# (in place operation )\n", "# i -= 50 \n", "# result is same as i = i - 50" ] }, { "cell_type": "code", "execution_count": 76, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[-50, -49, -48, ..., 47, 48, 49],\n", " [-50, -49, -48, ..., 47, 48, 49],\n", " [-50, -49, -48, ..., 47, 48, 49],\n", " ...,\n", " [-50, -49, -48, ..., 47, 48, 49],\n", " [-50, -49, -48, ..., 47, 48, 49],\n", " [-50, -49, -48, ..., 47, 48, 49]])" ] }, "execution_count": 76, "metadata": {}, "output_type": "execute_result" } ], "source": [ "j - 50" ] }, { "cell_type": "code", "execution_count": 77, "metadata": {}, "outputs": [], "source": [ "r = np.sqrt((i-50)**2 + (j-50)**2)" ] }, { "cell_type": "code", "execution_count": 81, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[2.82842712, 2.23606798, 2. , 2.23606798],\n", " [2.23606798, 1.41421356, 1. , 1.41421356],\n", " [2. , 1. , 0. , 1. ],\n", " [2.23606798, 1.41421356, 1. , 1.41421356]])" ] }, "execution_count": 81, "metadata": {}, "output_type": "execute_result" } ], "source": [ "\n", "r[48:52,48:52]" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Some more discussion about memory\n", "r0 = r\n", "r += 1 # will apply to r0\n", "r = r+1 # Now r is a new object different from r0\n", "\n" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.8.5" } }, "nbformat": 4, "nbformat_minor": 4 }