{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Python 101\n", "## Part I.\n", "\n", "---" ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "slideshow": { "slide_type": "-" } }, "outputs": [], "source": [ "# run me using [Shift] + [Enter]!\n", "import datetime\n", "import random" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "----\n", "\n", "## Before we start\n", "\n", "### Who am I?\n", "\n", "* ex-PhD student (absolutorium achieved)\n", "* ex-teaching assistant @ Unideb\n", "* ex-python developer @ HigherClick\n", "* ex-data scientist @ Lensa\n", "* solutions architect / ml engineer @ Datapao\n", "* programmer (github)\n", "* geek (twitter)\n", "\n", "### What are we gonna do?\n", "\n", "1. Have fun!\n", "2. Learn something useful.\n", "3. Create something awesome.\n", "4. Conquer the world!\n", "\n", "### Requirements?\n", "\n", "* Solve the weekly challenges.\n", "* Define a problem and solve it.\n", "* Write some docs about it.\n", "* Free your mind!\n", "\n", "### AI Q&A\n", "\n", "1. **What is realistic?**\n", " - You will use AI to write code, to understand code and concepts. Both during the course and when you do projects. \n", "1. **What to avoid?**\n", " - **The illusion of competence**: the mistaken belief that recognizing or understanding material (like reading code) is the same as being able to recall or apply it (like writing code).\n", " - **Cognitive offloading without verification**: In learning science, this happens when people offload thinking to a tool (calculator, search engine, AI) but don’t verify the output.\n", "1. **How to get the most out of this course?**\n", " - Try to understand concepts during the class, ask questions.\n", " - Try to ask questions from each others and from the TAs to undestand concepts.\n", " - Use LLMs to explain concepts from the class to deepen your understanding.\n", " - Solve the assignments on your own.\n", " - If you struggle:\n", " - Struggle a bit more - if you are struggling, you are on the right track.\n", " - Ask your peers / TAs before giving up.\n", " - Prompt the LLM to act as a teacher and do not write the assignment for you but guide/steer you to the right answer instead.\n", "\n", "---" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Warming up - Computer Science in 5 min:\n", "\n", "To get everyone to the same pace." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Where it begins: NAND\n", "\n", "\n", "" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### How NAND gates have been produced?" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Relays\n", "\n", "" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Vacuum tubes\n", "\n", "" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Transistors\n", "\n", "\n", "" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### From human readable to \"machine readable\"" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Low level languages\n", "\n", "" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Compiled languages\n", "\n", "\n", "
\n", "\n", "* BASIC\n", "* C, C++\n", "* Pascal" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Interpreted languages\n", "\n", "\n", "
\n", "\n", "* Javascript\n", "* PHP" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Python\n", "\n", "Something from both worlds:\n", "\n", "" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Meet the designer: Guido van Rossum\n", "\n", "" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Origin of the name:\n", "\n", "" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "---\n", "\n", "## Everyone's first lines\n", "\n", "I don't want to ruin the moment and do it for you, so follow the instructions below:\n", "\n", "- Hit [Enter] on the following cell\n", "- Position your cursor to the first empty line\n", "- Type the `print` word following with parenthesis (`()`) and type \"Hello SZISZ\" in quotation inside of the parenthesis.\n", "- Hit [Ctrl] + [Enter] to run your code" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Before we start:\n", "# This line is a comment. It is useful for others to understand your code,\n", "# so use it often. Every text after the # character is comment.\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "__Let's do some math too!__\n", "\n", "Try out the following operators:\n", "- `+, -, *, /, //`\n", "- `%, **`" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "1 + 1" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Try out the followings logical operators:\n", "- `==, !=, >, >=, <=, <`\n", "- `not, and, or` \n", "\n", "and logical values:\n", "- `True, False`" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "True or False" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Variables\n", "\n", "![bookshelf_of_memory](pics/bookshelf_of_memory.png)\n", "\n", "What if we want to store some values? We use the assignment (`=`) operator!\n", "\n", "- __`a`__ is a number" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "a = 3\n", "print(a)\n", "print(type(a))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "- __`a`__ is a floating point number" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "a = 3.14\n", "print(a)\n", "print(type(a))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "- __`a`__ is a boolean value" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "a = True # False\n", "print(a)\n", "print(type(a))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "- __`a`__ is a character" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "a = 'a'\n", "print(a)\n", "print(type(a))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "- __`a`__ is a string" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "a = 'spam'\n", "print(a)\n", "# or\n", "a = \"spam and eggs\"\n", "print(a)\n", "# or\n", "a = '''spam'\n", "spam\n", "sausage'''\n", "print(a)\n", "# or\n", "a = \"\"\"spam\n", "spam\n", "eggs\n", "and spam\"\"\"\n", "print(a)\n", "print(type(a))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "__According to strings:__ \n", "There are some so called __`escape character`__s. They always starts with a __\\\\__ following by an another character, like:\n", "- `\\n`\n", "- `\\t`\n", "- `\\r`\n", "- `\\'`\n", "- `\\\"`\n", "\n", "Try them out!" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "a = 'sp\\nam'\n", "print(a)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Naming conventions" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "* starts with a letter\n", "* available characters:\n", " * a-z\n", " * A-Z\n", " * 0-9\n", " * _" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "spam_is_a_valid_variable_name = True" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "9spam_is_not = True" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Let's try it out...\n", "\n", "...and make something (almost) useful!" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "your_name = '' # please specify your name\n", "print('Hello', your_name, '!')\n", "\n", "year_of_birth = XXXX # please specify your year of birth\n", "print('You are', datetime.datetime.now().year - year_of_birth, 'years old.')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Intermission: generating random numbers\n", "\n", "Since playing with static values are not that fun, we are going to use random numbers wherever we can.\n", "To generate a random number you can use the random module's `random` and `randint` functions:\n", "- `random()` generates a random floating point number between `0.0` and `1.0`.\n", "- `randint(from, to)` generates an integer value from the range `from` and `to`. You have the option to only specify the upper limit - in that case the lower limit will be set to 0.\n", "\n", "Let's generate a few examples." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "random_float = random.random()\n", "random_int = random.randint(10, 45)\n", "\n", "print(f'Random float: {random_float}\\nRandom int: {random_int}')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Let's do some...\n", "\n", "\n", "
\n", "\n", "### Cool library of the week: qrcode\n", "* Generate a QR code in two lines!\n", "\n", "Install lib in the anaconda prompt (Windowns) / terminal (macOS, linux) with:\n", "```bash\n", "pip install qrcode\n", "```" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import qrcode\n", "qrcode.make('fulibacsi.github.io/notebooks')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Now it's your turn! Write the missing code snippets!\n", "\n", "**1. Print the \"your_name\" variable \"repeat\" times!**" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "your_name = ''\n", "repeat = random.randint(1, 5)\n", "# hint: you can multiply strings too.\n", "\n", "\n", "print() # YOUR SOLUTION COMES HERE\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**2. Calculate the selling price to achieve the desired profit after tax!**\n", "\n", "$$ \\text{price} = \\frac{1 + \\text{profit}}{1 + \\text{tax}} * \\text{cost} $$" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "cost = random.randint(1000, 2000) # currency\n", "tax = random.randint(20, 50) / 100.0 # percent\n", "profit = random.randint(0, 100) / 100.0 # percent\n", "\n", "\n", "price = # YOUR SOLUTION COMES HERE\n", "\n", "\n", "print(f'If I bought the product for {cost:.1f} '\n", " f'and I would like to achieve {profit:.1%} '\n", " f'profit and the tax I have to pay after my income is '\n", " f'{tax:.1%}, then I have to set the price to: {price:.1f}.')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**3. Now let the geek side of the force shine!** \n", "\n", "\n", "\n", "A warrior fights against an evil wizard lizard. He has a magical sword and a magical axe. \n", "The evil wizard lizard cast a protective spell on himself, so he is resistant to one of the \n", "damage types. The warrior has two weapons to choose from. Help him defeat the evil wizard lizard \n", "by calculating the damage values of his weapons!\n", "\n", "I'm pretty sure that this exercise was the catalyst for this [kickstarter](https://www.kickstarter.com/projects/954412004/lizard-wizard) project!" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "He has:\n", "- a sword" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "sword_damage = random.randint(5, 10)\n", "sword_damage_type = random.choice(['fire', 'ice', 'lightning', 'arcane', 'poison'])\n", "\n", "print(f'This sword will inflict {sword_damage} {sword_damage_type} type damage.')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "- an axe" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "axe_damage = random.randint(5, 10)\n", "axe_damage_type = random.choice(['fire', 'ice', 'lightning', 'arcane', 'poison'])\n", "\n", "print(f'This axe will inflict {axe_damage} {axe_damage_type} type damage.')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The evil wizard lizard is resistant to one element:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "evil_wizard_lizard_resist_type = random.choice(['fire', 'ice', 'lightning', 'arcane', 'poison'])\n", "evil_wizard_lizard_damage_reduction = random.randint(0, 75) / 100.0\n", "\n", "print(f'The evil lizard will receive {evil_wizard_lizard_damage_reduction:.0%}'\n", " f' less damage if hit with a {evil_wizard_lizard_resist_type} type weapon.')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Which weapon should the warrior choose?\n", "\n", "Calculate the `actual_sword_damage` and `actual_axe_damage` by generating a formula using the variables above. Finally compute `answer` by comparing the previously calculated actual damages. \n", "\n", "Example:\n", "\n", "sword_damage | sword_damage_type | evil_wizard_lizard_damage_reduction (%) | evil_wizard_lizard_resist_type | actual_sword_damage\n", "---|------|-----|------|------\n", "10 | fire | 25% | ice | 10.0 \n", "10 | fire | 25% | fire | 7.5 \n" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "sword_actual_damage = # compute sword and axe damage\n", "axe_actual_damage = # hint: True is 1, False is 0\n", "\n", "# should the warrior choose the sword?\n", "answer = # solution should be boolean\n", "\n", "\n", "print('Sould the warrior use the sword? - asked the dungeon master, and you yelled:')\n", "print('Yes!' if answer else 'No!')\n", "print(f'Because the sword does {sword_actual_damage} damage and '\n", " f'the axe does {axe_actual_damage} damage to the evil wizard lizard.')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**4. Aaaaare we there yet?** \n", "Ever wonder how much time do you need to get home? \n", "Let's calculate the trip time! It is possible to hit a traffic jam, so add that to the trip time too." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "distance = random.randint(30, 230) * 1.0\n", "average_speed = random.randint(30, 90)\n", "# if I hit the traffic jam, my trip takes `traffic_jam_penalty` minutes more then without one.\n", "traffic_jam = random.randint(0, 1)\n", "traffic_jam_penalty = random.randint(30, 60)\n", "\n", "\n", "trip_time = # compute in minutes\n", "hours = # hint: use the int(value) function to get an integer number\n", "minutes =\n", "seconds =\n", "\n", "\n", "print(f\"I need {trip_time} minutes or {hours} hours, {minutes} minutes, {seconds} seconds to get home \"\n", " f\"if I live {distance}kms away and I usually drive at{average_speed} km/hs. \"\n", " f\"I have to wait {traffic_jam_penalty * traffic_jam} minutes because of a traffic jam.\")," ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Variant from real life:** \n", "An average hungarian coach travels at 60 km/h on average. (Self collected empiric data.) \n", "Calculate the travel time!" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "hungarian_coach_average_speed = 60\n", "distance_till_home = random.randint(30, 300)\n", "\n", "\n", "minutes_left = # YOUR SOLUTION COMES HERE\n", "\n", "\n", "print(f\"I only have to survive {minutes_left} minutes, and I'm home!\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**5. Rock-paper-scissors.** \n", "Generate a logical expression which generates the game's outcome." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "your_choice = 'r' # 'r', 'p' or 's'\n", "comp_choice = random.choice('rps')\n", "\n", "\n", "winner = # compute if player's winning\n", "\n", "\n", "print('Did you win?')\n", "print('YES!' if winner else 'not this time...')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**6. Calculate the good-old (and boring) quadratic equation!** \n", "For those who forgot: \n", "$$x_{1,2}=\\dfrac{-b \\pm \\sqrt{b^2 - 4ac}}{2a}$$" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "a = random.randint(1, 10)\n", "b = random.randint(1, 10)\n", "c = random.randint(1, 10)\n", "\n", "\n", "x_1 = # YOUR SOLUTION COMES HERE\n", "x_2 = # YOUR SOLUTION COMES HERE\n", "\n", "\n", "print(f'The solutions to the {a}x^2 + {b}x + {c} = 0 '\n", " f'is: {x_1} and {x_2}')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "----" ] } ], "metadata": { "anaconda-cloud": {}, "kernelspec": { "display_name": "sandbox", "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.9.12" } }, "nbformat": 4, "nbformat_minor": 1 }