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": [
2019-03-06 16:46:12 +00:00
"Department of Telematic Engineering Systems, Universidad Politécnica de Madrid, © 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,
2019-02-28 14:30:33 +00:00
"metadata": {},
2016-02-01 12:05:58 +00:00
"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",
2024-02-08 16:33:48 +00:00
"* **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",
2020-02-13 16:56:36 +00:00
"* **strongly typed**: the interpreter tracks variable types. There is no implicit type conversion. This means that all the type variables should be converted manually, preventing from unexpected behaviour. "
2016-02-01 12:05:58 +00:00
]
},
{
"cell_type": "code",
"execution_count": null,
2019-02-28 14:30:33 +00:00
"metadata": {},
2016-02-01 12:05:58 +00:00
"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,
2019-02-28 14:30:33 +00:00
"metadata": {},
2016-02-01 12:05:58 +00:00
"outputs": [],
"source": [
"a = 'd'\n",
"a"
]
},
{
"cell_type": "code",
"execution_count": null,
2019-02-28 14:30:33 +00:00
"metadata": {},
2016-02-01 12:05:58 +00:00
"outputs": [],
"source": [
"a = 'd' + 3\n",
"a"
]
},
{
"cell_type": "markdown",
2019-02-28 14:30:33 +00:00
"metadata": {},
2016-02-01 12:05:58 +00:00
"source": [
"## 2. Mutability"
]
},
{
"cell_type": "markdown",
2019-02-28 14:30:33 +00:00
"metadata": {},
2016-02-01 12:05:58 +00:00
"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,
2019-02-28 14:30:33 +00:00
"metadata": {},
2016-02-01 12:05:58 +00:00
"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,
2019-02-28 14:30:33 +00:00
"metadata": {},
2016-02-01 12:05:58 +00:00
"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,
2019-02-28 14:30:33 +00:00
"metadata": {},
2016-02-01 12:05:58 +00:00
"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,
2019-02-28 14:30:33 +00:00
"metadata": {},
2016-02-01 12:05:58 +00:00
"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,
2019-02-28 14:30:33 +00:00
"metadata": {},
2016-02-01 12:05:58 +00:00
"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,
2019-02-28 14:30:33 +00:00
"metadata": {},
2016-02-01 12:05:58 +00:00
"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,
2019-02-28 14:30:33 +00:00
"metadata": {},
2016-02-01 12:05:58 +00:00
"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",
2019-03-06 16:46:12 +00:00
"© 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",
2019-03-06 16:46:12 +00:00
"version": "3.7.1"
},
"latex_envs": {
"LaTeX_envs_menu_present": true,
"autocomplete": true,
"bibliofile": "biblio.bib",
"cite_by": "apalike",
"current_citInitial": 1,
"eqLabelWithNumbers": true,
"eqNumInitial": 1,
"hotkeys": {
"equation": "Ctrl-E",
"itemize": "Ctrl-I"
},
"labels_anchors": false,
"latex_user_defs": false,
"report_style_numbering": false,
"user_envs_cfg": false
2016-02-01 12:05:58 +00:00
}
},
"nbformat": 4,
2019-02-28 14:30:33 +00:00
"nbformat_minor": 1
2016-02-01 12:05:58 +00:00
}