{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# SW05 - InClass - Repetition and Programming - VERSION 2\n", "\n", "This file is intended for interactive repetition during the first hour in class. You write directly into the code cells." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "***" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Tuple with strings - ordered, non-mutable sequential datatype\n", "\n", "**Tuples store immutable collections of elements.**\n", "\n", "A tuple of words is given. The following tasks are required:\n", "\n", "- Create a tuple named `words` that contains at least 5 different strings (e.g., names, fruits, or random objects).\n", "- Print the first and last element of the tuple.\n", "- Determine and print the length of the tuple.\n", "- Extract a sub-tuple from the second to the fourth element (inclusive) and print it.\n", "- Check if a specific word (e.g., \"apple\") is present in the tuple, and print a corresponding message." ] }, { "cell_type": "code", "execution_count": 16, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "word1 word5\n", "5\n", "('word2', 'word3')\n", "('word2', 'word3', 'word4')\n", "True\n", "False\n" ] } ], "source": [ "words = (\"word1\", \"word2\", \"word3\", \"word4\", \"word5\")\n", "\n", "print(words[0],words[-1])\n", "\n", "print(len(words))\n", "\n", "print(words[1:3])\n", "\n", "print(words[1:4])\n", "\n", "print((\"word1\") in words)\n", "\n", "print((\"word1\") in words and (\"word7\" in words))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The given tuple of words from above. Perform the following tasks:\n", "\n", "- Find and print all elements that contain a specific letter (e.g., \"B\").\n", "- Find the longest word in the tuple and print it along with its length.\n", "- Create a dictionary where each word in the tuple is a key and its length is the value, and print this dictionary." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Schritt 1: Finde und gib alle Elemente aus, die ein 'B' beinhalten\n", "\n", "\n", "# Schritt 2: Finde das längste Wort und gib es mit seiner Länge aus\n", "\n", "\n", "# Schritt 3: Erstelle ein Dictionary mit Wörtern und ihrer Länge\n", "\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "***" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Sets - mutable, elements are unique, but can be added, removed\n", "\n", "**Sets store unique, immutable elements - but sets themselves are mutable.**\n", "\n", "Write a Python program that takes the lists `list_a` and `list_b` and generates a new set `set_common` which contains the common elements of both lists.\n", "\n", "Example output in the console:\n", "```\n", "list_a = [1, 4, 3, 8, 9, 8]\n", "list_b = [2, 2, 4, 9, 8]\n", "\n", ">>> {8, 9, 4}\n", "```" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# non efficient way with loops\n", "\n", "\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# classic way with conversion into sets\n", "\n", "\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "***\n", "Write a Python program that takes two lists and generates the symmetric difference of the elements in these two lists as a set, then prints it to the console.\n", "\n", "Example output in the console:\n", "```\n", "list_a = [1, 4, 3, 8, 9]\n", "list_b = [2, 2, 4, 9, 8]\n", "\n", ">>> Difference_set: {1, 2, 3}\n", "```" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# classic way with conversion into sets\n", "\n", "\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "***" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Dictionary - lookup table of English-Spanish\n", "\n", "**Dictionaries store key-value pairs.**\n", "\n", "A lookup table is given with English words and their corresponding Spanish translations.\n", "Write a Python program that takes the English sentence stored in `text` and returns the corresponding Spanish sentence. If a word is not in the list, print it in uppercase." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# lookup table as dictionary given:\n", "\n", "\n", "\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "***\n", "end" ] } ], "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.12.7" } }, "nbformat": 4, "nbformat_minor": 4 }