{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Programming and Data Analysis\n", "\n", "*Alla Tambovtseva, NRU HSE*\n", "\n", "\n", "## Seminar 2: lists" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Problem 0\n", "\n", "There are two lists `a` and `b` (see below).\n", " \n", "1. Print the first element of `a`, the third element of `a`, and the last element of `a`.\n", "2. Add to the list `b` a number 7 (just write to the end).\n", "3. Change the fifth element of the list `a` to 8.\n", "4. Create the list `merged`, that includes all the elements of `a` and of `b`.\n", "5. Create the list `c` that can be obtained by changing the last element of `a` to 100. The list `a` should not be changed!" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "a = [1, 0, 9, 12, 18, 34, 89, 91, 33, 127]\n", "b = [2, 8, 9, 11, 76, 25, 44]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Problem 1\n", "\n", "There is a list of names:" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [], "source": [ "girls = [\"Ivetta\", \"Violetta\", \"Cassandra\", \n", " \"Viktoria\", \"Amelia\", \"Rose\", \"Elizabeth\"] " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Using `girls`, print the following lists:\n", "\n", " [\"Violetta\", \"Cassandra\", \"Viktoria\", \"Amelia\"]\n", " [\"Viktoria\", \"Amelia\", \"Rose\", \"Elizabeth\"]\n", " [\"Ivetta\", \"Violetta\", \"Viktoria\", \"Amelia\"]\n", " [\"Cassandra\", \"Amelia\", \"Rose\"]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Problem 2\n", "\n", "We have the list `L`. Print (one element from a new line): \n", "\n", "* all elements of `L`;\n", "* natural logarithms of elements of `L`." ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [], "source": [ "L = [12, 3, 8, 125, 10, 98, 54, 199]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Change the fifth element of `L` to 0. Do the same operations as above. Why do you get such results?" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Problem 3\n", "\n", "In the list `age` we store respondents' age: \n", "\n", " age = [24, 35, 42, 27, 45, 48, 33]\n", " \n", "Create the list `age2` with values of respondents' age squared.\n", "\n", "**Hint:** use a for-loop." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Problem 4\n", "\n", "Python asks a user to enter an integer number $k$ from 1 to 10. Python takes this value $k$ and prints the $k$-th element of the list `numbers`, assuming that the numeration starts from 1. " ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [], "source": [ "numbers = [1, 5, 6, 8, 10, 21, 25, 1, 0, -9, 9]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Example:**\n", " \n", "*Input:*\n", "\n", " Enter an integer from 1 to 10: 3\n", " \n", "*Output:*\n", "\n", " 6" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Problem 5\n", "\n", "Explain, what the following code does and why it returns such results:" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "5\n", "3\n", "5\n", "7\n" ] } ], "source": [ "l = [1,2,3,4]\n", "\n", "for i in range(len(l)):\n", " a = l[i] + l[i-1]\n", " print(a)" ] } ], "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.7.4" } }, "nbformat": 4, "nbformat_minor": 2 }