{"id":12207,"date":"2020-12-27T00:00:00","date_gmt":"2020-12-26T16:00:00","guid":{"rendered":"https:\/\/fgchen.com\/wpedu2\/2020\/12\/27\/python%e9%80%9f%e8%a8%98\/"},"modified":"2026-03-30T14:39:34","modified_gmt":"2026-03-30T06:39:34","slug":"python%e9%80%9f%e8%a8%98","status":"publish","type":"post","link":"https:\/\/fgchen.com\/wpedu\/2020\/12\/python%e9%80%9f%e8%a8%98\/","title":{"rendered":"\u3010Python\u3011\u901f\u8a18"},"content":{"rendered":"<span style=\"font-weight: 400\">Python 3 is a truly versatile programming language, loved\u00a0 both by web developers, data scientists and software\u00a0 engineers. And there are several good reasons for that!\u00a0<\/span>\n\n<ul>\n    <li><span style=\"font-weight: 400\"> Python is open-source and has a great support community,\u00a0 \u2022 Plus, extensive support libraries.\u00a0\u00a0<\/span><\/li>\n    <li><span style=\"font-weight: 400\"> Its data structures are user-friendly.<\/span><\/li>\n<\/ul>\n\n<h1 id=\"mcetoc_1eqia0qhg0\"><b>Python Basics: Getting Started\u00a0<\/b><\/h1>\n\n<span style=\"font-weight: 400\">Most Windows and Mac computers come with Python pre-installed. You can check\u00a0 that via a Command Line search. The particular appeal of Python is that you can\u00a0 write a program in any text editor, save it in .py format and then run via a Command\u00a0 Line. But as you learn to write more complex code or venture into data science, you\u00a0 might want to switch to an IDE or IDLE.\u00a0<\/span>\n\n<h1 id=\"mcetoc_1eqia0ubn1\"><b>What is IDLE (Integrated Development and Learning)\u00a0<\/b><\/h1>\n\n<span style=\"font-weight: 400\">IDLE (Integrated Development and Learning Environment) comes with every\u00a0 Python installation. Its advantage over other text editors is that it highlights\u00a0 important keywords (e.g. string functions), making it easier for you to interpret code.\u00a0<\/span>\n\n<span style=\"font-weight: 400\">Shell is the default mode of operation for Python IDLE. In essence, it\u2019s a simple loop\u00a0 that performs that following four steps:\u00a0<\/span>\n\n<ul>\n    <li><span style=\"font-weight: 400\"> Reads the Python statement\u00a0<\/span><\/li>\n    <li><span style=\"font-weight: 400\"> Evaluates the results of it\u00a0\u00a0<\/span><\/li>\n    <li><span style=\"font-weight: 400\"> Prints the result on the screen\u00a0<\/span><\/li>\n    <li><span style=\"font-weight: 400\"> And then loops back to read the next statement.\u00a0\u00a0<\/span><\/li>\n<\/ul>\n\n<span style=\"font-weight: 400\">Python shell is a great place to test various small code snippets. <\/span><span style=\"font-weight: 400\"><b>\u00a0<\/b><\/span><span style=\"font-weight: 400\">\n<\/span>\n\n<h1 id=\"mcetoc_1eqia1c7h2\"><b>Main Python Data Types\u00a0<\/b><\/h1>\n\n<span style=\"font-weight: 400\">Every value in Python is called an \u201c<\/span><b>object<\/b><span style=\"font-weight: 400\">\u201d. And every object has a specific data\u00a0 type. The three most-used data types are as follows:\u00a0<\/span>\n\n<b>Integers (int) <\/b><span style=\"font-weight: 400\">\u2014 an integer number to represent an object such as \u201cnumber 3\u201d. <\/span><b>Integers -2, -1, 0, 1, 2, 3, 4, 5\u00a0<\/b>\n\n<b>Floating-point numbers (float) <\/b><span style=\"font-weight: 400\">\u2014 use them to represent floating-point numbers. <\/span><b>Floating-point numbers -1.25, -1.0, &#8211;0.5, 0.0, 0.5, 1.0, 1.25\u00a0<\/b>\n\n<b>Strings <\/b><span style=\"font-weight: 400\">\u2014 codify a sequence of characters using a string. For example, the word\u00a0 \u201chello\u201d. In Python 3, strings are immutable. If you already defined one, you cannot\u00a0 change it later on.\u00a0\u00a0<\/span>\n\n<span style=\"font-weight: 400\">While you can modify a string with commands such as <\/span><b>replace() <\/b><span style=\"font-weight: 400\">or <\/span><b>join()<\/b><span style=\"font-weight: 400\">, they will\u00a0 create a copy of a string and apply modification to it, rather than rewrite the original\u00a0 one.\u00a0<\/span>\n\n<b>Strings \u2018yo\u2019, \u2018hey\u2019, \u2018Hello!\u2019, \u2018what\u2019s up!\u2019\u00a0<\/b>\n\n<span style=\"font-weight: 400\">Plus, another three types worth mentioning are <\/span><b>list<\/b><span style=\"font-weight: 400\">s, <\/span><b>dict<\/b><span style=\"font-weight: 400\">ionaries, and <\/span><b>tuple<\/b><span style=\"font-weight: 400\">s. All of\u00a0 them are discussed in the next sections.\u00a0\u00a0<\/span>\n\n<span style=\"font-weight: 400\">For now, let\u2019s focus on the <\/span><b>strings<\/b><span style=\"font-weight: 400\">.\u00a0<\/span>\n\n<h1 id=\"mcetoc_1eqia26153\"><b>How to Create a String in Python\u00a0<\/b><\/h1>\n\n<span style=\"font-weight: 400\">You can create a string in three ways using <\/span><b>single<\/b><span style=\"font-weight: 400\">, <\/span><b>double <\/b><span style=\"font-weight: 400\">or <\/span><b>triple <\/b><span style=\"font-weight: 400\">quotes. Here\u2019s an\u00a0 example of every option:\u00a0<\/span>\n\n<b>Basic Python String\u00a0<\/b>\n\n<b>my_string = <\/b><b>\u201cLet\u2019s Learn Python!\u201d\u00a0<\/b>\n\n<b>another_string = <\/b><b>\u2018It may seem difficult first, but you\u00a0 can do it!\u2019\u00a0<\/b>\n\n<b>a_long_string = <\/b><b>\u2018\u2019\u2019Yes, you can even master multi-line\u00a0 strings\u00a0<\/b>\n\n<b>\u00a0that cover more than one line\u00a0<\/b>\n\n<b>\u00a0with some practice\u2019\u2019\u2019\u00a0<\/b>\n\n<b>IMP! <\/b><span style=\"font-weight: 400\">Whichever option you choose, you should stick to it and use it consistently\u00a0 within your program.\u00a0\u00a0<\/span>\n\n<span style=\"font-weight: 400\">As the next step, you can use the <\/span><b>print() <\/b><span style=\"font-weight: 400\">function to output your string in the console\u00a0 window. This lets you review your code and ensure that all functions well.\u00a0\u00a0<\/span>\n\n<span style=\"font-weight: 400\">Here\u2019s a snippet for that:\u00a0\u00a0<\/span>\n\n<b>print(<\/b><b>\u201cLet\u2019s print out a string!\u201d<\/b><b>)\u00a0<\/b>\n\n<b>String Concatenation\u00a0<\/b>\n\n<span style=\"font-weight: 400\">The next thing you can master is <\/span><b>concatenation <\/b><span style=\"font-weight: 400\">\u2014 a way to add two strings\u00a0 together using the \u201c+\u201d operator. Here\u2019s how it\u2019s done:\u00a0\u00a0<\/span>\n\n<b>string_one = <\/b><b>\u201cI\u2019m reading \u201c\u00a0<\/b>\n\n<b>string_two = <\/b><b>\u201ca new great book!\u201d\u00a0<\/b>\n\n<b>string_three = string_one + string_two\u00a0<\/b>\n\n<b>Note: <\/b><span style=\"font-weight: 400\">You can\u2019t apply + operator to two different data types e.g. string + integer. If\u00a0 you try to do that, you\u2019ll get the following Python error:\u00a0\u00a0<\/span>\n\n<b>TypeError: Can<\/b><b>\u2019t convert \u2018<\/b><b>int<\/b><b>\u2019 object to str implicitly\u00a0<\/b>\n\n<h2 id=\"mcetoc_1eqia2jbv4\"><b>String Replication\u00a0<\/b><\/h2>\n\n<span style=\"font-weight: 400\">As the name implies, this command lets you repeat the same string several times.\u00a0 This is done using <\/span><b>* <\/b><span style=\"font-weight: 400\">operator. Mind that this operator acts as a replicator only with\u00a0 string data types. When applied to numbers, it acts as a multiplier.\u00a0\u00a0<\/span>\n\n<span style=\"font-weight: 400\">String replication example:\u00a0<\/span>\n\n<b>\u2018Alice\u2019 <\/b><b>* <\/b><b>5 <\/b><b>\u2018AliceAliceAliceAliceAlice\u2019\u00a0<\/b>\n\n<span style=\"font-weight: 400\">And with print ()\u00a0<\/span>\n\n<b>print(<\/b><b>\u201cAlice\u201d <\/b><b>* <\/b><b>5<\/b><b>)\u00a0<\/b>\n\n<span style=\"font-weight: 400\">And your output will be Alice written five times in a row.\u00a0<\/span>\n\n<b>Math Operators\u00a0<\/b>\n\n<span style=\"font-weight: 400\">For reference, here\u2019s a list of other math operations you can apply towards numbers:\u00a0<\/span>\n\n<b>Operators Operation Example ** Exponent 2 ** 3 = 8 % Modulus\/Remainder 22 % 8 = 6 \/\/ Integer division 22 \/\/ 8 = 2 \/ Division 22 \/ 8 = 2.75 * Multiplication 3 * 3 = 9 <\/b><b>&#8211; <\/b><b>Subtraction 5 &#8211; 2 = 3 + Addition 2 + 2 = 4\u00a0<\/b>\n\n<h2 id=\"mcetoc_1eqia305b5\"><b>How to Store Strings in Variables\u00a0<\/b><\/h2>\n\n<b>Variables <\/b><span style=\"font-weight: 400\">in Python 3 are special symbols that assign a specific storage location to\u00a0 a value that\u2019s tied to it. In essence, variables are like special labels that you place on\u00a0 some value to know where it\u2019s stored.\u00a0\u00a0<\/span>\n\n<span style=\"font-weight: 400\">Strings incorporate data. So you can \u201cpack\u201d them inside a variable. Doing so makes\u00a0 it easier to work with complex Python programs.\u00a0\u00a0<\/span>\n\n<span style=\"font-weight: 400\">Here\u2019s how you can store a string inside a variable.\u00a0<\/span>\n\n<b>my_str = <\/b><b>\u201cHello World\u201d\u00a0<\/b>\n\n<span style=\"font-weight: 400\">Let\u2019s break it down a bit further:\u00a0<\/span>\n\n<ul>\n    <li><span style=\"font-weight: 400\"> my_str is the variable name.\u00a0\u00a0<\/span><\/li>\n    <li><span style=\"font-weight: 400\"> = is the assignment operator.\u00a0\u00a0<\/span><\/li>\n    <li><span style=\"font-weight: 400\"> \u201cJust a random string\u201d is a value you tie to the variable name.\u00a0\u00a0<\/span><\/li>\n<\/ul>\n\n<span style=\"font-weight: 400\">Now when you print this out, you receive the string output.\u00a0<\/span>\n\n<b>print(my_str)\u00a0 <\/b><b>= Hello World\u00a0<\/b>\n\n<span style=\"font-weight: 400\">See? By using variables, you save yourself heaps of effort as you don\u2019t need to\u00a0 retype the complete string every time you want to use it.\u00a0<\/span>\n\n<h1 id=\"mcetoc_1eqia3l816\"><b>Built-in Functions in Python\u00a0<\/b><\/h1>\n\n<span style=\"font-weight: 400\">You already know the most popular function in Python \u2014 <\/span><b>print()<\/b><span style=\"font-weight: 400\">. Now let\u2019s take a\u00a0 look at its equally popular cousins that are in-built in the platform.\u00a0<\/span>\n\n<b>Input() Function\u00a0<\/b>\n\n<b>input() <\/b><span style=\"font-weight: 400\">function is a simple way to prompt the user for some input (e.g. provide their\u00a0 name). All user input is stored as a string.\u00a0<\/span>\n\n<span style=\"font-weight: 400\">Here\u2019s a quick snippet to illustrate this:\u00a0\u00a0<\/span>\n\n<b>name = input(<\/b><b>\u201cHi! What\u2019s your name? \u201c<\/b><b>)\u00a0<\/b>\n\n<b>print(<\/b><b>\u201cNice to meet you \u201c <\/b><b>+ name + <\/b><b>\u201c!\u201d<\/b><b>)\u00a0<\/b>\n\n<b>age = input(<\/b><b>\u201cHow old are you \u201c<\/b><b>)\u00a0<\/b>\n\n<b>print(<\/b><b>\u201cSo, you are already \u201c <\/b><b>+ str(age) + <\/b><b>\u201c years old, \u201c <\/b><b>+ name + <\/b><b>\u201c!\u201d<\/b><b>)\u00a0<\/b>\n\n<span style=\"font-weight: 400\">When you run this short program, the results will look like this:\u00a0<\/span>\n\n<b>Hi! What\u2019s your name? \u201cJim\u201d\u00a0<\/b>\n\n<b>Nice to meet you, Jim!\u00a0<\/b>\n\n<b>How old are you? 25\u00a0<\/b>\n\n<b>So, you are already 25 years old, Jim!\u00a0<\/b>\n\n<b>len() Function\u00a0<\/b>\n\n<b>len() <\/b><span style=\"font-weight: 400\">function helps you find the length of any string, list, tuple, dictionary, or another\u00a0 data type. It\u2019s a handy command to determine excessive values and trim them to\u00a0 optimize the performance of your program.\u00a0<\/span>\n\n<span style=\"font-weight: 400\">Here\u2019s an input function example for a string:\u00a0<\/span>\n\n<b><i># testing len()\u00a0\u00a0<\/i><\/b>\n\n<b>str1 = <\/b><b>\u201cHope you are enjoying our tutorial!\u201d\u00a0<\/b>\n\n<b>print(<\/b><b>\u201cThe length of the string is :\u201d<\/b><b>, len(str1))\u00a0<\/b>\n\n<span style=\"font-weight: 400\">Output:\u00a0<\/span>\n\n<b>The length of the string is: 35\u00a0<\/b>\n\n<h2 id=\"mcetoc_1eqia45uo7\"><b>filter()\u00a0<\/b><\/h2>\n\n<span style=\"font-weight: 400\">Use the <\/span><b>Filter() <\/b><span style=\"font-weight: 400\">function to exclude items in an iterable object (lists, tuples,\u00a0 dictionaries, etc)\u00a0<\/span>\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\">ages = [5, 12, 17, 18, 24, 32]\u00a0\ndef myFunc(x):\u00a0\n\u00a0if x &lt; 18:\u00a0\n\u00a0return False\u00a0\n\u00a0else:\u00a0\n\u00a0return True\u00a0\nadults = filter(myFunc, ages)\u00a0\nfor x in adults:\u00a0\n\u00a0print(x)<\/pre>\n\n<h1 id=\"mcetoc_1eqia4j488\"><b>How to Define a Function\u00a0<\/b><\/h1>\n\n<span style=\"font-weight: 400\">Apart from using in-built functions, Python 3 also allows you to define your own\u00a0 functions for your program.\u00a0\u00a0<\/span>\n\n<span style=\"font-weight: 400\">To recap, a <\/span><b>function <\/b><span style=\"font-weight: 400\">is a block of coded instructions that perform a certain action.\u00a0 Once properly defined, a function can be reused throughout your program i.e. re-use\u00a0 the same code.\u00a0\u00a0<\/span>\n\n<span style=\"font-weight: 400\">Here\u2019s a quick walkthrough explaining how to define a function in Python:\u00a0<\/span>\n\n<span style=\"font-weight: 400\">First, use <\/span><b>def <\/b><span style=\"font-weight: 400\">keyword followed by the function <\/span><b>name()<\/b><span style=\"font-weight: 400\">:. The parentheses can\u00a0 contain any parameters that your function should take (or stay empty).\u00a0<\/span>\n\n<b>def <\/b><b>name<\/b><b>():\u00a0<\/b>\n\n<span style=\"font-weight: 400\">Next, you\u2019ll need to add a second code line with a 4-space indent to specify what\u00a0 this function should do.\u00a0<\/span>\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\">def name():\u00a0\n\u00a0print(\u201cWhat\u2019s your name?\u201d)<\/pre>\n\n<span style=\"font-weight: 400\">Now, you have to call this function to run the code.\u00a0<\/span>\n\n<b>name.py\u00a0<\/b>\n\n<b>def <\/b><b>name<\/b><b>():\u00a0<\/b>\n\n<b>\u00a0print(<\/b><b>\u201cWhat\u2019s your name?\u201d<\/b><b>)\u00a0<\/b>\n\n<b>hello()\u00a0<\/b>\n\n<span style=\"font-weight: 400\">Now, let\u2019s take a look at a defined function with a parameter \u2014 an entity, specifying\u00a0 an argument that a function can accept.\u00a0<\/span>\n\n<b>def <\/b><b>add_numbers<\/b><b>(x, y, z):\u00a0<\/b>\n\n<b>\u00a0a = x + y\u00a0<\/b>\n\n<b>\u00a0b = x + z\u00a0<\/b>\n\n<b>\u00a0c = y + z\u00a0<\/b>\n\n<b>\u00a0print(a, b, c)\u00a0<\/b>\n\n<b>add_numbers(<\/b><b>1<\/b><b>, <\/b><b>2<\/b><b>, <\/b><b>3<\/b><b>)\u00a0<\/b>\n\n<span style=\"font-weight: 400\">In this case, you pass the number 1 in for the x parameter, 2 in for the y parameter,\u00a0 and 3 in for the z parameter. The program will that do the simple math of adding up\u00a0 the numbers:\u00a0<\/span>\n\n<span style=\"font-weight: 400\">Output:\u00a0\u00a0<\/span>\n\n<b>a = 1 + 2\u00a0<\/b>\n\n<b>b = 1 + 3\u00a0<\/b>\n\n<b>c = 2 + 3\u00a0<\/b>\n\n<h2 id=\"mcetoc_1eqib4u990\"><b>How to Pass Keyword Arguments to a Function\u00a0<\/b><\/h2>\n\n<span style=\"font-weight: 400\">A function can also accept keyword arguments. In this case, you can use\u00a0 parameters in random order as the Python interpreter will use the provided\u00a0 keywords to match the values to the parameters.\u00a0<\/span>\n\n<span style=\"font-weight: 400\">Here\u2019s a simple example of how you pass a keyword argument to a function.\u00a0\u00a0<\/span>\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\"># Define function with parameters\u00a0\n\ndef product_info(product name, price):\u00a0\n\u00a0print(\u201cproductname: \u201c + product name)\u00a0\n\u00a0print(\u201cPrice \u201c + str(dollars))\u00a0\n\n# Call function with parameters assigned as above product_info(\u201cWhite T-shirt\u201d, 15 dollars)\u00a0\n# Call function with keyword arguments\u00a0\nproduct_info(productname=\u201djeans\u201d, price=45)<\/pre>\n\n<span style=\"font-weight: 400\">Output:\u00a0<\/span>\n\n<b>Productname: White T-shirt\u00a0<\/b>\n\n<b>Price: 15\u00a0<\/b>\n\n<b>Productname: Jeans\u00a0\u00a0<\/b>\n\n<b>Price: 45\u00a0<\/b>\n\n<h1 id=\"mcetoc_1eqia58fu9\"><b>Lists\u00a0<\/b><\/h1>\n\n<b>Lists <\/b><span style=\"font-weight: 400\">are another cornerstone data type in Python used to specify an ordered\u00a0 sequence of elements. In short, they help you keep related data together and\u00a0 perform the same operations on several values at once. Unlike strings, lists are\u00a0 mutable (=changeable).\u00a0<\/span>\n\n<span style=\"font-weight: 400\">Each value inside a list is called an <\/span><b>item <\/b><span style=\"font-weight: 400\">and these are placed between square\u00a0 brackets.\u00a0<\/span>\n\n<b>Example lists\u00a0<\/b>\n\n<b>my_list = [<\/b><b>1<\/b><b>, <\/b><b>2<\/b><b>, <\/b><b>3<\/b><b>]\u00a0<\/b>\n\n<b>my_list2 = [<\/b><b>\u201ca\u201d<\/b><b>, <\/b><b>\u201cb\u201d<\/b><b>, <\/b><b>\u201cc\u201d<\/b><b>]\u00a0<\/b>\n\n<b>my_list3 = [<\/b><b>\u201c4\u201d<\/b><b>, d, <\/b><b>\u201cbook\u201d<\/b><b>, <\/b><b>5<\/b><b>]\u00a0<\/b>\n\n<span style=\"font-weight: 400\">Alternatively, you can use <\/span><b>list() <\/b><span style=\"font-weight: 400\">function to do the same:\u00a0<\/span>\n\n<b>alpha_list = list((<\/b><b>\u201c1\u201d<\/b><b>, <\/b><b>\u201c2\u201d<\/b><b>, <\/b><b>\u201c3\u201d<\/b><b>))\u00a0\u00a0<\/b>\n\n<b>print(alpha_list)\u00a0<\/b>\n\n<b>How to Add Items to a List\u00a0<\/b>\n\n<span style=\"font-weight: 400\">You have two ways to add new items to existing lists.\u00a0\u00a0<\/span>\n\n<span style=\"font-weight: 400\">The first one is using <\/span><b>append() <\/b><span style=\"font-weight: 400\">function:\u00a0\u00a0<\/span>\n\n<b>beta_list = [<\/b><b>\u201capple\u201d<\/b><b>, <\/b><b>\u201cbanana\u201d<\/b><b>, <\/b><b>\u201corange\u201d<\/b><b>]\u00a0<\/b>\n\n<b>beta_list.append(<\/b><b>\u201cgrape\u201d<\/b><b>)\u00a0<\/b>\n\n<b>print(beta_list)\u00a0<\/b>\n\n<span style=\"font-weight: 400\">The second option is to <\/span><b>insert() <\/b><span style=\"font-weight: 400\">function to add an item at the specified index:\u00a0<\/span>\n\n<b>beta_list = [<\/b><b>\u201capple\u201d<\/b><b>, <\/b><b>\u201cbanana\u201d<\/b><b>, <\/b><b>\u201corange\u201d<\/b><b>]\u00a0<\/b>\n\n<b>beta_list.insert(<\/b><b>\u201c2 grape\u201d<\/b><b>)\u00a0<\/b>\n\n<b>print(beta_list)\u00a0<\/b>\n\n<h2 id=\"mcetoc_1eqia5i23a\"><b>How to Remove an Item from a List\u00a0<\/b><\/h2>\n\n<span style=\"font-weight: 400\">Again, you have several ways to do so. First, you can use <\/span><b>remove() <\/b><span style=\"font-weight: 400\">function:\u00a0\u00a0<\/span>\n\n<b>beta_list = [<\/b><b>\u201capple\u201d<\/b><b>, <\/b><b>\u201cbanana\u201d<\/b><b>, <\/b><b>\u201corange\u201d<\/b><b>]\u00a0<\/b>\n\n<b>beta_list.remove(<\/b><b>\u201capple\u201d<\/b><b>)\u00a0<\/b>\n\n<b>print(beta_list)\u00a0<\/b>\n\n<span style=\"font-weight: 400\">Secondly, you can use the <\/span><b>pop() function<\/b><span style=\"font-weight: 400\">. If no index is specified, it will remove the\u00a0 last item.\u00a0\u00a0<\/span>\n\n<b>beta_list = [<\/b><b>\u201capple\u201d<\/b><b>, <\/b><b>\u201cbanana\u201d<\/b><b>, <\/b><b>\u201corange\u201d<\/b><b>]\u00a0<\/b>\n\n<b>beta_list.pop()\u00a0<\/b>\n\n<b>print(beta_list)\u00a0<\/b>\n\n<span style=\"font-weight: 400\">The last option is to use <\/span><b>del keyword <\/b><span style=\"font-weight: 400\">to remove a specific item:\u00a0\u00a0<\/span>\n\n<b>beta_list = [<\/b><b>\u201capple\u201d<\/b><b>, <\/b><b>\u201cbanana\u201d<\/b><b>, <\/b><b>\u201corange\u201d<\/b><b>]\u00a0<\/b>\n\n<b>del <\/b><b>beta_list [<\/b><b>1<\/b><b>]\u00a0<\/b>\n\n<b>print(beta_list)\u00a0<\/b>\n\n<span style=\"font-weight: 400\">P.S. You can also apply del towards the entire list to scrap it.\u00a0\u00a0<\/span>\n\n<b>Combine Two Lists\u00a0\u00a0<\/b>\n\n<span style=\"font-weight: 400\">To mash up two lists use the + operator.\u00a0<\/span>\n\n<b>my_list = [<\/b><b>1<\/b><b>, <\/b><b>2<\/b><b>, <\/b><b>3<\/b><b>]\u00a0<\/b>\n\n<b>my_list2 = [<\/b><b>\u201ca\u201d<\/b><b>, <\/b><b>\u201cb\u201d<\/b><b>, <\/b><b>\u201cc\u201d<\/b><b>]\u00a0<\/b>\n\n<b>combo_list = my_list + my_list2\u00a0<\/b>\n\n<b>combo_list\u00a0<\/b>\n\n<b>[<\/b><b>1<\/b><b>, <\/b><b>2<\/b><b>, <\/b><b>3<\/b><b>, <\/b><b>\u2018a\u2019<\/b><b>, <\/b><b>\u2018b\u2019<\/b><b>, <\/b><b>\u2018c\u2019<\/b><b>]\u00a0<\/b>\n\n<b>Create a Nested List\u00a0<\/b>\n\n<span style=\"font-weight: 400\">You can also create a list of your lists when you have plenty of them \ud83d\ude42\u00a0<\/span>\n\n<b>my_nested_list = [my_list, my_list2]\u00a0<\/b>\n\n<b>my_nested_list\u00a0<\/b>\n\n<b>[[<\/b><b>1<\/b><b>, <\/b><b>2<\/b><b>, <\/b><b>3<\/b><b>], [<\/b><b>\u2018a\u2019<\/b><b>, <\/b><b>\u2018b\u2019<\/b><b>, <\/b><b>\u2018c\u2019<\/b><b>]]\u00a0<\/b>\n\n<h2 id=\"mcetoc_1eqia5sthb\"><b>Sort a List\u00a0<\/b><\/h2>\n\n<span style=\"font-weight: 400\">Use the <\/span><b>sort() <\/b><span style=\"font-weight: 400\">function to organize all items in your list.\u00a0\u00a0<\/span>\n\n<b>alpha_list = [<\/b><b>34<\/b><b>, <\/b><b>23<\/b><b>, <\/b><b>67<\/b><b>, <\/b><b>100<\/b><b>, <\/b><b>88<\/b><b>, <\/b><b>2<\/b><b>]\u00a0<\/b>\n\n<b>alpha_list.sort()\u00a0<\/b>\n\n<b>alpha_list\u00a0<\/b>\n\n<b>[<\/b><b>2<\/b><b>, <\/b><b>23<\/b><b>, <\/b><b>34<\/b><b>, <\/b><b>67<\/b><b>, <\/b><b>88<\/b><b>, <\/b><b>100<\/b><b>]\u00a0<\/b>\n\n<b>Slice a List\u00a0<\/b>\n\n<span style=\"font-weight: 400\">Now, if you want to call just a few elements from your list (e.g. the first 4 items),\u00a0 you need to specify a range of index numbers separated by a colon [x:y]. Here\u2019s an\u00a0 example:\u00a0<\/span>\n\n<b>alpha_list[<\/b><b>0<\/b><b>:<\/b><b>4<\/b><b>]\u00a0<\/b>\n\n<b>[<\/b><b>2<\/b><b>, <\/b><b>23<\/b><b>, <\/b><b>34<\/b><b>, <\/b><b>67<\/b><b>]\u00a0<\/b>\n\n<b>Change Item Value on Your List\u00a0<\/b>\n\n<span style=\"font-weight: 400\">You can easily overwrite a value of one list items:\u00a0<\/span>\n\n<b>beta_list = [<\/b><b>\u201capple\u201d<\/b><b>, <\/b><b>\u201cbanana\u201d<\/b><b>, <\/b><b>\u201corange\u201d<\/b><b>]\u00a0<\/b>\n\n<b>beta_list[<\/b><b>1<\/b><b>] = <\/b><b>\u201cpear\u201d\u00a0<\/b>\n\n<b>print(beta_list)\u00a0<\/b>\n\n<span style=\"font-weight: 400\">Output:\u00a0<\/span>\n\n<b>[\u2018apple\u2019, \u2018pear\u2019, \u2018cherry\u2019]\u00a0<\/b>\n\n<b>Loop Through the List\u00a0<\/b>\n\n<span style=\"font-weight: 400\">Using <\/span><b>for loop <\/b><span style=\"font-weight: 400\">you can multiply the usage of certain items, similarly to what *\u00a0 operator does. Here\u2019s an example:\u00a0<\/span>\n\n<b>for <\/b><b>x <\/b><b>in <\/b><b>range(<\/b><b>1<\/b><b>,<\/b><b>4<\/b><b>):\u00a0<\/b>\n\n<b>\u00a0beta_list += [<\/b><b>\u2018fruit\u2019<\/b><b>]\u00a0<\/b>\n\n<b>\u00a0print(beta_list)\u00a0<\/b>\n\n<h2 id=\"mcetoc_1eqia69onc\"><b>Copy a List\u00a0<\/b><\/h2>\n\n<span style=\"font-weight: 400\">Use the built-in <\/span><b>copy() <\/b><span style=\"font-weight: 400\">function to replicate your data:\u00a0<\/span>\n\n<b>beta_list = [<\/b><b>\u201capple\u201d<\/b><b>, <\/b><b>\u201cbanana\u201d<\/b><b>, <\/b><b>\u201corange\u201d<\/b><b>]\u00a0<\/b>\n\n<b>beta_list = beta_list.copy()\u00a0<\/b>\n\n<b>print(beta_list)\u00a0<\/b>\n\n<span style=\"font-weight: 400\">Alternatively, you can copy a list with the <\/span><b>list() <\/b><span style=\"font-weight: 400\">method:\u00a0<\/span>\n\n<b>beta_list = [<\/b><b>\u201capple\u201d<\/b><b>, <\/b><b>\u201cbanana\u201d<\/b><b>, <\/b><b>\u201corange\u201d<\/b><b>]\u00a0<\/b>\n\n<b>beta_list = list (beta_list)\u00a0\u00a0<\/b>\n\n<b>print(beta_list)\u00a0<\/b>\n\n<h1 id=\"mcetoc_1eqia77tpd\"><b>List Comprehensions\u00a0<\/b><\/h1>\n\n<b>List comprehensions <\/b><span style=\"font-weight: 400\">are a handy option for creating lists based on existing lists.\u00a0 When using them you can build by using <\/span><b>strings <\/b><span style=\"font-weight: 400\">and <\/span><b>tuples <\/b><span style=\"font-weight: 400\">as well.\u00a0\u00a0<\/span>\n\n<b>List comprehensions examples\u00a0<\/b>\n\n<b>list_variable = [x <\/b><b>for <\/b><b>x <\/b><b>in <\/b><b>iterable]\u00a0<\/b>\n\n<span style=\"font-weight: 400\">Here\u2019s a more complex example that features math operators, integers, and the\u00a0 <\/span><b>range() <\/b><span style=\"font-weight: 400\">function:\u00a0\u00a0<\/span>\n\n<b>number_list = [x ** <\/b><b>2 <\/b><b>for <\/b><b>x <\/b><b>in <\/b><b>range(<\/b><b>10<\/b><b>) <\/b><b>if <\/b><b>x % <\/b><b>2 <\/b><b>== <\/b><b>0<\/b><b>] print(number_list)\u00a0<\/b>\n\n<h1 id=\"mcetoc_1eqib73oo1\"><b>Tuples\u00a0<\/b><\/h1>\n\n<span style=\"font-weight: 400\">Tuples are similar to lists \u2014 they allow you to display an ordered sequence of\u00a0 elements. However, they are immutable and you can\u2019t change the values stored in a\u00a0 tuple.\u00a0\u00a0<\/span>\n\n<span style=\"font-weight: 400\">The advantage of using tuples over lists is that the former are slightly faster. So it\u2019s\u00a0 a nice way to optimize your code.\u00a0\u00a0<\/span>\n\n<b>How to Create a Tuple\u00a0<\/b>\n\n<b>my_tuple = (<\/b><b>1<\/b><b>, <\/b><b>2<\/b><b>, <\/b><b>3<\/b><b>, <\/b><b>4<\/b><b>, <\/b><b>5<\/b><b>)\u00a0<\/b>\n\n<b>my_tuple[<\/b><b>0<\/b><b>:<\/b><b>3<\/b><b>]\u00a0<\/b>\n\n<b>(<\/b><b>1<\/b><b>, <\/b><b>2<\/b><b>, <\/b><b>3<\/b><b>)\u00a0<\/b>\n\n<span style=\"font-weight: 400\">Note: Once you create a tuple, you can\u2019t add new items to it or change it in any other way!\u00a0<\/span>\n\n<h2 id=\"mcetoc_1eqib77f22\"><b>How to Slide a Tuple\u00a0<\/b><\/h2>\n\n<span style=\"font-weight: 400\">The process is similar to slicing lists.\u00a0<\/span>\n\n<b>numbers = (<\/b><b>0<\/b><b>, <\/b><b>1<\/b><b>, <\/b><b>2<\/b><b>, <\/b><b>3<\/b><b>, <\/b><b>4<\/b><b>, <\/b><b>5<\/b><b>, <\/b><b>6<\/b><b>, <\/b><b>7<\/b><b>, <\/b><b>8<\/b><b>, <\/b><b>9<\/b><b>, <\/b><b>10<\/b><b>, <\/b><b>11<\/b><b>, <\/b><b>12<\/b><b>) print(numbers[<\/b><b>1<\/b><b>:<\/b><b>11<\/b><b>:<\/b><b>2<\/b><b>])\u00a0<\/b>\n\n<span style=\"font-weight: 400\">Output:\u00a0<\/span>\n\n<b>(1, 3, 5, 7, 9)\u00a0<\/b>\n\n<h2 id=\"mcetoc_1eqia7i53e\"><b>Convert Tuple to a List\u00a0<\/b><\/h2>\n\n<span style=\"font-weight: 400\">Since Tuples are immutable, you can\u2019t change them. What you can do though is\u00a0 convert a tuple into a list, make an edit and then convert it back to a tuple.\u00a0\u00a0<\/span>\n\n<span style=\"font-weight: 400\">Here\u2019s how to accomplish this:\u00a0<\/span>\n\n<b>x = (<\/b><b>\u201capple\u201d<\/b><b>, <\/b><b>\u201corange\u201d<\/b><b>, <\/b><b>\u201cpear\u201d<\/b><b>)\u00a0<\/b>\n\n<b>y = list(x)\u00a0<\/b>\n\n<b>y[<\/b><b>1<\/b><b>] = <\/b><b>\u201cgrape\u201d\u00a0<\/b>\n\n<b>x = tuple(y)\u00a0<\/b>\n\n<b>print(x)\u00a0<\/b>\n\n<h1 id=\"mcetoc_1eqia81rjf\"><b>Dictionaries\u00a0<\/b><\/h1>\n\n<span style=\"font-weight: 400\">A dictionary holds indexes with keys that are mapped to certain values. These\u00a0 key-value pairs offer a great way of organizing and storing data in Python. They are\u00a0 mutable, meaning you can change the stored information.\u00a0\u00a0<\/span>\n\n<span style=\"font-weight: 400\">A key value can be either a <\/span><b>string<\/b><span style=\"font-weight: 400\">, <\/span><b>Boolean<\/b><span style=\"font-weight: 400\">, or <\/span><b>integer<\/b><span style=\"font-weight: 400\">. Here\u2019s an example dictionary\u00a0 illustrating this:\u00a0<\/span>\n\n<b>Customer <\/b><b>1<\/b><b>= {<\/b><b>\u2018username\u2019<\/b><b>: <\/b><b>\u2018john-sea\u2019<\/b><b>, <\/b><b>\u2018online\u2019<\/b><b>: false,\u00a0 <\/b><b>\u2018friends\u2019<\/b><b>:<\/b><b>100<\/b><b>}\u00a0<\/b>\n\n<b>How to Create a Python Dictionary\u00a0<\/b>\n\n<span style=\"font-weight: 400\">Here\u2019s a quick example showcasing how to make an empty dictionary.\u00a0 Option 1: <\/span><b>new_dict = {}\u00a0<\/b>\n\n<span style=\"font-weight: 400\">Option 2: <\/span><b>other_dict= dict()\u00a0<\/b>\n\n<span style=\"font-weight: 400\">And you can use the same two approaches to add values to your dictionary:\u00a0<\/span>\n\n<b>new_dict = {\u00a0<\/b>\n\n<b>\u00a0<\/b><b>\u201cbrand\u201d<\/b><b>: <\/b><b>\u201cHonda\u201d<\/b><b>,\u00a0<\/b>\n\n<b>\u00a0<\/b><b>\u201cmodel\u201d<\/b><b>: <\/b><b>\u201cCivic\u201d<\/b><b>,\u00a0<\/b>\n\n<b>\u00a0<\/b><b>\u201cyear\u201d<\/b><b>: <\/b><b>1995\u00a0<\/b>\n\n<b>}\u00a0<\/b>\n\n<b>print(new_dict)\u00a0<\/b>\n\n&nbsp;\n\n<h2 id=\"mcetoc_1eqia85ekg\"><b>How to Access a Value in a Dictionary\u00a0<\/b><\/h2>\n\n<span style=\"font-weight: 400\">You can access any of the values in your dictionary the following way: <\/span><b>x = new_dict[<\/b><b>\u201cbrand\u201d<\/b><b>]\u00a0<\/b>\n\n<span style=\"font-weight: 400\">You can also use the following methods to accomplish the same. <\/span><b>\u2022 dict.keys() <\/b><span style=\"font-weight: 400\">isolates keys\u00a0<\/span>\n\n<ul>\n    <li><b> dict.values() <\/b><span style=\"font-weight: 400\">isolates values\u00a0<\/span><\/li>\n    <li><b> dict.items() <\/b><span style=\"font-weight: 400\">returns items in a list format of (key, value) tuple pairs\u00a0<\/span><\/li>\n<\/ul>\n\n<h2 id=\"mcetoc_1eqia8e56h\"><b>Change Item Value\u00a0<\/b><\/h2>\n\n<span style=\"font-weight: 400\">To change one of the items, you need to refer to it by its key name: <\/span><b><i>#Change the \u201cyear\u201d to 2020:\u00a0<\/i><\/b>\n\n<b>new_dict= {\u00a0<\/b>\n\n<b>\u00a0<\/b><b>\u201cbrand\u201d<\/b><b>: <\/b><b>\u201cHonda\u201d<\/b><b>,\u00a0<\/b>\n\n<b>\u00a0<\/b><b>\u201cmodel\u201d<\/b><b>: <\/b><b>\u201cCivic\u201d<\/b><b>,\u00a0<\/b>\n\n<b>\u00a0<\/b><b>\u201cyear\u201d<\/b><b>: <\/b><b>1995\u00a0<\/b>\n\n<b>}\u00a0<\/b>\n\n<b>new_dict[<\/b><b>\u201cyear\u201d<\/b><b>] = <\/b><b>2020\u00a0<\/b>\n\n<b>Loop Through the Dictionary\u00a0<\/b>\n\n<span style=\"font-weight: 400\">Again to implement looping, use for loop command.\u00a0<\/span>\n\n<span style=\"font-weight: 400\">Note: In this case, the return values are the keys of the dictionary. But, you can also return\u00a0 values using another method.\u00a0<\/span>\n\n<b><i>#print all key names in the dictionary\u00a0\u00a0<\/i><\/b>\n\n<b>for <\/b><b>x <\/b><b>in <\/b><b>new_dict:\u00a0<\/b>\n\n<b>\u00a0print(x)\u00a0<\/b>\n\n<b><i>#print all values in the dictionary\u00a0\u00a0<\/i><\/b>\n\n<b>for <\/b><b>x <\/b><b>in <\/b><b>new_dict:\u00a0<\/b>\n\n<b>\u00a0print(new_dict[x])\u00a0<\/b>\n\n<b><i>#loop through both keys and values\u00a0<\/i><\/b>\n\n<b>for <\/b><b>x, y <\/b><b>in <\/b><b>my_dict.items():\u00a0<\/b>\n\n<b>\u00a0print(x, y)\u00a0<\/b>\n\n<h1 id=\"mcetoc_1eqia8q26i\"><b>If Statements (Conditional\u00a0\u00a0<\/b><b>Statements) in Python\u00a0<\/b><\/h1>\n\n<span style=\"font-weight: 400\">Just like other programming languages, Python supports the basic logical\u00a0 conditions from math:\u00a0\u00a0<\/span>\n\n<ul>\n    <li><span style=\"font-weight: 400\"> Equals: a == b\u00a0<\/span><\/li>\n    <li><span style=\"font-weight: 400\"> Not Equals: a != b\u00a0<\/span><\/li>\n    <li><span style=\"font-weight: 400\"> Less than: a &lt; b\u00a0<\/span><\/li>\n    <li><span style=\"font-weight: 400\"> Less than or equal to a &lt;= b\u00a0<\/span><\/li>\n    <li><span style=\"font-weight: 400\"> Greater than: a &gt; b\u00a0<\/span><\/li>\n    <li><span style=\"font-weight: 400\"> Greater than or equal to: a &gt;= b\u00a0<\/span><\/li>\n<\/ul>\n\n<span style=\"font-weight: 400\">You can leverage these conditions in various ways. But most likely, you\u2019ll use them in\u00a0 \u201c<\/span><b>if statements\u201d <\/b><span style=\"font-weight: 400\">and <\/span><b>loops.\u00a0<\/b>\n\n<h2 id=\"mcetoc_1eqia8v3sj\"><b>If Statement Example\u00a0<\/b><\/h2>\n\n<span style=\"font-weight: 400\">The goal of a conditional statement is to check if it\u2019s True or False.\u00a0<\/span>\n\n<b>if <\/b><b>5 <\/b><b>&gt; <\/b><b>1<\/b><b>:\u00a0<\/b>\n\n<b>\u00a0print(<\/b><b>\u201cThat\u2019s True!\u201d<\/b><b>)\u00a0<\/b>\n\n<span style=\"font-weight: 400\">Output:\u00a0<\/span>\n\n<b>That\u2019s True!\u00a0<\/b>\n\n<h2 id=\"mcetoc_1eqia94qmk\"><b>Nested If Statements\u00a0<\/b><\/h2>\n\n<span style=\"font-weight: 400\">For more complex operations, you can create nested if statements. Here\u2019s how it\u00a0 looks:\u00a0<\/span>\n\n<b>x = <\/b><b>35\u00a0<\/b>\n\n<b>if <\/b><b>x &gt; <\/b><b>20<\/b><b>:\u00a0<\/b>\n\n<b>\u00a0print(<\/b><b>\u201cAbove twenty,\u201d<\/b><b>)\u00a0<\/b>\n\n<b>\u00a0<\/b><b>if <\/b><b>x &gt; <\/b><b>30<\/b><b>:\u00a0<\/b>\n\n<b>\u00a0print(<\/b><b>\u201cand also above 30!\u201d<\/b><b>)\u00a0<\/b>\n\n<h2 id=\"mcetoc_1eqia9ketl\"><b>Elif Statements\u00a0<\/b><\/h2>\n\n<b>elif <\/b><span style=\"font-weight: 400\">keyword prompts your program to try another condition if the previous one(s)\u00a0 was not true. Here\u2019s an example:\u00a0<\/span>\n\n<b>a = <\/b><b>45\u00a0<\/b>\n\n<b>b = <\/b><b>45\u00a0<\/b>\n\n<b>if <\/b><b>b &gt; a:\u00a0<\/b>\n\n<b>\u00a0print(<\/b><b>\u201cb is greater than a\u201d<\/b><b>)\u00a0<\/b>\n\n<b>elif <\/b><b>a == b:\u00a0<\/b>\n\n<b>\u00a0print(<\/b><b>\u201ca and b are equal\u201d<\/b><b>)\u00a0<\/b>\n\n<h2 id=\"mcetoc_1eqia9r6am\"><b>If Else Statements\u00a0<\/b><\/h2>\n\n<b>else <\/b><span style=\"font-weight: 400\">keyword helps you add some additional filters to your condition clause. Here\u2019s\u00a0 how an if-elif-else combo looks:\u00a0<\/span>\n\n<b>if <\/b><b>age &lt; <\/b><b>4<\/b><b>:\u00a0\u00a0<\/b>\n\n<b>ticket_price = <\/b><b>0\u00a0<\/b>\n\n<b>elif <\/b><b>age &lt; <\/b><b>18<\/b><b>:\u00a0\u00a0<\/b>\n\n<b>ticket_price = <\/b><b>10\u00a0<\/b>\n\n<b>else<\/b><b>: ticket_price = <\/b><b>15\u00a0<\/b>\n\n<h2 id=\"mcetoc_1eqia9vo2n\"><b>If-Not-Statements\u00a0<\/b><\/h2>\n\n<b>Not <\/b><span style=\"font-weight: 400\">keyword let\u2019s you check for the opposite meaning to verify whether the value is\u00a0 NOT True:\u00a0\u00a0<\/span>\n\n<b>new_list = [<\/b><b>1<\/b><b>, <\/b><b>2<\/b><b>, <\/b><b>3<\/b><b>, <\/b><b>4<\/b><b>]\u00a0<\/b>\n\n<b>x = <\/b><b>10\u00a0<\/b>\n\n<b>if <\/b><b>x <\/b><b>not in <\/b><b>new_list:\u00a0<\/b>\n\n<b>\u00a0print(<\/b><b>\u201c\u2019x\u2019 isn\u2019t on the list, so this is True!\u201d<\/b><b>)\u00a0<\/b>\n\n<h2 id=\"mcetoc_1eqiaa57jo\"><b>Pass Statements\u00a0<\/b><\/h2>\n\n<span style=\"font-weight: 400\">If statements can\u2019t be empty. But if that\u2019s your case, add the <\/span><b>pass <\/b><span style=\"font-weight: 400\">statement to avoid\u00a0 having an error:\u00a0\u00a0<\/span>\n\n<b>a = <\/b><b>33\u00a0<\/b>\n\n<b>b = <\/b><b>200\u00a0<\/b>\n\n<b>if <\/b><b>b &gt; a:\u00a0<\/b>\n\n<b>\u00a0<\/b><b>pass\u00a0<\/b>\n\n<h1 id=\"mcetoc_1eqiaam58p\"><b>Python Loops\u00a0<\/b><\/h1>\n\n<span style=\"font-weight: 400\">Python has two simple loop commands that are good to know:\u00a0<\/span>\n\n<ul>\n    <li><span style=\"font-weight: 400\"> for loops\u00a0\u00a0<\/span><\/li>\n    <li><span style=\"font-weight: 400\"> while loops\u00a0\u00a0<\/span><\/li>\n<\/ul>\n\n<span style=\"font-weight: 400\">Let\u2019s take a look at each of these.\u00a0<\/span>\n\n<h2 id=\"mcetoc_1eqiaavq5q\"><b>For Loop\u00a0<\/b><\/h2>\n\n<span style=\"font-weight: 400\">As already illustrated in the other sections of this Python checklist, <\/span><b>for loop <\/b><span style=\"font-weight: 400\">is a\u00a0 handy way for iterating over a sequence such as a list, tuple, dictionary, string, etc.\u00a0\u00a0<\/span>\n\n<span style=\"font-weight: 400\">Here\u2019s an example showing how to loop through a string:\u00a0<\/span>\n\n<b>for <\/b><b>x <\/b><b>in <\/b><b>\u201capple\u201d<\/b><b>:\u00a0<\/b>\n\n<b>\u00a0print(x)\u00a0<\/b>\n\n<span style=\"font-weight: 400\">Plus, you\u2019ve already seen other examples for lists and dictionaries.\u00a0<\/span>\n\n<h2 id=\"mcetoc_1eqiab5epr\"><b>While Loops\u00a0<\/b><\/h2>\n\n<span style=\"font-weight: 400\">While loop enables you to execute a set of statements as long as the condition for\u00a0 them is true.\u00a0<\/span>\n\n<b><i>#print as long as x is less than 8\u00a0\u00a0<\/i><\/b>\n\n<b>i = <\/b><b>1\u00a0<\/b>\n\n<b>while <\/b><b>i&lt; <\/b><b>8<\/b><b>:\u00a0<\/b>\n\n<b>\u00a0print(x)\u00a0<\/b>\n\n<b>\u00a0i += <\/b><b>1\u00a0<\/b>\n\n<b>How to Break a Loop\u00a0<\/b>\n\n<span style=\"font-weight: 400\">You can also stop the loop from running even if the condition is met. For that, use\u00a0 the break statement both in while and for loops:\u00a0<\/span>\n\n<b>i = <\/b><b>1\u00a0<\/b>\n\n<b>while <\/b><b>i &lt; <\/b><b>8<\/b><b>:\u00a0<\/b>\n\n<b>\u00a0print(i)\u00a0<\/b>\n\n<b>\u00a0<\/b><b>if <\/b><b>i == <\/b><b>4<\/b><b>:\u00a0<\/b>\n\n<b>\u00a0<\/b><b>break\u00a0<\/b>\n\n<b>\u00a0<\/b><b>i += <\/b><b>1\u00a0<\/b>\n\n<h1 id=\"mcetoc_1eqiabgkgs\"><b>Class\u00a0<\/b><\/h1>\n\n<span style=\"font-weight: 400\">Since Python is an object-oriented programming language almost every element of\u00a0 it is an <\/span><b>object <\/b><span style=\"font-weight: 400\">\u2014 with its methods and properties.\u00a0\u00a0<\/span>\n\n<b>Class <\/b><span style=\"font-weight: 400\">acts as a blueprint for creating different objects. <\/span><b>Objects <\/b><span style=\"font-weight: 400\">are an instance of a\u00a0 class, where the class is manifested in some program.\u00a0<\/span>\n\n<h2 id=\"mcetoc_1eqiabm9rt\"><b>How to Create a Class\u00a0<\/b><\/h2>\n\n<span style=\"font-weight: 400\">Let\u2019s create a class named TestClass, with one property named z:\u00a0<\/span>\n\n<b>class <\/b><b>TestClass<\/b><b>:\u00a0<\/b>\n\n<b>\u00a0z = <\/b><b>5\u00a0<\/b>\n\n<h2 id=\"mcetoc_1eqiabptju\"><b>How To Create an Object\u00a0<\/b><\/h2>\n\n<span style=\"font-weight: 400\">As a next step, you can create an object using your class. Here\u2019s how it\u2019s done:\u00a0<\/span>\n\n<b>p1 = TestClass()\u00a0<\/b>\n\n<b>print(p1.x)\u00a0<\/b>\n\n<span style=\"font-weight: 400\">Further, you can assign different attributes and methods to your object. The\u00a0 example is below:\u00a0<\/span>\n\n<b>class <\/b><b>car<\/b><b>(object):\u00a0<\/b>\n\n<b>\u00a0<\/b><b>\u201c\u201d\u201ddocstring\u201d\u201d\u201d\u00a0<\/b>\n\n<b>\u00a0<\/b><b>def <\/b><b>__init__<\/b><b>(self, color, doors, tires):\u00a0<\/b>\n\n<b>\u00a0<\/b><b>\u201c\u201d\u201dConstructor\u201d\u201d\u201d\u00a0<\/b>\n\n<b>\u00a0<\/b><b>self.color = color\u00a0<\/b>\n\n<b>\u00a0self.doors = doors\u00a0<\/b>\n\n<b>\u00a0self.tires = tires\u00a0<\/b>\n\n<b>\u00a0<\/b><b>def <\/b><b>brake<\/b><b>(self):\u00a0<\/b>\n\n<b>\u00a0<\/b><b>\u201c\u201d\u201d\u00a0<\/b>\n\n<b>\u00a0Stop the car\u00a0<\/b>\n\n<b>\u00a0\u201c\u201d\u201d\u00a0<\/b>\n\n<b>\u00a0<\/b><b>return <\/b><b>\u201cBraking\u201d\u00a0<\/b>\n\n<b>\u00a0<\/b><b>def <\/b><b>drive<\/b><b>(self):\u00a0<\/b>\n\n<b>\u00a0<\/b><b>\u201c\u201d\u201d\u00a0<\/b>\n\n<b>\u00a0Drive the car\u00a0<\/b>\n\n<b>\u00a0\u201c\u201d\u201d\u00a0<\/b>\n\n<b>\u00a0<\/b><b>return <\/b><b>\u201cI\u2019m driving!\u201d\u00a0<\/b>\n\n<h2 id=\"mcetoc_1eqiac5b7v\"><b>How to Create a Subclass\u00a0<\/b><\/h2>\n\n<span style=\"font-weight: 400\">Every object can be further sub-classified. Here\u2019s an example\u00a0<\/span>\n\n<b>class <\/b><b>Car<\/b><b>(Vehicle):\u00a0<\/b>\n\n<b>\u00a0<\/b><b>\u201c\u201d\u201d\u00a0<\/b>\n\n<b>\u00a0The Car class\u00a0<\/b>\n\n<b>\u00a0\u201c\u201d\u201d\u00a0<\/b>\n\n<b>\u00a0<\/b><b>def <\/b><b>brake<\/b><b>(self):\u00a0<\/b>\n\n<b>\u00a0<\/b><b>\u201c\u201d\u201d\u00a0<\/b>\n\n<b>\u00a0Override brake method\u00a0<\/b>\n\n<b>\u00a0\u201c\u201d\u201d\u00a0<\/b>\n\n<b>\u00a0<\/b><b>return <\/b><b>\u201cThe car class is breaking slowly!\u201d\u00a0<\/b>\n\n<b>if <\/b><b>__name__ == <\/b><b>\u201c<strong>main<\/strong>\u201d<\/b><b>:\u00a0<\/b>\n\n<b>\u00a0car = Car(<\/b><b>\u201cyellow\u201d<\/b><b>, <\/b><b>2<\/b><b>, <\/b><b>4<\/b><b>, <\/b><b>\u201ccar\u201d<\/b><b>)\u00a0<\/b>\n\n<b>\u00a0car.brake()\u00a0<\/b>\n\n<b>\u00a0<\/b><b>\u2018The car class is breaking slowly!\u2019\u00a0<\/b>\n\n<b>\u00a0<\/b><b>car.drive()\u00a0<\/b>\n\n<b>\u00a0<\/b><b>\u201cI\u2019m driving a yellow car!\u201d\u00a0<\/b>\n\n<h1 id=\"mcetoc_1eqiadm0p10\"><b>Dealing with Python Exceptions (Errors)\u00a0<\/b><\/h1>\n\n<span style=\"font-weight: 400\">Python has a list of in-built exceptions (errors) that will pop up whenever you make\u00a0 a mistake in your code. As a newbie, it\u2019s good to know how to fix these.\u00a0<\/span>\n\n<h2 id=\"mcetoc_1eqiadqli11\"><b>The Most Common Python Exceptions\u00a0<\/b><\/h2>\n\n<ul>\n    <li><span style=\"font-weight: 400\"> AttributeError <\/span><span style=\"font-weight: 400\">\u2014 pops up when an attribute reference or assignment fails.\u00a0<\/span><\/li>\n    <li><span style=\"font-weight: 400\"> IOError <\/span><span style=\"font-weight: 400\">\u2014 emerges when some I\/O operation (e.g. an <\/span><span style=\"font-weight: 400\">open() <\/span><span style=\"font-weight: 400\">function) fails\u00a0 for an I\/O-related reason, e.g., \u201cfile not found\u201d or \u201cdisk full\u201d.\u00a0<\/span><\/li>\n    <li><span style=\"font-weight: 400\"> ImportError <\/span><span style=\"font-weight: 400\">\u2014 comes up when an import statement cannot locate the\u00a0 module definition. Also, when a from\u2026 import can\u2019t find a name that must be\u00a0 imported.\u00a0<\/span><\/li>\n    <li><span style=\"font-weight: 400\"> IndexError <\/span><span style=\"font-weight: 400\">\u2014 emerges when a sequence subscript is out of range. <\/span><span style=\"font-weight: 400\">\u2022 KeyError <\/span><span style=\"font-weight: 400\">\u2014 raised when a dictionary key isn\u2019t found in the set of existing keys.\u00a0<\/span><\/li>\n    <li><span style=\"font-weight: 400\"> KeyboardInterrupt <\/span><span style=\"font-weight: 400\">\u2014 lights up when the user hits the interrupt key (such\u00a0 as Control-C or Delete).\u00a0<\/span><\/li>\n    <li><span style=\"font-weight: 400\"> NameError <\/span><span style=\"font-weight: 400\">\u2014 shows up when a local or global name can\u2019t be found.<\/span><\/li>\n    <li><span style=\"font-weight: 400\">OSError <\/span><span style=\"font-weight: 400\">\u2014 indicated a system-related error.<\/span><\/li>\n    <li><span style=\"font-weight: 400\">SyntaxError <\/span><span style=\"font-weight: 400\">\u2014 pops up when a parser encounters a syntax error.\u00a0<\/span><\/li>\n    <li><span style=\"font-weight: 400\"> TypeError <\/span><span style=\"font-weight: 400\">\u2014 comes up when an operation or function is applied to an object\u00a0 of inappropriate type.\u00a0\u00a0<\/span><\/li>\n    <li><span style=\"font-weight: 400\"> ValueError <\/span><span style=\"font-weight: 400\">\u2014 raised when a built-in operation\/function gets an argument\u00a0 that has the right type but not an appropriate value, and the situation is not\u00a0 described by a more precise exception such as IndexError.\u00a0<\/span><\/li>\n    <li><span style=\"font-weight: 400\"> ZeroDivisionError <\/span><span style=\"font-weight: 400\">\u2014 emerges when the second argument of a division or\u00a0 modulo operation is zero.\u00a0<\/span><\/li>\n<\/ul>\n\n<h2 id=\"mcetoc_1eqiaebhp12\"><b>How to Troubleshoot the Errors\u00a0<\/b><\/h2>\n\n<span style=\"font-weight: 400\">Python has a useful statement, design just for the purpose of handling exceptions <\/span><span style=\"font-weight: 400\">\u2014\u00a0 <\/span><b>try\/except <\/b><span style=\"font-weight: 400\">statement. Here\u2019s a code snippet showing how you can catch KeyErrors\u00a0 in a dictionary using this statement:\u00a0\u00a0<\/span>\n\n<b>my_dict = {<\/b><b>\u201ca\u201d<\/b><b>:<\/b><b>1<\/b><b>, <\/b><b>\u201cb\u201d<\/b><b>:<\/b><b>2<\/b><b>, <\/b><b>\u201cc\u201d<\/b><b>:<\/b><b>3<\/b><b>}\u00a0<\/b>\n\n<b>try<\/b><b>:\u00a0<\/b>\n\n<b>\u00a0value = my_dict[<\/b><b>\u201cd\u201d<\/b><b>]\u00a0<\/b>\n\n<b>\u00a0<\/b><b>except <\/b><b>KeyError:\u00a0<\/b>\n\n<b>\u00a0print(<\/b><b>\u201cThat key does not exist!\u201d<\/b><b>)\u00a0<\/b>\n\n<span style=\"font-weight: 400\">You can also detect several exceptions at once with a single statement. Here\u2019s an\u00a0 example for that:\u00a0\u00a0<\/span>\n\n<b>my_dict = {<\/b><b>\u201ca\u201d<\/b><b>:<\/b><b>1<\/b><b>, <\/b><b>\u201cb\u201d<\/b><b>:<\/b><b>2<\/b><b>, <\/b><b>\u201cc\u201d<\/b><b>:<\/b><b>3<\/b><b>}\u00a0<\/b>\n\n<b>try<\/b><b>:\u00a0<\/b>\n\n<b>\u00a0value = my_dict[<\/b><b>\u201cd\u201d<\/b><b>]\u00a0<\/b>\n\n<b>except <\/b><b>IndexError:\u00a0<\/b>\n\n<b>\u00a0print(<\/b><b>\u201cThis index does not exist!\u201d<\/b><b>)\u00a0<\/b>\n\n<b>except <\/b><b>KeyError:\u00a0<\/b>\n\n<b>\u00a0print(<\/b><b>\u201cThis key is not in the dictionary!\u201d<\/b><b>) <\/b><b>except<\/b><b>:\u00a0<\/b>\n\n<b>\u00a0print(<\/b><b>\u201cSome other problem happened!\u201d<\/b><b>)\u00a0<\/b>\n\n<h2 id=\"mcetoc_1eqiaenj913\"><b>try\/except with else clause\u00a0<\/b><\/h2>\n\n<span style=\"font-weight: 400\">Adding an else clause will help you confirm that no errors\u00a0 were found:\u00a0<\/span>\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\">my_dict = {\u201ca\u201d:1, \u201cb\u201d:2, \u201cc\u201d:3}\u00a0\ntry:\u00a0\n\u00a0value = my_dict[\u201ca\u201d]\u00a0\nexcept KeyError:\u00a0\n\u00a0print(\u201cA KeyError occurred!\u201d)\u00a0\nelse:\u00a0\n\u00a0print(\u201cNo error occurred!\u201d)<\/pre>\n\n<h1 id=\"mcetoc_1eqiaf5ig14\"><b>Conclusions<\/b><\/h1>\n\n&nbsp;","protected":false},"excerpt":{"rendered":"<p>Python 3 is a truly versatile programmin &hellip; <\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_import_markdown_pro_load_document_selector":0,"_import_markdown_pro_submit_text_textarea":"","fifu_image_url":"","fifu_image_alt":"","footnotes":""},"categories":[266],"tags":[],"class_list":["post-12207","post","type-post","status-publish","format-standard","hentry","category-266"],"_links":{"self":[{"href":"https:\/\/fgchen.com\/wpedu\/wp-json\/wp\/v2\/posts\/12207","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/fgchen.com\/wpedu\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/fgchen.com\/wpedu\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/fgchen.com\/wpedu\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/fgchen.com\/wpedu\/wp-json\/wp\/v2\/comments?post=12207"}],"version-history":[{"count":1,"href":"https:\/\/fgchen.com\/wpedu\/wp-json\/wp\/v2\/posts\/12207\/revisions"}],"predecessor-version":[{"id":13683,"href":"https:\/\/fgchen.com\/wpedu\/wp-json\/wp\/v2\/posts\/12207\/revisions\/13683"}],"wp:attachment":[{"href":"https:\/\/fgchen.com\/wpedu\/wp-json\/wp\/v2\/media?parent=12207"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fgchen.com\/wpedu\/wp-json\/wp\/v2\/categories?post=12207"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fgchen.com\/wpedu\/wp-json\/wp\/v2\/tags?post=12207"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}