Skip to content

Instantly share code, notes, and snippets.

@ka-pr
Last active October 23, 2019 12:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ka-pr/dd661faa40da8931edef6a5cb2e39f2b to your computer and use it in GitHub Desktop.
Save ka-pr/dd661faa40da8931edef6a5cb2e39f2b to your computer and use it in GitHub Desktop.
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "markdown",
"metadata": {
"ein.tags": "worksheet-0",
"slideshow": {
"slide_type": "-"
}
},
"source": [
"# Variables and data types!"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"You can define variables with the \"=\" operator."
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"x=1\n",
"print(x)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Every Variable represents a Class object defined by the Python language or by yourself. You do not need to know at this early stage, how it all works internally, for now just remember some useful functions that work on every variable to explore what they are and how Python deals with them. "
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"print() prints the content of a variable or statement to the command line. \n",
"type() returns the type of the object the variable is linked to. \n",
"help() prints a description of every type of object you work with. \n",
"dir() prints summary of all methods and functions associated with you variable"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"The type() function returns you the type of Object your Variable is linked/pointing to. In comparison to other languages, Python determines the type automatic. The type of a variable can change when doing a new assignment to it, or even when doing an operation. "
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
"x=1.5\n",
"print(type(x))\n",
"x=1\n",
"print(type(x))\n",
"x=x+0.5\n",
"print(type(x))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"The help() function is very useful when working with 3rd party Software written in Python. For now just call it on standard Python objects."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"create a complex number and check the type"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [],
"source": [
"x=10+1.5j\n",
"print(type(x))"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [],
"source": [
"help(x)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Ignore methods with the two underscores attached to them, as they are for internal use. \n",
"At the bottom we see that the \"complex-numbers\"-class has 2 Attributes, the real and the imaginary part. \n",
"We can call them in the following way, and see what types thous Attributes have."
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [],
"source": [
"real=x.real\n",
"imag=x.imag\n",
"print(real,imag)\n",
"print(type(real),type(imag))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"We can also see, that the complex numbers defines by Python have single method 'conjugate' for us to use. First use the help() function on this method, but without calling the method using (), to see what it does."
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [],
"source": [
"help(x.conjugate)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Call Class methods in the following way."
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [],
"source": [
"conj=x.conjugate()\n",
"print(conj)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"The dir() function shows you a short summary of all attributes and methods your variable has excess to. In the Jupyter Notebook you have the luxury, that you can instead just press the 'Tabulator' key on your keyboard after typing a '.' to see only public and a '._' to see the complete overview of options."
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"outputs": [],
"source": [
"print(dir(x))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Strings!"
]
},
{
"cell_type": "markdown",
"metadata": {
"ein.tags": "worksheet-0",
"slideshow": {
"slide_type": "-"
}
},
"source": [
"Strings are defined with single or double quotes:"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {
"autoscroll": false,
"ein.tags": "worksheet-0",
"slideshow": {
"slide_type": "-"
}
},
"outputs": [],
"source": [
"print('panda bears are cute!')"
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {
"autoscroll": false,
"ein.tags": "worksheet-0",
"slideshow": {
"slide_type": "-"
}
},
"outputs": [],
"source": [
"print(\"panda bears are cute!\")"
]
},
{
"cell_type": "markdown",
"metadata": {
"ein.tags": "worksheet-0",
"slideshow": {
"slide_type": "-"
}
},
"source": [
"Escape characters work as in other languages:"
]
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {
"autoscroll": false,
"ein.tags": "worksheet-0",
"slideshow": {
"slide_type": "-"
}
},
"outputs": [],
"source": [
"print('hello\\n\\thow\\nare\\tyou')"
]
},
{
"cell_type": "markdown",
"metadata": {
"ein.tags": "worksheet-0",
"slideshow": {
"slide_type": "-"
}
},
"source": [
"You can stick to strings together with a `+` this is called a concatentation:"
]
},
{
"cell_type": "code",
"execution_count": 12,
"metadata": {
"autoscroll": false,
"ein.tags": "worksheet-0",
"slideshow": {
"slide_type": "-"
}
},
"outputs": [],
"source": [
"'how' + 'are' + 'you'"
]
},
{
"cell_type": "markdown",
"metadata": {
"ein.tags": "worksheet-0",
"slideshow": {
"slide_type": "-"
}
},
"source": [
"strings can also be indexed"
]
},
{
"cell_type": "code",
"execution_count": 13,
"metadata": {
"autoscroll": false,
"ein.tags": "worksheet-0",
"slideshow": {
"slide_type": "-"
}
},
"outputs": [],
"source": [
"'panda bears are cute!'[6:]"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"strings can be iterated, see introduction of for statement down below"
]
},
{
"cell_type": "code",
"execution_count": 14,
"metadata": {},
"outputs": [],
"source": [
"for letter in 'panda':\n",
" print(letter)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"A string, like every thing in Python is a Class, lets call help() on a string, after that you should get the hang of it and be able to explore Python by yourself. The keyword 'str' stands for the string class."
]
},
{
"cell_type": "code",
"execution_count": 15,
"metadata": {},
"outputs": [],
"source": [
"print(dir(str))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"What could the startswith method be doing? lets find out."
]
},
{
"cell_type": "code",
"execution_count": 16,
"metadata": {},
"outputs": [],
"source": [
"help(str.startswith)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"So we should be able to test if the word 'Pandas' starts with a 'P'"
]
},
{
"cell_type": "code",
"execution_count": 17,
"metadata": {},
"outputs": [],
"source": [
"'Pandas'.startswith('P')"
]
},
{
"cell_type": "markdown",
"metadata": {
"ein.tags": "worksheet-0",
"slideshow": {
"slide_type": "-"
}
},
"source": [
"# Iterables"
]
},
{
"cell_type": "markdown",
"metadata": {
"ein.tags": "worksheet-0",
"slideshow": {
"slide_type": "-"
}
},
"source": [
"## lists"
]
},
{
"cell_type": "code",
"execution_count": 58,
"metadata": {
"autoscroll": false,
"ein.tags": "worksheet-0",
"slideshow": {
"slide_type": "-"
}
},
"outputs": [],
"source": [
"x='pandas'\n",
"y='brown'\n",
"z='black'\n",
"bears = [x,y,z]"
]
},
{
"cell_type": "markdown",
"metadata": {
"ein.tags": "worksheet-0",
"slideshow": {
"slide_type": "-"
}
},
"source": [
"lists are for storing variables in an unordered way.\n",
"The variables can be accessed via their index starting with 0 using the [] brackets."
]
},
{
"cell_type": "code",
"execution_count": 64,
"metadata": {
"autoscroll": false,
"ein.tags": "worksheet-0",
"slideshow": {
"slide_type": "-"
}
},
"outputs": [],
"source": [
"bears[0]"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Subsets of lists can be created using the : inside the brackets. \n",
"Note that indexing in Python starts with 0 and that the element at the last index is NOT included in the subset! "
]
},
{
"cell_type": "code",
"execution_count": 63,
"metadata": {
"autoscroll": false,
"ein.tags": "worksheet-0",
"slideshow": {
"slide_type": "-"
}
},
"outputs": [],
"source": [
"bears[0:2]"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"negative index means starting from the last entry of the list."
]
},
{
"cell_type": "code",
"execution_count": 21,
"metadata": {
"autoscroll": false,
"ein.tags": "worksheet-0",
"slideshow": {
"slide_type": "-"
}
},
"outputs": [],
"source": [
"bears[-1]"
]
},
{
"cell_type": "markdown",
"metadata": {
"ein.tags": "worksheet-0",
"slideshow": {
"slide_type": "-"
}
},
"source": [
"lists can have there insides changed"
]
},
{
"cell_type": "code",
"execution_count": 22,
"metadata": {
"autoscroll": false,
"ein.tags": "worksheet-0",
"slideshow": {
"slide_type": "-"
}
},
"outputs": [],
"source": [
"bears.append('red')"
]
},
{
"cell_type": "code",
"execution_count": 23,
"metadata": {
"autoscroll": false,
"ein.tags": "worksheet-0",
"slideshow": {
"slide_type": "-"
}
},
"outputs": [],
"source": [
"bears"
]
},
{
"cell_type": "code",
"execution_count": 24,
"metadata": {
"autoscroll": false,
"ein.tags": "worksheet-0",
"slideshow": {
"slide_type": "-"
}
},
"outputs": [],
"source": [
"bears[1] = 'blue'"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"lists have their own set of methods attached to them as well"
]
},
{
"cell_type": "code",
"execution_count": 25,
"metadata": {
"autoscroll": false,
"ein.tags": "worksheet-0",
"slideshow": {
"slide_type": "-"
}
},
"outputs": [],
"source": [
"bears.sort()"
]
},
{
"cell_type": "code",
"execution_count": 26,
"metadata": {
"autoscroll": false,
"ein.tags": "worksheet-0",
"slideshow": {
"slide_type": "-"
}
},
"outputs": [],
"source": [
"bears"
]
},
{
"cell_type": "markdown",
"metadata": {
"ein.tags": "worksheet-0",
"slideshow": {
"slide_type": "-"
}
},
"source": [
"you can do things to the items in a list"
]
},
{
"cell_type": "code",
"execution_count": 27,
"metadata": {
"autoscroll": false,
"ein.tags": "worksheet-0",
"slideshow": {
"slide_type": "-"
}
},
"outputs": [],
"source": [
"bears[1] = bears[1] + ' is not a type of bear'"
]
},
{
"cell_type": "code",
"execution_count": 28,
"metadata": {
"autoscroll": false,
"ein.tags": "worksheet-0",
"slideshow": {
"slide_type": "-"
}
},
"outputs": [],
"source": [
"bears"
]
},
{
"cell_type": "markdown",
"metadata": {
"ein.tags": "worksheet-0",
"slideshow": {
"slide_type": "-"
}
},
"source": [
"you can iterate through a list (it's an iterable!)"
]
},
{
"cell_type": "code",
"execution_count": 29,
"metadata": {
"autoscroll": false,
"ein.tags": "worksheet-0",
"slideshow": {
"slide_type": "-"
}
},
"outputs": [],
"source": [
"for variable in bears:\n",
" print(variable,type(variable))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Bonus1: List comprehension is nice for creating lists out of iterators."
]
},
{
"cell_type": "code",
"execution_count": 30,
"metadata": {},
"outputs": [],
"source": [
"[x*x for x in [1,2,3,5,7,11]]"
]
},
{
"cell_type": "code",
"execution_count": 67,
"metadata": {},
"outputs": [],
"source": [
"xx, yy, zz = [1,2,3], [4,5,6], [7,8,9]\n",
"[[a,b,c] for a in xx for b in yy for c in zz]"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Bonus2: Using the \"enumerate\" function lets you work with both, the elment itself and its index!"
]
},
{
"cell_type": "code",
"execution_count": 65,
"metadata": {},
"outputs": [],
"source": [
"for i_variable, variable in enumerate(bears):\n",
" print(i_variable, variable)"
]
},
{
"cell_type": "markdown",
"metadata": {
"ein.tags": "worksheet-0",
"slideshow": {
"slide_type": "-"
}
},
"source": [
"## Tuples"
]
},
{
"cell_type": "markdown",
"metadata": {
"ein.tags": "worksheet-0",
"slideshow": {
"slide_type": "-"
}
},
"source": [
"Tuples are like lists but you can't change their insides. This is called \"immutable\". Lists are mutable."
]
},
{
"cell_type": "code",
"execution_count": 32,
"metadata": {
"autoscroll": false,
"ein.tags": "worksheet-0",
"slideshow": {
"slide_type": "-"
}
},
"outputs": [],
"source": [
"cats = ('fat', 'lazy', 'garfield')"
]
},
{
"cell_type": "code",
"execution_count": 33,
"metadata": {
"autoscroll": false,
"ein.tags": "worksheet-0",
"slideshow": {
"slide_type": "-"
}
},
"outputs": [],
"source": [
"cats[2] = 'tails'"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"tupels can be stored in a List, what can be useful if you want to see the tupel as one thing that is not supposed to change. For example a point is only valid with immutable (x,y,z) coordinates, a list of points may define a cube."
]
},
{
"cell_type": "code",
"execution_count": 34,
"metadata": {},
"outputs": [],
"source": [
"a=(0,0,0)\n",
"b=(1,0,0)\n",
"c=(1,1,0)\n",
"d=(0,1,0)\n",
"e=(0,0,1)\n",
"f=(1,0,1)\n",
"g=(1,1,1)\n",
"h=(0,1,1)\n",
"Cube=[a,b,c,d,e,f,g,h]"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Tupels can be iterated the same as list can"
]
},
{
"cell_type": "code",
"execution_count": 35,
"metadata": {},
"outputs": [],
"source": [
"for point in Cube: #iter over list get a tupel each time\n",
" for value in point: #iter over tupel get a int each time\n",
" print(value)"
]
},
{
"cell_type": "markdown",
"metadata": {
"ein.tags": "worksheet-0",
"slideshow": {
"slide_type": "-"
}
},
"source": [
"## Dictionaries"
]
},
{
"cell_type": "markdown",
"metadata": {
"ein.tags": "worksheet-0",
"slideshow": {
"slide_type": "-"
}
},
"source": [
"dictionaries are a data type called a key/value pair.\n",
"\n",
"* Curly braces create dictionaries.\n",
"* I Contents of a dictionary are unordered.\n",
"* I Keys are unique"
]
},
{
"cell_type": "code",
"execution_count": 36,
"metadata": {
"autoscroll": false,
"ein.tags": "worksheet-0",
"slideshow": {
"slide_type": "-"
}
},
"outputs": [],
"source": [
"cats = {'marly':[12, 'tabby'], 'garfield':[8, 'calico']}"
]
},
{
"cell_type": "code",
"execution_count": 37,
"metadata": {},
"outputs": [],
"source": [
"type(cats)"
]
},
{
"cell_type": "code",
"execution_count": 38,
"metadata": {},
"outputs": [],
"source": [
"cats.keys(),cats.values()"
]
},
{
"cell_type": "code",
"execution_count": 39,
"metadata": {
"autoscroll": false,
"ein.tags": "worksheet-0",
"slideshow": {
"slide_type": "-"
}
},
"outputs": [],
"source": [
"cats['marly']"
]
},
{
"cell_type": "markdown",
"metadata": {
"ein.tags": "worksheet-0",
"slideshow": {
"slide_type": "-"
}
},
"source": [
"To check if a dict has a certain key you can do:"
]
},
{
"cell_type": "code",
"execution_count": 40,
"metadata": {
"autoscroll": false,
"ein.tags": "worksheet-0",
"slideshow": {
"slide_type": "-"
}
},
"outputs": [],
"source": [
"'marly' in cats"
]
},
{
"cell_type": "markdown",
"metadata": {
"ein.tags": "worksheet-0",
"slideshow": {
"slide_type": "-"
}
},
"source": [
"Delete an element from the dictionary:"
]
},
{
"cell_type": "code",
"execution_count": 41,
"metadata": {
"autoscroll": false,
"ein.tags": "worksheet-0",
"slideshow": {
"slide_type": "-"
}
},
"outputs": [],
"source": [
"del cats['marly']"
]
},
{
"cell_type": "code",
"execution_count": 42,
"metadata": {
"autoscroll": false,
"ein.tags": "worksheet-0",
"slideshow": {
"slide_type": "-"
}
},
"outputs": [],
"source": [
"cats"
]
},
{
"cell_type": "markdown",
"metadata": {
"ein.tags": "worksheet-0",
"slideshow": {
"slide_type": "-"
}
},
"source": [
"To construct a dictionary from a list of tuples:"
]
},
{
"cell_type": "code",
"execution_count": 69,
"metadata": {
"autoscroll": false,
"ein.tags": "worksheet-0",
"slideshow": {
"slide_type": "-"
}
},
"outputs": [],
"source": [
"dict([('seismology', 254),\n",
"('seismics', 250),\n",
" ('vulcanology', 253)])"
]
},
{
"cell_type": "markdown",
"metadata": {
"ein.tags": "worksheet-0",
"slideshow": {
"slide_type": "-"
}
},
"source": [
"# Indentation"
]
},
{
"cell_type": "markdown",
"metadata": {
"ein.tags": "worksheet-0",
"slideshow": {
"slide_type": "-"
}
},
"source": [
"## INDENTATION IS IMPORTANT!\n",
"* Don’t mix tabs an spaces.\n",
"* Python looks at the exact whitespace characters in your\n",
"source.\n",
"* Your editor doesn’t show you if there’s a tab or 8 spaces,\n",
"but it matters to python.\n",
"* Your editor can be set to use only spaces, even when you\n",
"press TAB."
]
},
{
"cell_type": "code",
"execution_count": 44,
"metadata": {
"autoscroll": false,
"ein.tags": "worksheet-0",
"slideshow": {
"slide_type": "-"
}
},
"outputs": [],
"source": [
"for x in range(5):\n",
"print(x)"
]
},
{
"cell_type": "code",
"execution_count": 45,
"metadata": {
"autoscroll": false,
"ein.tags": "worksheet-0",
"slideshow": {
"slide_type": "-"
}
},
"outputs": [],
"source": [
"for x in range(5):\n",
" print(x)"
]
},
{
"cell_type": "code",
"execution_count": 46,
"metadata": {
"autoscroll": false,
"ein.tags": "worksheet-0",
"slideshow": {
"slide_type": "-"
}
},
"outputs": [],
"source": [
"if 'garfield' in cats:\n",
"print(cats['garfield'])"
]
},
{
"cell_type": "code",
"execution_count": 47,
"metadata": {
"autoscroll": false,
"ein.tags": "worksheet-0",
"slideshow": {
"slide_type": "-"
}
},
"outputs": [],
"source": [
"if 'garfield' in cats:\n",
" print(cats['garfield'])"
]
},
{
"cell_type": "markdown",
"metadata": {
"ein.tags": "worksheet-0",
"slideshow": {
"slide_type": "-"
}
},
"source": [
"# for statements"
]
},
{
"cell_type": "markdown",
"metadata": {
"ein.tags": "worksheet-0",
"slideshow": {
"slide_type": "-"
}
},
"source": [
"The for statement iterates over the items of any sequence:"
]
},
{
"cell_type": "code",
"execution_count": 48,
"metadata": {
"autoscroll": false,
"ein.tags": "worksheet-0",
"slideshow": {
"slide_type": "-"
}
},
"outputs": [],
"source": [
"starfleet_captains = ['kirk', 'janeway', 'picard', 'sisko', 'scott bakula']\n",
"\n",
"for captain in starfleet_captains:\n",
" print(captain)"
]
},
{
"cell_type": "code",
"execution_count": 49,
"metadata": {
"autoscroll": false,
"ein.tags": "worksheet-0",
"slideshow": {
"slide_type": "-"
}
},
"outputs": [],
"source": [
"for letter in 'star trek':\n",
" print(letter)"
]
},
{
"cell_type": "markdown",
"metadata": {
"ein.tags": "worksheet-0",
"slideshow": {
"slide_type": "-"
}
},
"source": [
"## The break statement"
]
},
{
"cell_type": "markdown",
"metadata": {
"ein.tags": "worksheet-0",
"slideshow": {
"slide_type": "-"
}
},
"source": [
"The break statement let’s you abort a loop abnormally:"
]
},
{
"cell_type": "code",
"execution_count": 70,
"metadata": {
"autoscroll": false,
"ein.tags": "worksheet-0",
"slideshow": {
"slide_type": "-"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"I want: sushi pizza\n"
]
}
],
"source": [
"good_foods = ['pizza', 'sushi', 'curry']\n",
"menu = ['hamburgers', 'sushi', 'pizza', 'curry', 'salad']\n",
"wanted = []\n",
"\n",
"for food in menu:\n",
"\n",
" if food in good_foods:\n",
" wanted.append( food )\n",
"\n",
" if len(wanted) == 2:\n",
" break # two things are enough today...\n",
"\n",
"print(\"I want: \" + ' '.join(wanted))"
]
},
{
"cell_type": "markdown",
"metadata": {
"ein.tags": "worksheet-0",
"slideshow": {
"slide_type": "-"
}
},
"source": [
"# Functions"
]
},
{
"cell_type": "markdown",
"metadata": {
"ein.tags": "worksheet-0",
"slideshow": {
"slide_type": "-"
}
},
"source": [
"The def keyword starts a function definition:"
]
},
{
"cell_type": "code",
"execution_count": 51,
"metadata": {
"autoscroll": false,
"ein.tags": "worksheet-0",
"slideshow": {
"slide_type": "-"
}
},
"outputs": [],
"source": [
"def powers_of_two(n):\n",
" \"\"\"\n",
" returns a list of powers of 2\n",
" starting from 0 and progressing\n",
" until n elements are generated.\n",
" \"\"\"\n",
" results = []\n",
"\n",
" for i in range(n):\n",
" results.append( 2**i )\n",
"\n",
" return results"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"The help() function works also on custom defined functions."
]
},
{
"cell_type": "code",
"execution_count": 52,
"metadata": {},
"outputs": [],
"source": [
"help(powers_of_two)"
]
},
{
"cell_type": "code",
"execution_count": 53,
"metadata": {
"autoscroll": false,
"ein.tags": "worksheet-0",
"slideshow": {
"slide_type": "-"
}
},
"outputs": [],
"source": [
"powers_of_two(10)"
]
},
{
"cell_type": "markdown",
"metadata": {
"ein.tags": "worksheet-0",
"slideshow": {
"slide_type": "-"
}
},
"source": [
"Sometimes you have a function taking several arguments:"
]
},
{
"cell_type": "code",
"execution_count": 54,
"metadata": {
"autoscroll": false,
"ein.tags": "worksheet-0",
"slideshow": {
"slide_type": "-"
}
},
"outputs": [],
"source": [
"def add_two_numbers(a, b):\n",
" \"\"\"\n",
" adds two input numbers together\n",
" returns the sum\n",
" \"\"\"\n",
"\n",
"\n",
" summed = a + b\n",
"\n",
" return summed"
]
},
{
"cell_type": "code",
"execution_count": 55,
"metadata": {
"autoscroll": false,
"ein.tags": "worksheet-0",
"slideshow": {
"slide_type": "-"
}
},
"outputs": [],
"source": [
"add_two_numbers(50, 125)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# import Functions and Libraries"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Python comes with a wide range of libraries that define functions and classes for all sorts of tasks, making the language so useful. You can import libraries using the import statement. Lets import a script that prints the most important rules to follow, when coding in Python."
]
},
{
"cell_type": "code",
"execution_count": 56,
"metadata": {},
"outputs": [],
"source": [
"import this"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"You can import single functions from a hole library using the 'from' statement,\n",
"You can import libraries and rename them in place using the 'as' for better readability later on."
]
},
{
"cell_type": "code",
"execution_count": 57,
"metadata": {},
"outputs": [],
"source": [
"from math import ceil\n",
"from math import ceil as up\n",
"print(ceil(2.3),up(2.3))"
]
}
],
"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.7.3"
},
"name": "introduction to python.ipynb"
},
"nbformat": 4,
"nbformat_minor": 1
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment