{ "cells": [ { "cell_type": "raw", "id": "e282e62f-3efb-498b-b156-778556b67e75", "metadata": {}, "source": [ "What is the value of x, y and swap after each line" ] }, { "cell_type": "code", "execution_count": 1, "id": "87caa323-f71c-49c8-800d-505f356fb579", "metadata": {}, "outputs": [], "source": [ "x = 1.0\n", "y = 3.0\n", "swap = x\n", "x = y\n", "y = swap" ] }, { "cell_type": "raw", "id": "21473fc6-1375-4129-9d99-3b364eb67c52", "metadata": {}, "source": [ "# Command # Value of x # Value of y # Value of swap\n", "x = 1.0 # 1.0 # not define # not define\n", "y = 3.0 # 1.0 # 3.0 # not define\n", "swap = x # 1.0 # 3.0 # 1.0\n", "x = y # 3.0 # 3.0 # 1.0\n", "y = swap # 3.0 # 1.0 # 1.0" ] }, { "cell_type": "raw", "id": "0901cbd6-2012-4035-a33b-b8d7d0562165", "metadata": {}, "source": [ "What is the final value of position in the program below?" ] }, { "cell_type": "code", "execution_count": 2, "id": "acac449d-98fa-499a-b9d9-b785ce8e82a9", "metadata": {}, "outputs": [], "source": [ "initial = 'left'\n", "position = initial\n", "initial = 'right'" ] }, { "cell_type": "raw", "id": "47ff13d1-56e3-488f-bb50-3ed27df80eb1", "metadata": {}, "source": [ "put in final value" ] }, { "cell_type": "code", "execution_count": 3, "id": "5a44f7aa-9a69-4ade-be0b-14d8ce0bab0f", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "2\n" ] } ], "source": [ "a = '123'\n", "print(a[1])" ] }, { "cell_type": "code", "execution_count": 4, "id": "2e7e1ae0-ca30-46f6-b947-8196d082ce74", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "atom_name[1:3] is: ar\n" ] } ], "source": [ "atom_name = 'carbon'\n", "print('atom_name[1:3] is:', atom_name[1:3])" ] }, { "cell_type": "raw", "id": "05ef371e-eed0-4a12-bf22-c4f783289e06", "metadata": {}, "source": [ "Start of Data Types and Type Conversion" ] }, { "cell_type": "code", "execution_count": 5, "id": "11213178-a45d-4211-9683-826b2bd291e7", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\n" ] } ], "source": [ "print(type(52))" ] }, { "cell_type": "code", "execution_count": 6, "id": "abf8398c-b9d5-490a-bdc9-a779a6643cdd", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\n" ] } ], "source": [ "fitness = 'average'\n", "print(type(fitness))" ] }, { "cell_type": "code", "execution_count": 7, "id": "4b539bf8-66da-492e-a68c-494950f3b04b", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "2\n" ] } ], "source": [ "print(5-3)" ] }, { "cell_type": "code", "execution_count": 8, "id": "4f989b7a-7638-49ff-b4dd-9684c3310bff", "metadata": {}, "outputs": [ { "ename": "TypeError", "evalue": "unsupported operand type(s) for -: 'str' and 'str'", "output_type": "error", "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)", "\u001b[0;32m/tmp/ipykernel_297/13340790.py\u001b[0m in \u001b[0;36m\u001b[0;34m\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m'hello'\u001b[0m \u001b[0;34m-\u001b[0m \u001b[0;34m'h'\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", "\u001b[0;31mTypeError\u001b[0m: unsupported operand type(s) for -: 'str' and 'str'" ] } ], "source": [ "print('hello' - 'h')" ] }, { "cell_type": "code", "execution_count": 9, "id": "0f9b624d-9354-41a5-8b0d-9d26036d6969", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Ahmed Walsh\n" ] } ], "source": [ "full_name = 'Ahmed' + ' ' + 'Walsh'\n", "print(full_name)" ] }, { "cell_type": "code", "execution_count": 10, "id": "905c0d24-72ad-468b-a51b-6b67f4f0d946", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "==========\n" ] } ], "source": [ "separator = '=' * 10\n", "print(separator)" ] }, { "cell_type": "code", "execution_count": 11, "id": "543fe92a-764d-4c30-a840-d92993f7be4e", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "11\n" ] } ], "source": [ "print(len(full_name))" ] }, { "cell_type": "code", "execution_count": 12, "id": "80393769-e3c5-4ded-9b8f-100b3095a547", "metadata": {}, "outputs": [ { "ename": "TypeError", "evalue": "unsupported operand type(s) for +: 'int' and 'str'", "output_type": "error", "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)", "\u001b[0;32m/tmp/ipykernel_297/3905401405.py\u001b[0m in \u001b[0;36m\u001b[0;34m\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;36m1\u001b[0m \u001b[0;34m+\u001b[0m \u001b[0;34m'2'\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", "\u001b[0;31mTypeError\u001b[0m: unsupported operand type(s) for +: 'int' and 'str'" ] } ], "source": [ "print(1 + '2')" ] }, { "cell_type": "code", "execution_count": 13, "id": "e2168d46-2900-4d5f-a82a-b029b3ec461e", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "12\n" ] } ], "source": [ "print('1' + '2')" ] }, { "cell_type": "code", "execution_count": 14, "id": "ce2e0c66-9039-4a21-b86f-f8a8d15dd7a4", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "3\n" ] } ], "source": [ "print(1+2)" ] }, { "cell_type": "code", "execution_count": 15, "id": "477cbbe2-d375-4468-8dc9-9ac68c302828", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "half is 0.5\n", "three squared is 9.0\n" ] } ], "source": [ "print('half is', 1 / 2.0)\n", "print('three squared is ', 3.0 ** 2)" ] }, { "cell_type": "code", "execution_count": 20, "id": "e12299cd-c9e5-4482-913b-15c1b5d1970f", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "first is 2 and second is 5\n" ] } ], "source": [ "first = 1\n", "second = 5 * first\n", "first = 2\n", "print('first is', first,' and second is',second)" ] }, { "cell_type": "raw", "id": "2172eb13-f881-4632-9a0a-a83fabd5e47b", "metadata": {}, "source": [ "What type of values is 3.4? How can you find out?" ] }, { "cell_type": "raw", "id": "40df3e9f-6669-462d-9c12-4af033750b9d", "metadata": {}, "source": [ "3.4 is type ???" ] }, { "cell_type": "code", "execution_count": 21, "id": "f8087ade-321f-4754-9c33-59e80940d47d", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\n" ] } ], "source": [ "print(type(3.4))" ] }, { "cell_type": "raw", "id": "68983c04-158e-4a30-92c4-060ea90212c6", "metadata": {}, "source": [ "1. Number of day since the start of the year.\n", "2. Time elasped from the start of the year until now in days.\n", "3. Serial number of a piece of lab equipment.\n", "4. A lab specimen's age?\n", "5. Current population of a city?\n", "6. Average population of a city over time?\n" ] }, { "cell_type": "code", "execution_count": 22, "id": "e4d08f93-f75f-415d-8154-dc14264e74ef", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "5 // 3: 1\n", "5 / 3: 1.6666666666666667\n", "5 % 3: 2\n" ] } ], "source": [ "print('5 // 3:', 5 //3)\n", "print('5 / 3:' , 5 / 3)\n", "print('5 % 3:' , 5 % 3)" ] }, { "cell_type": "raw", "id": "3636a5b7-3704-4804-812a-23599931c9e3", "metadata": {}, "source": [ "If num_subject is the number of subjects taking part in a study, and num_per_survey is the number that can take part in a single survey, write an expession that calculates the number of surveys needed to rach everyone once." ] }, { "cell_type": "code", "execution_count": 32, "id": "a02cf529-973d-428c-9834-6b77db5ee70e", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "588 subjects, 42 per survey: 14\n" ] } ], "source": [ "num_subjects = 588\n", "num_per_survey = 42\n", "num_surveys = (num_subjects -1 ) // num_per_survey + 1\n", "print(num_subjects, 'subjects,',num_per_survey,'per survey:',num_surveys)" ] }, { "cell_type": "code", "execution_count": 27, "id": "4e04e34d-ca86-41fc-865c-4219062588fd", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "14.285714285714286\n" ] } ], "source": [ "print(600/42)\n" ] }, { "cell_type": "code", "execution_count": 29, "id": "e3bd0d1f-8b5b-4562-9437-ab2b8555010f", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "588\n" ] } ], "source": [ "print(14*42)\n" ] }, { "cell_type": "code", "execution_count": 33, "id": "8a8403f8-0158-460d-961b-5d05483b0784", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "string to float: 3.4\n", "float to int: 3\n" ] } ], "source": [ "print(\"string to float:\",float(\"3.4\"))\n", "print(\"float to int:\",int(3.4))" ] }, { "cell_type": "code", "execution_count": 34, "id": "20a2a617-c85c-402a-9b0d-4c46021e3854", "metadata": {}, "outputs": [ { "ename": "ValueError", "evalue": "could not convert string to float: 'Hello world!'", "output_type": "error", "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[0;31mValueError\u001b[0m Traceback (most recent call last)", "\u001b[0;32m/tmp/ipykernel_297/2192291161.py\u001b[0m in \u001b[0;36m\u001b[0;34m\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m\"string to float:\"\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mfloat\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m\"Hello world!\"\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", "\u001b[0;31mValueError\u001b[0m: could not convert string to float: 'Hello world!'" ] } ], "source": [ "print(\"string to float:\", float(\"Hello world!\"))" ] }, { "cell_type": "code", "execution_count": 35, "id": "192dbb98-8785-4157-ae01-4b66f045bcfc", "metadata": {}, "outputs": [ { "ename": "ValueError", "evalue": "invalid literal for int() with base 10: '3.4'", "output_type": "error", "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[0;31mValueError\u001b[0m Traceback (most recent call last)", "\u001b[0;32m/tmp/ipykernel_297/2003631168.py\u001b[0m in \u001b[0;36m\u001b[0;34m\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m\"fractional string to int:\"\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m\"3.4\"\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", "\u001b[0;31mValueError\u001b[0m: invalid literal for int() with base 10: '3.4'" ] } ], "source": [ "print(\"fractional string to int:\", int(\"3.4\"))" ] }, { "cell_type": "code", "execution_count": 42, "id": "01f1aa6a-c1be-4094-9e30-a9e1728691a1", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "4\n" ] } ], "source": [ "print(int(round((float(\"3.9\")))))" ] }, { "cell_type": "markdown", "id": "6991f2ca-20fe-4c3a-ac5e-67e93e75be98", "metadata": {}, "source": [ "Which of the following will return the floating point number of 2.0?" ] }, { "cell_type": "code", "execution_count": 43, "id": "ae405f43-81c0-4b6f-85b9-2a7541403dbe", "metadata": {}, "outputs": [], "source": [ "first = 1.0\n", "second = \"1\"\n", "third = \"1.1\"" ] }, { "cell_type": "raw", "id": "7f0b4113-d0a4-4b85-91b1-e99ae4149bf1", "metadata": {}, "source": [ "1. first + float(second)\n", "2. float(second) + float(third)\n", "3. first + int(third)\n", "4. first + int(float(third))\n", "5. int(first) + int(float(third))\n", "6. 2.0 * second" ] }, { "cell_type": "code", "execution_count": 44, "id": "d78735dd-4dc2-49b8-a8b2-9f8b13629c8f", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "6.0\n", "2.0\n" ] } ], "source": [ "complex = 6 + 2j\n", "print(complex.real)\n", "print(complex.imag)" ] }, { "cell_type": "raw", "id": "3a5aa715-91c0-4876-bd82-7b3235a46960", "metadata": {}, "source": [ "what do you expect 1+2j + 3 to procude?" ] }, { "cell_type": "code", "execution_count": 45, "id": "e16a1442-8b7d-4d7c-8aa8-7c5ea61f6512", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "(4+2j)\n" ] } ], "source": [ "print(1 + 2j +3)" ] }, { "cell_type": "raw", "id": "cbbc4bf4-e700-4ac1-9067-4fb6a9866cc0", "metadata": {}, "source": [ "What do you expct for 4j, 4 j or 4 + j?" ] }, { "cell_type": "code", "execution_count": 46, "id": "17734b43-4ca4-4bf3-a870-5265d25b016c", "metadata": {}, "outputs": [ { "ename": "SyntaxError", "evalue": "invalid syntax (1478133240.py, line 1)", "output_type": "error", "traceback": [ "\u001b[0;36m File \u001b[0;32m\"/tmp/ipykernel_297/1478133240.py\"\u001b[0;36m, line \u001b[0;32m1\u001b[0m\n\u001b[0;31m print(1 + 4 j)\u001b[0m\n\u001b[0m ^\u001b[0m\n\u001b[0;31mSyntaxError\u001b[0m\u001b[0;31m:\u001b[0m invalid syntax\n" ] } ], "source": [ "print(1 + 4 j)" ] }, { "cell_type": "code", "execution_count": null, "id": "ecc871a7-6df8-4dba-90cf-cc73d6d2cb9f", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "id": "edba1716-b6d5-45a0-ad69-85523bc83f36", "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Python 3 (ipykernel)", "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.6" } }, "nbformat": 4, "nbformat_minor": 5 }