{ "cells": [ { "cell_type": "markdown", "metadata": { "collapsed": false, "slideshow": { "slide_type": "slide" } }, "source": [ "
\n", "
\n", " This is an introduction to the basic functionality of Python and Sage, with an emphasis on seeing how to handle a worksheet and how to get more detailed help. We will point out a little bit of the power that Sage has hiding in it, but this is not a full-fledged tutorial.\n", "
\n", "\n" ] }, { "cell_type": "markdown", "metadata": { "collapsed": false }, "source": [ "There are two (main) ways to use sage: terminal and browser.
\n", "\n",
" Terminal: You can use sagemath from your computer itself. Everything lives on your computer. Uses your computers components, so it'll be faster.
\n",
" On Ubuntu, you can type sage
from your computer and it'll open up the terminal. You can then type whatever code you want and hit enter
and you'll get the results.\n",
"
\n",
" Browser: You can use sagemath from any website. Everything lives on the cloud (good for collaboration/sharing). Uses cocalc servers, so it'll be slower.
\n",
" This file is the browser version. We normally call these \"worksheets\" and each little box in a worksheet is a \"cell\". This is where you write you code on the browser. Once you've written your code, you type shift + enter
and you'll get your results.\n",
"
\n",
" Let's test out that sage works. Try doing 2+7
in sage.\n",
"
If you don't know what a function does, type the function, add a ? and hit shift+enter
for a quick description.
Try typing:
\n", "fibonacci?
\n",
"\n",
" and pressing shift+enter
\n",
"
type
function. Or you can ask directly if the object is of a certain type using isinstance
.\n",
" bool
has one of two values: True
and False
.\n",
"
\n",
" There are three boolean operators: and
, or
, & not
.\n",
"
\n",
" You can convert something into a boolean by the bool
function.\n",
"
\n",
" Notice in the following that True
returns 'bool'
as its type.\n",
"
\n",
" int
is the python built-in integer type. The problem with the python version is the integer conversion problem.\n",
" Due to this restriction, Sage uses its own type called Integer
.\n",
"
\n",
" float
is the python built-in rational number type. Due to similar restrictions, Sage uses the RealLiteral
type.\n",
"
\n",
" The str
type is fairly common. You can use single or double quotes. Additionally you can use triple quotes if you want a multi-line string. You can also fill a string using dictionary substitutions as can be seen in the following example.\n",
"
\n", " There are four more (complicated) types:\n", "
\n", "set
- is a set of items with no multiplicites or order.list
- is an ordered list of itemstuple
- is the same as a list, but it is immutable (it cannot be changed).dict
- Is an associative array which associates values with keys.\n",
" This is how you can construct a set
, list
, tuple
and dict
.\n",
"
len
function."
]
},
{
"cell_type": "code",
"execution_count": 0,
"metadata": {
"collapsed": false,
"slideshow": {
"slide_type": "fragment"
}
},
"outputs": [
],
"source": [
"print len(S)\n",
"print len(L)\n",
"print len(T)\n",
"print len(D)"
]
},
{
"cell_type": "markdown",
"metadata": {
"collapsed": false,
"slideshow": {
"slide_type": "subslide"
}
},
"source": [
"in
operator."
]
},
{
"cell_type": "code",
"execution_count": 0,
"metadata": {
"collapsed": false,
"slideshow": {
"slide_type": "fragment"
}
},
"outputs": [
],
"source": [
"S = set([1,2,3])\n",
"L = [1,2,3]\n",
"T = (1,2,3)\n",
"D = {2:1, 3:2, 4:3}\n",
"\n",
"print 1 in S\n",
"print 1 in L\n",
"print 1 in T\n",
"print 1 in D"
]
},
{
"cell_type": "code",
"execution_count": 0,
"metadata": {
"collapsed": false,
"slideshow": {
"slide_type": "fragment"
}
},
"outputs": [
],
"source": [
"print 4 not in S\n",
"print 4 not in L\n",
"print 4 not in T\n",
"print 4 not in D"
]
},
{
"cell_type": "markdown",
"metadata": {
"collapsed": false,
"slideshow": {
"slide_type": "fragment"
}
},
"source": [
"Did you notice how for `dictionaries` the in operator is looking at the key and not the value? Remember this forever!"
]
},
{
"cell_type": "markdown",
"metadata": {
"collapsed": false,
"slideshow": {
"slide_type": "subslide"
}
},
"source": [
"add
function\n",
"append
a list at the end, extend
a list with another list, or just plain add two lists together.\n",
"sort
reverse
Operation | \n", "set | \n",
" list | \n",
" tuple | \n",
" dict | \n",
"
---|---|---|---|---|
Construction | \n", "set([]) | \n",
" [] | \n",
" () | \n",
" {} | \n",
"
One element construction | \n", "set([1]) | \n",
" [1] | \n",
" (1,) | \n",
" {1:1} | \n",
"
Size | \n", "len(set) | \n",
" len(list) | \n",
" len(tuple) | \n",
" len(dict) | \n",
"
Contains | \n", "x in set | \n",
" x in list | \n",
" x in tuple | \n",
" key in dict | \n",
"
Accessing entries | \n", "- | \n", "list[0] | \n",
" tuple[0] | \n",
" dict[key] | \n",
"
Adding stuff | \n", "set.add(x) | \n",
" list.append(x) list.extend(list2) list3 = list1 + list2 | \n",
" tuple3 = tuple1 + tuple2 | \n",
" dict[key] = x key must be immutable | \n",
"
Deleting stuff | \n", "set.discard(item) | \n",
" list.pop(i) del list[i] list = list[start:end] list.remove(value) | \n",
" tuple = tuple[start:end] | \n",
" dict.pop(key) del dict[key] | \n",
"
[5,1,3,6,7,2,12]
.deepcopy
to the rescue!\n",
"None
type\n",
"\n",
" If you want to set a variable but don't want to set it as any particular type, you can use None
\n",
"
\n",
" Ranges allow us to make numerical lists easily. We use the range
function to construct a range.\n",
"
range
to construct the list of even numbers between $1$ and $40$ (including $40$).if
- Does something if the condition is true.for
- Loops through a range of objects.while
- Keeps looping until condition is false.or
and
not
- Boolean operators==
- Equal!=
- Not equal<
>
<=
>=
- Inequalitiesis
- Object identityis not
- Negated object identityif
\n",
"elif
and else
.\n",
"for
\n",
"for
loop to print out all the non-even primes from $1$ to $100$."
]
},
{
"cell_type": "code",
"execution_count": 0,
"metadata": {
"collapsed": false,
"slideshow": {
"slide_type": "fragment"
}
},
"outputs": [
],
"source": [
"# Try it yourself"
]
},
{
"cell_type": "markdown",
"metadata": {
"collapsed": false,
"slideshow": {
"slide_type": "subslide"
}
},
"source": [
"while
\n",
"for
and list
\n",
"def
statement. We can return things from functions using the return
keyword."
]
},
{
"cell_type": "code",
"execution_count": 0,
"metadata": {
"collapsed": false,
"slideshow": {
"slide_type": "fragment"
}
},
"outputs": [
],
"source": [
"def f(x):\n",
" return x^2\n",
"\n",
"f(2)"
]
},
{
"cell_type": "markdown",
"metadata": {
"collapsed": false,
"slideshow": {
"slide_type": "subslide"
}
},
"source": [
"\n", " Sage allows us to construct digraphs using dictionaries. Let's take a sidestep and look at how to construct digraphs and some operations we can do on them.\n", "
" ] }, { "cell_type": "code", "execution_count": 0, "metadata": { "collapsed": false, "slideshow": { "slide_type": "fragment" } }, "outputs": [ ], "source": [ "D = {0:[1,2,3], 1:[0,3], 2:[3,4], 4:[5,5,5,1], 5:[5]}\n", "G = DiGraph(D)\n", "G.plot()" ] }, { "cell_type": "code", "execution_count": 0, "metadata": { "collapsed": false, "slideshow": { "slide_type": "subslide" } }, "outputs": [ ], "source": [ "G.adjacency_matrix()" ] }, { "cell_type": "markdown", "metadata": { "collapsed": false, "slideshow": { "slide_type": "subslide" } }, "source": [ "Exercise: Plot your favourite digraph." ] }, { "cell_type": "code", "execution_count": 0, "metadata": { "collapsed": false, "slideshow": { "slide_type": "fragment" } }, "outputs": [ ], "source": [ "# Test this out here." ] }, { "cell_type": "markdown", "metadata": { "collapsed": false, "slideshow": { "slide_type": "slide" } }, "source": [ "Thanks for reading this tutorial!!!\n", "\n", "If you have any suggestions, just email me and let me know =D I'm more than happy to make this more accessible to everyone =)\n", "Also, feel free to use this for anything =)\n", "The more humans using sage the better =D\n", "\n", "**Email:** aram.dermenjian@gmail.com\n", "**Website:** dermenjian.com" ] } ], "metadata": { "kernelspec": { "argv": [ "sage-10.2", "--python", "-m", "sage.repl.ipython_kernel", "--matplotlib=inline", "-f", "{connection_file}" ], "display_name": "SageMath 10.2", "env": { }, "language": "sagemath", "metadata": { "cocalc": { "description": "Open-source mathematical software system", "priority": 2, "url": "https://www.sagemath.org/" } }, "name": "sage-10.2", "resource_dir": "/ext/jupyter/kernels/sage-10.2" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 2 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython2", "version": "2.7.15" } }, "nbformat": 4, "nbformat_minor": 4 }