diff --git a/Seminar_1/seminar_1_empty.ipynb b/Seminar_1/seminar_1_empty.ipynb new file mode 100644 index 0000000..687e135 --- /dev/null +++ b/Seminar_1/seminar_1_empty.ipynb @@ -0,0 +1,506 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "id": "fa9e11ab", + "metadata": {}, + "source": [ + "# Seminar: Lists, dictionaries, functions, exceptions\n" + ] + }, + { + "cell_type": "markdown", + "id": "239ab00a-b400-4a6b-a51f-780c76eb3f3e", + "metadata": { + "tags": [] + }, + "source": [ + "### Task 0. \n", + "Write a loop which will add \"_adj\" to the each element of the list:\n", + "[\"close\", \"open\", \"high\", \"high\"]" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "cf359ec2-8f2b-4570-b075-a3bf5afd73ff", + "metadata": { + "tags": [] + }, + "outputs": [], + "source": [ + "my_list = [\"close\", \"open\", \"high\", \"high\"]" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "0d51f0d0-cdf4-4eda-9288-07d00c0b8906", + "metadata": { + "tags": [] + }, + "outputs": [], + "source": [] + }, + { + "cell_type": "markdown", + "id": "9d88e376-d4f8-43e5-8131-29eb944a9ced", + "metadata": {}, + "source": [ + "Do the same but write all of the code in one line (list comprehension)." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "50ab8814-f05b-4ed0-9099-d9d761fe98ff", + "metadata": { + "tags": [] + }, + "outputs": [], + "source": [ + "my_list = [\"close\", \"open\", \"high\", \"high\"]" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "d329b4d6-7b43-431e-a670-fdd78799c1c6", + "metadata": { + "tags": [] + }, + "outputs": [], + "source": [] + }, + { + "cell_type": "markdown", + "id": "d0dda828", + "metadata": {}, + "source": [ + "### Task 1. \n", + "\n", + "Write a function which will sum up all values in the list" + ] + }, + { + "cell_type": "markdown", + "id": "1401ca62", + "metadata": {}, + "source": [ + "#### a. For the case when input is list of integers." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "f6d17a45-c02d-43e5-ae03-144d79d7121c", + "metadata": { + "tags": [] + }, + "outputs": [], + "source": [ + "my_list_int = [0, 1, 1, 2, 3, 5, 8, 13]" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "8750cd32", + "metadata": { + "tags": [] + }, + "outputs": [], + "source": [] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "fefdbef8", + "metadata": { + "tags": [] + }, + "outputs": [], + "source": [ + "sum_up(my_list_int)" + ] + }, + { + "cell_type": "markdown", + "id": "e06770f1", + "metadata": {}, + "source": [ + "#### b. What will be if the input is list of strings?" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "f07c53c5-edc2-473c-80c4-b141c3a85c27", + "metadata": { + "tags": [] + }, + "outputs": [], + "source": [ + "my_list_str = ['0', 'a', '1', 'bb', 'ccc', '5', '8', '13']" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "10ec1e55-5ea2-4e4d-86c0-985f118e2802", + "metadata": { + "tags": [] + }, + "outputs": [], + "source": [ + "sum_up(my_list_str)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "6f214987-4948-4afb-85fb-4a140196d051", + "metadata": {}, + "outputs": [], + "source": [ + "# change the function in order to avoid error and get the answer: '0a1bbccc5813'" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "c4aa17cc", + "metadata": { + "tags": [] + }, + "outputs": [], + "source": [] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "10fac1f0", + "metadata": { + "tags": [] + }, + "outputs": [], + "source": [ + "sum_up_str(my_list_str)" + ] + }, + { + "cell_type": "markdown", + "id": "d67d8c92", + "metadata": {}, + "source": [ + "#### c. Rewrite the function sum_up in the way, that we will get a message 'can not sum it up' if the summation can't be performed" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "461057db-76ed-45ce-9195-94e7449b8042", + "metadata": { + "tags": [] + }, + "outputs": [], + "source": [ + "mixed_list = [1, 4, 'd']" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "7b489ffb-38d3-46e5-b150-ed2c8470dda3", + "metadata": { + "tags": [] + }, + "outputs": [], + "source": [] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "2c31130b-b976-4861-9609-484d8a22ff72", + "metadata": { + "tags": [] + }, + "outputs": [], + "source": [ + "sum_up_except(mixed_list)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "93ba0f67-a433-4104-a57a-7a897d3dcf2b", + "metadata": {}, + "outputs": [], + "source": [ + "# return nothing if the summation can not be performed " + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "fb521cf9-ba60-49ef-b144-8448c2f8c908", + "metadata": { + "tags": [] + }, + "outputs": [], + "source": [] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "759ddcbd-076c-4135-bbb2-b4bc9159248a", + "metadata": { + "tags": [] + }, + "outputs": [], + "source": [ + "sum_up_except(mixed_list)" + ] + }, + { + "cell_type": "markdown", + "id": "3240b759", + "metadata": {}, + "source": [ + "#### d. Why do we need to write the exceptions?" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "ad2390cf", + "metadata": { + "tags": [] + }, + "outputs": [], + "source": [ + "print('Step 1')\n", + "print(f'Step 2: {sum_up_except([1, 4, \"d\"])}')\n", + "print('Step 3')" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "ec92a671", + "metadata": { + "tags": [] + }, + "outputs": [], + "source": [ + "print('Step 1')\n", + "print(f'Step 2: {sum_up([1, 4, \"d\"])}')\n", + "print('Step 3')" + ] + }, + { + "cell_type": "markdown", + "id": "278db275", + "metadata": {}, + "source": [ + "### Task 2." + ] + }, + { + "cell_type": "markdown", + "id": "d3d3a943", + "metadata": {}, + "source": [ + "In this task we will work with the dictionary:" + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "id": "eba4a98b", + "metadata": { + "tags": [] + }, + "outputs": [], + "source": [ + "dic_food = {'apple':5, 'orange':4, 'onion':9}" + ] + }, + { + "cell_type": "markdown", + "id": "3074601e", + "metadata": {}, + "source": [ + "#### a. Calculate how many products in total do we have. How many fruits?" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "65fc0173", + "metadata": { + "tags": [] + }, + "outputs": [], + "source": [] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "f35e6c38", + "metadata": { + "tags": [] + }, + "outputs": [], + "source": [] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "c205408f", + "metadata": { + "tags": [] + }, + "outputs": [], + "source": [ + "list_fruits = ['apple', 'orange']" + ] + }, + { + "cell_type": "markdown", + "id": "a3ca5ec2", + "metadata": {}, + "source": [ + "#### b. Add one more apple and change the numbers of onions to 1" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "d85c63a1", + "metadata": { + "tags": [] + }, + "outputs": [], + "source": [] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "831fe4ba", + "metadata": { + "tags": [] + }, + "outputs": [], + "source": [] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "0b9ace80", + "metadata": { + "tags": [] + }, + "outputs": [], + "source": [] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "6d513519", + "metadata": { + "tags": [] + }, + "outputs": [], + "source": [] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "9e7d3831", + "metadata": { + "tags": [] + }, + "outputs": [], + "source": [] + }, + { + "cell_type": "markdown", + "id": "7c03197c", + "metadata": {}, + "source": [ + "#### c. Make the same dictionary from the lists: " + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "cc3b305e", + "metadata": {}, + "outputs": [], + "source": [ + "prod_name = ['apple', 'orange', 'onion']\n", + "prod_amount = [1,5,5]" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "6672ef13", + "metadata": {}, + "outputs": [], + "source": [ + "new_dict = {name:amount for name,amount in zip(prod_name, prod_amount)}\n", + "new_dict" + ] + }, + { + "cell_type": "markdown", + "id": "3649c1cb", + "metadata": {}, + "source": [ + "#### d. What will happened if we have dublicates in the list of keys?" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "c66a0a13", + "metadata": {}, + "outputs": [], + "source": [ + "prod_name = ['apple', 'orange', 'onion', 'apple']\n", + "prod_amount = [1,5,3,2]" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "a1188206", + "metadata": {}, + "outputs": [], + "source": [] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3 (ipykernel)", + "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.11.6" + } + }, + "nbformat": 4, + "nbformat_minor": 5 +}