From 724b663109cd89785cea6d8b1b30d06f41780d22 Mon Sep 17 00:00:00 2001 From: ochienglawre-254 Date: Wed, 24 Jun 2026 18:58:35 +0300 Subject: [PATCH] Upoal 2026 Python Programming Notebook --- 2026 Python Programming Notebook.ipynb | 1 + 1 file changed, 1 insertion(+) create mode 100644 2026 Python Programming Notebook.ipynb diff --git a/2026 Python Programming Notebook.ipynb b/2026 Python Programming Notebook.ipynb new file mode 100644 index 00000000000000..02142dce151c5e --- /dev/null +++ b/2026 Python Programming Notebook.ipynb @@ -0,0 +1 @@ +{"cells":[{"cell_type":"markdown","metadata":{"id":"3S9UGf8T3lRn"},"source":["# A Quick Tour of Variables and Data Types in Python\n","\n","This tutorial is a part of [Data Analysis with Python: Zero to Pandas](https://jovian.ai/learn/data-analysis-with-python-zero-to-pandas) and [Zero to Data Analyst Science Bootcamp](https://jovian.ai/learn/zero-to-data-analyst-bootcamp).\n","\n","\n","![](https://i.imgur.com/6cg2E9Q.png)\n","These tutorials take a practical and coding-focused approach. The best way to learn the material is to execute the code and experiment with it yourself."]},{"cell_type":"markdown","metadata":{"id":"m97AARqc3lRs"},"source":["This tutorial covers the following topics:\n","\n","- Storing information using variables\n","- Primitive data types in Python: Integer, Float, Boolean, None and String\n","- Built-in data structures in Python: List, Tuple and Dictionary\n","- Methods and operators supported by built-in data types"]},{"cell_type":"markdown","metadata":{"id":"PJwB5y2M3lRt"},"source":["## Storing information using variables\n","\n","Computers are useful for two purposes: storing information (also known as data) and performing operations on stored data. While working with a programming language such as Python, data is stored in variables. You can think of variables are containers for storing data. The data stored within a variable is called its value. Creating variables in Python is pretty easy, as we've already seen in the [previous tutorial](https://jovian.ml/aakashns/first-steps-with-python/v/4#C15).\n","\n"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"irfeJuNf3lRt"},"outputs":[],"source":["my_favorite_color = \"blue\""]},{"cell_type":"code","execution_count":null,"metadata":{"id":"eTOJJlxY3lRv"},"outputs":[],"source":["my_favorite_color"]},{"cell_type":"markdown","metadata":{"id":"M2Uab_S83lRw"},"source":["A variable is created using an assignment statement. It begins with the variable's name, followed by the assignment operator `=` followed by the value to be stored within the variable. Note that the assignment operator `=` is different from the equality comparison operator `==`.\n","\n","You can also assign values to multiple variables in a single statement by separating the variable names and values with commas."]},{"cell_type":"code","execution_count":null,"metadata":{"id":"4PdyzbzV3lRx"},"outputs":[],"source":["color1, color2, color3 = \"red\", \"green\", \"blue\""]},{"cell_type":"code","execution_count":null,"metadata":{"id":"6IMpJST53lRy"},"outputs":[],"source":["color1"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"SA4KRli03lRy"},"outputs":[],"source":["color2"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"1Ns_Dppw3lRz"},"outputs":[],"source":["color3"]},{"cell_type":"markdown","metadata":{"id":"vZcnY_E_3lRz"},"source":["You can assign the same value to multiple variables by chaining multiple assignment operations within a single statement."]},{"cell_type":"code","execution_count":null,"metadata":{"id":"PomJtXlp3lRz"},"outputs":[],"source":["color4 = color5 = color6 = \"magenta\""]},{"cell_type":"code","execution_count":null,"metadata":{"id":"hwKFqtCJ3lR0"},"outputs":[],"source":["color4"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"rP7npLbc3lR0"},"outputs":[],"source":["color5"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"GDEFJMMo3lR1"},"outputs":[],"source":["color6"]},{"cell_type":"markdown","metadata":{"id":"W63f5iY73lR1"},"source":["You can change the value stored within a variable by assigning a new value to it using another assignment statement. Be careful while reassigning variables: when you assign a new value to the variable, the old value is lost and no longer accessible."]},{"cell_type":"code","execution_count":null,"metadata":{"id":"GCsb7-mh3lR2"},"outputs":[],"source":["my_favorite_color = \"red\""]},{"cell_type":"code","execution_count":null,"metadata":{"id":"qOv3_Aj13lR2"},"outputs":[],"source":["my_favorite_color"]},{"cell_type":"markdown","metadata":{"id":"uELe2ZKh3lR2"},"source":["While reassigning a variable, you can also use the variable's previous value to compute the new value."]},{"cell_type":"code","execution_count":null,"metadata":{"id":"C9NusN4K3lR2"},"outputs":[],"source":["counter = 10"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"Wj1PGl3B3lR3"},"outputs":[],"source":["counter"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"NnDccGT03lR3"},"outputs":[],"source":["counter = 11"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"rujB2hqO3lR3"},"outputs":[],"source":["counter = counter + 1"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"JZW-BpDD3lR4"},"outputs":[],"source":["counter"]},{"cell_type":"markdown","metadata":{"id":"hHcUTEKn3lR4"},"source":["The pattern `var = var op something` (where `op` is an arithmetic operator like `+`, `-`, `*`, `/`) is very common, so Python provides a *shorthand* syntax for it."]},{"cell_type":"code","execution_count":null,"metadata":{"id":"msEwHItz3lR4"},"outputs":[],"source":["counter = 10"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"wsWviXQO3lR4"},"outputs":[],"source":["counter = counter + 4"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"0t5g5g8x3lR4"},"outputs":[],"source":["counter"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"bv2Puc__3lR4"},"outputs":[],"source":["# Same as `counter = counter + 4`\n","counter += 4"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"bQNZCm6W3lR5"},"outputs":[],"source":["counter"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"5vUMk-ZL3lR5"},"outputs":[],"source":["counter += 4"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"CTmg76463lR5"},"outputs":[],"source":["COUNTER"]},{"cell_type":"markdown","metadata":{"id":"BN7D22Bt3lR5"},"source":["Variable names can be short (`a`, `x`, `y`, etc.) or descriptive ( `my_favorite_color`, `profit_margin`, `the_3_musketeers`, etc.). However, you must follow these rules while naming Python variables:\n","\n","* A variable's name must start with a letter or the underscore character `_`. It cannot begin with a number.\n","* A variable name can only contain lowercase (small) or uppercase (capital) letters, digits, or underscores (`a`-`z`, `A`-`Z`, `0`-`9`, and `_`).\n","* Variable names are case-sensitive, i.e., `a_variable`, `A_Variable`, and `A_VARIABLE` are all different variables.\n","\n","Here are some valid variable names:"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"X97A_Yun3lR6"},"outputs":[],"source":["a_variable = 23\n","is_today_Saturday = False\n","my_favorite_car = \"Delorean\"\n","the_3_musketeers = [\"Athos\", \"Porthos\", \"Aramis\"]"]},{"cell_type":"markdown","metadata":{"id":"tJ0-gX2a3lR6"},"source":["Let's try creating some variables with invalid names. Python prints a syntax error if your variable's name is invalid.\n","\n","> **Syntax**: The syntax of a programming language refers to the rules that govern the structure of a valid instruction or *statement*. If a statement does not follow these rules, Python stops execution and informs you that there is a *syntax error*. You can think of syntax as the rules of grammar for a programming language."]},{"cell_type":"code","execution_count":null,"metadata":{"id":"q0hbK6MT3lR6"},"outputs":[],"source":["2_day = 555"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"UPfrX8L03lR6"},"outputs":[],"source":["so@oi = '7848'"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"FfJ-9m4M3lR6"},"outputs":[],"source":[]},{"cell_type":"code","execution_count":null,"metadata":{"id":"bxYrradZ3lR7"},"outputs":[],"source":[]},{"cell_type":"code","execution_count":null,"metadata":{"id":"uhaSLl-73lR8"},"outputs":[],"source":[]},{"cell_type":"code","execution_count":null,"metadata":{"id":"AvGat-F43lR9"},"outputs":[],"source":[]},{"cell_type":"code","execution_count":null,"metadata":{"id":"1JrC8Axo3lR9"},"outputs":[],"source":[]},{"cell_type":"markdown","metadata":{"id":"lac_oQpO3lR9"},"source":["## Built-in data types in Python\n","\n","Any data or information stored within a Python variable has a *type*. You can view the type of data stored within a variable using the `type` function."]},{"cell_type":"code","execution_count":null,"metadata":{"id":"uXZ6tDum3lR9"},"outputs":[],"source":["my_age = 29"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"300ZwRjF3lR9"},"outputs":[],"source":["my_favorite_car = \"Lamborgini\""]},{"cell_type":"code","execution_count":null,"metadata":{"id":"s4rEDRGp3lR-"},"outputs":[],"source":["is_today_Saturday = True"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"zXfGmAKH3lR-"},"outputs":[],"source":["the_3_learners = [\"Reuben\", \"Victor\", \"Favour\"]"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"KJj6nL9F3lR-"},"outputs":[],"source":["my_age"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"9uNZ3ylS3lR-"},"outputs":[],"source":["type(my_age)"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"NJZdUICv3lR-"},"outputs":[],"source":["type(is_today_Saturday)"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"86FfNXJj3lR_"},"outputs":[],"source":["my_favorite_car"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"qvMtbRDi3lR_"},"outputs":[],"source":["type(my_favorite_car)"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"PRafKxJC3lR_"},"outputs":[],"source":["the_3_learners"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"Dncq_J0c3lR_"},"outputs":[],"source":["type(the_3_learners)"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"1oeWAgB53lR_"},"outputs":[],"source":["the_seed_learners = ('Marvelous', 'Akeem', 'Jennifer', \"Hero\", \"Lovelyn\")"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"-dVR6N2j3lSA"},"outputs":[],"source":["the_seed_learners"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"VIYLVVWU3lSA"},"outputs":[],"source":["type(the_seed_learners)"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"RgE-JiRq3lSA"},"outputs":[],"source":["print(the_seed_learners)"]},{"cell_type":"markdown","metadata":{"id":"ec480Kvc3lSA"},"source":["Python has several built-in data types for storing different kinds of information in variables. Following are some commonly used data types:\n","\n","1. Integer\n","2. Float\n","3. Boolean\n","4. None\n","5. String\n","6. List\n","7. Tuple\n","8. Dictionary\n","\n","Integer, float, boolean, None, and string are *primitive data types* because they represent a single value. Other data types like list, tuple, and dictionary are often called *data structures* or *containers* because they hold multiple pieces of data together."]},{"cell_type":"markdown","metadata":{"id":"pt7Pd_s83lSB"},"source":["### Integer\n","\n","Integers represent positive or negative whole numbers, from negative infinity to infinity.\n","`Note that integers should not include decimal points`.\n","\n","Integers have the type `int`."]},{"cell_type":"code","execution_count":null,"metadata":{"id":"9Uo1M7XN3lSB"},"outputs":[],"source":["current_year = 2024"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"9WI0ZBqu3lSB"},"outputs":[],"source":["current_year"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"DmJxT2ID3lSB"},"outputs":[],"source":["type(current_year)"]},{"cell_type":"markdown","metadata":{"id":"tt5zMLOO3lSC"},"source":["Unlike some other programming languages, integers in Python can be arbitrarily large (or small). There's no lowest or highest value for integers, and there's just one `int` type (as opposed to `short`, `int`, `long`, `long long`, `unsigned int`, etc. in C/C++/Java)."]},{"cell_type":"code","execution_count":null,"metadata":{"id":"4JMheSU-3lSC"},"outputs":[],"source":["a_large_negative_number = -23374038374832934334234317348343"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"3bnHxKE73lSC"},"outputs":[],"source":["a_large_negative_number"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"3XRD51s73lSD"},"outputs":[],"source":["type(a_large_negative_number)"]},{"cell_type":"markdown","metadata":{"id":"HGsYt3Ns3lSE"},"source":["### Float\n","\n","Floats (or floating-point numbers) are numbers with a decimal point. There are no limits on the value or the number of digits before or after the decimal point. Floating-point numbers have the type `float`."]},{"cell_type":"code","execution_count":null,"metadata":{"id":"aniekogA3lSJ"},"outputs":[],"source":["pi = 3.141592653589793238"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"E0UrxdFm3lSK"},"outputs":[],"source":["pi"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"xQlf_2HP3lSK"},"outputs":[],"source":["type(pi)"]},{"cell_type":"markdown","metadata":{"id":"h4_WcpB63lSL"},"source":["Note that a whole number is treated as a float if written with a decimal point, even though the decimal portion of the number is zero."]},{"cell_type":"code","execution_count":null,"metadata":{"id":"kNTOOFdj3lSM"},"outputs":[],"source":["a_number = 3."]},{"cell_type":"code","execution_count":null,"metadata":{"id":"OjA3UFHu3lSM"},"outputs":[],"source":["a_number"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"knm-93Yh3lSN"},"outputs":[],"source":["type(a_number)"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"cCbLNL6q3lSN"},"outputs":[],"source":["another_number = 4.0"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"V0hY-4vf3lSO"},"outputs":[],"source":["another_number"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"dfuARcQS3lSO"},"outputs":[],"source":["type(another_number)"]},{"cell_type":"markdown","metadata":{"id":"8r5oWbTm3lSO"},"source":["Floating point numbers can also be written using the scientific notation with an \"e\" to indicate the power of 10."]},{"cell_type":"code","execution_count":null,"metadata":{"id":"PHGcJ2UZ3lSP"},"outputs":[],"source":["one_hundredth = 1e-2"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"AeSdyJBm3lSP"},"outputs":[],"source":["one_hundredth"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"6oM-9NtE3lSP"},"outputs":[],"source":["type(one_hundredth)"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"sCLGYwdx3lSQ"},"outputs":[],"source":["one_hundred = 1e2"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"42pTyEhF3lSQ"},"outputs":[],"source":["one_hundred"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"S-ukAGjA3lSQ"},"outputs":[],"source":["type(one_hundred)"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"apfXO-DQ3lSR"},"outputs":[],"source":["type(one_hundredth)"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"9rwcyL3B3lSR"},"outputs":[],"source":["avogadro_number = 6.02214076e23"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"CNfbeoPh3lSR"},"outputs":[],"source":["avogadro_number"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"M4cCLbmk3lSS"},"outputs":[],"source":["type(avogadro_number)"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"rIRsdnps3lSS"},"outputs":[],"source":["num = 1e2"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"wBnTPvsR3lSS"},"outputs":[],"source":["num"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"nazbxzf93lST"},"outputs":[],"source":["type(num)"]},{"cell_type":"markdown","metadata":{"id":"7njlS35W3lST"},"source":["**You can convert floats into integers and vice versa using the `float` and `int` functions.**\n","\n","**The operation of converting one type of value into another is called `casting`.**"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"EYywDbem3lST"},"outputs":[],"source":["current_year"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"ftv7ZRaj3lST"},"outputs":[],"source":["float(current_year)"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"NGrVG6tx3lST"},"outputs":[],"source":["current_year = float(current_year)"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"UvXWPqO93lST"},"outputs":[],"source":["current_year"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"LbtOiUDQ3lSU"},"outputs":[],"source":["type(current_year)"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"4riPV3OZ3lSU"},"outputs":[],"source":["a_large_negative_number"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"EOFuZlQ33lSU"},"outputs":[],"source":["float(a_large_negative_number)"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"beUD0mJQ3lSU"},"outputs":[],"source":["pi"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"AwDbyBjK3lSU"},"outputs":[],"source":["int(pi)"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"tBDU-Wyf3lSV"},"outputs":[],"source":["avogadro_number"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"8nZjWWUo3lSV"},"outputs":[],"source":["int(avogadro_number)"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"0KdFcy-b3lSV"},"outputs":[],"source":["int(3456.0)"]},{"cell_type":"markdown","metadata":{"id":"m22qK7-c3lSV"},"source":["While performing arithmetic operations, integers are automatically converted to `float`s if any of the operands is a `float`. Also, the division operator `/` always returns a `float`, even if both operands are integers. Use the `//` operator if you want the result of the division to be an `int`."]},{"cell_type":"code","execution_count":null,"metadata":{"id":"_9ZjXOtf3lSV"},"outputs":[],"source":["45 + 3.0"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"9x5npr-q3lSW"},"outputs":[],"source":["type(45 * 3.0)"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"CxHp2h2U3lSW"},"outputs":[],"source":["45 * 3."]},{"cell_type":"code","execution_count":null,"metadata":{"id":"4nqE2AYG3lSW"},"outputs":[],"source":["type(45 * 3)"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"ehE8Cc613lSW"},"outputs":[],"source":["10/2"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"Va0TX2zA3lSW"},"outputs":[],"source":["type(10/3)"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"FlCXFaGg3lSX"},"outputs":[],"source":["10/4"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"6rTstbPS3lSX"},"outputs":[],"source":["10//2"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"FKZgQcvr3lSX"},"outputs":[],"source":["type(10/2)"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"LAcRTZ_W3lSX"},"outputs":[],"source":["10 % 3"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"cx6L0VSb3lSX"},"outputs":[],"source":["type(10//2)"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"3SLmpSW23lSY"},"outputs":[],"source":["round(23.67798)"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"GFgJ0ZhQ3lSY"},"outputs":[],"source":["round(23.67798, 2)"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"rMvu2T_L3lSY"},"outputs":[],"source":["import math"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"mw4JESXq3lSY"},"outputs":[],"source":["math.ceil(23.2798)"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"sDkVZ3tq3lSZ"},"outputs":[],"source":["math.floor(23.2798)"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"xrsatmGe3lSZ"},"outputs":[],"source":["round(23.27798)"]},{"cell_type":"markdown","metadata":{"id":"hgrJYcxs3lSZ"},"source":["### Boolean\n","\n","Booleans represent one of 2 values: `True` and `False`. Booleans have the type `bool`."]},{"cell_type":"code","execution_count":null,"metadata":{"id":"xS_1zDf03lSZ"},"outputs":[],"source":["type(False)"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"EdV5FTe23lSZ"},"outputs":[],"source":["type(True)"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"q4mrRs-b3lSa"},"outputs":[],"source":["is_today_Sunday = False"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"ielTvfAQ3lSa"},"outputs":[],"source":["is_today_Sunday"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"XPxxXCCX3lSa"},"outputs":[],"source":["is_today_Saturday = True"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"pSl4o84y3lSb"},"outputs":[],"source":["is_today_Saturday"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"0ku5hTf73lSb"},"outputs":[],"source":["type(is_today_Saturday)"]},{"cell_type":"markdown","metadata":{"id":"ZQzI6SQ43lSb"},"source":["Booleans are generally the result of a comparison operation, e.g., `==`, `>=`, etc."]},{"cell_type":"code","execution_count":null,"metadata":{"id":"W7gQcW0K3lSc"},"outputs":[],"source":["cost_of_ice_bag = 1.25\n","\n","\n","is_ice_bag_expensive = cost_of_ice_bag >= 10"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"tVbVvdAQ3lSc"},"outputs":[],"source":["is_ice_bag_expensive"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"mQq9Z6tf3lSc"},"outputs":[],"source":["type(is_ice_bag_expensive)"]},{"cell_type":"markdown","metadata":{"id":"RWlT5wwr3lSc"},"source":["Booleans are automatically converted to `int`s when used in arithmetic operations. `True` is converted to `1` and `False` is converted to `0`."]},{"cell_type":"code","execution_count":null,"metadata":{"id":"v13huM1n3lSc"},"outputs":[],"source":["5 + False ## False = 0"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"U9b6zghL3lSd"},"outputs":[],"source":["3.0 + True ### True = 1"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"BKtSWi5L3lSd"},"outputs":[],"source":["type(10000)"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"h9cmGJlL3lSd"},"outputs":[],"source":["hgu = bool()\n","hgu"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"qvR-Jkfk3lSd"},"outputs":[],"source":["type(hgu)"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"9dv_0_rS3lSd"},"outputs":[],"source":["\"Solomon\""]},{"cell_type":"code","execution_count":null,"metadata":{"id":"zXphiHz-3lSe"},"outputs":[],"source":["bool(())"]},{"cell_type":"markdown","metadata":{"id":"yiHtONRo3lSe"},"source":["Any value in Python can be converted to a Boolean using the `bool` function.\n","\n","Only the following values evaluate to `False` (they are often called *falsy* values):\n","\n","1. The value `False` itself\n","2. The integer `0`\n","3. The float `0.0`\n","4. The empty value `None`\n","5. The empty text `\"\"`\n","6. The empty list `[]`\n","7. The empty tuple `()`\n","8. The empty dictionary `{}`\n","9. The empty set `set()`\n","10. The empty range `range(0)`\n","\n","Everything else evaluates to `True` (a value that evaluates to `True` is often called a *truthy* value)."]},{"cell_type":"code","execution_count":null,"metadata":{"id":"Up-DJcSd3lSe"},"outputs":[],"source":["bool(False)"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"YtCpArxz3lSf"},"outputs":[],"source":["bool(0)"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"VbHub7Ti3lSf"},"outputs":[],"source":["bool(0.0)"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"tjr3yxwC3lSf"},"outputs":[],"source":["bool(None)"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"gh7csio93lSf"},"outputs":[],"source":["bool(\"\")"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"YsS2jan23lSf"},"outputs":[],"source":["bool([])"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"W7EUFklG3lSf"},"outputs":[],"source":["bool(())"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"x2ULQgGI3lSg"},"outputs":[],"source":["bool({})"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"dmPF5fDX3lSg"},"outputs":[],"source":["bool(set())"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"ZFqBtI7z3lSg"},"outputs":[],"source":["bool(range(0))"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"kc4Q7yAN3lSg"},"outputs":[],"source":["bool(True), bool(1), bool(2000.0), bool(\"hello\"), bool([1,2]), bool((2,3)), bool(range(10))"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"-LpX3jYc3lSg"},"outputs":[],"source":["bool(0)"]},{"cell_type":"markdown","metadata":{"id":"-sG4ppvo3lSg"},"source":["### None\n","\n","The None type includes a single value `None`, used to indicate the absence of a value. `None` has the type `NoneType`. It is often used to declare a variable whose value may be assigned later."]},{"cell_type":"code","execution_count":null,"metadata":{"id":"iGnZOFMO3lSh"},"outputs":[],"source":["Nothing = None"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"YFg4KkNR3lSh"},"outputs":[],"source":["nothing = None"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"reZF0gEa3lSh"},"outputs":[],"source":["print(nothing)"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"l7WoQ-Sg3lSh"},"outputs":[],"source":["type(nothing)"]},{"cell_type":"markdown","metadata":{"jp-MarkdownHeadingCollapsed":true,"id":"fYRAFZ943lSh"},"source":["### String\n","\n","A string is used to represent text (*a string of characters*) in Python. Strings must be surrounded using quotations (either the single quote `'` or the double quote `\"`). Strings have the type `string`."]},{"cell_type":"code","execution_count":null,"metadata":{"id":"QUbT4Luh3lSh"},"outputs":[],"source":["'Monday'"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"wAQiE1PH3lSi"},"outputs":[],"source":["\"Monday\""]},{"cell_type":"code","execution_count":null,"metadata":{"id":"GdoZP3A43lSi"},"outputs":[],"source":["'Monday\""]},{"cell_type":"code","execution_count":null,"metadata":{"id":"tsPiUJZT3lSi"},"outputs":[],"source":["today = \"Sunday\""]},{"cell_type":"code","execution_count":null,"metadata":{"id":"AI0vJpHu3lSi"},"outputs":[],"source":["today"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"Sh9AQubX3lSi"},"outputs":[],"source":["type(today)"]},{"cell_type":"markdown","metadata":{"id":"C4axqTj_3lSj"},"source":["You can use single quotes inside a string written with double quotes, and vice versa."]},{"cell_type":"code","execution_count":null,"metadata":{"id":"b0uAyGi33lSj"},"outputs":[],"source":["my_favorite_movie = \"One Flew over the Cuckoo's Nest\""]},{"cell_type":"code","execution_count":null,"metadata":{"id":"Bhpj-Akz3lSj"},"outputs":[],"source":["my_favorite_movie"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"x6y1aRLe3lSj"},"outputs":[],"source":["my_favorite_pun = 'Thanks for explaining the word \"many\" to me, it means a lot.'"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"OqtlXcvT3lSj"},"outputs":[],"source":["my_favorite_pun"]},{"cell_type":"markdown","metadata":{"id":"N10PcS0p3lSk"},"source":["To use a double quote within a string written with double quotes, *escape* the inner quotes by prefixing them with the `\\` character."]},{"cell_type":"code","execution_count":null,"metadata":{"id":"JPcxQVOs3lSk"},"outputs":[],"source":["another_pun = \"The first time I got a universal remote control, I thought to myself \\\"This changes everything\\\".\""]},{"cell_type":"code","execution_count":null,"metadata":{"id":"E5_PvQmR3lSl"},"outputs":[],"source":["another_pun"]},{"cell_type":"markdown","metadata":{"id":"M__lDGOu3lSl"},"source":["Strings created using single or double quotes must begin and end on the same line. To create multiline strings, use three single quotes `'''` or three double quotes `\"\"\"` to begin and end the string. Line breaks are represented using the newline character `\\n`."]},{"cell_type":"code","execution_count":null,"metadata":{"id":"5ZXwAnI03lSl"},"outputs":[],"source":["yet_another_pun = '''Son: \"Dad, can you tell me what a solar eclipse is?\"\n","Dad: \"No sun.\"\n","\"Sunny\" '''"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"4e76oNKo3lSm"},"outputs":[],"source":["yet_another_pun"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"rWAT8IQU3lSm"},"outputs":[],"source":["print(yet_another_pun)"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"e19Nz0Ld3lSm"},"outputs":[],"source":["print('Dad: \\n\"No sun.\"')"]},{"cell_type":"markdown","metadata":{"id":"v3U38tHr3lSm"},"source":["Multiline strings are best displayed using the `print` function."]},{"cell_type":"code","execution_count":null,"metadata":{"id":"fjgl6SNQ3lSn"},"outputs":[],"source":["print(yet_another_pun)"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"RxXAa5Kr3lSn"},"outputs":[],"source":["a_music_pun = '''\n","Two windmills are standing in a field and one asks the other,\n","\"What kind of music do you like?\"\n","\n","The other says,\n","\"I'm a big metal fan.\"\n","'''"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"Kslq1QyE3lSn"},"outputs":[],"source":["a_music_pun"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"c9UdY-3i3lSn"},"outputs":[],"source":["print(a_music_pun)"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"uIitxk883lSn"},"outputs":[],"source":["help(len)"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"hhTxaHkw3lSn"},"outputs":[],"source":["len(a_music_pun)"]},{"cell_type":"markdown","metadata":{"id":"hAg0NhYP3lSo"},"source":["You can check the length of a string using the `len` function."]},{"cell_type":"code","execution_count":null,"metadata":{"id":"O47Hwygd3lSo"},"outputs":[],"source":["my_favorite_movie"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"sjckia3V3lSo"},"outputs":[],"source":["len(my_favorite_movie)"]},{"cell_type":"markdown","metadata":{"id":"oMfA5IWz3lSp"},"source":["Note that special characters like `\\n` and escaped characters like `\\\"` count as a single character, even though they are written and sometimes printed as two characters."]},{"cell_type":"code","execution_count":null,"metadata":{"id":"TkN5iTcY3lSp"},"outputs":[],"source":["empty = ' '\n","len(empty)"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"GMUGNgmd3lSp"},"outputs":[],"source":["multiline_string = \"\"\"a\n","b\"\"\"\n","print(multiline_string)"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"uqUXL-9C3lSq"},"outputs":[],"source":["len(multiline_string)"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"DSREyPO43lSq"},"outputs":[],"source":["print('a \\nb')"]},{"cell_type":"markdown","metadata":{"id":"4hA4Ct1Q3lSr"},"source":["A string can be converted into a list of characters using `list` function."]},{"cell_type":"code","execution_count":null,"metadata":{"id":"vZSIk0Xh3lSr"},"outputs":[],"source":["list(multiline_string)"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"TxPbFQGQ3lSs"},"outputs":[],"source":["a_music_pun"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"NIvUOKrf3lSs"},"outputs":[],"source":["print(a_music_pun)"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"JoM7st7d3lSs"},"outputs":[],"source":["list(a_music_pun)"]},{"cell_type":"markdown","metadata":{"id":"vZcsDJsP3lSt"},"source":["Strings also support several list operations, which are discussed in the next section. We'll look at a couple of examples here.\n","\n","#### Indexing and Selection\n","\n","You can access individual characters within a string using the **`[]`** `indexing notation`. Note the character indices go from `0` to `n-1`, where `n` is the length of the string."]},{"cell_type":"code","execution_count":null,"metadata":{"id":"sPyf4gEj3lSt"},"outputs":[],"source":["name = 'Jennifer'"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"k2_Qymot3lSu"},"outputs":[],"source":["name[3]"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"HxQNpAdf3lSu"},"outputs":[],"source":["name[6]"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"5xWEEKMX3lSv"},"outputs":[],"source":["name[7]"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"7-IYjoNO3lSv"},"outputs":[],"source":["name[0]"]},{"cell_type":"markdown","metadata":{"id":"eBpplQ683lSv"},"source":["##### Negative"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"f6Wzwcwg3lSv"},"outputs":[],"source":["name"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"ae2BVdyR3lSv"},"outputs":[],"source":["name[-4]"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"eTRblrBS3lSw"},"outputs":[],"source":["name[-1]"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"FulhYT4w3lSw"},"outputs":[],"source":["name[-8]"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"y7oy9CCb3lSw"},"outputs":[],"source":["df = 'Saturday'"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"DssPEq8u3lSw"},"outputs":[],"source":["df[-6]"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"CPKoHkbO3lSx"},"outputs":[],"source":["df[2]"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"ye7bQ5-s3lSx"},"outputs":[],"source":["df[-4]"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"KOL8JcoE3lSx"},"outputs":[],"source":["df[4]"]},{"cell_type":"markdown","metadata":{"id":"jH8-BDPa3lSx"},"source":["#### Slicing/Selection"]},{"cell_type":"markdown","metadata":{"id":"GiVbJkWG3lSy"},"source":["You can access a part of a string using by providing a `start:end:stepping` range instead of a single index in `[]`."]},{"cell_type":"code","execution_count":null,"metadata":{"id":"aDiF5YBB3lSy"},"outputs":[],"source":["name"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"0pZVkaNF3lSy"},"outputs":[],"source":["name[:4]"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"qEA7hNHa3lSy"},"outputs":[],"source":["name[3:]"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"VSqSfi7i3lSy"},"outputs":[],"source":["name[::3]"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"6XvZbE7r3lSz"},"outputs":[],"source":["name[::-2]"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"7x58x2UA3lSz"},"outputs":[],"source":["name"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"uiQIwzOt3lSz"},"outputs":[],"source":["name[3:7]"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"rIpEHvAC3lSz"},"outputs":[],"source":["name[0:5]"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"CVg6ET2i3lSz"},"outputs":[],"source":["name"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"8u7A5Skj3lS0"},"outputs":[],"source":["name[:6:2]"]},{"cell_type":"code","execution_count":null,"metadata":{"scrolled":true,"id":"PDOub3E53lS0"},"outputs":[],"source":["name[5:8]"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"OUW2jEJB3lS0"},"outputs":[],"source":["name[0:7:3]"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"jtYf_Jpv3lS0"},"outputs":[],"source":["name"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"53ScY7m63lS0"},"outputs":[],"source":["name[-5:-1]"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"11aTANai3lS1"},"outputs":[],"source":["name[1:-4]"]},{"cell_type":"markdown","metadata":{"id":"5g5Yla4Z3lS1"},"source":["#### Membership"]},{"cell_type":"markdown","metadata":{"id":"XNHdk3Cw3lS1"},"source":["You can also check whether a string contains some text using the `in` operator."]},{"cell_type":"code","execution_count":null,"metadata":{"id":"eWzOeg6Q3lS1"},"outputs":[],"source":["today = 'Saturday'"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"-s9yDyJ73lS1"},"outputs":[],"source":["today"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"0CXoboeA3lS2"},"outputs":[],"source":["'Day' in today"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"Au-qlBl03lS2"},"outputs":[],"source":["'day' == 'Day'"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"rtOk7oeE3lS2"},"outputs":[],"source":["'Sat' in today"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"m0eriDD33lS2"},"outputs":[],"source":["'Wed' not in today"]},{"cell_type":"markdown","metadata":{"id":"YXw4-CcS3lS2"},"source":["#### Concating Strings"]},{"cell_type":"markdown","metadata":{"id":"IKM8bMkB3lS2"},"source":["Two or more strings can be joined or *concatenated* using the `+` operator.\n","\n","**`Note`**: Be careful while concatenating strings, sometimes you may need to add a space character `\" \"` between words."]},{"cell_type":"code","execution_count":null,"metadata":{"id":"sw9g9vA33lS3"},"outputs":[],"source":["full_name = \"Philip Eze\"\n","full_name"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"G8Ewsbu43lS3"},"outputs":[],"source":["name = 'Philip'\n","\n","surname = 'Eze'"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"GrlzRucE3lS3"},"outputs":[],"source":["name"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"q0wrtL853lS4"},"outputs":[],"source":["surname"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"NIPHR40v3lS4"},"outputs":[],"source":["full_name_1 = name + surname\n","full_name_1"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"zRq49jv-3lS4"},"outputs":[],"source":["full_name_1 = name + ' ' +surname\n","full_name_1"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"0DQXmnUB3lS4"},"outputs":[],"source":["name + 2"]},{"cell_type":"markdown","metadata":{"id":"Flnm7hxW3lS5"},"source":["#### Replicating Strings"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"cOzZ4acR3lS5"},"outputs":[],"source":["name = 'Philip'\n","name"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"osy0gM9L3lS5"},"outputs":[],"source":["name * 2"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"yeSRkQBU3lS6"},"outputs":[],"source":["name * 4"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"1EQnnKK93lS6"},"outputs":[],"source":["name * 4"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"-SWtoLEG3lS6"},"outputs":[],"source":["name * name"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"tgvCrZyP3lS6"},"outputs":[],"source":["name_5 = name * 5\n","name_5"]},{"cell_type":"markdown","metadata":{"id":"lyjvVZ-a3lS7"},"source":["#### String Methods"]},{"cell_type":"markdown","metadata":{"id":"LLnFmZgt3lS7"},"source":["Strings in Python have many built-in *methods* that are used to manipulate them. Let's try out some common string methods.\n","\n","> **Methods**: Methods are functions associated with data types and are accessed using the `.` notation e.g. `variable_name.method()` or `\"a string\".method()`. Methods are a powerful technique for associating common operations with values of specific data types.\n","\n","The `.lower()`, `.upper()` and `.capitalize()` methods are used to change the case of the characters."]},{"cell_type":"code","execution_count":null,"metadata":{"id":"Rws5BRlo3lS7"},"outputs":[],"source":["today = \"TUESDAY\""]},{"cell_type":"code","execution_count":null,"metadata":{"id":"C1q2KDBu3lS8"},"outputs":[],"source":["today"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"Eis1ZcBc3lS8"},"outputs":[],"source":["today = today.lower()"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"v_2RgpNb3lS8"},"outputs":[],"source":["today"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"W55L05ew3lS8"},"outputs":[],"source":["tomorrow = \"Sunday\""]},{"cell_type":"code","execution_count":null,"metadata":{"id":"Aj0aOW9f3lS9"},"outputs":[],"source":["tomorrow"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"iXd991zS3lS9"},"outputs":[],"source":["tomorrow.lower()"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"dGJKPV9T3lS9"},"outputs":[],"source":["tomorrow"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"BnkNn75Z3lS9"},"outputs":[],"source":["today = 'saturday'\n","today"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"Rb3qS9qi3lS9"},"outputs":[],"source":["today.upper()"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"jn83MHjc3lS9"},"outputs":[],"source":["today"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"CA3vmMmB3lS-"},"outputs":[],"source":["today = today.upper()\n","today"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"AjNBcpds3lS-"},"outputs":[],"source":["today"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"t3pq30NH3lS-"},"outputs":[],"source":["name = 'solomon'"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"GvqY7r6J3lS_"},"outputs":[],"source":["name.capitalize()"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"rVq0HrJl3lS_"},"outputs":[],"source":["name"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"tOdTmCGL3lS_"},"outputs":[],"source":["name = name.capitalize()\n","name"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"2mIcomWz3lTA"},"outputs":[],"source":["\"monday\".capitalize() # changes first character to uppercase"]},{"cell_type":"markdown","metadata":{"id":"dW2D5KCr3lTA"},"source":["The `.replace` method replaces a part of the string with another string. It takes the portion to be replaced and the replacement text as *inputs* or *arguments*."]},{"cell_type":"code","execution_count":null,"metadata":{"id":"zGmAFCvQ3lTA"},"outputs":[],"source":["today = \"SATURDAY\""]},{"cell_type":"code","execution_count":null,"metadata":{"id":"Qh87l81R3lTB"},"outputs":[],"source":["today.replace('SATUR', 'WEDNES')"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"BFx8t5iN3lTB"},"outputs":[],"source":["today"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"sK853J7y3lTB"},"outputs":[],"source":["another_day = today.replace(\"SATUR\", \"WEDNES\")"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"SXtKJzwq3lTB"},"outputs":[],"source":["another_day"]},{"cell_type":"markdown","metadata":{"id":"AF25_DZK3lTB"},"source":["Note that `replace` returns a new string, and the original string is not modified."]},{"cell_type":"code","execution_count":null,"metadata":{"id":"xEiVRMSA3lTC"},"outputs":[],"source":["today"]},{"cell_type":"markdown","metadata":{"id":"s6loxR2F3lTC"},"source":["The `.split` method splits a string into a list of strings at every occurrence of provided character(s)."]},{"cell_type":"code","execution_count":null,"metadata":{"id":"DmBc-epr3lTC"},"outputs":[],"source":["\"Sun,Mon,Tue,Wed,Thu,Fri,Sat\""]},{"cell_type":"code","execution_count":null,"metadata":{"id":"njrYeJ5x3lTC"},"outputs":[],"source":["week = \"Sun,Mon,Tue,Wed,Thu,Fri,Sat\".split(',')\n","week"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"Eg0g4Lb-3lTC"},"outputs":[],"source":["\"1|2|3|4|5|6|7\".split(\"|\")"]},{"cell_type":"markdown","metadata":{"id":"wVMgC07E3lTC"},"source":["The `.strip` method removes whitespace characters from the beginning and end of a string."]},{"cell_type":"code","execution_count":null,"metadata":{"id":"J6ryNU9b3lTD"},"outputs":[],"source":["a_long_line = \" This is a long line with some space before, after, and some space in the middle.. \""]},{"cell_type":"code","execution_count":null,"metadata":{"id":"ogGf06pD3lTD"},"outputs":[],"source":["print(a_long_line)"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"V16sxL7R3lTD"},"outputs":[],"source":["a_long_line"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"QQ8Chu2C3lTD"},"outputs":[],"source":["a_long_line_stripped = a_long_line.strip()"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"K0n8EQoZ3lTD"},"outputs":[],"source":["a_long_line_stripped"]},{"cell_type":"markdown","metadata":{"id":"UsLLv5a73lTD"},"source":["The `.format` method combines values of other data types, e.g., integers, floats, booleans, lists, etc. with strings. You can use `format` to construct output messages for display."]},{"cell_type":"code","execution_count":null,"metadata":{"id":"sAthnMjj3lTE"},"outputs":[],"source":["\"I am Solomon and I am\" 27 \"year old\""]},{"cell_type":"code","execution_count":null,"metadata":{"id":"Vozmx9UY3lTE"},"outputs":[],"source":["\"I am Solomon and I am {} year old\".format(27)"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"rFuo8lHn3lTE"},"outputs":[],"source":["print('I am Solomon and I am {} year old'.format(27))"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"aas6aVAE3lTF"},"outputs":[],"source":["# Input variables\n","cost_of_ice_bag = 1.25\n","profit_margin = .2\n","number_of_bags = 500"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"ExsJOtmi3lTF"},"outputs":[],"source":["# Template for output message\n","output_template = \"\"\"If a grocery store sells ice bags at $ {} per bag, with a profit margin of {} %,\n","then the total profit it makes by selling {} ice bags is $ {}.\"\"\"\n","\n","print(output_template)"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"pSA3xuAa3lTF"},"outputs":[],"source":["print(output_template)"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"XMZ01JMV3lTF"},"outputs":[],"source":["# Inserting values into the string\n","\n","total_profit = cost_of_ice_bag * profit_margin * number_of_bags\n","total_profit"]},{"cell_type":"code","execution_count":null,"metadata":{"scrolled":true,"id":"cZWJl97x3lTG"},"outputs":[],"source":["output_message = output_template.format(cost_of_ice_bag,\n"," profit_margin*100,\n"," number_of_bags, total_profit)\n","\n","print(output_message)"]},{"cell_type":"markdown","metadata":{"id":"qjZnNDQF3lTG"},"source":["Notice how the placeholders `{}` in the `output_template` string are replaced with the arguments provided to the `.format` method.\n","\n","It is also possible to use the string concatenation operator `+` to combine strings with other values. However, those values must first be converted to strings using the `str` function."]},{"cell_type":"code","execution_count":null,"metadata":{"id":"ZlY472DJ3lTG"},"outputs":[],"source":["\"If a grocery store sells ice bags at $ \" + cost_of_ice_bag + \", with a profit margin of \" + profit_margin"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"Su-MUi1Z3lTG"},"outputs":[],"source":["\"If a grocery store sells ice bags at $ \" + str(cost_of_ice_bag) + \", with a profit margin of \" + str(profit_margin*100)"]},{"cell_type":"markdown","metadata":{"id":"t2xhGHVy3lTH"},"source":["#### Converting other data types to strings"]},{"cell_type":"markdown","metadata":{"id":"FJpkLayi3lTH"},"source":["You can us the `str` function to convert a value of any data type into a string."]},{"cell_type":"code","execution_count":null,"metadata":{"id":"JSiPJqMO3lTH"},"outputs":[],"source":["goh = 123\n","goh"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"mfcMPps03lTH"},"outputs":[],"source":["goh = str(123) ## converting an integer to a string\n","goh"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"3MuRb3PQ3lTH"},"outputs":[],"source":["str(23.432) ## converting a floating value to a string"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"FyCi5Ozu3lTI"},"outputs":[],"source":["str(True)"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"dh477Htj3lTI"},"outputs":[],"source":["the_3_musketeers = [\"Athos\", \"Porthos\", \"Aramis\"]\n","str(the_3_musketeers)"]},{"cell_type":"markdown","metadata":{"id":"d7fTV61R3lTI"},"source":["**Note**: All string methods return new values and DO NOT change the existing string. You can find a full list of string methods here: https://www.w3schools.com/python/python_ref_string.asp."]},{"cell_type":"markdown","metadata":{"id":"UrQAo4LK3lTI"},"source":["#### Comparism Operations on Strings"]},{"cell_type":"markdown","metadata":{"id":"BO412NCP3lTI"},"source":["Strings also support the comparison operators `==` and `!=` for checking whether two strings are equal."]},{"cell_type":"code","execution_count":null,"metadata":{"id":"mGzYaPl_3lTJ"},"outputs":[],"source":["first_name = \"Mary\""]},{"cell_type":"code","execution_count":null,"metadata":{"id":"f_UD0Bfr3lTJ"},"outputs":[],"source":["first_name == \"Mariam\""]},{"cell_type":"code","execution_count":null,"metadata":{"id":"duF6i-s73lTJ"},"outputs":[],"source":["first_name == \"Mary\""]},{"cell_type":"code","execution_count":null,"metadata":{"id":"afrWoe123lTJ"},"outputs":[],"source":["first_name != \"Jane\""]},{"cell_type":"markdown","metadata":{"id":"cSnAJfZ63lTJ"},"source":["We've looked at the primitive data types in Python. We're now ready to explore non-primitive data structures, also known as containers."]},{"cell_type":"markdown","metadata":{"id":"hZNglk073lTJ"},"source":["### List\n","\n","`A list in Python is an ordered collection of values`.\n","\n","Lists can hold values of different data types and support operations to `add`, `remove`, and `change values`. Lists have the type `list`.\n","\n","To create a list, enclose a sequence of values within square brackets `[` and `]`, separated by commas."]},{"cell_type":"code","execution_count":null,"metadata":{"id":"7D4cZhLE3lTK"},"outputs":[],"source":["sol = ['Promise', 'Solomon']\n","solo = ['Promise', 'Solomon']"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"rfjV7KJe3lTK"},"outputs":[],"source":["sol == solo"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"mRFaiq453lTK"},"outputs":[],"source":["sol[1]"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"JI_q3rjz3lTK"},"outputs":[],"source":["solo[1]"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"6Ld98UhD3lTL"},"outputs":[],"source":[]},{"cell_type":"code","execution_count":null,"metadata":{"id":"zGBtn3j13lTL"},"outputs":[],"source":["fruits = ['apple', 'banana', 'cherry']\n","fruits"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"sNhHmfwD3lTL"},"outputs":[],"source":["fruits_i = ['banana','apple', 'cherry']\n","fruits_i"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"G7EXvrmX3lTL"},"outputs":[],"source":["fruits == fruits_i"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"ooDOQoNQ3lTL"},"outputs":[],"source":["fruits_2 = ['apple', 'banana', 'cherry', 1, False, 45.99]"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"-Cz4HHx_3lTL"},"outputs":[],"source":["fruits_2"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"PcdX9eTy3lTM"},"outputs":[],"source":["fruits"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"HleMLqBd3lTM"},"outputs":[],"source":["type(fruits)"]},{"cell_type":"markdown","metadata":{"id":"BFFTYQH93lTM"},"source":["Let's try creating a list containing values of different data types, including another list."]},{"cell_type":"code","execution_count":null,"metadata":{"id":"ut2KltBp3lTM"},"outputs":[],"source":["a_list = [23, 'hello', None, 3.14, fruits, 3 <= 5]"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"HMxYgRZg3lTM"},"outputs":[],"source":["a_list"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"2eTqn_QQ3lTM"},"outputs":[],"source":["empty_list = []"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"L1714MOk3lTN"},"outputs":[],"source":["empty_list"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"nlassHnO3lTN"},"outputs":[],"source":["empty_list"]},{"cell_type":"markdown","metadata":{"id":"_Blr3NcI3lTN"},"source":["To determine the number of values in a list, use the `len` function. You can use `len` to determine the number of values in several other data types."]},{"cell_type":"code","execution_count":null,"metadata":{"id":"ElyM3SM63lTN"},"outputs":[],"source":["fruits"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"QhOoANAX3lTN"},"outputs":[],"source":["len(fruits)"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"3A7TcRcA3lTN"},"outputs":[],"source":["print(\"Number of fruits:\", len(fruits))"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"OrGnj6ZL3lTO"},"outputs":[],"source":["a_list"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"M5xziCSG3lTO"},"outputs":[],"source":["len(a_list)"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"M0nUeRol3lTO"},"outputs":[],"source":["len(empty_list)"]},{"cell_type":"markdown","metadata":{"id":"Wbpe7aay3lTO"},"source":["#### Indexing and Selection"]},{"cell_type":"markdown","metadata":{"id":"o-tha6Oz3lTO"},"source":["You can access an element from the list using its *index*, e.g., `fruits[2]` returns the element at index 2 within the list `fruits`. The starting index of a list is 0."]},{"cell_type":"code","execution_count":null,"metadata":{"id":"cNKHCbtD3lTO"},"outputs":[],"source":["fruits"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"ebG8jVMW3lTP"},"outputs":[],"source":["fruits[0]"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"oWDsnFCW3lTP"},"outputs":[],"source":["fruits[1]"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"uXCH_r9v3lTP"},"outputs":[],"source":["fruits[2]"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"Yv0CTg2W3lTP"},"outputs":[],"source":["fruits[-1]"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"-veTIG-r3lTP"},"outputs":[],"source":["fruits[-2]"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"213EWP2i3lTQ"},"outputs":[],"source":["fruits[-3]"]},{"cell_type":"markdown","metadata":{"id":"X582U7FS3lTQ"},"source":["If you try to access an index equal to or higher than the length of the list, Python returns an `IndexError`."]},{"cell_type":"code","execution_count":null,"metadata":{"id":"i7MuwFWV3lTQ"},"outputs":[],"source":["fruits"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"1m79kCGy3lTQ"},"outputs":[],"source":["fruits[3]"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"ULbZKuVu3lTQ"},"outputs":[],"source":["fruits[4]"]},{"cell_type":"markdown","metadata":{"id":"6bq4SoOX3lTR"},"source":["You can use negative indices to access elements from the end of a list, e.g., `fruits[-1]` returns the last element, `fruits[-2]` returns the second last element, and so on."]},{"cell_type":"code","execution_count":null,"metadata":{"id":"M6Zslync3lTR"},"outputs":[],"source":["fruits[-1]"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"NRbSw_973lTR"},"outputs":[],"source":["fruits[-2]"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"huvhuxas3lTR"},"outputs":[],"source":["fruits[-3]"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"diYcKOiM3lTR"},"outputs":[],"source":["fruits[-4]"]},{"cell_type":"markdown","metadata":{"id":"5w9Xy0973lTR"},"source":["You can also access a range of values from the list. The result is itself a list. Let us look at some examples."]},{"cell_type":"code","execution_count":null,"metadata":{"id":"rKvpoy6Y3lTS"},"outputs":[],"source":["a_list = [23, 'hello', None, 3.14, fruits, 3 <= 5]"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"GexCyEeo3lTS"},"outputs":[],"source":["a_list"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"x2QaChUZ3lTS"},"outputs":[],"source":["len(a_list)"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"c2an5J6L3lTS"},"outputs":[],"source":["a_list[0]"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"vha3jVHJ3lTS"},"outputs":[],"source":["a_list[1]"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"p3KTrWbE3lTS"},"outputs":[],"source":["a_list[2]"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"w6bwNhxC3lTT"},"outputs":[],"source":["a_list[3]"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"Kmze_cqZ3lTT"},"outputs":[],"source":["a_list[4]"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"714hmEaR3lTT"},"outputs":[],"source":["a_list[4][0]"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"5r4wtmeQ3lTT"},"outputs":[],"source":["a_list[4][1]"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"w6z9J5-W3lTT"},"outputs":[],"source":["a_list[4][2]"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"T89idHE03lTT"},"outputs":[],"source":["a_list[5]"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"kHbGrj833lTT"},"outputs":[],"source":["a_list"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"6WyQNT2E3lTU"},"outputs":[],"source":["a_list[:4]"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"x3NqX8h33lTU"},"outputs":[],"source":["a_list[2:]"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"N-v1fbtW3lTU"},"outputs":[],"source":["a_list[::2]"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"_OY1v-6g3lTU"},"outputs":[],"source":["a_list[0:3]"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"tX8K-KWQ3lTV"},"outputs":[],"source":["a_list[10]"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"C7JeHMLA3lTV"},"outputs":[],"source":["a_list[2:6]"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"7Lo9PV9M3lTV"},"outputs":[],"source":["a_list[5:1]"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"sExuq_ff3lTV"},"outputs":[],"source":["a_list"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"TqUhmGUj3lTV"},"outputs":[],"source":["a_list[2:] ### stopping at the last index (value)"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"7RxCZYVb3lTV"},"outputs":[],"source":["a_list[:4] ### Starting at the very begining"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"8Me4vVMx3lTW"},"outputs":[],"source":["a_list"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"G6b1oEuD3lTW"},"outputs":[],"source":[" a_list[-3:]"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"mjBVliMq3lTW"},"outputs":[],"source":["a_list[-2:-5]"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"69-yoZ7V3lTW"},"outputs":[],"source":[" a_list[5:2]"]},{"cell_type":"markdown","metadata":{"id":"HM9cMnFc3lTW"},"source":["Note that the range `2:5` includes the element at the start index `2` but does not include the element at the end index `5`. So, the result has 3 values (index `2`, `3`, and `4`).\n","\n","Here are some experiments you should try out (use the empty cells below):\n","\n","* Try setting one or both indices of the range are larger than the size of the list, e.g., `a_list[2:10]`\n","* Try setting the start index of the range to be larger than the end index, e.g., `a_list[12:10]`\n","* Try leaving out the start or end index of a range, e.g., `a_list[2:]` or `a_list[:5]`\n","* Try using negative indices for the range, e.g., `a_list[-2:-5]` or `a_list[-5:-2]` (can you explain the results?)\n","\n","> The flexible and interactive nature of Jupyter notebooks makes them an excellent tool for learning and experimentation. If you are new to Python, you can resolve most questions as soon as they arise simply by typing the code into a cell and executing it. Let your curiosity run wild, discover what Python is capable of and what it isn't!"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"TNHPf8OE3lTW"},"outputs":[],"source":[]},{"cell_type":"code","execution_count":null,"metadata":{"id":"8LzOMWsg3lTX"},"outputs":[],"source":[]},{"cell_type":"code","execution_count":null,"metadata":{"id":"ScfkVhec3lTX"},"outputs":[],"source":[]},{"cell_type":"markdown","metadata":{"id":"r-dyspWR3lTX"},"source":["#### Updating Values in a list"]},{"cell_type":"markdown","metadata":{"id":"ZgH4_Qiv3lTX"},"source":["You can also change the value at a specific index within a list using the assignment operation."]},{"cell_type":"code","execution_count":null,"metadata":{"id":"ws-_3-fl3lTX"},"outputs":[],"source":["fruits"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"rYV8FSuf3lTY"},"outputs":[],"source":["fruits[1]"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"r1Qv1TwW3lTY"},"outputs":[],"source":["fruits[1] = 'blueberry'"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"h9n_CpDP3lTY"},"outputs":[],"source":["fruits"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"oEUB02Gg3lTY"},"outputs":[],"source":["fruits[2] = 'mango'"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"QxIjruyR3lTY"},"outputs":[],"source":["fruits"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"FJXNn_zZ3lTZ"},"outputs":[],"source":["names = ['Seyi', 'Isaiah', 'Chris', 'Samuel', 'Sodiq']"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"1lZXD7a13lTZ"},"outputs":[],"source":["names[3] = 1000\n","names"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"uMcLPURn3lTZ"},"outputs":[],"source":["names[2:4] = ['Promise']"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"fRqtRK1B3lTa"},"outputs":[],"source":["names"]},{"cell_type":"markdown","metadata":{"id":"B-KXvWa73lTa"},"source":["#### List methods"]},{"cell_type":"markdown","metadata":{"id":"lMQLT-K33lTa"},"source":["A new value can be added to the end of a list using the `append` method."]},{"cell_type":"code","execution_count":null,"metadata":{"id":"l9GcU14Y3lTa"},"outputs":[],"source":["fruits"]},{"cell_type":"markdown","metadata":{"id":"KLKM7HVK3lTa"},"source":["#### `.append()`\n","\n","`Append one object at a time to the end of the list.`\n","\n","It takes only one value at a time."]},{"cell_type":"code","execution_count":null,"metadata":{"id":"U6lsb2n_3lTa"},"outputs":[],"source":["fruits.append('dates')"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"YtB5ia2F3lTb"},"outputs":[],"source":["fruits"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"lna3PXqr3lTb"},"outputs":[],"source":["fruits.append('guava', 'pawpaw')"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"CZSzzOvS3lTb"},"outputs":[],"source":["fruits + ['guava', 'apple'] ## concating two list together !!!"]},{"cell_type":"markdown","metadata":{"id":"IeWzf6aR3lTc"},"source":["#### `insert`"]},{"cell_type":"markdown","metadata":{"id":"8_e1uO533lTc"},"source":["A new value can also be inserted at a specific index using the `insert` method."]},{"cell_type":"code","execution_count":null,"metadata":{"id":"y6bP3NMP3lTc"},"outputs":[],"source":["fruits"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"kQSz6YZg3lTd"},"outputs":[],"source":["fruits.insert(1, 'banana')\n","fruits"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"4rYtN-R63lTd"},"outputs":[],"source":["fruits.insert(4, 'pawpaw')\n","fruits"]},{"cell_type":"markdown","metadata":{"id":"UqS2QCYn3lTe"},"source":["`Extend` method"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"h3aNVVBI3lTe"},"outputs":[],"source":["fruits.extend(['guava', 'apple'])"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"wh4c8Vuc3lTe"},"outputs":[],"source":["fruits"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"JLYLphK33lTf"},"outputs":[],"source":["fruits.extend(['kiwi', 'peach' , 'pineapple', 'watermelon', 'tingerine'])\n","fruits"]},{"cell_type":"markdown","metadata":{"id":"pLGyUU063lTf"},"source":["#### `remove`"]},{"cell_type":"markdown","metadata":{"id":"mMQ3xlrD3lTf"},"source":["You can remove a value from a list using the `remove` method."]},{"cell_type":"code","execution_count":null,"metadata":{"id":"Ke9hcqU63lTg"},"outputs":[],"source":["fruits"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"RLxXaAnL3lTg"},"outputs":[],"source":["fruits.remove('peach')\n","fruits"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"ICqCWl5e3lTg"},"outputs":[],"source":["fruits.remove(['guava', 'apple'])"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"f-wJdN7V3lTg"},"outputs":[],"source":["fruits"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"9EqybhH43lTg"},"outputs":[],"source":["fruits.remove('apple', 'pawpaw')"]},{"cell_type":"markdown","metadata":{"id":"BZUcqH5e3lTg"},"source":["What happens if a list has multiple instances of the value passed to `.remove`? Try it out."]},{"cell_type":"code","execution_count":null,"metadata":{"id":"ooU7qA_-3lTh"},"outputs":[],"source":["fruits"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"2Aklq_yZ3lTh"},"outputs":[],"source":["fruits.remove('watermelon')"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"-YTt0oSZ3lTh"},"outputs":[],"source":["fruits.remove('tingerine')"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"EimfSURB3lTh"},"outputs":[],"source":["fruits"]},{"cell_type":"markdown","metadata":{"id":"9dak-0zR3lTh"},"source":["#### `Pop`"]},{"cell_type":"markdown","metadata":{"id":"mihr0zQl3lTh"},"source":["To remove an element from a specific index, use the `pop` method. The method also returns the removed element."]},{"cell_type":"code","execution_count":null,"metadata":{"id":"f5Wz_f313lTh"},"outputs":[],"source":["fruits"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"M1MnGmG83lTi"},"outputs":[],"source":["fruits.pop(-4)"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"NKbMUc4l3lTi"},"outputs":[],"source":["fruits"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"mPzS-0aV3lTi"},"outputs":[],"source":["mango = fruits.pop(3)"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"mrhr69J83lTi"},"outputs":[],"source":["mango"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"wGvPsD4a3lTi"},"outputs":[],"source":["fruits"]},{"cell_type":"markdown","metadata":{"id":"7aKNSSJL3lTi"},"source":["If no index is provided, the `pop` method removes the last element of the list."]},{"cell_type":"code","execution_count":null,"metadata":{"id":"R82UZaqn3lTj"},"outputs":[],"source":["fruits.pop()"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"Xdyj5HCd3lTj"},"outputs":[],"source":["fruits"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"nj7gEZcV3lTj"},"outputs":[],"source":["fruits.insert(-1, 'pineapple')"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"KouXLu6J3lTj"},"outputs":[],"source":["fruits"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"2ebq9T2C3lTj"},"outputs":[],"source":["popped = fruits.pop()"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"5SNu_gcK3lTj"},"outputs":[],"source":["fruits"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"CMUfnmky3lTj"},"outputs":[],"source":["popped"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"USgnLm4o3lTk"},"outputs":[],"source":["fruits_i = fruits.pop()"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"8SdCOQr23lTk"},"outputs":[],"source":["fruits_i"]},{"cell_type":"markdown","metadata":{"id":"tP7pi2BC3lTk"},"source":["#### Membership Test"]},{"cell_type":"markdown","metadata":{"id":"LeDcm5M33lTk"},"source":["You can test whether a list contains a value using the `in` operator."]},{"cell_type":"code","execution_count":null,"metadata":{"id":"N_LkDHIZ3lTk"},"outputs":[],"source":["fruits.pop(-2)"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"FzGtyOVJ3lTk"},"outputs":[],"source":["fruits"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"W9ZaXhgE3lTl"},"outputs":[],"source":["'pineapple' not in fruits"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"XBrfPGLQ3lTl"},"outputs":[],"source":["'cherry' in fruits"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"ozATton63lTl"},"outputs":[],"source":["'apple' in fruits"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"hRxxuJFk3lTl"},"outputs":[],"source":["'pawpaw' in fruits"]},{"cell_type":"markdown","metadata":{"id":"J_yt17LO3lTl"},"source":["#### Concate List"]},{"cell_type":"markdown","metadata":{"id":"qEa5dsdk3lTl"},"source":["To combine two or more lists, use the `+` operator. This operation is also called *concatenation*."]},{"cell_type":"code","execution_count":null,"metadata":{"id":"tgzz2L0B3lTm"},"outputs":[],"source":["fruits"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"O4k92OIG3lTm"},"outputs":[],"source":["['orange', 'tomato', 'ginger']"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"MXtk1h6j3lTm"},"outputs":[],"source":["['dates', 'guava']"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"UxQ99DeN3lTm"},"outputs":[],"source":["more_fruits = fruits + ['orange', 'tomato', 'ginger'] + ['dates', 'guava']"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"J3KFqpcN3lTm"},"outputs":[],"source":["more_fruits"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"agUktlzD3lTm"},"outputs":[],"source":["more_fruits"]},{"cell_type":"markdown","metadata":{"id":"k72TAiMj3lTm"},"source":["#### Copy (Deep Copy) and View (Shallow Copy)"]},{"cell_type":"markdown","metadata":{"id":"x5iiB4Vl3lTn"},"source":["To create a copy of a list, use the `.copy()` method. Modifying the copied list does not affect the original."]},{"cell_type":"code","execution_count":null,"metadata":{"id":"v3oUOAWh3lTn"},"outputs":[],"source":["more_fruits"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"6Yqq7hoq3lTn"},"outputs":[],"source":["more_fruits_copied = more_fruits.copy() ### Copyomg just the values"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"8niyEb3q3lTn"},"outputs":[],"source":["more_fruits_copied"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"qgzOvuFp3lTn"},"outputs":[],"source":["more_fruits"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"ouzkSeMf3lTn"},"outputs":[],"source":["more_fruits == more_fruits_copied"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"AU4KmdMb3lTn"},"outputs":[],"source":["# Modify the copy\n","more_fruits_copied.remove('dates')"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"tpPHulYH3lTo"},"outputs":[],"source":["more_fruits_copied.pop()"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"mJNpRWmj3lTo"},"outputs":[],"source":["more_fruits_copied"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"Bz0QD0FQ3lTo"},"outputs":[],"source":["# Original list remains unchanged\n","more_fruits"]},{"cell_type":"markdown","metadata":{"id":"e20Sa4_13lTo"},"source":["Note that you cannot create a copy of a list by simply creating a new variable using the assignment operator `=`. The new variable will point to the same list, and any modifications performed using either variable will affect the other."]},{"cell_type":"code","execution_count":null,"metadata":{"id":"mXdghV3d3lTo"},"outputs":[],"source":["more_fruits"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"s73p9m2U3lTo"},"outputs":[],"source":["more_fruits_view = more_fruits ### Copying both values and memory space.\n","more_fruits_view"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"JjTW7ZuY3lTp"},"outputs":[],"source":["more_fruits_view == more_fruits"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"glbopgR23lTp"},"outputs":[],"source":["more_fruits_view.remove('dates')\n","more_fruits_view.pop()"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"K2loYBhB3lTp"},"outputs":[],"source":["more_fruits_view"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"BaHHkfbF3lTp"},"outputs":[],"source":["more_fruits"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"_gwd0rLs3lTp"},"outputs":[],"source":["more_fruits.remove('apple')"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"-rseg3xp3lTq"},"outputs":[],"source":["more_fruits.append('dates')"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"td8HDN5D3lTq"},"outputs":[],"source":["more_fruits"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"rPaP7oCK3lTq"},"outputs":[],"source":["more_fruits_view[::-1]"]},{"cell_type":"markdown","metadata":{"id":"xWLi_zj73lTq"},"source":["Just like strings, there are several in-built methods to manipulate a list. However, unlike strings, most list methods modify the original list rather than returning a new one. Check out some common list operations here: https://www.w3schools.com/python/python_ref_list.asp .\n","\n","\n","Following are some exercises you can try out with list methods (use the blank code cells below):\n","\n","* Reverse the order of elements in a list\n","* Add the elements of one list at the end of another list\n","* Sort a list of strings in alphabetical order\n","* Sort a list of numbers in decreasing order"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"3unlgZAc3lTr"},"outputs":[],"source":[]},{"cell_type":"markdown","metadata":{"id":"HwZoF7rW3lTr"},"source":["### Tuple\n","\n","A tuple is an ordered collection of values, similar to a list. However, it is not possible to add, remove, or modify values in a tuple. A tuple is created by enclosing values within parentheses `(` and `)`, separated by commas.\n","\n","> Any data structure that cannot be modified after creation is called *immutable*. You can think of tuples as immutable lists.\n","\n","Let's try some experiments with tuples."]},{"cell_type":"code","execution_count":null,"metadata":{"id":"faogli753lTs"},"outputs":[],"source":["fruits = ('apple', 'cherry', 'dates', 'cherry')\n","fruits"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"nRmp3vB63lTs"},"outputs":[],"source":["# check no. of elements\n","len(fruits)"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"NqLDEvIB3lTs"},"outputs":[],"source":["# get an element (positive index)\n","fruits[0]"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"L5vOXqlo3lTs"},"outputs":[],"source":["# get an element (negative index)\n","fruits[-2]"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"t2gyFA8L3lTt"},"outputs":[],"source":["# check if it contains an element\n","'dates' in fruits"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"RWR7taiR3lTt"},"outputs":[],"source":["# try to change an element\n","fruits[0] = 'avocado'"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"7qTzSfJ43lTt"},"outputs":[],"source":["# try to append an element\n","fruits.append('blueberry')"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"EV7WIx513lTt"},"outputs":[],"source":["# try to remove an element\n","fruits.remove('apple')"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"1MtGieYP3lTu"},"outputs":[],"source":["fruits.pop()"]},{"cell_type":"markdown","metadata":{"id":"1A4cgcsL3lTv"},"source":["You can also skip the parantheses `(` and `)` while creating a tuple. Python automatically converts comma-separated values into a tuple."]},{"cell_type":"code","execution_count":null,"metadata":{"id":"CFVxacZV3lTx"},"outputs":[],"source":["the_3_musketeers = 'Athos', 'Porthos', 'Aramis'"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"L-Jg1kAy3lTy"},"outputs":[],"source":["the_3_musketeers"]},{"cell_type":"markdown","metadata":{"id":"4dtUGdPf3lTz"},"source":["You can also create a tuple with just one element by typing a comma after it. Just wrapping it with parentheses `(` and `)` won't make it a tuple."]},{"cell_type":"code","execution_count":null,"metadata":{"id":"7uG9UWp83lT0"},"outputs":[],"source":["single_element_tuple = 4,"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"akN12n7k3lT0"},"outputs":[],"source":["single_element_tuple"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"eNLTIrNB3lT1"},"outputs":[],"source":["another_single_element_tuple = (4,)"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"H1rROdeM3lT1"},"outputs":[],"source":["another_single_element_tuple"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"6CEh66J53lT2"},"outputs":[],"source":["not_a_tuple = (4)"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"H1waxiZe3lT3"},"outputs":[],"source":["not_a_tuple"]},{"cell_type":"markdown","metadata":{"id":"dBXEa92n3lT3"},"source":["Tuples are often used to create multiple variables with a single statement."]},{"cell_type":"code","execution_count":null,"metadata":{"id":"6sA0cyMZ3lT3"},"outputs":[],"source":["point = (3, 4, 5)"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"k0jSrmWO3lT4"},"outputs":[],"source":["point_x, point_y, point_z = (3, 4, 5)"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"wQia8SmB3lT5"},"outputs":[],"source":["point_x"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"CgudTLo93lT6"},"outputs":[],"source":["point_y"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"fAfiAy0S3lT6"},"outputs":[],"source":["point_z"]},{"cell_type":"markdown","metadata":{"id":"fpmMxqq53lT7"},"source":["You can convert a list into a tuple using the `tuple` function, and vice versa using the `list` function"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"vRrY6h7t3lT7"},"outputs":[],"source":["['one', 'two', 'three']"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"hozplOpi3lT8"},"outputs":[],"source":["tuple(['one', 'two', 'three'])"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"Ob_cM5473lT8"},"outputs":[],"source":["list(('Athos', 'Porthos', 'Aramis'))"]},{"cell_type":"markdown","metadata":{"id":"nYbdmfyj3lT8"},"source":["Tuples have just two built-in methods: `count` and `index`. Can you figure out what they do? While you look could look for documentation and examples online, there's an easier way to check a method's documentation, using the `help` function."]},{"cell_type":"code","execution_count":null,"metadata":{"id":"wOrJFijE3lT9"},"outputs":[],"source":["a_tuple = 23, \"hello\", False, None, 23, 37, \"hello\", 'hello'"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"iqBQZYbF3lT9"},"outputs":[],"source":["a_tuple"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"p0eU8tse3lT9"},"outputs":[],"source":["help(a_tuple.count)"]},{"cell_type":"markdown","metadata":{"id":"L_sg4cr_3lT-"},"source":["Within a Jupyter notebook, you can also start a code cell with `?` and type the name of a function or method. When you execute this cell, you will see the function/method's documentation in a pop-up window."]},{"cell_type":"code","execution_count":null,"metadata":{"id":"jESua1Fi3lT-"},"outputs":[],"source":["?a_tuple.index"]},{"cell_type":"markdown","metadata":{"id":"6ERYqPcV3lT-"},"source":["Try using `count` and `index` with `a_tuple` in the code cells below."]},{"cell_type":"code","execution_count":null,"metadata":{"id":"wipmQzR13lT-"},"outputs":[],"source":["a_tuple"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"E3NYAv5W3lT-"},"outputs":[],"source":["a_tuple.count(None)"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"YOwAFD2u3lT_"},"outputs":[],"source":["a_tuple"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"jK7opBa_3lT_"},"outputs":[],"source":["a_tuple.index('hello')"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"62ZBWDuo3lT_"},"outputs":[],"source":["a_tuple.index(23)"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"rhzdK_gY3lT_"},"outputs":[],"source":["a_tuple.index(23)"]},{"cell_type":"markdown","metadata":{"id":"438JobQL3lT_"},"source":["### Dictionary\n","\n","A dictionary is an unordered collection of items. Each item stored in a dictionary has a key and value. You can use a key to retrieve the corresponding value from the dictionary. Dictionaries have the type `dict`.\n","\n","Dictionaries are often used to store many pieces of information e.g. details about a person, in a single variable. Dictionaries are created by enclosing key-value pairs within braces or curly brackets `{` and `}`."]},{"cell_type":"code","execution_count":null,"metadata":{"id":"m-MYMNhT3lUA"},"outputs":[],"source":["person1 = {\n"," 'name': 'John Doe',\n"," 'sex': 'Male',\n"," 'age': 32,\n"," 'married': True,\n","}"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"W0F708Vz3lUA"},"outputs":[],"source":["person1"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"DxdGHi3j3lUA"},"outputs":[],"source":["print(person1)"]},{"cell_type":"markdown","metadata":{"id":"GiwXxG3d3lUA"},"source":["Dictionaries can also be created using the `dict` function."]},{"cell_type":"code","execution_count":null,"metadata":{"id":"riRBCsVy3lUA"},"outputs":[],"source":["person2 = dict(name='Jane Judy', sex='Female', age=28, married=False)"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"1nf0vRTT3lUB"},"outputs":[],"source":["person2"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"qsPjz4WX3lUB"},"outputs":[],"source":["type(person1)"]},{"cell_type":"markdown","metadata":{"id":"6v2wvM1p3lUB"},"source":["Keys can be used to access values using square brackets `[` and `]`."]},{"cell_type":"code","execution_count":null,"metadata":{"id":"mkxOwlXr3lUB"},"outputs":[],"source":["person1"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"sK1Uktdj3lUB"},"outputs":[],"source":["person1['name']"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"SQ-h39nF3lUB"},"outputs":[],"source":["person1['married']"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"w4P3JZNs3lUC"},"outputs":[],"source":["person1['sex']"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"HJlMAF3D3lUC"},"outputs":[],"source":["person1['age']"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"B7l6ZziO3lUC"},"outputs":[],"source":[]},{"cell_type":"code","execution_count":null,"metadata":{"id":"BToSk8-d3lUC"},"outputs":[],"source":["person2"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"OJfiO3Jq3lUC"},"outputs":[],"source":["person2['name']"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"-uCQP4v43lUC"},"outputs":[],"source":["person2['age']"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"Qy-rgRNv3lUD"},"outputs":[],"source":["person2['sex']"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"dOqN9Mi23lUD"},"outputs":[],"source":["person2['married']"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"2EEKIkEd3lUD"},"outputs":[],"source":[]},{"cell_type":"markdown","metadata":{"id":"qHwlLDRX3lUD"},"source":["If a key isn't present in the dictionary, then a `KeyError` is *thrown*."]},{"cell_type":"code","execution_count":null,"metadata":{"scrolled":true,"id":"TTRHCZKF3lUD"},"outputs":[],"source":["person1['address']"]},{"cell_type":"markdown","metadata":{"id":"RigM7Bqo3lUE"},"source":["You can also use the `get` method to access the value associated with a key."]},{"cell_type":"code","execution_count":null,"metadata":{"id":"G0l_Fg523lUE"},"outputs":[],"source":["person2.get(\"name\")"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"X6__aNKF3lUF"},"outputs":[],"source":["person2"]},{"cell_type":"markdown","metadata":{"id":"nmc-0cRv3lUG"},"source":["The `get` method also accepts a default value, returned if the key is not present in the dictionary."]},{"cell_type":"code","execution_count":null,"metadata":{"id":"AmJ2amyU3lUH"},"outputs":[],"source":["person2.get(\"address\", \"None\")"]},{"cell_type":"markdown","metadata":{"id":"uhAMSMyV3lUH"},"source":["You can check whether a key is present in a dictionary using the `in` operator."]},{"cell_type":"code","execution_count":null,"metadata":{"id":"ZNzl4H513lUH"},"outputs":[],"source":["'name' in person1"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"9b9_9xBH3lUH"},"outputs":[],"source":["'address' in person1"]},{"cell_type":"markdown","metadata":{"id":"bp7enUFg3lUI"},"source":["You can change the value associated with a key using the assignment operator."]},{"cell_type":"code","execution_count":null,"metadata":{"id":"YAuLfy803lUI"},"outputs":[],"source":["person2['married']"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"tzPWAq__3lUI"},"outputs":[],"source":["person2['married'] = True"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"JUzijM_E3lUI"},"outputs":[],"source":["person2['married']"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"qYNccR_03lUI"},"outputs":[],"source":["person2"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"YWxeuNfA3lUI"},"outputs":[],"source":[]},{"cell_type":"markdown","metadata":{"id":"2A_DHzF23lUI"},"source":["**The assignment operator can also be used to add new key-value pairs to the dictionary.**"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"8SfO4UZi3lUJ"},"outputs":[],"source":["person1"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"OtYmHfCG3lUJ"},"outputs":[],"source":["person1['address'] = '1, Penny Lane'"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"HtIN1VQc3lUJ"},"outputs":[],"source":["person1"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"v5o6c3vi3lUJ"},"outputs":[],"source":["person1['address']"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"Y8XNY2V43lUJ"},"outputs":[],"source":["person1['email'] = 'johndoe42@gmail.com'"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"M4XOmxp53lUJ"},"outputs":[],"source":["person1['email']"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"X_D3nEro3lUJ"},"outputs":[],"source":["person1"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"T6ZOzgfD3lUJ"},"outputs":[],"source":[]},{"cell_type":"markdown","metadata":{"id":"q4wrTCiq3lUK"},"source":["To remove a key and the associated value from a dictionary, use the `pop` method."]},{"cell_type":"code","execution_count":null,"metadata":{"id":"X0pFqy9N3lUK"},"outputs":[],"source":["person1.pop('address')"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"ACm7ADN43lUK"},"outputs":[],"source":["person1"]},{"cell_type":"markdown","metadata":{"id":"vZyJuzCl3lUT"},"source":["Dictionaries also provide methods to view the list of keys, values, or key-value pairs inside it."]},{"cell_type":"code","execution_count":null,"metadata":{"id":"EIA0krtz3lUU"},"outputs":[],"source":["person1.keys()"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"gJpFPmJj3lUU"},"outputs":[],"source":["person1.values()"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"lADdYaZ63lUU"},"outputs":[],"source":["person1.items()"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"U13b5WIY3lUV"},"outputs":[],"source":["person1.keys()[1]"]},{"cell_type":"markdown","metadata":{"id":"EFmvm81x3lUV"},"source":["The results of `keys`, `values`, and `items` look like lists. However, they don't support the indexing operator `[]` for retrieving elements.\n","\n","Can you figure out how to access an element at a specific index from these results? Try it below. *Hint: Use the `list` function*"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"Dp1aL2Hn3lUV"},"outputs":[],"source":[]},{"cell_type":"code","execution_count":null,"metadata":{"id":"ptAXkIhr3lUW"},"outputs":[],"source":[]},{"cell_type":"markdown","metadata":{"id":"Qmdrggr13lUW"},"source":["Dictionaries provide many other methods. You can learn more about them here: https://www.w3schools.com/python/python_ref_dictionary.asp .\n","\n","Here are some experiments you can try out with dictionaries (use the empty cells below):\n","* What happens if you use the same key multiple times while creating a dictionary?\n","* How can you create a copy of a dictionary (modifying the copy should not change the original)?\n","* Can the value associated with a key itself be a dictionary?\n","* How can you add the key-value pairs from one dictionary into another dictionary? Hint: See the `update` method.\n","* Can the dictionary's keys be something other than a string, e.g., a number, boolean, list, etc.?"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"5UDQHIXP3lUW"},"outputs":[],"source":[]},{"cell_type":"code","execution_count":null,"metadata":{"id":"1LAt1HSU3lUX"},"outputs":[],"source":[]},{"cell_type":"code","execution_count":null,"metadata":{"id":"9GBdiqYw3lUX"},"outputs":[],"source":["persons = {\n"," 'name': ['John Doe','Solomon Promise', 'John Otor', 'Sarah Adams', 'Joyce Moses'],\n"," 'sex': ['Male', 'Male', 'Male', 'Female', 'Female'],\n"," 'age': [32,34, 24, 36, 25],\n"," 'married': [True, False, False, True, True]\n","}"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"ef4nPE_K3lUY"},"outputs":[],"source":["persons"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"fp1mL6-33lUY"},"outputs":[],"source":["persons['name']"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"ZK0dIzta3lUY"},"outputs":[],"source":["persons['name'][0]"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"ECKgLDk93lUZ"},"outputs":[],"source":["persons['name'].append('Akpan Felix')"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"_pGsrLCl3lUZ"},"outputs":[],"source":["persons['age'].append(27)"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"OY2f877y3lUZ"},"outputs":[],"source":["persons['name'][2]"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"PbUs6t4f3lUZ"},"outputs":[],"source":["persons['name'][-1]"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"t1WkIKNf3lUa"},"outputs":[],"source":["persons['name'][4]"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"49PfqqMm3lUa"},"outputs":[],"source":["persons['married'].append('True')"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"Q7yBR5Dv3lUa"},"outputs":[],"source":["persons['sex'].append('Male')"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"gBZIwzqP3lUa"},"outputs":[],"source":["persons"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"nSpxLhbZ3lUa"},"outputs":[],"source":["persons['sex'].pop()"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"zo-0xn0a3lUb"},"outputs":[],"source":["print(persons)"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"v8KsMqQi3lUb"},"outputs":[],"source":["persons.keys()"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"5Iba8IMj3lUc"},"outputs":[],"source":["persons.values()"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"it4Y_O053lUc"},"outputs":[],"source":["persons.items()"]},{"cell_type":"markdown","metadata":{"id":"TL7u6jv33lUc"},"source":["## Further Reading\n","\n","We've now completed our exploration of variables and common data types in Python. Following are some resources to learn more about data types in Python:\n","\n","* Python official documentation: https://docs.python.org/3/tutorial/index.html\n","* Python Tutorial at W3Schools: https://www.w3schools.com/python/\n","* Practical Python Programming: https://dabeaz-course.github.io/practical-python/Notes/Contents.html\n","\n","You are now ready to move on to the next tutorial: [Branching using conditional statements and loops in Python](https://jovian.ml/aakashns/python-branching-and-loops)"]},{"cell_type":"markdown","metadata":{"id":"r9wNI4au3lUc"},"source":["## Conditional Statements and Loops\n","\n","Learning Objectives\n","By the end of this lesson, students should be able to:\n","1.\tUnderstand decision-making in Python.\n","2.\tUse comparison and logical operators.\n","3.\tWrite if, if-else, and if-elif-else statements.\n","4.\tUse nested conditional statements.\n","5.\tUnderstand and use for loops.\n","6.\tUnderstand and use while loops.\n","7.\tUse break and continue statements.\n","8.\tSolve practical problems using loops and conditions.\n"]},{"cell_type":"markdown","metadata":{"id":"jXD4mX4E3lUc"},"source":["# 1. Introduction to Conditional Statements\n","Conditional statements allow a program to make decisions.\n","Example:\n"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"gBdKIaX73lUd"},"outputs":[],"source":["age = 18\n","\n","if age >= 18:\n"," print(\"You are an adult\")\n"]},{"cell_type":"markdown","metadata":{"id":"k3u0yXUk3lUd"},"source":["# 2. Comparison Operators\n","these are some of the comparison Operators we will consider here:\n","\n","==\tEqual to\n","!=\tNot equal to\n"," >\tGreater than\n"," <\tLess than\n",">=\tGreater than or equal to\n","<=\tLess than or equal to"]},{"cell_type":"markdown","metadata":{"id":"FQ1sp5iq3lUd"},"source":["Example:"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"cPFjI9733lUd"},"outputs":[],"source":["x = 10\n","y = 5\n","\n","print(x > y)\n","print(x == y)"]},{"cell_type":"markdown","metadata":{"id":"WaNsf7Lm3lUd"},"source":["# Practice Exercise 1\n","Predict the output before running."]},{"cell_type":"code","execution_count":null,"metadata":{"id":"i5xA2XY03lUd"},"outputs":[],"source":["a = 15\n","b = 20\n","\n","print(a < b)\n","print(a >= b)\n","print(a != b)"]},{"cell_type":"markdown","metadata":{"id":"iI_cH7S53lUd"},"source":["# 3. The if Statement\n","Syntax:\n","if condition:\n"," statement\n","\n"]},{"cell_type":"markdown","metadata":{"id":"dlKHtNGi3lUd"},"source":["Example:"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"4dxl_nbv3lUe"},"outputs":[],"source":["score = 70\n","\n","if score >= 50:\n"," print(\"Pass\")\n"]},{"cell_type":"markdown","metadata":{"id":"DcJVF69v3lUe"},"source":["# Activity\n","Write a program that checks whether a number is positive.\n"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"YItYQhZm3lUe"},"outputs":[],"source":["number = int(input(\"Enter a number: \"))\n","\n","if number > 0:\n"," print(\"Positive Number\")"]},{"cell_type":"markdown","metadata":{"id":"yhb8xbdT3lUe"},"source":["# 4. The if-else Statement\n","Syntax:\n","if condition:\n"," statement1\n","else:\n"," statement2"]},{"cell_type":"markdown","metadata":{"id":"RNA-0RiZ3lUf"},"source":["Example:"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"8tSsPIK53lUf"},"outputs":[],"source":["\n","age = 19\n","\n","if age >= 18:\n"," print(\"You can vote\")\n","else:\n"," print(\"You cannot vote\")"]},{"cell_type":"markdown","metadata":{"id":"hgXxLWbG3lUf"},"source":["# Exercise\n","Write a program that determines whether a number is even or odd.\n"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"19IwzkO13lUf"},"outputs":[],"source":["number = int(input(\"Enter a number: \"))\n","\n","if number % 2 == 0:\n"," print(\"Even\")\n","else:\n"," print(\"Odd\")"]},{"cell_type":"markdown","metadata":{"id":"SfRsqsBe3lUf"},"source":["# 5. The if-elif-else Statement\n","Used when multiple conditions exist."]},{"cell_type":"markdown","metadata":{"id":"TFzU_UED3lUf"},"source":["Example:"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"RCGNV4EV3lUg"},"outputs":[],"source":["\n","score = 90\n","\n","if score >= 70:\n"," print(\"Grade A\")\n","elif score >= 60:\n"," print(\"Grade B\")\n","elif score >= 50:\n"," print(\"Grade C\")\n","else:\n"," print(\"Fail\")"]},{"cell_type":"markdown","metadata":{"id":"92Ww1d4b3lUg"},"source":["# Exercise\n","Create a grading system:\n","* 90–100 = A\n","* 80–89 = B\n","* 70–79 = C\n","* 60–69 = D\n","* Below 60 = F\n"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"R5_AmLis3lUg"},"outputs":[],"source":["score = int(input(\"Enter score: \"))\n","\n","if score >= 90:\n"," print(\"A\")\n","elif score >= 80:\n"," print(\"B\")\n","elif score >= 70:\n"," print(\"C\")\n","elif score >= 60:\n"," print(\"D\")\n","else:\n"," print(\"F\")\n"]},{"cell_type":"markdown","metadata":{"id":"ZRHgakek3lUg"},"source":["# 6. Logical Operators\n","\n","Operator\tMeaning\n","and\t-> Both conditions must be True\n","or -> At least one condition must be True\n","not\t-> Reverses a condition\n"]},{"cell_type":"markdown","metadata":{"id":"vl8NC1Bp3lUg"},"source":["# Example:"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"4ZXek2CY3lUg"},"outputs":[],"source":["age = 25\n","citizen = True\n","\n","if age >= 18 and citizen:\n"," print(\"Eligible to vote\")"]},{"cell_type":"markdown","metadata":{"id":"MPfBucRZ3lUh"},"source":["# Exercise\n","Create a login authentication system that check for user name and password"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"QbtvjeNz3lUh"},"outputs":[],"source":["\n","username = \"admin\"\n","password = \"1234\"\n","\n","if username == \"admin\" and password == \"1234\":\n"," print(\"Login Successful\")\n","else:\n"," print(\"Login Failed\")"]},{"cell_type":"markdown","metadata":{"id":"k4z1StTM3lUh"},"source":["# 7. Nested if Statements\n","An if statement inside another if statement."]},{"cell_type":"markdown","metadata":{"id":"gUI5HGkp3lUh"},"source":["Example:"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"Lj17bmPr3lUh"},"outputs":[],"source":["\n","age = 20\n","has_id = True\n","\n","if age >= 18:\n"," if has_id:\n"," print(\"Entry Granted\")"]},{"cell_type":"markdown","metadata":{"id":"ENQmLg5G3lUh"},"source":["# Practical Challenge\n","Write a program that checks the conditions for student to be promoted:\n","1.\tStudent must score at least 50.\n","2.\tAttendance must be at least 75%."]},{"cell_type":"code","execution_count":null,"metadata":{"id":"IdvfOfMz3lUh"},"outputs":[],"source":["score = 65\n","attendance = 80\n","\n","if score >= 50:\n"," if attendance >= 75:\n"," print(\"Promoted\")\n"," else:\n"," print(\"Low Attendance\")\n","else:\n"," print(\"Failed\")\n"]},{"cell_type":"markdown","metadata":{"id":"QPlsj3o23lUh"},"source":["# 8. Introduction to Loops\n","Loops allow us to repeat tasks automatically.\n","Python has:\n","1.\tfor loop\n","2.\twhile loop\n","\n"]},{"cell_type":"markdown","metadata":{"id":"_R0OJkh73lUh"},"source":["# The for Loop\n","Syntax:\n","\n","for variable in sequence:\n"," statements\n","\n"]},{"cell_type":"markdown","metadata":{"id":"K9Cgt4Sp3lUi"},"source":["Example:"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"CkfBTHTC3lUi"},"outputs":[],"source":["for i in range(7):\n"," print(i)\n"]},{"cell_type":"markdown","metadata":{"id":"VYAY77s73lUi"},"source":["# Understanding range()\n","* range(stop)\n","* range(start, stop)\n","* range(start, stop, step)\n"]},{"cell_type":"markdown","metadata":{"id":"MaOhVEMN3lUi"},"source":["Examples:"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"OMT0iWV13lUi"},"outputs":[],"source":["\n","for i in range(1, 6):\n"," print(i)\n"]},{"cell_type":"markdown","metadata":{"id":"9nzq9MsT3lUi"},"source":["# Counting Backward\n","Reversing the range\n"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"-Jw0zu1e3lUi"},"outputs":[],"source":["for i in range(10, 0, -1):\n"," print(i)"]},{"cell_type":"markdown","metadata":{"id":"GhVHrI0G3lUj"},"source":["# Exercise\n","Print all even numbers from 2 to 20.\n"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"u4NvYr983lUj"},"outputs":[],"source":["for i in range(2, 21, 2):\n"," print(i)\n"]},{"cell_type":"markdown","metadata":{"id":"9nnrY-my3lUj"},"source":["# 10. Looping Through Strings\n","Example:\n"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"yf6jQZ6u3lUj"},"outputs":[],"source":["name = \"PYTHON\"\n","\n","for letter in name:\n"," print(letter)"]},{"cell_type":"markdown","metadata":{"id":"l3f-mAye3lUj"},"source":["# Exercise\n","Print each character in your name.\n","\n"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"2y94cGN43lUj"},"outputs":[],"source":["name = input(\"Enter your name: \")\n","for letter in name:\n"," print(letter)"]},{"cell_type":"markdown","metadata":{"id":"EUpdagKm3lUk"},"source":["# 11. The while Loop\n","Repeats as long as a condition remains True.\n","Syntax:\n","while condition:\n"," statements\n"]},{"cell_type":"markdown","metadata":{"id":"NB4s-VUS3lUk"},"source":["Example:"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"TM4KCH3E3lUk"},"outputs":[],"source":["count = 1\n","\n","while count <= 5:\n"," print(count)\n"," count += 1\n"]},{"cell_type":"markdown","metadata":{"id":"ROjiZ_473lUk"},"source":["# Exercise\n","Print numbers from 1 to 10.\n"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"tnNQpOue3lUk"},"outputs":[],"source":["num = 1\n","\n","while num <= 10:\n"," print(num)\n"," num += 1\n"]},{"cell_type":"markdown","metadata":{"id":"0JB5US8d3lUk"},"source":["# 12. Infinite Loops\n","Danger:\n"]},{"cell_type":"markdown","metadata":{"id":"Ehk8kiKJ3lUk"},"source":["# 13. break Statement\n","Used to terminate a loop immediately.\n","\n","\n","\n","Example:"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"cpZoFGI13lUk"},"outputs":[],"source":["for i in range(10):\n"," if i == 5:\n"," break\n"," print(i)\n"]},{"cell_type":"markdown","metadata":{"id":"whjVsEKX3lUl"},"source":["# 14. Continue Statement\n","Skips the current iteration.\n"]},{"cell_type":"markdown","metadata":{"id":"rx7xAKmf3lUl"},"source":["Example:"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"URZh9HJQ3lUl"},"outputs":[],"source":["\n","for i in range(10):\n"," if i == 5:\n"," continue\n"," print(i)\n"]},{"cell_type":"markdown","metadata":{"id":"0OtUWMAx3lUl"},"source":["# 15. Practical Examples\n","Example 1: Multiplication Table\n"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"gM_qCV6i3lUl"},"outputs":[],"source":["number = int(input(\"Enter a number: \"))\n","\n","for i in range(1, 13):\n"," print(number, \"x\", i, \"=\", number * i)\n"]},{"cell_type":"markdown","metadata":{"id":"dpdVSuQs3lUl"},"source":["Example 2: Sum of Numbers"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"bgx0-a_B3lUl"},"outputs":[],"source":["total = 0\n","\n","for i in range(1, 11):\n"," total += i\n","\n","print(\"Sum =\", total)\n"]},{"cell_type":"markdown","metadata":{"id":"04EZJbb33lUl"},"source":["Example 3: Guessing Game"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"2jbi8fCL3lUm"},"outputs":[],"source":["secret = 7\n","\n","guess = int(input(\"Guess the number: \"))\n","\n","while guess != secret:\n"," print(\"Try Again\")\n"," guess = int(input(\"Guess again: \"))\n","\n","print(\"Correct!\")\n"]},{"cell_type":"markdown","metadata":{"id":"-CdsK7tR3lUm"},"source":["# Class Project\n","Student Result Checker\n","Requirements:\n","1.\tAsk for student name.\n","2.\tAsk for score.\n","3.\tDisplay grade.\n","4.\tDetermine Pass/Fail.\n"]},{"cell_type":"markdown","metadata":{"id":"OM0SETCm3lUm"},"source":["# Assessment Questions\n","1.\tWhat is the purpose of an if statement?\n","2.\tDifferentiate between if and if-else.\n","3.\tWhat is a nested if statement?\n","4.\tWhat is the difference between a for loop and a while loop?\n","5.\tExplain break and continue.\n","6.\tWrite a program that prints numbers from 1 to 50.\n","7.\tWrite a program that prints all odd numbers from 1 to 100.\n","8.\tWrite a grading system using if-elif-else.\n"]},{"cell_type":"markdown","metadata":{"id":"vSWsqN193lUm"},"source":["Thank you for joining us on this training we are interested in your journey in faith and also your journing in programming, we pray for greater heights for you in these journeys in Jesus name Amen."]}],"metadata":{"kernelspec":{"display_name":".venv (3.13.3.final.0)","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.13.3"},"colab":{"provenance":[]}},"nbformat":4,"nbformat_minor":0} \ No newline at end of file