Commit 5ca4d9f2 authored by Arucas Chacon's avatar Arucas Chacon

Completed notebooks

parent 2820c217
This diff is collapsed.
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Variables and assignment"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Use variables to store values"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"* Variables are names for values.\n",
"* In Python the = symbol assigns the value on the right to the name on the left.\n",
"* The variable is created when a value is assigned to it.\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"weight_kg = 55"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"* Variable names:\n",
" * can’t start with a digit\n",
" * can’t contain spaces, quotation marks, or other punctuation\n",
" * may contain an underscore (typically used to separate words in long variable names)\n",
"* Underscores at the start like __alistairs_real_age have a special meaning so we won’t do that until we understand the convention."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Use print to display values"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"* Python has a built-in function called <span class=\"label label-default\">print</span> that prints things as text.\n",
"* To add a string to the printout, wrap the string in single quotations."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"print(weight_kg)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"print('weight in kg:', weight_kg)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Variables must be created before they are used."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"print(last_name)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<div class=\"alert alert-success\" role=\"alert\">\n",
"Variables defined in one cell exist in all other cells once executed, so the relative location of cells in the notebook does\n",
"not matter (i.e., cells lower down can still affect those above). Remember: Notebook cells are just a way to organize a program: as far as Python is concerned, all of the source code is one long set of instructions.\n",
"</div>\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Variables can be used in calculations."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"print('weight in pounds:', 2.2 * weight_kg)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"weight_lb = 2.2 * weight_kg\n",
"print('weight in kilograms:', weight_kg, 'and in pounds:', weight_lb)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Use an index to get a single character from a string.\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"* Strings are lists of characters.\n",
"* <span class=\"label label-success\">Indices start numbered from 0</span>.\n",
"* Use the position’s index in square brackets to get the character at that position."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"workshop_name = 'Software carpentry'\n",
"print(workshop_name)\n",
"print('the first character is',workshop_name[0])"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Use a slice to get a substring.\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"* A part of a string is called a substring. A substring can be as short as a single character.\n",
"* We take a slice of a string by using <span class=\"label label-default\">[start:stop]</span>, where start is replaced with the index of the first element we want and stop is replaced with the index of the element just after the last element we want.\n",
"* Mathematically, you might say that a slice selects [start:stop), from start to stop-1\n",
"* Taking a slice does not change the contents of the original string. Instead, the slice is a copy of part of the original string.\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"print(workshop_name[0:6])\n",
"print(workshop_name[2:10])"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Use the built-in function len to find the length of a string.\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"print(len(workshop_name))\n",
"print(len(workshop_name[1:6]))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Python is case-sensitive."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"* Python thinks that upper- and lower-case letters are different, so <span class=\"label label-default\">Name</span> and <span class=\"label label-default\">name</span> are different variables.\n",
"* There are conventions for using upper-case letters at the start of variable names so we will use lower-case letters for now."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Use meaningful variable names."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"qwert = 55\n",
"poiu = \"Arucas'\"\n",
"print (poiu, 'weight is', qwert)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<div class=\"panel panel-warning\">\n",
" <div class=\"panel-heading\">\n",
" <h3 class=\"panel-title\">Exercise - Swapping Values</h3>\n",
" </div>\n",
" <div class=\"panel-body\">\n",
" Fill the table showing the values of the variables in this program after each statement is executed.\n",
" </div>\n",
"</div>"
]
},
{
"cell_type": "raw",
"metadata": {},
"source": [
"# Command # Value of x # Value of y # Value of swap #\n",
"x = 1.0 # # # #\n",
"y = 3.0 # # # #\n",
"swap = x # # # #\n",
"x = y # # # #\n",
"y = swap # # # #"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<div class=\"panel panel-warning\">\n",
" <div class=\"panel-heading\">\n",
" <h3 class=\"panel-title\">Exercise - Predicting Values</h3>\n",
" </div>\n",
" <div class=\"panel-body\">\n",
"What is the final value of <span class=\"label label-default\">position</span> in the program below? (Try to predict the value without running the program, then check your prediction.)\n",
"\n",
" </div>\n",
"</div>"
]
},
{
"cell_type": "raw",
"metadata": {},
"source": [
"initial = \"left\"\n",
"position = initial\n",
"initial = \"right\""
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<div class=\"panel panel-warning\">\n",
" <div class=\"panel-heading\">\n",
" <h3 class=\"panel-title\">Exercise - Choosing a name</h3>\n",
" </div>\n",
" <div class=\"panel-body\">\n",
"Which is a better variable name, <span class=\"label label-default\">m</span>, <span class=\"label label-default\">min</span>, or <span class=\"label label-default\">minutes</span>? Why? Hint: think about which code you would rather inherit from someone who is leaving the lab:\n",
"\n",
" </div>\n",
"</div>\n",
"\n",
"\n",
"\n"
]
},
{
"cell_type": "raw",
"metadata": {},
"source": [
"ts = m * 60 + s\n",
"tot_sec = min * 60 + sec\n",
"total_seconds = minutes * 60 + seconds"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<div class=\"panel panel-primary\">\n",
" <div class=\"panel-heading\">\n",
" <h3 class=\"panel-title\">Key Points</h3>\n",
" </div>\n",
" <div class=\"panel-body\">\n",
" <ul>\n",
" <li>Use variables to store values.</li>\n",
" <li>Use print to display values.</li>\n",
" <li>Variables persist between cells.</li>\n",
" <li>Variables must be created before they are used.</li>\n",
" <li>Variables can be used in calculations.</li>\n",
" <li>Use an index to get a single character from a string.</li>\n",
" <li>Use a slice to get a substring.</li>\n",
" <li>Use the built-in function len to find the length of a string.</li>\n",
" <li>Python is case-sensitive.</li>\n",
" <li>Use meaningful variable names.</li> \n",
" </ul> \n",
"</div>\n",
"</div>"
]
}
],
"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.6.1"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Data Types and Type Conversion\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Every value has a type."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"* Every value in a program has a specific type.\n",
"* For instance: integer,float, string\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true,
"scrolled": true
},
"outputs": [],
"source": [
"age = 32\n",
"pi = 3.14\n",
"first_name = 'Arucas'\n",
"print(age)\n",
"print(pi)\n",
"print(first_name)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Use the built-in function type to find the type of a value.\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"print(type(age))\n",
"print(type(pi))\n",
"print(type(first_name))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Types control what operations (or methods) can be performed on a given value.\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"print(5-2)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"print('hello' - 'h')"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## You can use the “+” and “*” operators on strings.\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"full_name = 'Arucas' + ' ' + 'Chacon'\n",
"print(full_name)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"separator = '=' * 10\n",
"print(separator)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Strings have a length (but numbers don’t)."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"print(len(full_name))"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"print(len(pi))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Must convert numbers to strings or vice versa when operating on them.\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"print(1 + '2')"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true,
"scrolled": true
},
"outputs": [],
"source": [
"print(1 + int('2'))\n",
"print(str(1) + '2')"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"print(\"string to float:\", float(\"3.4\"))\n",
"print(\"float to int:\", int(3.9))\n",
"print(\"int to float:\", float(3))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Can mix integers and floats freely in operations.\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"print('half is', 1 / 2.0)\n",
"print('three squared is', 3.0 ** 2)\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Complex numbers"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Python provides complex numbers, which are written as <span class=\"label label-default\">1.0+2.0j</span>. If val is an imaginary number, its real and imaginary parts can be accessed using dot notation as <span class=\"label label-default\">val.real</span> and <span class=\"label label-default\">val.imag</span>."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"val = 1.0+2.0j\n",
"print(val.real)\n",
"print(val.imag)\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<div class=\"panel panel-warning\">\n",
" <div class=\"panel-heading\">\n",
" <h3 class=\"panel-title\">Exercise - Types</h3>\n",
" </div>\n",
" <div class=\"panel-body\">\n",
" What type of value is 3.4?<br>\n",
" What type of value is 3.25 + 4?<br>\n",
" What type of value is 4/2?<br>\n",
" </div>\n",
"</div>"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<div class=\"panel panel-warning\">\n",
" <div class=\"panel-heading\">\n",
" <h3 class=\"panel-title\">Exercise - Division Types</h3>\n",
" </div>\n",
" <div class=\"panel-body\">\n",
"In Python 3, the // operator performs integer (whole-number) floor division, the / operator performs floating-point division, and the ‘%’ (or modulo) operator calculates and returns the remainder from integer division.<br>\n",
"\n",
"However in Python2 (and other languages), the / operator between two integer types perform a floor (//) division. To perform a float division, we have to convert one of the integers to float\n",
" </div>\n",
"</div>\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"print('5 // 3 =', 5//3)\n",
"print('5 / 3 =', 5/3)\n",
"print('5 % 3 =', 5%3)\n",
"print('float(5)/3 =', float(5) / 3 )\n",
"\n",
"\n"
]
},
{
"cell_type": "markdown",
"metadata": {
"collapsed": true
},
"source": [
"\n",
"<div class=\"panel panel-warning\">\n",
" <div class=\"panel-heading\">\n",
" <h3 class=\"panel-title\">Exercise - Arithmetic with Different Types</h3>\n",
" </div>\n",
" <div class=\"panel-body\">\n",
" Which of the following will print 2.0? Note: there may be more than one right answer.<br>\n",
" first = 1.0<br>\n",
"second = \"1\"<br>\n",
"third = \"1.1\"<br>\n",
"<br> \n",
"<br> \n",
"1. first + float(second)<br>\n",
"2. float(second) + float(third)<br>\n",
"3. first + int(third)<br>\n",
"4. first + int(float(third))<br>\n",
"5. int(first) + int(float(third))<br>\n",
"6. 2.0 * second\n",
" </div>\n",
"</div>\n",
"\n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"first = 1.0\n",
"second = \"1\"\n",
"third = \"1.1\"\n",
"\n",
"first + float(second)\n",
"float(second) + float(third)\n",
"#first + int(third)\n",
"first + int(float(third))\n",
"int(first) + int(float(third))\n",
"2.0 * second"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<div class=\"panel panel-primary\">\n",
" <div class=\"panel-heading\">\n",
" <h3 class=\"panel-title\">Key Points</h3>\n",
" </div>\n",
" <div class=\"panel-body\">\n",
" <ul>\n",
" <li>Every value has a type.</li>\n",
" <li>Use the built-in function <span class=\"label label-default\">type()</span> to find the type of a value.</li>\n",
" <li>Types control what operations can be done on values.</li>\n",
" <li>Strings can be added and multiplied.</li>\n",
" <li>Strings have a length (but numbers don’t).</li>\n",
" <li>Must convert numbers to strings or vice versa when operating on them.</li>\n",
" <li>Must convert numbers to strings or vice versa when operating on them.</li>\n",
" <li>Can mix integers and floats freely in operations.</li>\n",
" </ul> \n",
"</div>\n",
"</div>\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.6.1"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
<
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Built-in Functions and Help"
]
},
{
"cell_type": "markdown",
"metadata": {
"collapsed": true
},
"source": [
"## Use comments to add documentation to programs."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"# This is a comment"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## A function may take zero or more arguments.\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"* We've alreadys een some built-in functions\n",
"* print(), len(), int(), float(), str()\n",
"* Always with parenthesis, even if empty"