{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Tuple Unpacking" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "joerg\n", "flo\n", "ted\n", "rainer\n" ] } ], "source": [ "l = ['joerg', 'flo', 'ted', 'rainer']\n", "for element in l:\n", " print(element)" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [], "source": [ "d = {\n", " 'joerg': 54,\n", " 'ted': 56,\n", " 'rainer': 55,\n", "}" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "joerg\n", "ted\n", "rainer\n" ] } ], "source": [ "for element in d:\n", " print(element)" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "rainer\n", "rainer\n", "rainer\n" ] } ], "source": [ "for key in d.keys():\n", " print(element)" ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "54\n", "56\n", "55\n" ] } ], "source": [ "for value in d.values():\n", " print(value)" ] }, { "cell_type": "code", "execution_count": 12, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "('joerg', 54)\n", "('ted', 56)\n", "('rainer', 55)\n" ] } ], "source": [ "for element in d.items():\n", " print(element)" ] }, { "cell_type": "code", "execution_count": 16, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "key: joerg, value: 54\n", "key: ted, value: 56\n", "key: rainer, value: 55\n" ] } ], "source": [ "for element in d.items():\n", " key = element[0]\n", " value = element[1]\n", " print(f'key: {key}, value: {value}')" ] }, { "cell_type": "code", "execution_count": 19, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "key: joerg, value: 54\n", "key: ted, value: 56\n", "key: rainer, value: 55\n" ] } ], "source": [ "for key, value in d.items():\n", " print(f'key: {key}, value: {value}')" ] }, { "cell_type": "code", "execution_count": 22, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "name: joerg, age: 54\n" ] } ], "source": [ "t = ('joerg', 54)\n", "name, age = t\n", "print(f'name: {name}, age: {age}')" ] }, { "cell_type": "code", "execution_count": 27, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "(1, 2)" ] }, "execution_count": 27, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a = 1\n", "b = 2\n", "a, b" ] }, { "cell_type": "code", "execution_count": 28, "metadata": {}, "outputs": [], "source": [ "b, a = a, b" ] }, { "cell_type": "code", "execution_count": 30, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "(2, 1)" ] }, "execution_count": 30, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a, b" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# String Methods" ] }, { "cell_type": "code", "execution_count": 31, "metadata": {}, "outputs": [], "source": [ "s = 'joerg'" ] }, { "cell_type": "code", "execution_count": 33, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'Joerg'" ] }, "execution_count": 33, "metadata": {}, "output_type": "execute_result" } ], "source": [ "s.capitalize()" ] }, { "cell_type": "code", "execution_count": 35, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'joerg'" ] }, "execution_count": 35, "metadata": {}, "output_type": "execute_result" } ], "source": [ "s" ] }, { "cell_type": "code", "execution_count": 37, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'JOERG'" ] }, "execution_count": 37, "metadata": {}, "output_type": "execute_result" } ], "source": [ "s.upper()" ] }, { "cell_type": "code", "execution_count": 39, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'joerg'" ] }, "execution_count": 39, "metadata": {}, "output_type": "execute_result" } ], "source": [ "s.upper().lower()" ] }, { "cell_type": "code", "execution_count": 41, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "' joerg '" ] }, "execution_count": 41, "metadata": {}, "output_type": "execute_result" } ], "source": [ "s.center(60)" ] }, { "cell_type": "code", "execution_count": 43, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "3" ] }, "execution_count": 43, "metadata": {}, "output_type": "execute_result" } ], "source": [ "s.find('rg')" ] }, { "cell_type": "code", "execution_count": 45, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "-1" ] }, "execution_count": 45, "metadata": {}, "output_type": "execute_result" } ], "source": [ "s.find('xxx')" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'joerg.faschingbauer'" ] }, "execution_count": 1, "metadata": {}, "output_type": "execute_result" } ], "source": [ "l = ['joerg', 'faschingbauer']\n", "'.'.join(l)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Strings vs. Bytes" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [], "source": [ "s = 'Jörg'\n", "b = b'Joerg'" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ " can only concatenate str (not \"bytes\") to str\n" ] } ], "source": [ "try:\n", " s + b\n", "except Exception as e:\n", " print(type(e), e)" ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "False" ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "s == b" ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "False" ] }, "execution_count": 11, "metadata": {}, "output_type": "execute_result" } ], "source": [ "7 == '7'" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "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.9.4" } }, "nbformat": 4, "nbformat_minor": 4 }