{ "cells": [ { "cell_type": "markdown", "id": "aa822bce-181c-42bc-a695-db004af6fa21", "metadata": {}, "source": [ "# Lesson 04 in-class demo\n", "\n", "## 1. Data types\n", "\n", "### 1.1. Strings" ] }, { "cell_type": "code", "execution_count": 1, "id": "af516941-4bd2-4628-b384-8fd3edb02e9b", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Hello world!\n" ] } ], "source": [ "my_string = 'Hello world!'\n", "print(my_string)" ] }, { "cell_type": "code", "execution_count": 2, "id": "c17287ec-8f25-404b-bd33-2c22d1f533c0", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "str" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "type(my_string)" ] }, { "cell_type": "code", "execution_count": 3, "id": "b01f5697-76fc-4ac6-ba3c-28772cebe177", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "12" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "len(my_string)" ] }, { "cell_type": "code", "execution_count": 4, "id": "6dfafe53-9da2-496a-9e65-bee56ebe65fc", "metadata": {}, "outputs": [], "source": [ "lower_string = my_string.lower()" ] }, { "cell_type": "code", "execution_count": 5, "id": "d71b04ef-84a7-476e-a07b-1094f9effd44", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "hello world!\n" ] } ], "source": [ "print(lower_string)" ] }, { "cell_type": "markdown", "id": "5141666f-80d9-44ec-a268-a51a8aac51eb", "metadata": {}, "source": [ "### 1.2. Numbers" ] }, { "cell_type": "code", "execution_count": 6, "id": "716f5bec-3111-4cbe-9db3-805121fae850", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "32\n" ] } ], "source": [ "my_int = 32\n", "print(my_int)" ] }, { "cell_type": "code", "execution_count": 7, "id": "9ab7de70-d2a7-46df-9f7f-54d0e850abd7", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "int" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "type(my_int)" ] }, { "cell_type": "code", "execution_count": 8, "id": "58d2d8c6-8735-4471-bb25-fa02e8b880a9", "metadata": {}, "outputs": [], "source": [ "my_float = 32.0" ] }, { "cell_type": "code", "execution_count": 9, "id": "8c844c76-427d-4d3e-8b4f-13d0f2cba773", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "32.0\n" ] } ], "source": [ "print(my_float)" ] }, { "cell_type": "code", "execution_count": 10, "id": "d3e83fb2-b065-435f-bbaa-df8d4275a71f", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "float" ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "type(my_float)" ] }, { "cell_type": "code", "execution_count": 11, "id": "f2904641-b26c-4ce3-9c51-d3f0f3770669", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "32.0" ] }, "execution_count": 11, "metadata": {}, "output_type": "execute_result" } ], "source": [ "my_float" ] }, { "cell_type": "code", "execution_count": 12, "id": "067a2a02-f9b6-4865-a36a-f09c5422f2f6", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "32" ] }, "execution_count": 12, "metadata": {}, "output_type": "execute_result" } ], "source": [ "int(my_float)" ] }, { "cell_type": "code", "execution_count": 13, "id": "121b7ca3-37d1-4548-98a7-2ae084b3b026", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "int" ] }, "execution_count": 13, "metadata": {}, "output_type": "execute_result" } ], "source": [ "type(int(my_float))" ] }, { "cell_type": "code", "execution_count": 14, "id": "afe8dc40-0411-4f54-a43b-508c64420a41", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "32.0\n" ] } ], "source": [ "print(my_float)" ] }, { "cell_type": "code", "execution_count": 15, "id": "0a9f99b5-de86-4a18-ba40-b833fb9d9b61", "metadata": {}, "outputs": [], "source": [ "my_float = 5" ] }, { "cell_type": "code", "execution_count": 16, "id": "b513da24-70ed-4de8-9c13-ac4a437b2e69", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "5\n" ] } ], "source": [ "print(my_float)" ] }, { "cell_type": "markdown", "id": "b37afc7f-160d-48b9-8f28-0de251ba4f8e", "metadata": {}, "source": [ "### 1.3. Lists" ] }, { "cell_type": "code", "execution_count": 17, "id": "fdfce683-3868-4c30-824f-c11f877725ab", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "list" ] }, "execution_count": 17, "metadata": {}, "output_type": "execute_result" } ], "source": [ "my_list = ['zero', 'one', 'two', 'three']\n", "type(my_list)" ] }, { "cell_type": "code", "execution_count": 18, "id": "38976328-db06-4070-831e-dfde4a0759ea", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "4" ] }, "execution_count": 18, "metadata": {}, "output_type": "execute_result" } ], "source": [ "len(my_list)" ] }, { "cell_type": "code", "execution_count": 19, "id": "baec64a6-6854-4a28-8f35-3ceddc4b26ad", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "['zero', 'one', 'two', 'three']\n" ] } ], "source": [ "print(my_list)" ] }, { "cell_type": "code", "execution_count": 20, "id": "c3290ce5-93e1-4a67-a2ba-726fb72428dc", "metadata": {}, "outputs": [], "source": [ "my_list.append('four')" ] }, { "cell_type": "code", "execution_count": 21, "id": "e3239a7e-02e2-4265-8571-1a61769066ac", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "['zero', 'one', 'two', 'three', 'four']\n" ] } ], "source": [ "print(my_list)" ] }, { "cell_type": "markdown", "id": "2a1fd81a-60b3-4cab-a87f-654e0b6630af", "metadata": {}, "source": [ "### 1.4. Dictionaries" ] }, { "cell_type": "code", "execution_count": 22, "id": "bba940cb-38d7-45f3-8108-eefef1fc26f0", "metadata": {}, "outputs": [], "source": [ "my_dict = {\n", " 'name': 'George',\n", " 'State': 'NY',\n", " 'status': 'Caffinated',\n", " 'Age': 999,\n", " 'Intrests': ['AI', 'ML', 'data science']\n", "}" ] }, { "cell_type": "code", "execution_count": 23, "id": "3daec62d-3d7e-4892-8777-461d5b060896", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "dict" ] }, "execution_count": 23, "metadata": {}, "output_type": "execute_result" } ], "source": [ "type(my_dict)" ] }, { "cell_type": "code", "execution_count": 24, "id": "fe2b34d2-fcc6-4c48-871a-4aca3889e62e", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "{'name': 'George', 'State': 'NY', 'status': 'Caffinated', 'Age': 999, 'Intrests': ['AI', 'ML', 'data science']}\n" ] } ], "source": [ "print(my_dict)" ] }, { "cell_type": "code", "execution_count": 25, "id": "5903352b-33d6-476f-849c-a34f19f7764a", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'George'" ] }, "execution_count": 25, "metadata": {}, "output_type": "execute_result" } ], "source": [ "my_dict['name']" ] }, { "cell_type": "code", "execution_count": 26, "id": "a4a18833-8d55-422e-9815-40c65abde55b", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "dict_keys(['name', 'State', 'status', 'Age', 'Intrests'])" ] }, "execution_count": 26, "metadata": {}, "output_type": "execute_result" } ], "source": [ "my_dict.keys()" ] }, { "cell_type": "code", "execution_count": 27, "id": "69a99f60-9a09-43a4-aa0e-e6ae8f4e47af", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "dict_values(['George', 'NY', 'Caffinated', 999, ['AI', 'ML', 'data science']])" ] }, "execution_count": 27, "metadata": {}, "output_type": "execute_result" } ], "source": [ "my_dict.values()" ] }, { "cell_type": "markdown", "id": "10e8ec84-cd3b-4cf2-83ae-6cd4a31498ae", "metadata": {}, "source": [ "## 2. Working with files\n", "\n", "### 3.1. Reading from a file\n", "\n", "**Note:** this example needs a file called `test_file.txt` in the same directory as the notebook to work." ] }, { "cell_type": "code", "execution_count": 28, "id": "6de3f962-a924-401c-86f8-c46a9e3f80bd", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "_io.TextIOWrapper" ] }, "execution_count": 28, "metadata": {}, "output_type": "execute_result" } ], "source": [ "file = open('data/test_file.txt','r')\n", "type(file)" ] }, { "cell_type": "code", "execution_count": 29, "id": "0c65c332-7cc4-40d3-89ed-28e330be24fc", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "This is the data!This is more new dataThis is more new dataThis is more new data\n" ] } ], "source": [ "contents = file.read()\n", "print(contents)" ] }, { "cell_type": "code", "execution_count": 30, "id": "4b6d020e-4425-4e9b-9e0b-e77ec517a786", "metadata": {}, "outputs": [], "source": [ "file.close()" ] }, { "cell_type": "markdown", "id": "af89ae80-9c5e-4671-8428-81bbd406f844", "metadata": {}, "source": [ "### 3.2. Context manager" ] }, { "cell_type": "code", "execution_count": 31, "id": "6a4e714d-e73c-4558-b82e-2ca273ca7f61", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "This is the data!This is more new dataThis is more new dataThis is more new data\n" ] } ], "source": [ "with open('data/test_file.txt', 'r') as file:\n", " contents = file.read()\n", " print(contents)" ] }, { "cell_type": "markdown", "id": "49edc05e-b021-4fa0-ae48-d87979582d90", "metadata": {}, "source": [ "### 3.3. Writing to a file" ] }, { "cell_type": "code", "execution_count": 32, "id": "4764576b-9445-493f-b5d1-7105ed21c8c8", "metadata": {}, "outputs": [], "source": [ "file = open('test_output.txt', 'w')\n", "new_data = 'This is the new data'\n", "file.write(new_data)\n", "file.close()" ] }, { "cell_type": "code", "execution_count": 33, "id": "d98c7b3c-c7ff-4a3e-b63a-e49777963579", "metadata": {}, "outputs": [], "source": [ "file = open('test_output.txt', 'w')\n", "more_data = 'This is more new data'\n", "file.write(more_data)\n", "file.close()" ] }, { "cell_type": "code", "execution_count": null, "id": "8b0ce221-4f86-4120-a10d-2cff5d3eaebf", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "This is the data!This is more new dataThis is more new dataThis is more new data\n" ] } ], "source": [ "with open('test_file.txt', 'a') as file:\n", " more_data = 'This is more new data'\n", " file.write(more_data)\n", "\n", "print(contents)" ] }, { "cell_type": "markdown", "id": "b755b178-d44d-4793-8aa4-34b7c7aeb242", "metadata": {}, "source": [ "## 4. Error handling" ] }, { "cell_type": "code", "execution_count": 38, "id": "e252306f-8ac2-4e75-9d40-c6065dd1deb1", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Now it exists!\n" ] } ], "source": [ "file = open('data/bad_file.txt', 'r')\n", "contents = file.read()\n", "print(contents)\n", "file.close()" ] }, { "cell_type": "code", "execution_count": null, "id": "8ae67224-78a1-4f4d-ac63-253c5559d5bc", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Now it exists!\n", "This is the finally clause - it closes the file handle\n", "Do other things\n" ] } ], "source": [ "try:\n", "\n", " file = open('data/bad_file.txt', 'r')\n", " contents = file.read()\n", " print(contents)\n", "\n", "except:\n", "\n", " print('File not found')\n", "\n", "finally:\n", "\n", " print('This is the finally clause - it closes the file handle')\n", " file.close()\n", "\n", "print('Do other things')\n" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3 [3.10]", "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.10.2" } }, "nbformat": 4, "nbformat_minor": 5 }