{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Программирование для всех (основы работы в Python)\n", "\n", "*Алла Тамбовцева, НИУ ВШЭ*\n", "\n", "## Набор задач 1 по темам: вычисления в Python, переменные\n", "\n", "## Решения" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Задача 1\n", "\n", "Посчитайте:\n", "\n", "* $12^{34}$;\n", "* $\\log(25)$, $\\log_{10}(1000)$, $\\log_{25}(15625)$;\n", "* $\\sqrt{459}$ + $\\sqrt{45}$." ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "4922235242952026704037113243122008064" ] }, "execution_count": 1, "metadata": {}, "output_type": "execute_result" } ], "source": [ "12 ** 34" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "3.2188758248682006" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "import math \n", "math.log(25)" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "3.0" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# через log10\n", "math.log10(1000) " ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "2.9999999999999996" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# через log – проблемы с дробными числами, это 3\n", "math.log(1000, 10) " ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "3.0000000000000004" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# и здесь тоже, это 3\n", "math.log(15625, 25)" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "28.13248921812792" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "math.sqrt(459) + math.sqrt(45)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Задача 2\n", "\n", "В переменных `a` и `b` хранятся некоторые числа. Напишите код, который бы менял значения переменных местами. Создавать вспомогательные переменные можно.\n", "\n", "**Пример:**" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [], "source": [ "# до\n", "a = 2\n", "b = 5" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [], "source": [ "c = a\n", "a = b\n", "b = c" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "5\n", "2\n" ] } ], "source": [ "# после\n", "print(a)\n", "print(b)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Задача 3\n", "\n", "Напишите программу, которая логарифмирует значение ВВП (натуральный логарифм), сохраненное в переменной `gdp`,и округляет результат до второго знака после запятой." ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "9.9\n" ] } ], "source": [ "gdp = 20000\n", "log_gdp = math.log(gdp)\n", "print(round(log_gdp, 2))" ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "9.9\n" ] } ], "source": [ "# или все сразу\n", "gdp = 20000\n", "print(round(math.log(gdp), 2))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Задача 4\n", "В переменной fh хранится значение индекса политической свободы *Freedom House*, а в переменной p значение индекса *Polity IV*. Напишите программу, которая будет считать индекс *Freedom*:\n", "\n", "$$\n", "\\text{Freedom}=0.4⋅ \\text{Freedom House}+ 0.6⋅\\text{Polity IV}\n", "$$" ] }, { "cell_type": "code", "execution_count": 12, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "10.4" ] }, "execution_count": 12, "metadata": {}, "output_type": "execute_result" } ], "source": [ "fh = 20\n", "p = 4\n", "Freedom = 0.4 * fh + 0.6 * p\n", "Freedom" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Задача 5\n", "С приходом весны питон решил каждый день выползать погреться на солнышко. Однако он знал, что солнце весной довольно активное, и поэтому разработал такую схему: в первый день он греется одну минуту, а в каждый последующий день увеличивает время пребывания на солнце на 3 минуты. Напишите код, который позволит вычислять, сколько минут питон будет тратить на солнечные ванны в некоторый выбранный день.\n", "\n", "Внимание: ваш код должен выводить номер дня и число минут. Использовать циклы нельзя, в задании предполагается многократный запуск ячеек с кодом.\n", "\n", "Если хочется избежать многократного запуска ячейки с кодом (что оправдано), попробуйте решить эту задачу без повторного запуска ячейки и при этом без цикла, предполагая, что пользователь с клавиатуры вводит номер интересующего его дня, а на экран выводится соответствующее число минут." ] }, { "cell_type": "code", "execution_count": 13, "metadata": {}, "outputs": [], "source": [ "# решение 1 – с многократным запуском следующей ячейки\n", "i = 1\n", "day = 1" ] }, { "cell_type": "code", "execution_count": 14, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "2 4\n" ] } ], "source": [ "i = i + 1\n", "day = day + 3\n", "print(i, day) " ] }, { "cell_type": "code", "execution_count": 15, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Enter day number: 2\n", "4\n" ] } ], "source": [ "# решение 2 – запрашиваем номер дня у пользователя и выводим результат – число минут\n", "n = int(input(\"Enter day number: \"))\n", "res = 1 + 3 * (n - 1)\n", "print(res)" ] } ], "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 }