{ "nbformat": 4, "nbformat_minor": 0, "metadata": { "colab": { "name": "exercise01.ipynb", "provenance": [], "collapsed_sections": [] }, "kernelspec": { "name": "python3", "display_name": "Python 3" } }, "cells": [ { "cell_type": "code", "metadata": { "id": "qOdnm-3YQhFL" }, "source": [ "# Variable\r\n", "# primite variables: integer, float, booleans, strings\r\n", "\r\n", "age=22.0\r\n", "name=\"Carlos\"\r\n", "address=\"Lisboa\"\r\n" ], "execution_count": 7, "outputs": [] }, { "cell_type": "code", "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "Gligy3S5TnH7", "outputId": "9f7e49e9-aa5a-4900-94d2-2eb6ce42615d" }, "source": [ "born=2020-age\r\n", "print(born)" ], "execution_count": 9, "outputs": [ { "output_type": "stream", "text": [ "1998.0\n" ], "name": "stdout" } ] }, { "cell_type": "code", "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "3CxQm2EfUQYj", "outputId": "abb2b096-9f84-4767-f6b8-92b7b22a439f" }, "source": [ "type(age)" ], "execution_count": 10, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "float" ] }, "metadata": { "tags": [] }, "execution_count": 10 } ] }, { "cell_type": "code", "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "Z7WqYf4dUf2c", "outputId": "5486b67c-4158-419c-cd4f-ab9206b55340" }, "source": [ "age==23" ], "execution_count": 11, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "False" ] }, "metadata": { "tags": [] }, "execution_count": 11 } ] }, { "cell_type": "code", "metadata": { "id": "NWCtVweIUyk0" }, "source": [ "age=24" ], "execution_count": 14, "outputs": [] }, { "cell_type": "code", "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "EOAyo98eU-Dd", "outputId": "d3e80cb9-3d67-4ea7-b862-0e8cab6ffa4b" }, "source": [ "print(age)" ], "execution_count": 15, "outputs": [ { "output_type": "stream", "text": [ "24\n" ], "name": "stdout" } ] }, { "cell_type": "code", "metadata": { "id": "3-RPz4_7VDub" }, "source": [ "# Variable\r\n", "# non primitive variables: list, dictionary, tuples, sets,..." ], "execution_count": null, "outputs": [] }, { "cell_type": "code", "metadata": { "id": "EUR2McKcVSZS" }, "source": [ "ageList=[21,20,24,30,31]" ], "execution_count": 16, "outputs": [] }, { "cell_type": "code", "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "ri8xAXx8VdOs", "outputId": "fb0f46f1-0d67-457d-d5ae-43ae86d7a2b7" }, "source": [ "len(ageList)" ], "execution_count": 17, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "5" ] }, "metadata": { "tags": [] }, "execution_count": 17 } ] }, { "cell_type": "code", "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "b7QvCBJmVrSb", "outputId": "623b3ce7-254d-4c9a-9dc1-535394383db5" }, "source": [ "sum(ageList)" ], "execution_count": 18, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "126" ] }, "metadata": { "tags": [] }, "execution_count": 18 } ] }, { "cell_type": "code", "metadata": { "id": "gWOwNlzwV321" }, "source": [ "studentDict={51000:23,53000:23,54000:20}" ], "execution_count": 20, "outputs": [] }, { "cell_type": "code", "metadata": { "id": "DaV7X_12WngG" }, "source": [ "studentDict[51000]=22" ], "execution_count": 21, "outputs": [] }, { "cell_type": "code", "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "yVja7-I0W7dL", "outputId": "230b3974-0fe9-4b83-b57f-bd60a2e47d68" }, "source": [ "print(studentDict)" ], "execution_count": 22, "outputs": [ { "output_type": "stream", "text": [ "{51000: 22, 53000: 23, 54000: 20}\n" ], "name": "stdout" } ] }, { "cell_type": "code", "metadata": { "id": "vjUloo_DW_C6" }, "source": [ "# Control Structure\r\n", "# sequence\r\n", "# decision\r\n", "# loops" ], "execution_count": null, "outputs": [] }, { "cell_type": "code", "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "n0vn2xrJXVOE", "outputId": "36e57409-3cef-42b8-fe9e-a1ab9c11f442" }, "source": [ "myAge=input(\"your age\")" ], "execution_count": 28, "outputs": [ { "output_type": "stream", "text": [ "your age12\n" ], "name": "stdout" } ] }, { "cell_type": "code", "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "aZB3YL_iXeB9", "outputId": "78df3cab-d985-439a-d3f6-f6c3d9d15b17" }, "source": [ "type(myAge)" ], "execution_count": 29, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "str" ] }, "metadata": { "tags": [] }, "execution_count": 29 } ] }, { "cell_type": "code", "metadata": { "id": "ItGTJ4isXkOU" }, "source": [ "myAgeNumber=int(myAge)" ], "execution_count": 30, "outputs": [] }, { "cell_type": "code", "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "367kFv_PXtnl", "outputId": "903f0267-29d5-45e1-d763-653183751209" }, "source": [ "type(myAgeNumber)" ], "execution_count": 31, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "int" ] }, "metadata": { "tags": [] }, "execution_count": 31 } ] }, { "cell_type": "code", "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "iyIXCkJAXx81", "outputId": "27885ec2-92d2-42c3-8b0e-939e76bc1af3" }, "source": [ "if (myAgeNumber>=18):\r\n", " print(\"you may vote\")\r\n", "else:\r\n", " print(\"you have to eat more Danoninhos\")" ], "execution_count": 34, "outputs": [ { "output_type": "stream", "text": [ "you have to eat more Danoninhos\n" ], "name": "stdout" } ] }, { "cell_type": "code", "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "xvOU1UXMYjOd", "outputId": "5d6f0a90-238c-48ca-e1b3-3c93224f1df2" }, "source": [ "for age in ageList:\r\n", " born=2021-age\r\n", " print(born)" ], "execution_count": 36, "outputs": [ { "output_type": "stream", "text": [ "2000\n", "2001\n", "1997\n", "1991\n", "1990\n" ], "name": "stdout" } ] } ] }