1
0
mirror of https://github.com/gsi-upm/sitc synced 2024-11-18 04:22:28 +00:00
sitc/python/1_7_Variables.ipynb

331 lines
7.1 KiB
Plaintext
Raw Normal View History

2016-02-01 12:05:58 +00:00
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"![](http://www.upm.es/sfs/Rectorado/Gabinete%20del%20Rector/Logos/UPM/EscPolitecnica/EscUpmPolit_p.gif \"UPM\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Course Notes for Learning Intelligent Systems"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
2016-02-15 08:01:37 +00:00
"Department of Telematic Engineering Systems, Universidad Politécnica de Madrid, © 2016 Carlos A. Iglesias"
2016-02-01 12:05:58 +00:00
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Variables"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## 1. Strongly typed and dynamically typed"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"As we have seen previously, in Python we can assign an object to a variable with the assignment operator (=).\n",
"\n",
"Binding a variable in Python means setting a name to hold a reference to some object. Assignment creates references, not copies.\n",
"\n",
"Names in Python do not have an intrinsic type. Objects have types. Python determines the type of the reference automatically based on the data object assigned to it.\n",
"\n",
"In Python, everything is an object, there no difference between primitive types, functions and objects: all are objects."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"a = 2\n",
"a"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Python is a **strongly typed** language and **dynamically typed** language.\n",
"\n",
"This means:\n",
"* ** dynamically typed**: variables do not declare a static type (as in Java int a = 2;). Variables have no type themselves, they are just names that hold a reference to some object. The type of the variable is changed dynamically when you change the type of the assigned data object. \n",
"* **strongly typed**: the interpreter tracks variable types. There is no explicit type conversion. For example, in Javascript we can "
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"type(a)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Exercises"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Can we assign a String value to the variable a? \n",
"Can we assign the expression 'd' + 3 to the variable a?\n",
"\n",
"\n",
"Give reasons for your answers and check them below."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"a = 'd'\n",
"a"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"a = 'd' + 3\n",
"a"
]
},
{
"cell_type": "markdown",
"metadata": {
"collapsed": true
},
"source": [
"## 2. Mutability"
]
},
{
"cell_type": "markdown",
"metadata": {
"collapsed": false
},
"source": [
"Objects whose value can change are said to be **mutable**; objects whose value is unchangeable once they are created are called **immutable**.\n",
"\n",
2016-02-23 15:30:29 +00:00
"* **Mutable types**: integers, floats, strings, lists, set\n",
"* **Inmutable types**: tuples, ranges, dictionaries"
2016-02-01 12:05:58 +00:00
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"# Exercise mutable type\n",
"a = 10\n",
"b = a\n",
"b = 5\n",
"print('a', a)\n",
"print('b', b)\n",
"\n",
"# Which will be the values of a and b?"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"# Exercise mutable type\n",
"a = 10\n",
"a = 10 + 1\n",
"print(a)\n",
"\n",
"# Can we change 'a' if it is inmutable?"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"# Exercise mutable type\n",
"a = \"Hola\"\n",
"b = a\n",
"b = \"Hi\"\n",
"print('a', a)\n",
"print('b', b)\n",
"\n",
"# Which will be the values of a and b?"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
2017-02-16 16:57:23 +00:00
"# Exercise mutable type\n",
2016-02-01 12:05:58 +00:00
"a = [1, 2, 3]\n",
"b = a\n",
"a.append(4)\n",
"print('a', a)\n",
"print('b', b)\n",
"\n",
2016-02-23 15:30:29 +00:00
"# Which will be the value of a and b?"
2016-02-01 12:05:58 +00:00
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## 3. Scope"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"# Example of a local variable\n",
"# The scope defines the visibility of a name within a block. \n",
"# If a local variable is defined in a block, its scope includes that block. \n",
"\n",
"c = 3\n",
"def add(a, b):\n",
" c = a + b\n",
" return c\n",
"d = add(2, 2)\n",
"print('d', d)\n",
"print('c', c)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"# Access global variables\n",
"\n",
"c = 3\n",
"def add(a, b):\n",
" global c\n",
" c = a + b\n",
" return c\n",
"d = add(2, 2)\n",
"print('d', d)\n",
"print('c', c)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## 4. Constants\n",
"\n",
"In Python there is no way to specify that a variable is constant, but there is the convention to write their names in all caps, with underscores separating words."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"NUMBER_OF_LIFES = 5\n",
"\n",
"print(NUMBER_OF_LIFES)\n",
"\n",
"# built-in constants usually do no follow the convention \n",
"import math\n",
"print(math.pi)\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Licence"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"The notebook is freely licensed under under the [Creative Commons Attribution Share-Alike license](https://creativecommons.org/licenses/by/2.0/). \n",
"\n",
2016-02-15 08:01:37 +00:00
"© 2016 Carlos A. Iglesias, Universidad Politécnica de Madrid."
2016-02-01 12:05:58 +00:00
]
}
],
"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.5.1"
}
},
"nbformat": 4,
"nbformat_minor": 0
}