{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "\n", " \"Open\n", "\n", "   \n", " \n", " \"Download\"\n", " " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "
\n", "

\n", " Week 2: Python Fundamental Part 1\n", "

\n", "
" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n", "### 1. Variables\n", "Variable in Python store data values\n", "\n", "`print()` is a built-in function in python" ] }, { "cell_type": "code", "execution_count": 35, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "summer\n" ] } ], "source": [ "x = 'summer'\n", "print(x)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### 2. Numeirc\n", "#### 2.1 Integer\n", "we can use the **built-in** `type()` **function** to identify the type of variable" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "int" ] }, "execution_count": 1, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a = 3\n", "type(3)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### 2.2. float" ] }, { "cell_type": "code", "execution_count": 42, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "float" ] }, "execution_count": 42, "metadata": {}, "output_type": "execute_result" } ], "source": [ "c = 6.3\n", "type(c)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### 2.3 Python Arithmetic Operators\n", "The common mathematical operations can be completed by aritemetic operatos in Python\n", "\n", "[Python Arithmetic Operators](https://www.w3schools.com/python/gloss_python_arithmetic_operators.asp)" ] }, { "cell_type": "code", "execution_count": 25, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "8" ] }, "execution_count": 25, "metadata": {}, "output_type": "execute_result" } ], "source": [ "3 + 5" ] }, { "cell_type": "code", "execution_count": 26, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "2.0" ] }, "execution_count": 26, "metadata": {}, "output_type": "execute_result" } ], "source": [ "8/4" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### 3. Strings\n", "Strings in python are surrounded by either double quotation marks, or single quotation marks" ] }, { "cell_type": "code", "execution_count": 32, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Hello\n", "Hello\n" ] } ], "source": [ "print(\"Hello\")\n", "print('Hello')" ] }, { "cell_type": "code", "execution_count": 28, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\n", "13\n" ] } ], "source": [ "strs_ex = \"It's an apple\"\n", "\n", "print(type(strs_ex))\n", "print(len(strs_ex))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "A number with quotation or without quotation is different" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\n", "\n" ] } ], "source": [ "num = 3\n", "str_3 = '3'\n", "print(type(num))\n", "print(type(str_3))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### 3.1 Slicing strings" ] }, { "cell_type": "code", "execution_count": 60, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "20\n", "la\n" ] } ], "source": [ "# Slicing: Return a range of characters by using the slice syntax\n", "x = \"Clark is pretty good\"\n", "print(len(x))\n", "print(x[1:3])" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Hello\n", "llo, Python!\n", "tho\n" ] } ], "source": [ "# Slice from the start\n", "b = \"Hello, Python!\"\n", "print(b[:5])\n", "\n", "# Slice to the end\n", "print(b[2:])\n", "\n", "# Negative indexing\n", "print(b[-5:-2])" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "

Exercise 1: Get the characters from string

\n", "1. Get the character that index is 0 in x = \"Clark is pretty good\"" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### 3.2 String Concatenation" ] }, { "cell_type": "code", "execution_count": 53, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "LevelUp\n", "Level Up\n" ] } ], "source": [ "a = \"Level\"\n", "b = \"Up\"\n", "c = a + b\n", "print(c)\n", "d = a + ' ' + b\n", "print(d)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### 3.3 Combine of string and number" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [ { "ename": "TypeError", "evalue": "can only concatenate str (not \"int\") to str", "output_type": "error", "traceback": [ "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[1;31mTypeError\u001b[0m Traceback (most recent call last)", "Cell \u001b[1;32mIn[9], line 2\u001b[0m\n\u001b[0;32m 1\u001b[0m age \u001b[38;5;241m=\u001b[39m \u001b[38;5;241m36\u001b[39m\n\u001b[1;32m----> 2\u001b[0m txt \u001b[38;5;241m=\u001b[39m \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mMy name is John, I am \u001b[39m\u001b[38;5;124m\"\u001b[39m \u001b[38;5;241m+\u001b[39m age\n\u001b[0;32m 3\u001b[0m \u001b[38;5;28mprint\u001b[39m(txt)\n", "\u001b[1;31mTypeError\u001b[0m: can only concatenate str (not \"int\") to str" ] } ], "source": [ "age = 36\n", "txt = \"My name is John, I am \" + age\n", "print(txt)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "

Exercise 2: Type conversion

\n", "\n", "1. We have used built-in function like `print()` and `type()` to do task, this time we use `str()` to convert a value to string \n", "2. Use `str()` function to remove the error" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "I have lived here for 5 years\n" ] } ], "source": [ "# F-Strings\n", "age = 5\n", "txt = f\"I have lived here for {age} years\"\n", "print(txt)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### 3.4 Methods for String\n", "A **method** is a special type of function, it allow you to do different things with different objects.\n", "\n", "We can use methods through **dot notation**: `variable.method()`" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "names = \"Clark\"\n", "u_names = names.upper()\n", "print(u_names)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "[Methods for String in Python Documentation](https://docs.python.org/3/library/stdtypes.html#string-methods)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "

Exercise 3: Try methods for String

\n", "\n", "1. get lowercase of string **names = \"Clark'** use `lower()`" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### 💡 Tip: Guidelines for Variables\n", "- Python is case-sensitive (`apple` and `Apple` are two separate variables).\n", "- Use meaningful variable names (e.g. `university` is clearer than `a_string`). Ideally, you code and variable names should be meaningful.\n", "- Avoid using variable names that conflict with existing python functions or keywords (e.g., `print`, `type`)." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### 4. Boolean\n", "- The expression in Python is True and False" ] }, { "cell_type": "code", "execution_count": 61, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "True\n" ] } ], "source": [ "print(5 > 3)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "- Most Values are True" ] }, { "cell_type": "code", "execution_count": 64, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "True\n", "True\n", "True\n" ] } ], "source": [ "print(bool(\"abc\"))\n", "print(bool(123))\n", "print(bool([\"apple\", \"cherry\", \"banana\"]))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "- Empty values are False" ] }, { "cell_type": "code", "execution_count": 65, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "False\n", "False\n", "False\n", "False\n", "False\n" ] } ], "source": [ "print(bool(False))\n", "print(bool(None))\n", "print(bool(0))\n", "print(bool(\"\"))\n", "print(bool(()))\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### 5. List" ] }, { "cell_type": "code", "execution_count": 13, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "['python', 'statistics', 'mapping', 'mapping']\n", "4\n" ] } ], "source": [ "myclass = ['python', 'statistics', 'mapping', 'mapping']\n", "print(myclass)\n", "print(len(myclass))" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "mynote = [\"python\", 10, True, 'mapping', 23]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### 5.1 Change list items" ] }, { "cell_type": "code", "execution_count": 66, "metadata": {}, "outputs": [], "source": [ "grocery_shops = [\"Whole Foods\", \"Trader Joe's\", \"Stop & stop\", \"Kroger\"]\n", "grocery_shops[1] = \"Wegmans\"" ] }, { "cell_type": "code", "execution_count": 36, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "['orange', 'blueberries', 'apple', 'Mangoes']\n" ] } ], "source": [ "#### 5.2 Sort the list\n", "fruits = ['apple', 'orange', 'blueberries', 'Mangoes']\n", "fruits.sort(reverse=True)\n", "print(fruits)\n" ] }, { "cell_type": "code", "execution_count": 33, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "['orange', 'blueberries', 'apple', 'Mangoes']\n" ] } ], "source": [ "fruits.sort(reverse=True)\n", "print(fruits)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "

Exercise 4: Try method for list

\n", "\n", "1. Append **kroger** to list **grocery_shops = [\"Whole Foods\", \"Trader Joe's\"]** use `append()`" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### 6 Dictionaries\n", "- Dictionaries store data value in key:value pairs\n", "- Access value using the key\n", "- Dictionaries cannot have two items with the same key:" ] }, { "cell_type": "code", "execution_count": 15, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "clark\n", "{'name': 'clark', 'location': 'worcester', 'year': 1887}\n" ] } ], "source": [ "university = {\n", " \"name\": \"clark\",\n", " \"location\": 'MA',\n", " \"location\": \"worcester\",\n", " \"year\": 1887,\n", " 'major': ['GIS', 'sociology', 'computer science']\n", "}\n", " \n", "print(university['name'])\n", "print(university['major'])\n", "print(university)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### 6.1 Adding items" ] }, { "cell_type": "code", "execution_count": 17, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "{'name': 'clark', 'location': 'worcester', 'year': 1887, 'room': 'BP310'}\n" ] } ], "source": [ "university['room'] = 'BP310'\n", "print(university)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### 6.2 Methods for dictionaries" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "university = {\n", " \"name\": \"clark\",\n", " \"location\": \"worcester\",\n", " \"year\": 1887\n", "}" ] }, { "cell_type": "code", "execution_count": 23, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "dict_keys(['name', 'location', 'year', 'room'])" ] }, "execution_count": 23, "metadata": {}, "output_type": "execute_result" } ], "source": [ "### Get keys\n", "key_un = university.keys()\n", "key_un" ] }, { "cell_type": "code", "execution_count": 42, "metadata": {}, "outputs": [], "source": [ "### Dictionaries in Dictionaries\n", "university = {\n", " \"name\": \"clark\",\n", " \"location\": \"worcester\",\n", " \"year\": 1887, \n", " \"description\": {'student':3827, 'intro':'''Founded in 1887, Clark offers undergraduate and graduate\n", " programs with a focus on interdisciplinary studies,\n", " research opportunities, and community engagement'''}}\n" ] }, { "cell_type": "code", "execution_count": 43, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Founded in 1887, Clark offers undergraduate and graduate\n", " programs with a focus on interdisciplinary studies,\n", " research opportunities, and community engagement\n" ] } ], "source": [ "print(university['description']['intro'])" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "

Exercise 5: Try methods for Dictionaries

\n", "\n", "1. Use `values()`, `items()` to get value and item from **university**" ] } ], "metadata": { "kernelspec": { "display_name": "base", "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.4" } }, "nbformat": 4, "nbformat_minor": 2 }