"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."
"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",
"* 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>."
"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",