{
  "intro": {
    "title": "Python Introduction",
    "blocks": [
      {
        "type": "header",
        "level": 2,
        "text": "What is Python?"
      },
      {
        "type": "paragraph",
        "text": "Python is a popular programming language. It was created by Guido van Rossum, \nand released in 1991."
      },
      {
        "type": "paragraph",
        "text": "It is used for:"
      },
      {
        "type": "list",
        "ordered": false,
        "items": [
          "web development (server-side),",
          "software development,",
          "mathematics,",
          "system scripting."
        ]
      },
      {
        "type": "header",
        "level": 3,
        "text": "What can Python do?"
      },
      {
        "type": "list",
        "ordered": false,
        "items": [
          "Python can be used on a server to create web applications.",
          "Python can be used alongside software to create workflows.",
          "Python can connect to database systems. It can also read and modify files.",
          "Python can be used to handle big data and perform complex mathematics.",
          "Python can be used for rapid prototyping, or for production-ready software development."
        ]
      },
      {
        "type": "header",
        "level": 2,
        "text": "Why Python?"
      },
      {
        "type": "list",
        "ordered": false,
        "items": [
          "Python works on different platforms (Windows, Mac, Linux, Raspberry Pi, etc).",
          "Python has a simple syntax similar to the English language.",
          "Python has syntax that allows developers to write programs with fewer lines than some other programming languages.",
          "Python runs on an interpreter system, meaning that code can be executed as soon as it is written. This means that prototyping can be very quick.",
          "Python can be treated in a procedural way, an object-oriented way or a functional way."
        ]
      },
      {
        "type": "header",
        "level": 3,
        "text": "Good to know"
      },
      {
        "type": "list",
        "ordered": false,
        "items": [
          "The most recent major version of Python is Python 3, which we shall be using in this tutorial.",
          "In this tutorial Python will be written in a text editor. It is possible to write Python in an Integrated Development Environment, such as Thonny, Pycharm, Netbeans or Eclipse which are particularly useful when managing larger collections of Python files."
        ]
      },
      {
        "type": "header",
        "level": 3,
        "text": "Python Syntax compared to other programming languages"
      },
      {
        "type": "list",
        "ordered": false,
        "items": [
          "Python was designed for readability, and has some similarities to the English language with influence from mathematics.",
          "Python uses new lines to complete a command, as opposed to other programming languages which often use semicolons or parentheses.",
          "Python relies on indentation, using whitespace, to define scope; such as the scope of loops, functions and classes. Other programming languages often use curly-brackets for this purpose."
        ]
      },
      {
        "type": "example",
        "title": "Example",
        "code": "print(\"Hello, World!\")"
      }
    ]
  },
  "get-started": {
    "title": "Python Getting Started",
    "blocks": [
      {
        "type": "header",
        "level": 2,
        "text": "Get Started With Python"
      },
      {
        "type": "paragraph",
        "text": "At Coding Tamilan, you can try Python without installing anything."
      },
      {
        "type": "paragraph",
        "text": "Our Online Python Editor runs directly in your browser, and shows both the code and the result:"
      },
      {
        "type": "example",
        "title": "Example",
        "code": "print(\"Hello, World!\")"
      },
      {
        "type": "paragraph",
        "text": "This editor will be used in the entire tutorial to demonstrate the different aspects of Python."
      },
      {
        "type": "header",
        "level": 2,
        "text": "Python Install"
      },
      {
        "type": "paragraph",
        "text": "However, if you want to run Python on your own computer, follow the instructions below."
      },
      {
        "type": "paragraph",
        "text": "Many Windows PCs and Mac computers already have Python pre-installed."
      },
      {
        "type": "paragraph",
        "text": "To check if Python is installed on Windows, search in the start bar for Python or run the following on the Command Line (cmd.exe):"
      },
      {
        "type": "example",
        "title": "Example",
        "code": "C:\\Users\\Your Name>python --version"
      },
      {
        "type": "paragraph",
        "text": "To check if you have python installed on a Linux or Mac, then on linux open the command line or on Mac open the Terminal and type:"
      },
      {
        "type": "example",
        "title": "Example",
        "code": "python --version"
      },
      {
        "type": "paragraph",
        "text": "If Python is not installed on your computer, you can download it for free from the official website: <a href=\"https://www.python.org/\" target=\"_blank\">https://www.python.org/</a>"
      },
      {
        "type": "header",
        "level": 2,
        "text": "Python Quickstart"
      },
      {
        "type": "paragraph",
        "text": "Python is an interpreted programming language, this means that as a developer you write Python (.py) files in a text editor and then put those files into the python interpreter to be executed."
      },
      {
        "type": "paragraph",
        "text": "Let's write our first Python file, called <code class=\"w3-codespan\">hello.py</code>, which can be done in any text editor:"
      },
      {
        "type": "paragraph",
        "text": "Simple as that. Save your file. Open your command line, navigate to the directory where you saved your file, and run:"
      },
      {
        "type": "example",
        "title": "Example",
        "code": "C:\\Users\\Your Name>python hello.py"
      },
      {
        "type": "paragraph",
        "text": "The output should be:"
      },
      {
        "type": "example",
        "title": "Example",
        "code": "Hello, World!"
      },
      {
        "type": "paragraph",
        "text": "Congratulations, you have written and executed your first Python program."
      },
      {
        "type": "header",
        "level": 2,
        "text": "Python Version"
      },
      {
        "type": "paragraph",
        "text": "To check the Python version of the editor, you can find it by importing the <code class=\"w3-codespan\">sys</code> module:"
      },
      {
        "type": "example",
        "title": "Example",
        "code": "import sys\n\nprint(sys.version)"
      },
      {
        "type": "paragraph",
        "text": "You will learn more about importing modules in our \n<a href=\"#/python/modules\">Python Modules</a> chapter."
      },
      {
        "type": "header",
        "level": 2,
        "text": "The Python Command Line"
      },
      {
        "type": "paragraph",
        "text": "To test a short amount of code in python sometimes it is quickest and easiest not to write the code in a file. This is made possible because Python can be run as a command line itself."
      },
      {
        "type": "paragraph",
        "text": "Type the following on the Windows, Mac or Linux command line:"
      },
      {
        "type": "example",
        "title": "Example",
        "code": "C:\\Users\\Your Name>python"
      },
      {
        "type": "example",
        "title": "Example",
        "code": "C:\\Users\\Your Name>py"
      },
      {
        "type": "paragraph",
        "text": "From there you can write any python code, including our hello world example from earlier in the tutorial:"
      },
      {
        "type": "example",
        "title": "Example",
        "code": "C:\\Users\\Your Name>python\nPython 3.6.4 (v3.6.4:d48eceb, Dec 19 2017, 06:04:45) [MSC v.1900 32 bit (Intel)] on win32\nType \"help\", \"copyright\", \"credits\" or \"license\" for more information.\n>>> print(\"Hello, World!\")"
      },
      {
        "type": "paragraph",
        "text": "Which will write \"Hello, World!\" in the command line:"
      },
      {
        "type": "example",
        "title": "Example",
        "code": "C:\\Users\\Your Name>python\nPython 3.6.4 (v3.6.4:d48eceb, Dec 19 2017, 06:04:45) [MSC v.1900 32 bit (Intel)] on win32\nType \"help\", \"copyright\", \"credits\" or \"license\" for more information.\n>>> print(\"Hello, World!\")\nHello, World!"
      },
      {
        "type": "paragraph",
        "text": "Whenever you are done in the python command line, you can simply type the following to quit the python command line interface:"
      },
      {
        "type": "example",
        "title": "Example",
        "code": "exit()"
      }
    ]
  },
  "syntax": {
    "title": "Python Syntax",
    "blocks": [
      {
        "type": "header",
        "level": 2,
        "text": "Execute Python Syntax"
      },
      {
        "type": "paragraph",
        "text": "As we learned in the previous page, Python syntax can be executed by writing directly in the Command Line:"
      },
      {
        "type": "example",
        "title": "Example",
        "code": ">>> print(\"Hello, World!\")\n    Hello, World!"
      },
      {
        "type": "paragraph",
        "text": "Or by creating a python file on the server, using the .py file extension, and running it in the Command Line:"
      },
      {
        "type": "example",
        "title": "Example",
        "code": "C:\\Users\\Your Name>python myfile.py"
      },
      {
        "type": "header",
        "level": 2,
        "text": "Python Indentation"
      },
      {
        "type": "paragraph",
        "text": "Indentation refers to the spaces at the beginning of a code line."
      },
      {
        "type": "paragraph",
        "text": "Where in other programming languages the indentation in code is for readability \nonly, the indentation in Python is very important."
      },
      {
        "type": "paragraph",
        "text": "Python uses indentation to indicate a block of code."
      },
      {
        "type": "example",
        "title": "Example",
        "code": "if 5 > 2: \nprint(\"Five is greater than two!\")"
      },
      {
        "type": "paragraph",
        "text": "Python will give you an error if you skip the indentation:"
      },
      {
        "type": "example",
        "title": "Example",
        "code": "if 5 > 2:\nprint(\"Five is greater than two!\")"
      },
      {
        "type": "paragraph",
        "text": "The number of spaces is up to you as a programmer, the most common use is four, but it has \nto be at least one."
      },
      {
        "type": "example",
        "title": "Example",
        "code": "if 5 > 2: print(\"Five is greater than two!\") \nif 5 > 2:        print(\"Five is greater than two!\")"
      },
      {
        "type": "paragraph",
        "text": "You have to use the same number of spaces in the same block of code, \notherwise Python will give you an error:"
      },
      {
        "type": "example",
        "title": "Example",
        "code": "if 5 > 2:\n print(\"Five is greater than two!\")\n        print(\"Five is greater than \n  two!\")"
      },
      {
        "type": "header",
        "level": 2,
        "text": "Python Variables"
      },
      {
        "type": "paragraph",
        "text": "In Python, variables are created when you assign a value to it:"
      },
      {
        "type": "example",
        "title": "Example",
        "code": "x = 5y = \"Hello, World!\""
      },
      {
        "type": "paragraph",
        "text": "Python has no command for declaring a variable."
      },
      {
        "type": "paragraph",
        "text": "You will learn more about variables in the <a href=\"#/python/variables\">\nPython Variables</a> chapter."
      },
      {
        "type": "header",
        "level": 2,
        "text": "Comments"
      },
      {
        "type": "paragraph",
        "text": "Python has commenting capability for the purpose of in-code documentation."
      },
      {
        "type": "paragraph",
        "text": "Comments start with a <code class=\"w3-codespan\">#</code>, and Python will render the rest of the line as a comment:"
      },
      {
        "type": "example",
        "title": "Example",
        "code": "#This is a comment.\nprint(\"Hello, World!\")"
      }
    ]
  },
  "comments": {
    "title": "Python Comments",
    "blocks": [
      {
        "type": "intro",
        "text": "Comments can be used to explain Python code."
      },
      {
        "type": "intro",
        "text": "Comments can be used to make the code more readable."
      },
      {
        "type": "intro",
        "text": "Comments can be used to prevent execution when testing code."
      },
      {
        "type": "header",
        "level": 2,
        "text": "Creating a Comment"
      },
      {
        "type": "paragraph",
        "text": "Comments starts with a <code class=\"w3-codespan\">#</code>, and Python will \nignore them:"
      },
      {
        "type": "example",
        "title": "Example",
        "code": "#This is a comment\nprint(\"Hello, World!\")"
      },
      {
        "type": "paragraph",
        "text": "Comments can be placed at the end of a line, and Python will ignore the rest \nof the line:"
      },
      {
        "type": "example",
        "title": "Example",
        "code": "print(\"Hello, World!\") #This is a comment"
      },
      {
        "type": "paragraph",
        "text": "A comment does not have to be text that explains the code, it can also be used to \nprevent Python from executing code:"
      },
      {
        "type": "example",
        "title": "Example",
        "code": "#print(\"Hello, World!\")print(\"Cheers, Mate!\")"
      },
      {
        "type": "header",
        "level": 2,
        "text": "Multiline Comments"
      },
      {
        "type": "paragraph",
        "text": "Python does not really have a syntax for multiline comments."
      },
      {
        "type": "paragraph",
        "text": "To add a multiline comment you could insert a <code class=\"w3-codespan\">#</code> for each line:"
      },
      {
        "type": "example",
        "title": "Example",
        "code": "#This is a comment#written in#more than just one lineprint(\"Hello, \n  World!\")"
      },
      {
        "type": "paragraph",
        "text": "Or, not quite as intended, you can use a multiline string."
      },
      {
        "type": "paragraph",
        "text": "Since Python will ignore string literals that are not assigned to a variable, you can add a multiline string (triple quotes) in your code, and place your comment inside it:"
      },
      {
        "type": "example",
        "title": "Example",
        "code": "\"\"\"This is a commentwritten in more than just \n  one line\"\"\"print(\"Hello, World!\")"
      },
      {
        "type": "paragraph",
        "text": "As long as the string is not assigned to a variable, Python will read the code, but then ignore it, and you have made a multiline comment."
      }
    ]
  },
  "variables": {
    "title": "Python Variables",
    "blocks": [
      {
        "type": "header",
        "level": 2,
        "text": "Variables"
      },
      {
        "type": "paragraph",
        "text": "Variables are containers for storing data values."
      },
      {
        "type": "header",
        "level": 2,
        "text": "Creating Variables"
      },
      {
        "type": "paragraph",
        "text": "Python has no command for declaring a variable."
      },
      {
        "type": "paragraph",
        "text": "A variable is created the moment you first assign a value to it."
      },
      {
        "type": "example",
        "title": "Example",
        "code": "x = 5\ny = \"John\"\nprint(x)\nprint(y)"
      },
      {
        "type": "paragraph",
        "text": "Variables do not need to be declared with any particular <em>type</em>, and can even change type after they have been set."
      },
      {
        "type": "example",
        "title": "Example",
        "code": "x = 4       # x is of type int\nx = \"Sally\" # x is now of type str\nprint(x)"
      },
      {
        "type": "header",
        "level": 2,
        "text": "Casting"
      },
      {
        "type": "paragraph",
        "text": "If you want to specify the data type of a variable, this can be done with casting."
      },
      {
        "type": "example",
        "title": "Example",
        "code": "x = \n  str(3)    # x will be '3'y = int(3)    # y \n  will be 3z = float(3)  # z will be 3.0"
      },
      {
        "type": "header",
        "level": 2,
        "text": "Get the Type"
      },
      {
        "type": "paragraph",
        "text": "You can get the data type of a variable with the <code class=\"w3-codespan\"><a href=\"ref_func_type.html\">type()</a></code> function."
      },
      {
        "type": "example",
        "title": "Example",
        "code": "x = 5y = \"John\"print(type(x))print(type(y))"
      },
      {
        "type": "note",
        "text": "You will learn more about \n  <a href=\"#/python/data-types\">data types</a> and\n  <a href=\"#/python/casting\">casting</a> later in this tutorial."
      },
      {
        "type": "header",
        "level": 2,
        "text": "Single or Double Quotes?"
      },
      {
        "type": "paragraph",
        "text": "String variables can be declared either by using single or double quotes:"
      },
      {
        "type": "example",
        "title": "Example",
        "code": "x = \"John\"# is the same asx = \n  'John'"
      },
      {
        "type": "header",
        "level": 2,
        "text": "Case-Sensitive"
      },
      {
        "type": "paragraph",
        "text": "Variable names are case-sensitive."
      },
      {
        "type": "example",
        "title": "Example",
        "code": "a = 4A = \n  \"Sally\"#A will not overwrite a"
      }
    ]
  },
  "data-types": {
    "title": "Python Data Types",
    "blocks": [
      {
        "type": "header",
        "level": 2,
        "text": "Built-in Data Types"
      },
      {
        "type": "paragraph",
        "text": "In programming, data type is an important concept."
      },
      {
        "type": "paragraph",
        "text": "Variables can store data of different types, and different types can do \ndifferent things."
      },
      {
        "type": "paragraph",
        "text": "Python has the following data types built-in by default, in these categories:"
      },
      {
        "type": "table",
        "headers": [],
        "rows": [
          [
            "Text Type:",
            "<code class=\"w3-codespan\">str</code>"
          ],
          [
            "Numeric Types:",
            "<code class=\"w3-codespan\">int</code>, <code class=\"w3-codespan\">float</code>,\n    <code class=\"w3-codespan\">complex</code>"
          ],
          [
            "Sequence Types:",
            "<code class=\"w3-codespan\">list</code>, <code class=\"w3-codespan\">tuple</code>, \n    <code class=\"w3-codespan\">range</code>"
          ],
          [
            "Mapping Type:",
            "<code class=\"w3-codespan\">dict</code>"
          ],
          [
            "Set Types:",
            "<code class=\"w3-codespan\">set</code>, <code class=\"w3-codespan\">frozenset</code>"
          ],
          [
            "Boolean Type:",
            "<code class=\"w3-codespan\">bool</code>"
          ],
          [
            "Binary Types:",
            "<code class=\"w3-codespan\">bytes</code>, <code class=\"w3-codespan\">bytearray</code>, \n    <code class=\"w3-codespan\">memoryview</code>"
          ],
          [
            "None Type:",
            "<code class=\"w3-codespan\">NoneType</code>"
          ]
        ]
      },
      {
        "type": "header",
        "level": 2,
        "text": "Getting the Data Type"
      },
      {
        "type": "paragraph",
        "text": "You can get the data type of any object by using the <code class=\"w3-codespan\"><a href=\"ref_func_type.html\">type()</a></code> function:"
      },
      {
        "type": "example",
        "title": "Example",
        "code": "x = 5\nprint(type(x))"
      },
      {
        "type": "header",
        "level": 2,
        "text": "Setting the Data Type"
      },
      {
        "type": "paragraph",
        "text": "In Python, the data type is set when you assign a value to a variable:"
      },
      {
        "type": "header",
        "level": 2,
        "text": "Setting the Specific Data Type"
      },
      {
        "type": "paragraph",
        "text": "If you want to specify the data type, you can use the following \nconstructor functions:"
      }
    ]
  },
  "numbers": {
    "title": "Python Numbers",
    "blocks": [
      {
        "type": "header",
        "level": 2,
        "text": "Python Numbers"
      },
      {
        "type": "paragraph",
        "text": "There are three numeric types in Python:"
      },
      {
        "type": "list",
        "ordered": false,
        "items": [
          "<code class=\"w3-codespan\">int</code>",
          "<code class=\"w3-codespan\">float</code>",
          "<code class=\"w3-codespan\">complex</code>"
        ]
      },
      {
        "type": "paragraph",
        "text": "Variables of numeric types are created when you assign a value to them:"
      },
      {
        "type": "example",
        "title": "Example",
        "code": "x = 1    \n  # inty = 2.8  # floatz = 1j   # complex"
      },
      {
        "type": "paragraph",
        "text": "To verify the type of any object in Python, use the <code class=\"w3-codespan\"><a href=\"ref_func_type.html\">type()</a></code> function:"
      },
      {
        "type": "example",
        "title": "Example",
        "code": "print(type(x))print(type(y))print(type(z))"
      },
      {
        "type": "header",
        "level": 2,
        "text": "Int"
      },
      {
        "type": "paragraph",
        "text": "Int, or integer, is a whole number, \npositive or negative, without decimals, of unlimited length."
      },
      {
        "type": "example",
        "title": "Example",
        "code": "x = 1y = 35656222554887711z = \n  -3255522print(type(x))print(type(y))print(type(z))"
      },
      {
        "type": "header",
        "level": 2,
        "text": "Float"
      },
      {
        "type": "paragraph",
        "text": "Float, or \"floating point number\" is a number, positive or negative, containing one or more decimals."
      },
      {
        "type": "example",
        "title": "Example",
        "code": "x = 1.10y = 1.0z = -35.59print(type(x))print(type(y))print(type(z))"
      },
      {
        "type": "paragraph",
        "text": "Float can also be scientific numbers with an \"e\" to indicate the power of 10."
      },
      {
        "type": "example",
        "title": "Example",
        "code": "x = 35e3y = 12E4z = -87.7e100print(type(x))print(type(y))\n  print(type(z))"
      },
      {
        "type": "header",
        "level": 2,
        "text": "Complex"
      },
      {
        "type": "paragraph",
        "text": "Complex numbers are written with a \"j\" as the imaginary part:"
      },
      {
        "type": "example",
        "title": "Example",
        "code": "x = 3+5jy = 5jz = -5jprint(type(x))print(type(y))\n  print(type(z))"
      },
      {
        "type": "header",
        "level": 2,
        "text": "Type Conversion"
      },
      {
        "type": "paragraph",
        "text": "You can convert from one type to another with the <code class=\"w3-codespan\"><a href=\"ref_func_int.html\">int()</a></code>, \n<code class=\"w3-codespan\"><a href=\"ref_func_float.html\">float()</a></code>, and <code class=\"w3-codespan\"><a href=\"ref_func_complex.html\">complex()</a></code> methods:"
      },
      {
        "type": "example",
        "title": "Example",
        "code": "x = 1    # inty = 2.8  # floatz = 1j   # complex#convert from int to float:\n  a = float(x)#convert from float to int:\n  b = int(y)#convert from int to complex:c = complex(x)print(a)print(b)\n  print(c)print(type(a))print(type(b))\n  print(type(c))"
      },
      {
        "type": "note",
        "text": "<p><strong>Note:</strong> You cannot convert complex numbers into another number type.</p>"
      },
      {
        "type": "header",
        "level": 2,
        "text": "Random Number"
      },
      {
        "type": "paragraph",
        "text": "Python does not have a <code class=\"w3-codespan\">random()</code> function to \nmake a random number, but Python has a built-in module called\n<code class=\"w3-codespan\"><a href=\"module_random.html\">random</a></code> that can be used to make random numbers:"
      },
      {
        "type": "example",
        "title": "Example",
        "code": "import randomprint(random.randrange(1, 10))"
      },
      {
        "type": "paragraph",
        "text": "In our <a href=\"module_random.html\">Random Module Reference</a> you will learn more about the Random module."
      }
    ]
  },
  "casting": {
    "title": "Python Casting",
    "blocks": [
      {
        "type": "header",
        "level": 2,
        "text": "Specify a Variable Type"
      },
      {
        "type": "paragraph",
        "text": "There may be times when you want to specify a type on to a variable. This can be done with casting. Python is an object-orientated language, and as such it uses classes to define data types, including its primitive types."
      },
      {
        "type": "paragraph",
        "text": "Casting in python is therefore done using constructor functions:"
      },
      {
        "type": "list",
        "ordered": false,
        "items": [
          "<span class=\"w3-codespan\"><a href=\"ref_func_int.html\">int()</a></span> - constructs an integer number from an integer literal, a float literal (by removing \n    all decimals), or a string literal (providing the string represents a whole number)",
          "<span class=\"w3-codespan\"><a href=\"ref_func_float.html\">float()</a></span> - constructs a float number from an integer literal, a float literal or a string literal (providing the string represents a float or an integer)",
          "<span class=\"w3-codespan\"><a href=\"ref_func_str.html\">str()</a></span> - constructs a string from a wide variety of data types, including strings, integer literals and float literals"
        ]
      },
      {
        "type": "example",
        "title": "Example",
        "code": "x = int(1)   # x will be 1\ny = int(2.8) # y will be 2\nz = int(\"3\") # z will be 3"
      },
      {
        "type": "example",
        "title": "Example",
        "code": "x = float(1)     # x will be 1.0\ny = float(2.8)   # y will be 2.8\nz = float(\"3\")   # z will be 3.0\nw = float(\"4.2\") # w will be 4.2"
      },
      {
        "type": "example",
        "title": "Example",
        "code": "x = str(\"s1\") # x will be 's1'\ny = str(2)    # y will be '2'\nz = str(3.0)  # z will be '3.0'"
      }
    ]
  },
  "strings": {
    "title": "Python Strings",
    "blocks": [
      {
        "type": "header",
        "level": 2,
        "text": "Strings"
      },
      {
        "type": "paragraph",
        "text": "Strings in python are surrounded by either single quotation marks, or double quotation marks."
      },
      {
        "type": "paragraph",
        "text": "<span class=\"w3-codespan\">'hello'</span> is the same as <span class=\"w3-codespan\">\"hello\"</span>."
      },
      {
        "type": "paragraph",
        "text": "You can display a string literal with the <code class=\"w3-codespan\"><a href=\"ref_func_print.html\">print()</a></code> function:"
      },
      {
        "type": "example",
        "title": "Example",
        "code": "print(\"Hello\")\nprint('Hello')"
      },
      {
        "type": "header",
        "level": 2,
        "text": "Quotes Inside Quotes"
      },
      {
        "type": "paragraph",
        "text": "You can use quotes inside a string, as long as they don't match the quotes surrounding the string:"
      },
      {
        "type": "example",
        "title": "Example",
        "code": "print(\"It's alright\")\nprint(\"He is called 'Johnny'\")\nprint('He is called \"Johnny\"')"
      },
      {
        "type": "header",
        "level": 2,
        "text": "Assign String to a Variable"
      },
      {
        "type": "paragraph",
        "text": "Assigning a string to a variable is done with the variable name followed by \nan equal sign and the string:"
      },
      {
        "type": "example",
        "title": "Example",
        "code": "a = \"Hello\"print(a)"
      },
      {
        "type": "header",
        "level": 2,
        "text": "Multiline Strings"
      },
      {
        "type": "paragraph",
        "text": "You can assign a multiline string to a variable by using three quotes:"
      },
      {
        "type": "example",
        "title": "Example",
        "code": "a = \"\"\"Lorem ipsum dolor sit amet,consectetur adipiscing elit,sed do \n  eiusmod tempor incididuntut labore et dolore magna aliqua.\"\"\"print(a)"
      },
      {
        "type": "paragraph",
        "text": "Or three single quotes:"
      },
      {
        "type": "example",
        "title": "Example",
        "code": "a = '''Lorem ipsum dolor sit amet,consectetur adipiscing elit,sed do \n  eiusmod tempor incididuntut labore et dolore magna aliqua.'''print(a)"
      },
      {
        "type": "note",
        "text": "<p><strong>Note:</strong> in the result, the line breaks are inserted at the same position as in the code.</p>"
      },
      {
        "type": "header",
        "level": 2,
        "text": "Strings are Arrays"
      },
      {
        "type": "paragraph",
        "text": "Like many other popular programming languages, strings in Python are arrays of unicode characters."
      },
      {
        "type": "paragraph",
        "text": "However, Python does not have a character data type, a single character is simply a string with a length of 1."
      },
      {
        "type": "paragraph",
        "text": "Square brackets can be used to access elements of the string."
      },
      {
        "type": "example",
        "title": "Example",
        "code": "a = \"Hello, World!\"\nprint(a[1])"
      },
      {
        "type": "header",
        "level": 2,
        "text": "Looping Through a String"
      },
      {
        "type": "paragraph",
        "text": "Since strings are arrays, we can loop through the characters in a string, with a <code class=\"w3-codespan\"><a href=\"ref_keyword_for.html\">for</a></code> loop."
      },
      {
        "type": "example",
        "title": "Example",
        "code": "for x in \"banana\":  print(x)"
      },
      {
        "type": "paragraph",
        "text": "Learn more about For Loops in our <a href=\"#/python/for-loops\">Python For Loops</a> chapter."
      },
      {
        "type": "header",
        "level": 2,
        "text": "String Length"
      },
      {
        "type": "paragraph",
        "text": "To get the length of a string, use the <code class=\"w3-codespan\"><a href=\"ref_func_len.html\">len()</a></code> function."
      },
      {
        "type": "example",
        "title": "Example",
        "code": "a = \"Hello, World!\"\nprint(len(a))"
      },
      {
        "type": "header",
        "level": 2,
        "text": "Check String"
      },
      {
        "type": "paragraph",
        "text": "To check if a certain phrase or character is present in a string, we can use \nthe keyword \n<code class=\"w3-codespan\"><a href=\"ref_keyword_in.html\">in</a></code>."
      },
      {
        "type": "example",
        "title": "Example",
        "code": "txt = \"The best things in life are free!\"print(\"free\" in txt)"
      },
      {
        "type": "paragraph",
        "text": "Use it in an <code class=\"w3-codespan\"><a href=\"ref_keyword_if.html\">if</a></code> statement:"
      },
      {
        "type": "example",
        "title": "Example",
        "code": "txt = \"The best things in life are free!\"if \"free\" in txt: \nprint(\"Yes, 'free' is present.\")"
      },
      {
        "type": "paragraph",
        "text": "Learn more about If statements in our <a href=\"#/python/if-else\">Python \nIf...Else</a> chapter."
      },
      {
        "type": "header",
        "level": 2,
        "text": "Check if NOT"
      },
      {
        "type": "paragraph",
        "text": "To check if a certain phrase or character is NOT present in a string, we can use \nthe keyword <code class=\"w3-codespan\"><a href=\"ref_keyword_not.html\">not in</a></code>."
      },
      {
        "type": "example",
        "title": "Example",
        "code": "txt = \"The best things in life are free!\"print(\"expensive\" not in txt)"
      },
      {
        "type": "paragraph",
        "text": "Use it in an <code class=\"w3-codespan\"><a href=\"ref_keyword_if.html\">if</a></code> statement:"
      },
      {
        "type": "example",
        "title": "Example",
        "code": "txt = \"The best things in life are free!\"if \"expensive\" not in txt: \nprint(\"No, 'expensive' is NOT present.\")"
      }
    ]
  },
  "booleans": {
    "title": "Python Booleans",
    "blocks": [
      {
        "type": "intro",
        "text": "Booleans represent one of two values: \n<code class=\"w3-codespan\"><a href=\"ref_keyword_true.html\">True</a></code> or <code class=\"w3-codespan\"><a href=\"ref_keyword_false.html\">False</a></code>."
      },
      {
        "type": "header",
        "level": 2,
        "text": "Boolean Values"
      },
      {
        "type": "paragraph",
        "text": "In programming you often need to know if an expression is \n<code class=\"w3-codespan\"><a href=\"ref_keyword_true.html\">True</a></code> or <code class=\"w3-codespan\"><a href=\"ref_keyword_false.html\">False</a></code>."
      },
      {
        "type": "paragraph",
        "text": "You can evaluate any expression in Python, and get one of two \nanswers, \n<code class=\"w3-codespan\"><a href=\"ref_keyword_true.html\">True</a></code> or <code class=\"w3-codespan\"><a href=\"ref_keyword_false.html\">False</a></code>."
      },
      {
        "type": "paragraph",
        "text": "When you compare two values, the expression is evaluated and Python returns \nthe Boolean answer:"
      },
      {
        "type": "example",
        "title": "Example",
        "code": "print(10 > 9)print(10 == 9)print(10 < 9)"
      },
      {
        "type": "paragraph",
        "text": "When you run a condition in an if statement, Python returns \n<code class=\"w3-codespan\"><a href=\"ref_keyword_true.html\">True</a></code> or <code class=\"w3-codespan\"><a href=\"ref_keyword_false.html\">False</a></code>:"
      },
      {
        "type": "example",
        "title": "Example",
        "code": "a = 200b = 33if b > a:  print(\"b is greater than a\")\n  else:  print(\"b is not greater than a\")"
      },
      {
        "type": "header",
        "level": 2,
        "text": "Evaluate Values and Variables"
      },
      {
        "type": "paragraph",
        "text": "The <code class=\"w3-codespan\"><a href=\"ref_func_bool.html\">bool()</a></code> function allows you to evaluate \nany value, and give you \n<code class=\"w3-codespan\"><a href=\"ref_keyword_true.html\">True</a></code> or <code class=\"w3-codespan\"><a href=\"ref_keyword_false.html\">False</a></code> \nin return,"
      },
      {
        "type": "example",
        "title": "Example",
        "code": "print(bool(\"Hello\"))print(bool(15))"
      },
      {
        "type": "example",
        "title": "Example",
        "code": "x = \"Hello\"y = 15print(bool(x))print(bool(y))"
      },
      {
        "type": "header",
        "level": 2,
        "text": "Most Values are True"
      },
      {
        "type": "paragraph",
        "text": "Almost any value is evaluated to <code class=\"w3-codespan\"><a href=\"ref_keyword_true.html\">True</a></code> if it \nhas some sort of content."
      },
      {
        "type": "paragraph",
        "text": "Any string is <code class=\"w3-codespan\"><a href=\"ref_keyword_true.html\">True</a></code>, except empty strings."
      },
      {
        "type": "paragraph",
        "text": "Any number is <code class=\"w3-codespan\"><a href=\"ref_keyword_true.html\">True</a></code>, except \n<code class=\"w3-codespan\">0</code>."
      },
      {
        "type": "paragraph",
        "text": "Any list, tuple, set, and dictionary are <code class=\"w3-codespan\"><a href=\"ref_keyword_true.html\">True</a></code>, except \nempty ones."
      },
      {
        "type": "example",
        "title": "Example",
        "code": "bool(\"abc\")bool(123)bool([\"apple\", \"cherry\", \"banana\"])"
      },
      {
        "type": "header",
        "level": 2,
        "text": "Some Values are False"
      },
      {
        "type": "paragraph",
        "text": "In fact, there are not many values that evaluate to\n<code class=\"w3-codespan\"><a href=\"ref_keyword_false.html\">False</a></code>, except empty values, such as <code class=\"w3-codespan\">()</code>,\n<code class=\"w3-codespan\">[]</code>, <code class=\"w3-codespan\">{}</code>, \n<code class=\"w3-codespan\">\"\"</code>, the number\n<code class=\"w3-codespan\">0</code>, and the value <code class=\"w3-codespan\">None</code>. \nAnd of course the value <code class=\"w3-codespan\"><a href=\"ref_keyword_false.html\">False</a></code> evaluates to\n<code class=\"w3-codespan\"><a href=\"ref_keyword_false.html\">False</a></code>."
      },
      {
        "type": "example",
        "title": "Example",
        "code": "bool(False)bool(None)bool(0)bool(\"\")bool(())bool([])\n  bool({})"
      },
      {
        "type": "paragraph",
        "text": "One more value, or object in this case, evaluates to \n<code class=\"w3-codespan\"><a href=\"ref_keyword_false.html\">False</a></code>, and that is if you have an object that \nis made from a class with a <code class=\"w3-codespan\">__len__</code> function that returns \n<code class=\"w3-codespan\">0</code> or \n<code class=\"w3-codespan\"><a href=\"ref_keyword_false.html\">False</a></code>:"
      },
      {
        "type": "example",
        "title": "Example",
        "code": "class myclass():  def __len__(self):    return 0\n  myobj = myclass()print(bool(myobj))"
      },
      {
        "type": "header",
        "level": 2,
        "text": "Functions can Return a Boolean"
      },
      {
        "type": "paragraph",
        "text": "You can create functions that returns a Boolean Value:"
      },
      {
        "type": "example",
        "title": "Example",
        "code": "def myFunction() :  return Trueprint(myFunction())"
      },
      {
        "type": "paragraph",
        "text": "You can execute code based on the Boolean answer of a function:"
      },
      {
        "type": "example",
        "title": "Example",
        "code": "def myFunction() :  return Trueif myFunction():  \n  print(\"YES!\")else:  print(\"NO!\")"
      },
      {
        "type": "paragraph",
        "text": "Python also has many built-in functions that return a boolean value, like the \n<code class=\"w3-codespan\"><a href=\"ref_func_isinstance.html\">isinstance()</a></code> \nfunction, which can be used to determine if an object is of a certain data type:"
      },
      {
        "type": "example",
        "title": "Example",
        "code": "x = 200print(isinstance(x, int))"
      }
    ]
  },
  "operators": {
    "title": "Python Operators",
    "blocks": [
      {
        "type": "header",
        "level": 2,
        "text": "Python Operators"
      },
      {
        "type": "paragraph",
        "text": "Operators are used to perform operations on variables and values."
      },
      {
        "type": "paragraph",
        "text": "In the example below, we use the <code class=\"w3-codespan\">+</code> operator to add together two values:"
      },
      {
        "type": "example",
        "title": "Example",
        "code": "print(10 + 5)"
      },
      {
        "type": "paragraph",
        "text": "Although the <code class=\"w3-codespan\">+</code> operator is often used to add together two values, like in the example above, it can also be used to add together a variable and a value, or two variables:"
      },
      {
        "type": "example",
        "title": "Example",
        "code": "sum1 = 100 + 50      # 150 (100 + 50)\nsum2 = sum1 + 250    # 400 (150 + 250)\nsum3 = sum2 + sum2   # 800 (400 + 400)"
      },
      {
        "type": "paragraph",
        "text": "Python divides the operators in the following groups:"
      },
      {
        "type": "list",
        "ordered": false,
        "items": [
          "<a href=\"python_operators_arithmetic.html\">Arithmetic operators</a>",
          "<a href=\"python_operators_assign.html\">Assignment operators</a>",
          "<a href=\"python_operators_comparison.html\">Comparison operators</a>",
          "<a href=\"python_operators_logical.html\">Logical operators</a>",
          "<a href=\"python_operators_identity.html\">Identity operators</a>",
          "<a href=\"python_operators_membership.html\">Membership operators</a>",
          "<a href=\"python_operators_bitwise.html\">Bitwise operators</a>"
        ]
      }
    ]
  },
  "lists": {
    "title": "Python Lists",
    "blocks": [
      {
        "type": "header",
        "level": 2,
        "text": "List"
      },
      {
        "type": "paragraph",
        "text": "Lists are used to store multiple items in a single variable."
      },
      {
        "type": "paragraph",
        "text": "Lists are one of 4 built-in data types in Python used to store collections of \ndata, the other 3 are <a href=\"#/python/tuples\">Tuple</a>, \n<a href=\"#/python/sets\">Set</a>, and <a href=\"#/python/dictionaries\">Dictionary</a>, all with different qualities and usage."
      },
      {
        "type": "paragraph",
        "text": "Lists are created using square brackets:"
      },
      {
        "type": "example",
        "title": "Example",
        "code": "thislist = [\"apple\", \"banana\", \"cherry\"]\nprint(thislist)"
      },
      {
        "type": "header",
        "level": 2,
        "text": "List Items"
      },
      {
        "type": "paragraph",
        "text": "List items are ordered, changeable, and allow duplicate values."
      },
      {
        "type": "paragraph",
        "text": "List items are indexed, the first item has index <code class=\"w3-codespan\">[0]</code>,\nthe second item has index <code class=\"w3-codespan\">[1]</code> etc."
      },
      {
        "type": "header",
        "level": 2,
        "text": "Ordered"
      },
      {
        "type": "paragraph",
        "text": "When we say that lists are ordered, it means that the items have a defined order, and that order will not change."
      },
      {
        "type": "paragraph",
        "text": "If you add new items to a list,\nthe new items will be placed at the end of the list."
      },
      {
        "type": "note",
        "text": "<p><strong>Note:</strong> There are some <a href=\"python_lists_methods.html\">list methods</a> that will change the order, but in general: the order of the items will not change.</p>"
      },
      {
        "type": "header",
        "level": 2,
        "text": "Changeable"
      },
      {
        "type": "paragraph",
        "text": "The list is changeable, meaning that we can change, add, and remove items in a list after it has been created."
      },
      {
        "type": "header",
        "level": 2,
        "text": "Allow Duplicates"
      },
      {
        "type": "paragraph",
        "text": "Since lists are indexed, lists can have items with the same value:"
      },
      {
        "type": "example",
        "title": "Example",
        "code": "thislist = [\"apple\", \"banana\", \"cherry\", \"apple\", \"cherry\"]\nprint(thislist)"
      },
      {
        "type": "header",
        "level": 2,
        "text": "List Length"
      },
      {
        "type": "paragraph",
        "text": "To determine how many items a list has, use the <code class=\"w3-codespan\"><a href=\"ref_func_len.html\">len()</a></code> function:"
      },
      {
        "type": "example",
        "title": "Example",
        "code": "thislist = [\"apple\", \"banana\", \"cherry\"]\nprint(len(thislist))"
      },
      {
        "type": "header",
        "level": 2,
        "text": "List Items - Data Types"
      },
      {
        "type": "paragraph",
        "text": "List items can be of any data type:"
      },
      {
        "type": "example",
        "title": "Example",
        "code": "list1 = [\"apple\", \"banana\", \"cherry\"]\nlist2 = [1, 5, 7, 9, 3]\nlist3 = [True, False, False]"
      },
      {
        "type": "paragraph",
        "text": "A list can contain different data types:"
      },
      {
        "type": "example",
        "title": "Example",
        "code": "list1 = [\"abc\", 34, True, 40, \"male\"]"
      },
      {
        "type": "header",
        "level": 2,
        "text": "type()"
      },
      {
        "type": "paragraph",
        "text": "From Python's perspective, lists are defined as objects with the data type 'list':"
      },
      {
        "type": "example",
        "title": "Example",
        "code": "mylist = [\"apple\", \"banana\", \"cherry\"]\nprint(type(mylist))"
      },
      {
        "type": "header",
        "level": 2,
        "text": "The list() Constructor"
      },
      {
        "type": "paragraph",
        "text": "It is also possible to use the <span class=\"w3-codespan\"><a href=\"ref_func_list.html\">list()</a></span> constructor when creating a \nnew list."
      },
      {
        "type": "example",
        "title": "Example",
        "code": "thislist = list((\"apple\", \"banana\", \"cherry\")) # note the double round-brackets\nprint(thislist)"
      },
      {
        "type": "header",
        "level": 2,
        "text": "Python Collections (Arrays)"
      },
      {
        "type": "paragraph",
        "text": "There are four collection data types in the Python programming language:"
      },
      {
        "type": "list",
        "ordered": false,
        "items": [
          "<strong>List</strong> is a collection which is ordered and changeable. Allows duplicate members.",
          "<strong><a href=\"#/python/tuples\">Tuple</a></strong> is a collection which is ordered and unchangeable. Allows duplicate members.",
          "<strong><a href=\"#/python/sets\">Set</a></strong> is a collection which is unordered, \nunchangeable*, and unindexed. No duplicate members.",
          "<strong><a href=\"#/python/dictionaries\">Dictionary</a></strong> is a collection which is ordered** \nand changeable. No duplicate members."
        ]
      },
      {
        "type": "note",
        "text": "<p>*Set <em>items</em> are unchangeable, but you can remove and/or add items \n  whenever you like.</p>\n  <p>**As of Python version 3.7, dictionaries are <em>ordered</em>. \n  In Python 3.6 and earlier, dictionaries are <em>unordered</em>.</p>"
      },
      {
        "type": "paragraph",
        "text": "When choosing a collection type, it is useful to understand the properties of that type. Choosing the right type for a particular data set could mean retention of meaning, and, it could mean an increase in efficiency or security."
      }
    ]
  },
  "tuples": {
    "title": "Python Tuples",
    "blocks": [
      {
        "type": "header",
        "level": 2,
        "text": "Tuple"
      },
      {
        "type": "paragraph",
        "text": "Tuples are used to store multiple items in a single variable."
      },
      {
        "type": "paragraph",
        "text": "Tuple is one of 4 built-in data types in Python used to store collections of \ndata, the other 3 are <a href=\"#/python/lists\">List</a>, \n<a href=\"#/python/sets\">Set</a>, and <a href=\"#/python/dictionaries\">Dictionary</a>, all with different qualities and usage."
      },
      {
        "type": "paragraph",
        "text": "A tuple is a collection which is ordered and <strong>unchangeable</strong>."
      },
      {
        "type": "paragraph",
        "text": "Tuples are written with round brackets."
      },
      {
        "type": "example",
        "title": "Example",
        "code": "thistuple = (\"apple\", \"banana\", \"cherry\")\nprint(thistuple)"
      },
      {
        "type": "header",
        "level": 2,
        "text": "Tuple Items"
      },
      {
        "type": "paragraph",
        "text": "Tuple items are ordered, unchangeable, and allow duplicate values."
      },
      {
        "type": "paragraph",
        "text": "Tuple items are indexed, the first item has index <code class=\"w3-codespan\">[0]</code>, the second item has index <code class=\"w3-codespan\">[1]</code> etc."
      },
      {
        "type": "header",
        "level": 2,
        "text": "Ordered"
      },
      {
        "type": "paragraph",
        "text": "When we say that tuples are ordered, it means that the items have a defined order, and that order will not change."
      },
      {
        "type": "header",
        "level": 2,
        "text": "Unchangeable"
      },
      {
        "type": "paragraph",
        "text": "Tuples are unchangeable, meaning that we cannot change, add or remove items after the tuple has been created."
      },
      {
        "type": "header",
        "level": 2,
        "text": "Allow Duplicates"
      },
      {
        "type": "paragraph",
        "text": "Since tuples are indexed, they can have items with the same value:"
      },
      {
        "type": "example",
        "title": "Example",
        "code": "thistuple = (\"apple\", \"banana\", \"cherry\", \"apple\", \"cherry\")\nprint(thistuple)"
      },
      {
        "type": "header",
        "level": 2,
        "text": "Tuple Length"
      },
      {
        "type": "paragraph",
        "text": "To determine how many items a tuple has, use the <code class=\"w3-codespan\"><a href=\"ref_func_len.html\">len()</a></code> function:"
      },
      {
        "type": "example",
        "title": "Example",
        "code": "thistuple = (\"apple\", \"banana\", \"cherry\")\nprint(len(thistuple))"
      },
      {
        "type": "header",
        "level": 2,
        "text": "Create Tuple With One Item"
      },
      {
        "type": "paragraph",
        "text": "To create a tuple with only one item, you have to add a comma after the item, \notherwise Python will not recognize it as a tuple."
      },
      {
        "type": "example",
        "title": "Example",
        "code": "thistuple = (\"apple\",)\n  print(type(thistuple))#NOT a tuplethistuple = (\"apple\")\n  print(type(thistuple))"
      },
      {
        "type": "header",
        "level": 2,
        "text": "Tuple Items - Data Types"
      },
      {
        "type": "paragraph",
        "text": "Tuple items can be of any data type:"
      },
      {
        "type": "example",
        "title": "Example",
        "code": "tuple1 = (\"apple\", \"banana\", \"cherry\")\ntuple2 = (1, 5, 7, 9, 3)\ntuple3 = (True, False, False)"
      },
      {
        "type": "paragraph",
        "text": "A tuple can contain different data types:"
      },
      {
        "type": "example",
        "title": "Example",
        "code": "tuple1 = (\"abc\", 34, True, 40, \"male\")"
      },
      {
        "type": "header",
        "level": 2,
        "text": "type()"
      },
      {
        "type": "paragraph",
        "text": "From Python's perspective, tuples are defined as objects with the data type 'tuple':"
      },
      {
        "type": "example",
        "title": "Example",
        "code": "mytuple = (\"apple\", \"banana\", \"cherry\")\nprint(type(mytuple))"
      },
      {
        "type": "header",
        "level": 2,
        "text": "The tuple() Constructor"
      },
      {
        "type": "paragraph",
        "text": "It is also possible to use the <span class=\"w3-codespan\"><a href=\"ref_func_tuple.html\">tuple()</a></span> constructor to make a tuple."
      },
      {
        "type": "example",
        "title": "Example",
        "code": "thistuple = tuple((\"apple\", \"banana\", \"cherry\")) # note the double round-brackets\nprint(thistuple)"
      },
      {
        "type": "header",
        "level": 2,
        "text": "Python Collections (Arrays)"
      },
      {
        "type": "paragraph",
        "text": "There are four collection data types in the Python programming language:"
      },
      {
        "type": "list",
        "ordered": false,
        "items": [
          "<strong><a href=\"#/python/lists\">List</a></strong> is a collection which is ordered and changeable. Allows duplicate members.",
          "<strong>Tuple</strong> is a collection which is ordered and unchangeable. Allows duplicate members.",
          "<strong><a href=\"#/python/sets\">Set</a></strong> is a collection which is unordered, \nunchangeable*, and unindexed. No duplicate members.",
          "<strong><a href=\"#/python/dictionaries\">Dictionary</a></strong> is a collection which is ordered** \nand changeable. No duplicate members."
        ]
      },
      {
        "type": "note",
        "text": "<p>*Set <em>items</em> are unchangeable, but you can remove and/or add items \n  whenever you like.</p>\n  <p>**As of Python version 3.7, dictionaries are <em>ordered</em>. \n  In Python 3.6 and earlier, dictionaries are <em>unordered</em>.</p>"
      },
      {
        "type": "paragraph",
        "text": "When choosing a collection type, it is useful to understand the properties of that type. Choosing the right type for a particular data set could mean retention of meaning, and, it could mean an increase in efficiency or security."
      }
    ]
  },
  "sets": {
    "title": "Python Sets",
    "blocks": [
      {
        "type": "header",
        "level": 2,
        "text": "Set"
      },
      {
        "type": "paragraph",
        "text": "Sets are used to store multiple items in a single variable."
      },
      {
        "type": "paragraph",
        "text": "Set is one of 4 built-in data types in Python used to store collections of \ndata, the other 3 are <a href=\"#/python/lists\">List</a>, \n<a href=\"#/python/tuples\">Tuple</a>, and <a href=\"#/python/dictionaries\">Dictionary</a>, all with different qualities and usage."
      },
      {
        "type": "paragraph",
        "text": "A set is a collection which is <em>unordered</em>, <em>unchangeable*</em>, and <em>unindexed</em>."
      },
      {
        "type": "note",
        "text": "<p><strong>* Note:</strong> Set <em>items</em> are unchangeable, but you can remove \n  items and add new items.</p>"
      },
      {
        "type": "paragraph",
        "text": "Sets are written with curly brackets."
      },
      {
        "type": "example",
        "title": "Example",
        "code": "thisset = {\"apple\", \"banana\", \"cherry\"}print(thisset)"
      },
      {
        "type": "note",
        "text": "<p><strong>Note:</strong> Sets are unordered, so you cannot be sure in which \n  order the items will appear.</p>"
      },
      {
        "type": "header",
        "level": 2,
        "text": "Set Items"
      },
      {
        "type": "paragraph",
        "text": "Set items are unordered, unchangeable, and do not allow duplicate values."
      },
      {
        "type": "header",
        "level": 2,
        "text": "Unordered"
      },
      {
        "type": "paragraph",
        "text": "Unordered means that the items in a set do not have a defined order."
      },
      {
        "type": "paragraph",
        "text": "Set items can appear in a different order every time you use them, \nand cannot be referred to by index or key."
      },
      {
        "type": "header",
        "level": 2,
        "text": "Unchangeable"
      },
      {
        "type": "paragraph",
        "text": "Set items are unchangeable, meaning that we cannot change the items after the set has been created."
      },
      {
        "type": "note",
        "text": "<p>Once a set is created, you cannot change its items, but you can remove items \nand add new items.</p>"
      },
      {
        "type": "header",
        "level": 2,
        "text": "Duplicates Not Allowed"
      },
      {
        "type": "paragraph",
        "text": "Sets cannot have two items with the same value."
      },
      {
        "type": "example",
        "title": "Example",
        "code": "thisset = {\"apple\", \"banana\", \"cherry\", \"apple\"}\n  print(thisset)"
      },
      {
        "type": "note",
        "text": "<p><strong>Note:</strong> The values <code class=\"w3-codespan\"><a href=\"ref_keyword_true.html\">True</a></code>\n  and <code class=\"w3-codespan\">1</code> are considered the same value in sets, \n  and are treated as duplicates:</p>"
      },
      {
        "type": "example",
        "title": "Example",
        "code": "thisset = {\"apple\", \"banana\", \"cherry\", True, 1, 2}\n  print(thisset)"
      },
      {
        "type": "note",
        "text": "<p><strong>Note:</strong> The values <code class=\"w3-codespan\"><a href=\"ref_keyword_false.html\">False</a></code>\n  and <code class=\"w3-codespan\">0</code> are considered the same value in sets, \n  and are treated as duplicates:</p>"
      },
      {
        "type": "example",
        "title": "Example",
        "code": "thisset = {\"apple\", \"banana\", \"cherry\", False, True, 0}\n  print(thisset)"
      },
      {
        "type": "header",
        "level": 2,
        "text": "Get the Length of a Set"
      },
      {
        "type": "paragraph",
        "text": "To determine how many items a set has, use the <code class=\"w3-codespan\"><a href=\"ref_func_len.html\">len()</a></code> \nfunction."
      },
      {
        "type": "example",
        "title": "Example",
        "code": "thisset = {\"apple\", \"banana\", \"cherry\"}\n  print(len(thisset))"
      },
      {
        "type": "header",
        "level": 2,
        "text": "Set Items - Data Types"
      },
      {
        "type": "paragraph",
        "text": "Set items can be of any data type:"
      },
      {
        "type": "example",
        "title": "Example",
        "code": "set1 = {\"apple\", \"banana\", \"cherry\"}\n  set2 = {1, 5, 7, 9, 3}\n  set3 = {True, False, False}"
      },
      {
        "type": "paragraph",
        "text": "A set can contain different data types:"
      },
      {
        "type": "example",
        "title": "Example",
        "code": "set1 = {\"abc\", 34, True, 40, \"male\"}"
      },
      {
        "type": "header",
        "level": 2,
        "text": "type()"
      },
      {
        "type": "paragraph",
        "text": "From Python's perspective, sets are defined as objects with the data type 'set':"
      },
      {
        "type": "example",
        "title": "Example",
        "code": "myset = {\"apple\", \"banana\", \"cherry\"}\nprint(type(myset))"
      },
      {
        "type": "header",
        "level": 2,
        "text": "The set() Constructor"
      },
      {
        "type": "paragraph",
        "text": "It is also possible to use the <span class=\"w3-codespan\"><a href=\"ref_func_set.html\">set()</a></span> \nconstructor to make a set."
      },
      {
        "type": "example",
        "title": "Example",
        "code": "thisset = set((\"apple\", \"banana\", \"cherry\")) # note the double round-brackets\nprint(thisset)"
      },
      {
        "type": "header",
        "level": 2,
        "text": "Python Collections (Arrays)"
      },
      {
        "type": "paragraph",
        "text": "There are four collection data types in the Python programming language:"
      },
      {
        "type": "list",
        "ordered": false,
        "items": [
          "<strong><a href=\"#/python/lists\">List</a></strong> is a collection which is ordered and changeable. Allows duplicate members.",
          "<strong><a href=\"#/python/tuples\">Tuple</a></strong> is a collection which is ordered and unchangeable. Allows duplicate members.",
          "<strong>Set</strong> is a collection which is unordered, unchangeable*, and unindexed. No duplicate members.",
          "<strong><a href=\"#/python/dictionaries\">Dictionary</a></strong> is a collection which is ordered** \nand changeable. No duplicate members."
        ]
      },
      {
        "type": "note",
        "text": "<p>*Set <em>items</em> are unchangeable, but you can remove items and add new \n  items.</p>\n  <p>**As of Python version 3.7, dictionaries are <em>ordered</em>. \n  In Python 3.6 and earlier, dictionaries are <em>unordered</em>.</p>"
      },
      {
        "type": "paragraph",
        "text": "When choosing a collection type, it is useful to understand the properties of that type. Choosing the right type for a particular data set could mean retention of meaning, and, it could mean an increase in efficiency or security."
      }
    ]
  },
  "dictionaries": {
    "title": "Python Dictionaries",
    "blocks": [
      {
        "type": "header",
        "level": 2,
        "text": "Dictionary"
      },
      {
        "type": "paragraph",
        "text": "Dictionaries are used to store data values in key:value pairs."
      },
      {
        "type": "paragraph",
        "text": "A dictionary is a collection which is ordered*, changeable and do not \nallow duplicates."
      },
      {
        "type": "note",
        "text": "<p>As of Python version 3.7, dictionaries are <em>ordered</em>. \n  In Python 3.6 and earlier, dictionaries are <em>unordered</em>.</p>"
      },
      {
        "type": "paragraph",
        "text": "Dictionaries are written with curly brackets, and have keys and values:"
      },
      {
        "type": "example",
        "title": "Example",
        "code": "thisdict =\t{\n  \"brand\": \"Ford\",\n  \"model\": \"Mustang\",\n  \"year\": 1964\n}\nprint(thisdict)"
      },
      {
        "type": "header",
        "level": 2,
        "text": "Dictionary Items"
      },
      {
        "type": "paragraph",
        "text": "Dictionary items are ordered, changeable, and do not allow duplicates."
      },
      {
        "type": "paragraph",
        "text": "Dictionary items are presented in key:value pairs, and can be referred to by \nusing the key name."
      },
      {
        "type": "example",
        "title": "Example",
        "code": "thisdict =\t{\n  \"brand\": \"Ford\",\n  \"model\": \"Mustang\",\n  \"year\": 1964\n}\nprint(thisdict[\"brand\"])"
      },
      {
        "type": "header",
        "level": 2,
        "text": "Ordered or Unordered?"
      },
      {
        "type": "note",
        "text": "<p>As of Python version 3.7, dictionaries are <em>ordered</em>. \n  In Python 3.6 and earlier, dictionaries are <em>unordered</em>.</p>"
      },
      {
        "type": "paragraph",
        "text": "When we say that dictionaries are ordered, it means that the items have a defined order, and that order will not change."
      },
      {
        "type": "paragraph",
        "text": "Unordered means that the items do not \nhave a defined order, you cannot refer to an item by using an index."
      },
      {
        "type": "header",
        "level": 2,
        "text": "Changeable"
      },
      {
        "type": "paragraph",
        "text": "Dictionaries are changeable, meaning that we can change, add or remove items after the \ndictionary has been created."
      },
      {
        "type": "header",
        "level": 2,
        "text": "Duplicates Not Allowed"
      },
      {
        "type": "paragraph",
        "text": "Dictionaries cannot have two items with the same key:"
      },
      {
        "type": "example",
        "title": "Example",
        "code": "thisdict =\t{\n  \"brand\": \"Ford\",\n  \"model\": \"Mustang\",\n  \"year\": 1964,  \"year\": 2020\n}\nprint(thisdict)"
      },
      {
        "type": "header",
        "level": 2,
        "text": "Dictionary Length"
      },
      {
        "type": "paragraph",
        "text": "To determine how many items a dictionary has, use the <code class=\"w3-codespan\">\n<a href=\"ref_func_len.html\">len()</a></code> function:"
      },
      {
        "type": "example",
        "title": "Example",
        "code": "print(len(thisdict))"
      },
      {
        "type": "header",
        "level": 2,
        "text": "Dictionary Items - Data Types"
      },
      {
        "type": "paragraph",
        "text": "The values in dictionary items can be of any data type:"
      },
      {
        "type": "example",
        "title": "Example",
        "code": "thisdict =\t{\n  \"brand\": \"Ford\",\n  \"electric\": False,\n  \"year\": 1964,  \"colors\": [\"red\", \"white\", \"blue\"]\n}"
      },
      {
        "type": "header",
        "level": 2,
        "text": "type()"
      },
      {
        "type": "paragraph",
        "text": "From Python's perspective, dictionaries are defined as objects with the data type 'dict':"
      },
      {
        "type": "example",
        "title": "Example",
        "code": "thisdict =\t{\n  \"brand\": \"Ford\",\n  \"model\": \"Mustang\",\n  \"year\": 1964\n}\nprint(type(thisdict))"
      },
      {
        "type": "header",
        "level": 2,
        "text": "The dict() Constructor"
      },
      {
        "type": "paragraph",
        "text": "It is also possible to use the <span class=\"w3-codespan\"><a href=\"ref_func_dict.html\">dict()</a></span> constructor to make a dictionary."
      },
      {
        "type": "example",
        "title": "Example",
        "code": "thisdict = \n  dict(name = \"John\", age = 36, country = \"Norway\")\nprint(thisdict)"
      },
      {
        "type": "header",
        "level": 2,
        "text": "Python Collections (Arrays)"
      },
      {
        "type": "paragraph",
        "text": "There are four collection data types in the Python programming language:"
      },
      {
        "type": "list",
        "ordered": false,
        "items": [
          "<strong><a href=\"#/python/lists\">List</a></strong> is a collection which is ordered and changeable. Allows duplicate members.",
          "<strong><a href=\"#/python/tuples\">Tuple</a></strong> is a collection which is ordered and unchangeable. Allows duplicate members.",
          "<strong><a href=\"#/python/sets\">Set</a></strong> is a collection which is unordered, \nunchangeable*, and unindexed. No duplicate members.",
          "<strong>Dictionary</strong> is a collection which is ordered** and changeable. No duplicate members."
        ]
      },
      {
        "type": "note",
        "text": "<p>*Set <em>items</em> are unchangeable, but you can remove and/or add items \n  whenever you like.</p>\n  <p>**As of Python version 3.7, dictionaries are <em>ordered</em>. \n  In Python 3.6 and earlier, dictionaries are <em>unordered</em>.</p>"
      },
      {
        "type": "paragraph",
        "text": "When choosing a collection type, it is useful to understand the properties of that type. Choosing the right type for a particular data set could mean retention of meaning, and, it could mean an increase in efficiency or security."
      }
    ]
  },
  "if-else": {
    "title": "Python If Statement",
    "blocks": [
      {
        "type": "header",
        "level": 2,
        "text": "Python Conditions and If statements"
      },
      {
        "type": "paragraph",
        "text": "Python supports the usual logical conditions from mathematics:"
      },
      {
        "type": "list",
        "ordered": false,
        "items": [
          "Equals: <span class=\"w3-codespan\">a == b</span>",
          "Not Equals: <span class=\"w3-codespan\">a != b</span>",
          "Less than: <span class=\"w3-codespan\">a &lt; b</span>",
          "Less than or equal to: <span class=\"w3-codespan\">a &lt;= b</span>",
          "Greater than: <span class=\"w3-codespan\">a &gt; b</span>",
          "Greater than or equal to: <span class=\"w3-codespan\">a &gt;= b</span>"
        ]
      },
      {
        "type": "paragraph",
        "text": "These conditions can be used in several ways, most commonly in \"if statements\" and loops."
      },
      {
        "type": "paragraph",
        "text": "An \"if statement\" is written by using the <span class=\"w3-codespan\"><a href=\"ref_keyword_if.html\">if</a></span> keyword."
      },
      {
        "type": "example",
        "title": "Example",
        "code": "a = 33\nb = 200\nif b > a:  print(\"b is greater than a\")"
      },
      {
        "type": "paragraph",
        "text": "In this example we use two variables, <span class=\"w3-codespan\">a</span> and <span class=\"w3-codespan\">b</span>,\nwhich are used as part of the if statement to test whether <span class=\"w3-codespan\">b</span> is greater than <span class=\"w3-codespan\">a</span>.\nAs <span class=\"w3-codespan\">a</span> is <span class=\"w3-codespan\">33</span>, and <span class=\"w3-codespan\">b</span> is <span class=\"w3-codespan\">200</span>,\nwe know that 200 is greater than 33, and so we print to screen that \"b is greater than a\"."
      },
      {
        "type": "header",
        "level": 2,
        "text": "How If Statements Work"
      },
      {
        "type": "paragraph",
        "text": "The if statement evaluates a condition (an expression that results in <span class=\"w3-codespan\">True</span> or <span class=\"w3-codespan\">False</span>). If the condition is true, the code block inside the if statement is executed. If the condition is false, the code block is skipped."
      },
      {
        "type": "example",
        "title": "Example",
        "code": "number = 15\nif number > 0:  print(\"The number is positive\")"
      },
      {
        "type": "header",
        "level": 2,
        "text": "Indentation"
      },
      {
        "type": "paragraph",
        "text": "Python relies on indentation (whitespace at the beginning of a line) to define scope in the code. Other programming languages often use curly-brackets for this purpose."
      },
      {
        "type": "example",
        "title": "Example",
        "code": "a = 33\nb = 200\nif b > a:\nprint(\"b is greater than a\")\n# you will get an error"
      },
      {
        "type": "note",
        "text": "<p><strong>Note:</strong> You can use spaces or tabs for indentation, but you must use the same amount of indentation for all statements within the same code block.</p>"
      },
      {
        "type": "header",
        "level": 2,
        "text": "Multiple Statements in If Block"
      },
      {
        "type": "paragraph",
        "text": "You can have multiple statements inside an if block. All statements must be indented at the same level."
      },
      {
        "type": "example",
        "title": "Example",
        "code": "age = 20\nif age >= 18:\n  print(\"You are an adult\")\n  print(\"You can vote\")\n  print(\"You have full legal rights\")"
      },
      {
        "type": "header",
        "level": 2,
        "text": "Using Variables in Conditions"
      },
      {
        "type": "paragraph",
        "text": "Boolean variables can be used directly in if statements without comparison operators."
      },
      {
        "type": "example",
        "title": "Example",
        "code": "is_logged_in = True\nif is_logged_in:\n  print(\"Welcome back!\")"
      },
      {
        "type": "paragraph",
        "text": "Python can evaluate many types of values as <span class=\"w3-codespan\">True</span> or <span class=\"w3-codespan\">False</span> in an if statement."
      },
      {
        "type": "paragraph",
        "text": "Zero (<span class=\"w3-codespan\">0</span>), empty strings (<span class=\"w3-codespan\">\"\"</span>), <span class=\"w3-codespan\">None</span>, and empty collections are treated as <span class=\"w3-codespan\">False</span>. Everything else is treated as <span class=\"w3-codespan\">True</span>."
      },
      {
        "type": "paragraph",
        "text": "This includes positive numbers (<span class=\"w3-codespan\">5</span>), negative numbers (<span class=\"w3-codespan\">-3</span>), and any non-empty string (even <span class=\"w3-codespan\">\"False\"</span> is treated as <span class=\"w3-codespan\">True</span> because it's a non-empty string)."
      }
    ]
  },
  "while-loops": {
    "title": "Python While Loops",
    "blocks": [
      {
        "type": "header",
        "level": 2,
        "text": "Python Loops"
      },
      {
        "type": "paragraph",
        "text": "Python has two primitive loop commands:"
      },
      {
        "type": "list",
        "ordered": false,
        "items": [
          "<span class=\"w3-codespan\"><a href=\"ref_keyword_while.html\">while</a></span> loops",
          "<span class=\"w3-codespan\"><a href=\"ref_keyword_for.html\">for</a></span> loops"
        ]
      },
      {
        "type": "header",
        "level": 2,
        "text": "The while Loop"
      },
      {
        "type": "paragraph",
        "text": "With the <span class=\"w3-codespan\"><a href=\"ref_keyword_while.html\">while</a></span> loop we can execute a set of statements as long as a condition is true."
      },
      {
        "type": "example",
        "title": "Example",
        "code": "i = 1\nwhile i < 6:\n  print(i)\n  i += 1"
      },
      {
        "type": "note",
        "text": "<p><strong>Note:</strong> remember to increment i, or else the loop will continue forever.</p>"
      },
      {
        "type": "paragraph",
        "text": "The <span class=\"w3-codespan\"><a href=\"ref_keyword_while.html\">while</a></span> loop requires relevant variables to be ready, in this example we need to define an indexing variable, <span class=\"w3-codespan\"><a href=\"ref_keyword_while.html\">i</a></span>, \nwhich we set to 1."
      },
      {
        "type": "header",
        "level": 2,
        "text": "The break Statement"
      },
      {
        "type": "paragraph",
        "text": "With the <span class=\"w3-codespan\"><a href=\"ref_keyword_break.html\">break</a></span> statement we can stop the loop even if the \nwhile condition is true:"
      },
      {
        "type": "example",
        "title": "Example",
        "code": "i = 1\nwhile i < 6:\n  print(i)\n    if i == 3:    break  i += 1"
      },
      {
        "type": "header",
        "level": 2,
        "text": "The continue Statement"
      },
      {
        "type": "paragraph",
        "text": "With the <span class=\"w3-codespan\"><a href=\"ref_keyword_continue.html\">continue</a></span> statement we can stop the \ncurrent iteration, and continue with the next:"
      },
      {
        "type": "example",
        "title": "Example",
        "code": "i = 0\nwhile i < 6:\n    i += 1\n  if i == 3:    continue  print(i)"
      },
      {
        "type": "header",
        "level": 2,
        "text": "The else Statement"
      },
      {
        "type": "paragraph",
        "text": "With the <span class=\"w3-codespan\"><a href=\"ref_keyword_else.html\">else</a></span> statement we can run a block of code once when the \ncondition no longer is true:"
      },
      {
        "type": "example",
        "title": "Example",
        "code": "i = 1\nwhile i < 6:\n  print(i)\n  i += 1else:  print(\"i is no longer less than 6\")"
      },
      {
        "type": "note",
        "text": "<p><strong>Note:</strong> The <code class=\"w3-codespan\"><a href=\"ref_keyword_else.html\">else</a></code> block will NOT be executed if the loop is stopped by a <code class=\"w3-codespan\"><a href=\"ref_keyword_break.html\">break</a></code> statement.</p>"
      }
    ]
  },
  "for-loops": {
    "title": "Python For Loops",
    "blocks": [
      {
        "type": "header",
        "level": 2,
        "text": "Python For Loops"
      },
      {
        "type": "paragraph",
        "text": "A <span class=\"w3-codespan\"><a href=\"ref_keyword_for.html\">for</a></span> loop is used for iterating over a sequence (that is either a list, a tuple, \na dictionary, a set, or a string)."
      },
      {
        "type": "paragraph",
        "text": "This is less like the <span class=\"w3-codespan\"><a href=\"ref_keyword_for.html\">for</a></span> keyword in other programming languages, and works more like an iterator method as found in other object-orientated programming languages."
      },
      {
        "type": "paragraph",
        "text": "With the <span class=\"w3-codespan\"><a href=\"ref_keyword_for.html\">for</a></span> loop we can execute a set of statements, once for each item in a list, tuple, set etc."
      },
      {
        "type": "example",
        "title": "Example",
        "code": "fruits = [\"apple\", \"banana\", \"cherry\"]for \n  x in fruits:\n\t \n\tprint(x)"
      },
      {
        "type": "paragraph",
        "text": "The <span class=\"w3-codespan\"><a href=\"ref_keyword_for.html\">for</a></span> loop does not require an indexing variable to set beforehand."
      },
      {
        "type": "header",
        "level": 2,
        "text": "Looping Through a String"
      },
      {
        "type": "paragraph",
        "text": "Even strings are iterable objects, they contain a sequence of characters:"
      },
      {
        "type": "example",
        "title": "Example",
        "code": "for x in \"banana\":  print(x)"
      },
      {
        "type": "header",
        "level": 2,
        "text": "The break Statement"
      },
      {
        "type": "paragraph",
        "text": "With the <span class=\"w3-codespan\"><a href=\"ref_keyword_break.html\">break</a></span> statement we can stop the \nloop before it has looped through all the items:"
      },
      {
        "type": "example",
        "title": "Example",
        "code": "fruits = [\"apple\", \"banana\", \"cherry\"]for x in fruits:  print(x)\n    if x == \n  \"banana\":    break"
      },
      {
        "type": "example",
        "title": "Example",
        "code": "fruits = [\"apple\", \"banana\", \"cherry\"]for x in fruits:  if x == \n  \"banana\":    break  print(x)"
      },
      {
        "type": "header",
        "level": 2,
        "text": "The continue Statement"
      },
      {
        "type": "paragraph",
        "text": "With the <span class=\"w3-codespan\"><a href=\"ref_keyword_continue.html\">continue</a></span> statement we can stop the \ncurrent iteration of the loop, and continue with the next:"
      },
      {
        "type": "example",
        "title": "Example",
        "code": "fruits = [\"apple\", \"banana\", \"cherry\"]for x in fruits:  if x == \n  \"banana\":    continue  print(x)"
      },
      {
        "type": "header",
        "level": 2,
        "text": "The range() Function"
      },
      {
        "type": "paragraph",
        "text": "To loop through a set of code a specified number of times, we can use the <code class=\"w3-codespan\"><a href=\"ref_func_range.html\">range()</a></code> function,"
      },
      {
        "type": "paragraph",
        "text": "The <code class=\"w3-codespan\"><a href=\"ref_func_range.html\">range()</a></code> function returns a sequence of numbers, starting from 0 by default, and increments by 1 (by default), and ends at a specified number."
      },
      {
        "type": "example",
        "title": "Example",
        "code": "for x in range(6):\n\t \n\tprint(x)"
      },
      {
        "type": "note",
        "text": "<p>Note that <code class=\"w3-codespan\">range(6)</code> is not the values of 0 to 6, but the values 0 to 5.</p>"
      },
      {
        "type": "paragraph",
        "text": "The <code class=\"w3-codespan\"><a href=\"ref_func_range.html\">range()</a></code> function defaults to 0 as a starting value, however it is possible to specify the starting value by adding a parameter: <code class=\"w3-codespan\">range(2, 6)</code>, which \nmeans values from 2 to 6 (but not including 6):"
      },
      {
        "type": "example",
        "title": "Example",
        "code": "for x in range(2, 6):\n\t \n\tprint(x)"
      },
      {
        "type": "paragraph",
        "text": "The <code class=\"w3-codespan\"><a href=\"ref_func_range.html\">range()</a></code> function defaults to increment the sequence by 1,\nhowever it is possible to specify the increment value by adding a third parameter: <code class=\"w3-codespan\">range(2, 30, <strong>3</strong>)</code>:"
      },
      {
        "type": "example",
        "title": "Example",
        "code": "for x in range(2, 30, 3):\n\t \n\tprint(x)"
      },
      {
        "type": "header",
        "level": 2,
        "text": "Else in For Loop"
      },
      {
        "type": "paragraph",
        "text": "The <code class=\"w3-codespan\"><a href=\"ref_keyword_else.html\">else</a></code> keyword in a\n<code class=\"w3-codespan\"><a href=\"ref_keyword_for.html\">for</a></code> loop specifies a block of code to be \nexecuted when the loop is finished:"
      },
      {
        "type": "example",
        "title": "Example",
        "code": "for x in range(6): \n\tprint(x)else: \n\tprint(\"Finally finished!\")"
      },
      {
        "type": "note",
        "text": "<p><strong>Note:</strong> The <code class=\"w3-codespan\"><a href=\"ref_keyword_else.html\">else</a></code> block will NOT be executed if the loop is stopped by a <code class=\"w3-codespan\"><a href=\"ref_keyword_break.html\">break</a></code> statement.</p>"
      },
      {
        "type": "example",
        "title": "Example",
        "code": "for x in range(6):  if x == 3: break \n\tprint(x)else: \n\tprint(\"Finally finished!\")"
      },
      {
        "type": "header",
        "level": 2,
        "text": "Nested Loops"
      },
      {
        "type": "paragraph",
        "text": "A nested loop is a loop inside a loop."
      },
      {
        "type": "paragraph",
        "text": "The \"inner loop\" will be executed one time for each iteration of the \"outer \nloop\":"
      },
      {
        "type": "example",
        "title": "Example",
        "code": "adj = [\"red\", \"big\", \"tasty\"]fruits = [\"apple\", \"banana\", \"cherry\"]\n  for x in adj:  for y in fruits:    print(x, y)"
      },
      {
        "type": "header",
        "level": 2,
        "text": "The pass Statement"
      },
      {
        "type": "paragraph",
        "text": "<code class=\"w3-codespan\"><a href=\"ref_keyword_for.html\">for</a></code> loops cannot be empty, but if you for \nsome reason have a <code class=\"w3-codespan\"><a href=\"ref_keyword_for.html\">for</a></code> loop with no content, put in the <code class=\"w3-codespan\"><a href=\"ref_keyword_pass.html\">pass</a></code> statement to avoid getting an error."
      },
      {
        "type": "example",
        "title": "Example",
        "code": "for x in [0, 1, 2]:  pass"
      }
    ]
  },
  "functions": {
    "title": "Python Functions",
    "blocks": [
      {
        "type": "header",
        "level": 2,
        "text": "Python Functions"
      },
      {
        "type": "paragraph",
        "text": "A function is a block of code which only runs when it is called."
      },
      {
        "type": "paragraph",
        "text": "A function can return data as a result."
      },
      {
        "type": "paragraph",
        "text": "A function helps avoiding code repetition."
      },
      {
        "type": "header",
        "level": 2,
        "text": "Creating a Function"
      },
      {
        "type": "paragraph",
        "text": "In Python, a function is defined using the <code class=\"w3-codespan\"><a href=\"ref_keyword_def.html\">def</a></code> \nkeyword, followed by a function name and parentheses:"
      },
      {
        "type": "example",
        "title": "Example",
        "code": "def my_function():  print(\"Hello from a function\")"
      },
      {
        "type": "paragraph",
        "text": "This creates a function named <code class=\"w3-codespan\">my_function</code> that prints \"Hello from a function\" when called."
      },
      {
        "type": "note",
        "text": "<p>The code inside the function must be indented. Python uses indentation to define code blocks.</p>"
      },
      {
        "type": "header",
        "level": 2,
        "text": "Calling a Function"
      },
      {
        "type": "paragraph",
        "text": "To call a function, write its name followed by parentheses:"
      },
      {
        "type": "example",
        "title": "Example",
        "code": "def my_function():  print(\"Hello from a function\")\n  my_function()"
      },
      {
        "type": "paragraph",
        "text": "You can call the same function multiple times:"
      },
      {
        "type": "example",
        "title": "Example",
        "code": "def my_function():  print(\"Hello from a function\")\n  my_function()my_function()my_function()"
      },
      {
        "type": "header",
        "level": 2,
        "text": "Function Names"
      },
      {
        "type": "paragraph",
        "text": "Function names follow the same rules as variable names in Python:"
      },
      {
        "type": "list",
        "ordered": false,
        "items": [
          "A function name must start with a letter or underscore",
          "A function name can only contain letters, numbers, and underscores",
          "Function names are case-sensitive (<code class=\"w3-codespan\">myFunction</code> and <code class=\"w3-codespan\">myfunction</code> are different)"
        ]
      },
      {
        "type": "example",
        "title": "Example",
        "code": "calculate_sum()\n_private_function()\nmyFunction2()"
      },
      {
        "type": "note",
        "text": "<p>It's good practice to use descriptive names that explain what the function does.</p>"
      },
      {
        "type": "header",
        "level": 2,
        "text": "Why Use Functions?"
      },
      {
        "type": "paragraph",
        "text": "Imagine you need to convert temperatures from Fahrenheit to Celsius several times in your program. Without functions, you would have to write the same calculation code repeatedly:"
      },
      {
        "type": "example",
        "title": "Example",
        "code": "temp1 = 77celsius1 = (temp1 - 32) * 5 / 9print(celsius1)\n  temp2 = 95celsius2 = (temp2 - 32) * 5 / 9print(celsius2)\n  temp3 = 50celsius3 = (temp3 - 32) * 5 / 9print(celsius3)"
      },
      {
        "type": "paragraph",
        "text": "With functions, you write the code once and reuse it:"
      },
      {
        "type": "example",
        "title": "Example",
        "code": "def fahrenheit_to_celsius(fahrenheit):  return (fahrenheit - 32) * 5 / 9\n  print(fahrenheit_to_celsius(77))print(fahrenheit_to_celsius(95))print(fahrenheit_to_celsius(50))"
      },
      {
        "type": "header",
        "level": 2,
        "text": "Return Values"
      },
      {
        "type": "paragraph",
        "text": "Functions can send data back to the code that called them using the <code class=\"w3-codespan\"><a href=\"ref_keyword_return.html\">return</a></code> statement."
      },
      {
        "type": "paragraph",
        "text": "When a function reaches a <code class=\"w3-codespan\">return</code> statement, it stops executing and sends the result back:"
      },
      {
        "type": "example",
        "title": "Example",
        "code": "def get_greeting():  return \"Hello from a function\"\n  message = get_greeting()print(message)"
      },
      {
        "type": "paragraph",
        "text": "You can use the returned value directly:"
      },
      {
        "type": "example",
        "title": "Example",
        "code": "def get_greeting():  return \"Hello from a function\"\n  print(get_greeting())"
      },
      {
        "type": "note",
        "text": "<p>If a function doesn't have a <code class=\"w3-codespan\">return</code> statement, it returns <code class=\"w3-codespan\">None</code> by default.</p>"
      },
      {
        "type": "header",
        "level": 2,
        "text": "The pass Statement"
      },
      {
        "type": "paragraph",
        "text": "Function definitions cannot be empty. If you need to create a function placeholder without any code, use the <code class=\"w3-codespan\"><a href=\"ref_keyword_pass.html\">pass</a></code> statement:"
      },
      {
        "type": "example",
        "title": "Example",
        "code": "def my_function():  pass"
      },
      {
        "type": "paragraph",
        "text": "The <code class=\"w3-codespan\">pass</code> statement is often used when developing, allowing you to define the structure first and implement details later."
      }
    ]
  },
  "lambda": {
    "title": "Python Lambda",
    "blocks": [
      {
        "type": "header",
        "level": 2,
        "text": "Lambda Functions"
      },
      {
        "type": "paragraph",
        "text": "A lambda function is a small anonymous function."
      },
      {
        "type": "paragraph",
        "text": "A lambda function can take any number of arguments, but can only have one expression."
      },
      {
        "type": "header",
        "level": 2,
        "text": "Syntax"
      },
      {
        "type": "code",
        "code": "lambda arguments : expression"
      },
      {
        "type": "paragraph",
        "text": "The expression is executed and the result is returned:"
      },
      {
        "type": "example",
        "title": "Example",
        "code": "x = lambda a : a + 10print(x(5))"
      },
      {
        "type": "paragraph",
        "text": "Lambda functions can take any number of arguments:"
      },
      {
        "type": "example",
        "title": "Example",
        "code": "x = lambda a, b : a * bprint(x(5, 6))"
      },
      {
        "type": "example",
        "title": "Example",
        "code": "x = lambda a, b, c : a + b + cprint(x(5, 6, \n  2))"
      },
      {
        "type": "header",
        "level": 2,
        "text": "Why Use Lambda Functions?"
      },
      {
        "type": "paragraph",
        "text": "The power of lambda is better shown when you use them as an anonymous \nfunction inside another function."
      },
      {
        "type": "paragraph",
        "text": "Say you have a function definition that takes one argument, and that argument \nwill be multiplied with an unknown number:"
      },
      {
        "type": "example",
        "title": "Example",
        "code": "def myfunc(n):\n  return lambda a : a * n"
      },
      {
        "type": "paragraph",
        "text": "Use that function definition to make a function that always doubles the \nnumber you send in:"
      },
      {
        "type": "example",
        "title": "Example",
        "code": "def myfunc(n):\n  return lambda a : a * n\n\n  mydoubler = myfunc(2)\n  print(mydoubler(11))"
      },
      {
        "type": "paragraph",
        "text": "Or, use the same function definition to make a function that always <em>triples</em> the \nnumber you send in:"
      },
      {
        "type": "example",
        "title": "Example",
        "code": "def myfunc(n):\n  return lambda a : a * n\n\n  mytripler = myfunc(3)\n  print(mytripler(11))"
      },
      {
        "type": "paragraph",
        "text": "Or, use the same function definition to make both functions, in the same \nprogram:"
      },
      {
        "type": "example",
        "title": "Example",
        "code": "def myfunc(n):\n  return lambda a : a * n\n\n  mydoubler = myfunc(2)mytripler = myfunc(3)\n  print(mydoubler(11))print(mytripler(11))"
      },
      {
        "type": "note",
        "text": "<p>Use lambda functions when an anonymous function is required for a short period of time.</p>"
      },
      {
        "type": "header",
        "level": 2,
        "text": "Lambda with Built-in Functions"
      },
      {
        "type": "paragraph",
        "text": "Lambda functions are commonly used with built-in functions like <code class=\"w3-codespan\">map()</code>, <code class=\"w3-codespan\">filter()</code>, and <code class=\"w3-codespan\">sorted()</code>."
      },
      {
        "type": "header",
        "level": 3,
        "text": "Using Lambda with map()"
      },
      {
        "type": "paragraph",
        "text": "The <code class=\"w3-codespan\">map()</code> function applies a function to every item in an iterable:"
      },
      {
        "type": "example",
        "title": "Example",
        "code": "numbers = [1, 2, 3, 4, 5]\ndoubled = list(map(lambda x: x * 2, numbers))\nprint(doubled)"
      },
      {
        "type": "header",
        "level": 3,
        "text": "Using Lambda with filter()"
      },
      {
        "type": "paragraph",
        "text": "The <code class=\"w3-codespan\">filter()</code> function creates a list of items for which a function returns <code class=\"w3-codespan\">True</code>:"
      },
      {
        "type": "example",
        "title": "Example",
        "code": "numbers = [1, 2, 3, 4, 5, 6, 7, 8]\nodd_numbers = list(filter(lambda x: x % 2 != 0, numbers))\nprint(odd_numbers)"
      },
      {
        "type": "header",
        "level": 3,
        "text": "Using Lambda with sorted()"
      },
      {
        "type": "paragraph",
        "text": "The <code class=\"w3-codespan\">sorted()</code> function can use a lambda as a key for custom sorting:"
      },
      {
        "type": "example",
        "title": "Example",
        "code": "students = [(\"Emil\", 25), (\"Tobias\", 22), (\"Linus\", 28)]\nsorted_students = sorted(students, key=lambda x: x[1])\nprint(sorted_students)"
      },
      {
        "type": "example",
        "title": "Example",
        "code": "words = [\"apple\", \"pie\", \"banana\", \"cherry\"]\nsorted_words = sorted(words, key=lambda x: len(x))\nprint(sorted_words)"
      }
    ]
  },
  "arrays": {
    "title": "Python Arrays",
    "blocks": [
      {
        "type": "note",
        "text": "<p><strong>Note:</strong> Python does not have built-in support for Arrays, \n  but <a href=\"#/python/lists\">Python Lists</a> can be used instead.</p>"
      },
      {
        "type": "header",
        "level": 2,
        "text": "Arrays"
      },
      {
        "type": "note",
        "text": "<p><strong>Note:</strong> This page shows you how to use LISTS as ARRAYS, however, to work with arrays in Python you will have to import\n  a library, like the <a href=\"numpy/default.html\">NumPy library</a>.</p>"
      },
      {
        "type": "paragraph",
        "text": "Arrays are used to store multiple values in one single variable:"
      },
      {
        "type": "example",
        "title": "Example",
        "code": "cars = [\"Ford\", \"Volvo\", \"BMW\"]"
      },
      {
        "type": "header",
        "level": 2,
        "text": "What is an Array?"
      },
      {
        "type": "paragraph",
        "text": "An array is a special variable, which can hold more than one value at a time."
      },
      {
        "type": "paragraph",
        "text": "If you have a list of items (a list of car names, for example), storing the \ncars in single variables could look like this:"
      },
      {
        "type": "code",
        "code": "car1 = \"Ford\"\n  car2 = \"Volvo\"\n  car3 = \"BMW\""
      },
      {
        "type": "paragraph",
        "text": "However, what if you want to loop through the cars and find a specific one? \nAnd what if you had not 3 cars, but 300?"
      },
      {
        "type": "paragraph",
        "text": "The solution is an array!"
      },
      {
        "type": "paragraph",
        "text": "An array can hold many values under a single name, and you can \naccess the values by referring to an index number."
      },
      {
        "type": "header",
        "level": 2,
        "text": "Access the Elements of an Array"
      },
      {
        "type": "paragraph",
        "text": "You refer to an array element by referring to the <em>index number</em>."
      },
      {
        "type": "example",
        "title": "Example",
        "code": "x = cars[0]"
      },
      {
        "type": "example",
        "title": "Example",
        "code": "cars[0] = \"Toyota\""
      },
      {
        "type": "header",
        "level": 2,
        "text": "The Length of an Array"
      },
      {
        "type": "paragraph",
        "text": "Use the <code class=\"w3-codespan\"><a href=\"ref_func_len.html\">len()</a></code> method to return the length of \nan array (the number of elements in an array)."
      },
      {
        "type": "example",
        "title": "Example",
        "code": "x = len(cars)"
      },
      {
        "type": "note",
        "text": "<p><strong>Note:</strong> The length of an array is always one more than the highest array index.</p>"
      },
      {
        "type": "header",
        "level": 2,
        "text": "Looping Array Elements"
      },
      {
        "type": "paragraph",
        "text": "You can use the <code class=\"w3-codespan\"><a href=\"#/python/for-loops\">for in</a></code> loop to loop through all the elements of an array."
      },
      {
        "type": "example",
        "title": "Example",
        "code": "for x in cars:  print(x)"
      },
      {
        "type": "header",
        "level": 2,
        "text": "Adding Array Elements"
      },
      {
        "type": "paragraph",
        "text": "You can use the <code class=\"w3-codespan\"><a href=\"ref_list_append.html\">append()</a></code> method to add an element to an array."
      },
      {
        "type": "example",
        "title": "Example",
        "code": "cars.append(\"Honda\")"
      },
      {
        "type": "header",
        "level": 2,
        "text": "Removing Array Elements"
      },
      {
        "type": "paragraph",
        "text": "You can use the <code class=\"w3-codespan\"><a href=\"ref_list_pop.html\">pop()</a></code> method to remove an element from the array."
      },
      {
        "type": "example",
        "title": "Example",
        "code": "cars.pop(1)"
      },
      {
        "type": "paragraph",
        "text": "You can also use the <code class=\"w3-codespan\"><a href=\"ref_list_remove.html\">remove()</a></code> method to remove an element from the array."
      },
      {
        "type": "example",
        "title": "Example",
        "code": "cars.remove(\"Volvo\")"
      },
      {
        "type": "note",
        "text": "<p><strong>Note:</strong> The list's <code class=\"w3-codespan\"><a href=\"ref_list_remove.html\">remove()</a></code> method \n  only removes the first occurrence of the specified value.</p>"
      },
      {
        "type": "header",
        "level": 2,
        "text": "Array Methods"
      },
      {
        "type": "paragraph",
        "text": "Python has a set of built-in methods that you can use on lists/arrays."
      },
      {
        "type": "table",
        "headers": [
          "Method",
          "Description"
        ],
        "rows": [
          [
            "<a href=\"ref_list_append.html\">append()</a>",
            "Adds an element at \n  the end of the list"
          ],
          [
            "<a href=\"ref_list_clear.html\">clear()</a>",
            "Removes all the \n  elements from the list"
          ],
          [
            "<a href=\"ref_list_copy.html\">copy()</a>",
            "Returns a copy of the \n  list"
          ],
          [
            "<a href=\"ref_list_count.html\">count()</a>",
            "Returns the number of \n  elements with the specified value"
          ],
          [
            "<a href=\"ref_list_extend.html\">extend()</a>",
            "Add the elements of a \n  list (or any iterable), to the end of the current list"
          ],
          [
            "<a href=\"ref_list_index.html\">index()</a>",
            "Returns the index of \n  the first element with the specified value"
          ],
          [
            "<a href=\"ref_list_insert.html\">insert()</a>",
            "Adds an element at \n  the specified position"
          ],
          [
            "<a href=\"ref_list_pop.html\">pop()</a>",
            "Removes the element at the \n  specified position"
          ],
          [
            "<a href=\"ref_list_remove.html\">remove()</a>",
            "Removes the first \n  item with the specified value"
          ],
          [
            "<a href=\"ref_list_reverse.html\">reverse()</a>",
            "Reverses the order \n  of the list"
          ],
          [
            "<a href=\"ref_list_sort.html\">sort()</a>",
            "Sorts the list"
          ]
        ]
      },
      {
        "type": "note",
        "text": "<p><strong>Note:</strong> Python does not have built-in support for Arrays, \n  but Python Lists can be used instead.</p>"
      }
    ]
  },
  "classes": {
    "title": "Python Classes and Objects",
    "blocks": [
      {
        "type": "header",
        "level": 2,
        "text": "Python Classes/Objects"
      },
      {
        "type": "paragraph",
        "text": "Python is an object oriented programming language."
      },
      {
        "type": "paragraph",
        "text": "Almost everything in Python is an object, with its properties and methods."
      },
      {
        "type": "paragraph",
        "text": "A Class is like an object constructor, or a \"blueprint\" for creating objects."
      },
      {
        "type": "header",
        "level": 2,
        "text": "Create a Class"
      },
      {
        "type": "paragraph",
        "text": "To create a class, use the keyword <code class=\"w3-codespan\"><a href=\"ref_keyword_class.html\">class</a></code>:"
      },
      {
        "type": "example",
        "title": "Example",
        "code": "class MyClass:  x = 5"
      },
      {
        "type": "header",
        "level": 2,
        "text": "Create Object"
      },
      {
        "type": "paragraph",
        "text": "Now we can use the class named MyClass to create objects:"
      },
      {
        "type": "example",
        "title": "Example",
        "code": "p1 = MyClass()print(p1.x)"
      },
      {
        "type": "header",
        "level": 2,
        "text": "Delete Objects"
      },
      {
        "type": "paragraph",
        "text": "You can delete objects by using the <code class=\"w3-codespan\"><a href=\"ref_keyword_del.html\">del</a></code> keyword:"
      },
      {
        "type": "example",
        "title": "Example",
        "code": "del p1"
      },
      {
        "type": "header",
        "level": 2,
        "text": "Multiple Objects"
      },
      {
        "type": "paragraph",
        "text": "You can create multiple objects from the same class:"
      },
      {
        "type": "example",
        "title": "Example",
        "code": "p1 = MyClass()p2 = MyClass()p3 = MyClass()print(p1.x)print(p2.x)print(p3.x)"
      },
      {
        "type": "note",
        "text": "<p><strong>Note:</strong> Each object is independent and has its own copy of the class properties.</p>"
      },
      {
        "type": "header",
        "level": 2,
        "text": "The pass Statement"
      },
      {
        "type": "paragraph",
        "text": "<code class=\"w3-codespan\"><a href=\"ref_keyword_class.html\">class</a></code> definitions cannot be empty, but if \nyou for some reason have a <code class=\"w3-codespan\"><a href=\"ref_keyword_class.html\">class</a></code> definition with no content, put in the <code class=\"w3-codespan\"><a href=\"ref_keyword_pass.html\">pass</a></code> statement to avoid getting an error."
      },
      {
        "type": "example",
        "title": "Example",
        "code": "class Person:  pass"
      }
    ]
  },
  "inheritance": {
    "title": "Python Inheritance",
    "blocks": [
      {
        "type": "header",
        "level": 2,
        "text": "Python Inheritance"
      },
      {
        "type": "paragraph",
        "text": "Inheritance allows us to define a class that inherits all the methods and properties from another class."
      },
      {
        "type": "paragraph",
        "text": "<strong>Parent class</strong> is the class being inherited from, also called \nbase class."
      },
      {
        "type": "paragraph",
        "text": "<strong>Child class</strong> is the class that inherits from another class, \nalso called derived class."
      },
      {
        "type": "header",
        "level": 2,
        "text": "Create a Parent Class"
      },
      {
        "type": "paragraph",
        "text": "Any class can be a parent class, so the syntax is the same as creating any \nother class:"
      },
      {
        "type": "example",
        "title": "Example",
        "code": "class Person:  def __init__(self, fname, lname):    \n  self.firstname = fname    self.lastname = lname  \n  def printname(self):    print(self.firstname, \n  self.lastname)#Use the Person class to create an object, and then \n  execute the printname method:x = Person(\"John\", \"Doe\")\n  x.printname()"
      },
      {
        "type": "header",
        "level": 2,
        "text": "Create a Child Class"
      },
      {
        "type": "paragraph",
        "text": "To create a class that inherits the functionality from another class, send the parent class as a parameter when creating the child \nclass:"
      },
      {
        "type": "example",
        "title": "Example",
        "code": "class Student(Person):  pass"
      },
      {
        "type": "note",
        "text": "<p><strong>Note:</strong> Use the <code class=\"w3-codespan\"><a href=\"ref_keyword_pass.html\">pass</a></code> \n  keyword when you do not want to add any other properties or methods to the \n  class.</p>"
      },
      {
        "type": "paragraph",
        "text": "Now the Student class has the same properties and methods as the Person \nclass."
      },
      {
        "type": "example",
        "title": "Example",
        "code": "x = Student(\"Mike\", \"Olsen\")x.printname()"
      },
      {
        "type": "header",
        "level": 2,
        "text": "Add the __init__() Function"
      },
      {
        "type": "paragraph",
        "text": "So far we have created a child class that inherits the properties and methods \nfrom its parent."
      },
      {
        "type": "paragraph",
        "text": "We want to add the <code class=\"w3-codespan\">__init__()</code> function to the child class (instead of the <code class=\"w3-codespan\"><a href=\"ref_keyword_pass.html\">pass</a></code> keyword)."
      },
      {
        "type": "note",
        "text": "<p><strong>Note:</strong> The <code class=\"w3-codespan\">__init__()</code> function is called automatically every time the class is being used to create a new object.</p>"
      },
      {
        "type": "example",
        "title": "Example",
        "code": "class Student(Person):  def __init__(self, fname, lname):    \n  #add properties etc."
      },
      {
        "type": "paragraph",
        "text": "When you add the <code class=\"w3-codespan\">__init__()</code> function, the child class will no longer inherit \nthe parent's <code class=\"w3-codespan\">__init__()</code> function."
      },
      {
        "type": "note",
        "text": "<p><strong>Note:</strong> The child's <code class=\"w3-codespan\">__init__()</code> \n  function <strong>overrides</strong> the inheritance of the parent's <code class=\"w3-codespan\">\n  __init__()</code> function.</p>"
      },
      {
        "type": "paragraph",
        "text": "To keep the inheritance of the parent's <code class=\"w3-codespan\">__init__()</code> \nfunction, add a call to the \nparent's <code class=\"w3-codespan\">__init__()</code> function:"
      },
      {
        "type": "example",
        "title": "Example",
        "code": "class Student(Person):  def __init__(self, fname, lname):    \n  Person.__init__(self, fname, lname)"
      },
      {
        "type": "paragraph",
        "text": "Now we have successfully added the <code class=\"w3-codespan\">__init__()</code> function, and kept the \ninheritance of the parent class, and we are ready to add functionality in the\n<code class=\"w3-codespan\">__init__()</code> function."
      },
      {
        "type": "header",
        "level": 2,
        "text": "Use the super() Function"
      },
      {
        "type": "paragraph",
        "text": "Python also has a <code class=\"w3-codespan\"><a href=\"ref_func_super.html\">super()</a></code> function that \nwill make the child class inherit all the methods and properties from its \nparent:"
      },
      {
        "type": "example",
        "title": "Example",
        "code": "class Student(Person):  def __init__(self, fname, lname):    \n    super().__init__(fname, lname)"
      },
      {
        "type": "paragraph",
        "text": "By using the <code class=\"w3-codespan\"><a href=\"ref_func_super.html\">super()</a></code> function, you do not \nhave to use the name of the parent element, it will automatically inherit the \nmethods and properties from its parent."
      },
      {
        "type": "header",
        "level": 2,
        "text": "Add Properties"
      },
      {
        "type": "example",
        "title": "Example",
        "code": "class Student(Person):  def __init__(self, fname, lname):    \n  super().__init__(fname, lname)    self.graduationyear \n  = 2019"
      },
      {
        "type": "paragraph",
        "text": "In the example below, the year <code class=\"w3-codespan\">2019</code> should be a variable, and passed into the \n<code class=\"w3-codespan\">Student</code> class when creating student objects.\nTo do so, add another parameter in the <code class=\"w3-codespan\">__init__()</code> function:"
      },
      {
        "type": "example",
        "title": "Example",
        "code": "class Student(Person):  def __init__(self, fname, lname, year):    \n  super().__init__(fname, lname)    self.graduationyear \n  = yearx = Student(\"Mike\", \"Olsen\", 2019)"
      },
      {
        "type": "header",
        "level": 2,
        "text": "Add Methods"
      },
      {
        "type": "example",
        "title": "Example",
        "code": "class Student(Person):  def __init__(self, fname, lname, year):    \n  super().__init__(fname, lname)    self.graduationyear \n  = year  def welcome(self):    print(\"Welcome\", \n  self.firstname, self.lastname, \"to the class of\", self.graduationyear)"
      },
      {
        "type": "paragraph",
        "text": "If you add a method in the child class with the same name as a function in \nthe parent class, the inheritance of the parent method will be overridden."
      }
    ]
  },
  "iterators": {
    "title": "Python Iterators",
    "blocks": [
      {
        "type": "header",
        "level": 2,
        "text": "Python Iterators"
      },
      {
        "type": "paragraph",
        "text": "An iterator is an object that contains a countable number of values."
      },
      {
        "type": "paragraph",
        "text": "An iterator is an object that can be iterated upon, meaning that you can \ntraverse through all the values."
      },
      {
        "type": "paragraph",
        "text": "Technically, in Python, an iterator is an object which implements the \niterator protocol, which consist of the methods <code class=\"w3-codespan\">__iter__()</code> \nand <code class=\"w3-codespan\">__next__()</code>."
      },
      {
        "type": "header",
        "level": 2,
        "text": "Iterator vs Iterable"
      },
      {
        "type": "paragraph",
        "text": "Lists, tuples, dictionaries, and sets are all iterable objects. They are iterable\n<em>containers</em> which you can get an iterator from."
      },
      {
        "type": "paragraph",
        "text": "All these objects have a <code class=\"w3-codespan\">iter()</code> method which is used to get an iterator:"
      },
      {
        "type": "example",
        "title": "Example",
        "code": "mytuple = (\"apple\", \"banana\", \"cherry\")myit = iter(mytuple)\n    print(next(myit))print(next(myit))print(next(myit))"
      },
      {
        "type": "paragraph",
        "text": "Even strings are iterable objects, and can return an iterator:"
      },
      {
        "type": "example",
        "title": "Example",
        "code": "mystr = \"banana\"myit = iter(mystr)\n    print(next(myit))print(next(myit))print(next(myit))\n    print(next(myit))print(next(myit))print(next(myit))"
      },
      {
        "type": "header",
        "level": 2,
        "text": "Looping Through an Iterator"
      },
      {
        "type": "paragraph",
        "text": "We can also use a <code class=\"w3-codespan\"><a href=\"ref_keyword_for.html\">for</a></code> loop to iterate through an iterable object:"
      },
      {
        "type": "example",
        "title": "Example",
        "code": "mytuple = (\"apple\", \"banana\", \"cherry\")\n  for x in mytuple:\n      print(x)"
      },
      {
        "type": "example",
        "title": "Example",
        "code": "mystr = \"banana\"\n  for x in mystr:\n      print(x)"
      },
      {
        "type": "paragraph",
        "text": "The <code class=\"w3-codespan\"><a href=\"ref_keyword_for.html\">for</a></code> loop actually creates an iterator object and executes the <code class=\"w3-codespan\"><a href=\"ref_func_next.html\">next()</a></code> \nmethod for each loop."
      },
      {
        "type": "header",
        "level": 2,
        "text": "Create an Iterator"
      },
      {
        "type": "paragraph",
        "text": "To create an object/class as an iterator you have to implement the methods\n<code class=\"w3-codespan\">__iter__()</code> and <code class=\"w3-codespan\">\n__next__()</code> to your object."
      },
      {
        "type": "paragraph",
        "text": "As you will learn in the <a href=\"#/python/classes\">Python \nClasses/Objects</a> chapter, all classes have a function called\n<code class=\"w3-codespan\">__init__()</code>, which allows you to do some \ninitializing when the object is being created."
      },
      {
        "type": "paragraph",
        "text": "The <code class=\"w3-codespan\">__iter__()</code> method acts similar, you can \ndo operations (initializing etc.), but must always return the iterator object \nitself."
      },
      {
        "type": "paragraph",
        "text": "The <code class=\"w3-codespan\">__next__()</code> method also allows you to do \noperations, and must return the next item in the sequence."
      },
      {
        "type": "example",
        "title": "Example",
        "code": "class MyNumbers:  def __iter__(self):    self.a = \n  1    return self  def __next__(self):    \n  x = self.a    self.a += 1    return x\n  myclass = MyNumbers()myiter =\n   iter(myclass)print(next(myiter))\n  print(next(myiter))print(next(myiter))print(next(myiter))\n  print(next(myiter))"
      },
      {
        "type": "header",
        "level": 2,
        "text": "StopIteration"
      },
      {
        "type": "paragraph",
        "text": "The example above would continue forever if you had enough next() statements, or if it was used in a \n<code class=\"w3-codespan\"><a href=\"ref_keyword_for.html\">for</a></code> loop."
      },
      {
        "type": "paragraph",
        "text": "To prevent the iteration from going on forever, we can use the \n<code class=\"w3-codespan\">StopIteration</code> statement."
      },
      {
        "type": "paragraph",
        "text": "In the <code class=\"w3-codespan\">__next__()</code> method, we can add a terminating condition to raise an error if the iteration is done a specified number of times:"
      },
      {
        "type": "example",
        "title": "Example",
        "code": "class MyNumbers:  def __iter__(self):    self.a = \n  1    return self  def __next__(self):    \n  if self.a <= 20:      x = self.a      \n  self.a += 1      return x    \n  else:      raise StopIterationmyclass = \n  MyNumbers()myiter =\n   iter(myclass)for x in myiter:  \n  print(x)"
      }
    ]
  },
  "polymorphism": {
    "title": "Python Polymorphism",
    "blocks": [
      {
        "type": "intro",
        "text": "The word \"polymorphism\" means \"many forms\", and in programming it refers to\nmethods/functions/operators with the same name that can be executed on many \nobjects or classes."
      },
      {
        "type": "header",
        "level": 2,
        "text": "Function Polymorphism"
      },
      {
        "type": "paragraph",
        "text": "An example of a Python function that can be used on different objects is the \n<code class=\"w3-codespan\"><a href=\"ref_func_len.html\">len()</a></code> function."
      },
      {
        "type": "header",
        "level": 3,
        "text": "String"
      },
      {
        "type": "paragraph",
        "text": "For strings <code class=\"w3-codespan\"><a href=\"ref_func_len.html\">len()</a></code> returns the number of characters:"
      },
      {
        "type": "example",
        "title": "Example",
        "code": "x = \"Hello World!\"\n\nprint(len(x))"
      },
      {
        "type": "header",
        "level": 3,
        "text": "Tuple"
      },
      {
        "type": "paragraph",
        "text": "For tuples <code class=\"w3-codespan\"><a href=\"ref_func_len.html\">len()</a></code> returns the number of items \nin the tuple:"
      },
      {
        "type": "example",
        "title": "Example",
        "code": "mytuple = (\"apple\", \"banana\", \"cherry\")\n\nprint(len(mytuple))"
      },
      {
        "type": "header",
        "level": 3,
        "text": "Dictionary"
      },
      {
        "type": "paragraph",
        "text": "For dictionaries <code class=\"w3-codespan\"><a href=\"ref_func_len.html\">len()</a></code> returns the number of key/value pairs \nin the dictionary:"
      },
      {
        "type": "example",
        "title": "Example",
        "code": "thisdict =\t{\n  \"brand\": \"Ford\",\n  \"model\": \"Mustang\",\n  \"year\": 1964\n}\n\nprint(len(thisdict))"
      },
      {
        "type": "header",
        "level": 2,
        "text": "Class Polymorphism"
      },
      {
        "type": "paragraph",
        "text": "Polymorphism is often used in Class methods, where we can have multiple \nclasses with the same method name."
      },
      {
        "type": "paragraph",
        "text": "For example, say we have three classes: <code class=\"w3-codespan\">Car</code>, \n<code class=\"w3-codespan\">Boat</code>, and <code class=\"w3-codespan\">Plane</code>, and they all have \na method called <code class=\"w3-codespan\">move()</code>:"
      },
      {
        "type": "example",
        "title": "Example",
        "code": "class Car:\n  def __init__(self, brand, model):\n    self.brand = brand\n    self.model = model\n\n  def move(self):\n    print(\"Drive!\")\n\nclass Boat:\n  def __init__(self, brand, model):\n    self.brand = brand\n    self.model = model\n\n  def move(self):\n    print(\"Sail!\")\n\nclass Plane:\n  def __init__(self, brand, model):\n    self.brand = brand\n    self.model = model\n\n  def move(self):\n    print(\"Fly!\")\n\ncar1 = Car(\"Ford\", \"Mustang\")        #Create a Car object\nboat1 = Boat(\"Ibiza\", \"Touring 20\") #Create a Boat object\nplane1 = Plane(\"Boeing\", \"747\")     #Create a Plane object\n\nfor x in (car1, boat1, plane1):\n  x.move()"
      },
      {
        "type": "paragraph",
        "text": "Look at the for loop at the end.\nBecause of polymorphism we can execute the same method for all three classes."
      },
      {
        "type": "header",
        "level": 2,
        "text": "Inheritance Class Polymorphism"
      },
      {
        "type": "paragraph",
        "text": "What about classes with child classes with the same name? Can we use polymorphism there?"
      },
      {
        "type": "paragraph",
        "text": "Yes. If we use the example above and make a parent class called \n<code class=\"w3-codespan\">Vehicle</code>, and make <code class=\"w3-codespan\">Car</code>, \n<code class=\"w3-codespan\">Boat</code>, <code class=\"w3-codespan\">Plane</code>\nchild classes of <code class=\"w3-codespan\">Vehicle</code>, the child classes \ninherits the <code class=\"w3-codespan\">Vehicle</code> methods, but can override \nthem:"
      },
      {
        "type": "example",
        "title": "Example",
        "code": "class Vehicle:  def __init__(self, brand, model):    \n    self.brand = brand    self.model = model  \n    def move(self):    print(\"Move!\")class \n    Car(Vehicle):  passclass Boat(Vehicle):  def \n    move(self):    print(\"Sail!\")class \n    Plane(Vehicle):  def move(self):    \n    print(\"Fly!\")car1 = Car(\"Ford\", \"Mustang\")       #Create a Car object\n    boat1 = Boat(\"Ibiza\", \"Touring 20\") #Create a Boat objectplane1 = \n    Plane(\"Boeing\", \"747\")     #Create a Plane objectfor x in (car1, boat1, \n    plane1):  print(x.brand)  print(x.model)  x.move()"
      },
      {
        "type": "paragraph",
        "text": "Child classes inherits the properties and methods from the parent class."
      },
      {
        "type": "paragraph",
        "text": "In the example above you can see that the <code class=\"w3-codespan\">Car</code> class is empty, but \nit inherits <code class=\"w3-codespan\">brand</code>, <code class=\"w3-codespan\">\nmodel</code>, and <code class=\"w3-codespan\">move()</code> from\n<code class=\"w3-codespan\">Vehicle</code>."
      },
      {
        "type": "paragraph",
        "text": "The <code class=\"w3-codespan\">Boat</code> and <code class=\"w3-codespan\">Plane</code> classes also \ninherit <code class=\"w3-codespan\">brand</code>, <code class=\"w3-codespan\">model</code>, \nand <code class=\"w3-codespan\">move()</code> from <code class=\"w3-codespan\">Vehicle</code>, but they both override the \n<code class=\"w3-codespan\">move()</code> method."
      },
      {
        "type": "paragraph",
        "text": "Because of polymorphism we can execute the same method for all classes."
      }
    ]
  },
  "scope": {
    "title": "Python Scope",
    "blocks": [
      {
        "type": "header",
        "level": 2,
        "text": "Scope"
      },
      {
        "type": "paragraph",
        "text": "A variable is only available from inside the region it is \ncreated. This is called <strong>scope</strong>."
      },
      {
        "type": "header",
        "level": 2,
        "text": "Local Scope"
      },
      {
        "type": "paragraph",
        "text": "A variable created inside a function belongs to the <em>local scope</em> of \nthat function, and can only be used inside that function."
      },
      {
        "type": "example",
        "title": "Example",
        "code": "def myfunc():  x = 300  print(x)myfunc()"
      },
      {
        "type": "header",
        "level": 3,
        "text": "Function Inside Function"
      },
      {
        "type": "paragraph",
        "text": "As explained in the example above, the variable <code class=\"w3-codespan\">x</code> is not available outside the function, \nbut it is available for any function inside the function:"
      },
      {
        "type": "example",
        "title": "Example",
        "code": "def myfunc():  x = 300  def myinnerfunc():    print(x)  \n  myinnerfunc()myfunc()"
      },
      {
        "type": "header",
        "level": 2,
        "text": "Global Scope"
      },
      {
        "type": "paragraph",
        "text": "A variable created in the main body of the Python code is a global variable \nand belongs to the global scope."
      },
      {
        "type": "paragraph",
        "text": "Global variables are available from within any scope, global and local."
      },
      {
        "type": "example",
        "title": "Example",
        "code": "x = 300def myfunc():  print(x)myfunc()print(x)"
      },
      {
        "type": "header",
        "level": 3,
        "text": "Naming Variables"
      },
      {
        "type": "paragraph",
        "text": "If you operate with the same variable name inside and outside of a function, Python will treat them as two \nseparate variables,\none available in the global scope (outside the function) and one available in the local scope (inside the function):"
      },
      {
        "type": "example",
        "title": "Example",
        "code": "x = 300def myfunc():  x = 200  \n    print(x)myfunc()print(x)"
      },
      {
        "type": "header",
        "level": 2,
        "text": "Global Keyword"
      },
      {
        "type": "paragraph",
        "text": "If you need to create a global variable, but are stuck in the local scope, you can use the \n<code class=\"w3-codespan\"><a href=\"ref_keyword_global.html\">global</a></code> keyword."
      },
      {
        "type": "paragraph",
        "text": "The <code class=\"w3-codespan\"><a href=\"ref_keyword_global.html\">global</a></code> keyword makes the variable global."
      },
      {
        "type": "example",
        "title": "Example",
        "code": "def myfunc():  global x  x = 300myfunc()\n    print(x)"
      },
      {
        "type": "paragraph",
        "text": "Also, use the <code class=\"w3-codespan\">global</code> keyword if you want to \nmake a change to a global variable inside a function."
      },
      {
        "type": "example",
        "title": "Example",
        "code": "x = 300def myfunc():  global x  x = 200myfunc()\n  print(x)"
      },
      {
        "type": "header",
        "level": 2,
        "text": "Nonlocal Keyword"
      },
      {
        "type": "paragraph",
        "text": "The <code class=\"w3-codespan\">nonlocal</code> keyword is used to work with variables inside nested functions."
      },
      {
        "type": "paragraph",
        "text": "The <code class=\"w3-codespan\">nonlocal</code> keyword makes the variable belong to the outer function."
      },
      {
        "type": "example",
        "title": "Example",
        "code": "def myfunc1():\n  x = \"Jane\"\n  def myfunc2():\n    nonlocal x\n    x = \"hello\"\n  myfunc2()\n  return x\n\nprint(myfunc1())"
      },
      {
        "type": "header",
        "level": 2,
        "text": "The LEGB Rule"
      },
      {
        "type": "paragraph",
        "text": "Python follows the LEGB rule when looking up variable names, and searches for them in this order:"
      },
      {
        "type": "list",
        "ordered": true,
        "items": [
          "<strong>L</strong>ocal - Inside the current function",
          "<strong>E</strong>nclosing - Inside enclosing functions (from inner to outer)",
          "<strong>G</strong>lobal - At the top level of the module",
          "<strong>B</strong>uilt-in - In Python's built-in namespace"
        ]
      },
      {
        "type": "example",
        "title": "Example",
        "code": "x = \"global\"\n\ndef outer():\n  x = \"enclosing\"\n  def inner():\n    x = \"local\"\n    print(\"Inner:\", x)\n  inner()\n  print(\"Outer:\", x)\n\nouter()\nprint(\"Global:\", x)"
      }
    ]
  },
  "modules": {
    "title": "Python Modules",
    "blocks": [
      {
        "type": "header",
        "level": 2,
        "text": "What is a Module?"
      },
      {
        "type": "paragraph",
        "text": "Consider a module to be the same as a code library."
      },
      {
        "type": "paragraph",
        "text": "A file containing a set of functions you want to include in your application."
      },
      {
        "type": "header",
        "level": 2,
        "text": "Create a Module"
      },
      {
        "type": "paragraph",
        "text": "To create a module just save the code you want in a file with the file extension <code class=\"w3-codespan\">.py</code>:"
      },
      {
        "type": "example",
        "title": "Example",
        "code": "def greeting(name):  print(\"Hello, \" + name)"
      },
      {
        "type": "header",
        "level": 2,
        "text": "Use a Module"
      },
      {
        "type": "paragraph",
        "text": "Now we can use the module we just created, by using the <code class=\"w3-codespan\"><a href=\"ref_keyword_import.html\">import</a></code> statement:"
      },
      {
        "type": "example",
        "title": "Example",
        "code": "import mymodulemymodule.greeting(\"Jonathan\")"
      },
      {
        "type": "note",
        "text": "<p><strong>Note:</strong> When using a function from a module, use the syntax: <em>module_name.function_name</em>.</p>"
      },
      {
        "type": "header",
        "level": 2,
        "text": "Variables in Module"
      },
      {
        "type": "paragraph",
        "text": "The module can contain functions, as already described, but also variables of \nall types (arrays, dictionaries, objects etc):"
      },
      {
        "type": "example",
        "title": "Example",
        "code": "person1 = {  \"name\": \"John\",  \"age\": 36,  \n  \"country\": \"Norway\"}"
      },
      {
        "type": "example",
        "title": "Example",
        "code": "import mymodulea = mymodule.person1[\"age\"]print(a)"
      },
      {
        "type": "header",
        "level": 2,
        "text": "Naming a Module"
      },
      {
        "type": "paragraph",
        "text": "You can name the module file whatever you like, but it must have the file extension \n<code class=\"w3-codespan\">.py</code>"
      },
      {
        "type": "header",
        "level": 2,
        "text": "Re-naming a Module"
      },
      {
        "type": "paragraph",
        "text": "You can create an alias when you import a module, by using the <code class=\"w3-codespan\"><a href=\"ref_keyword_as.html\">as</a></code> keyword:"
      },
      {
        "type": "example",
        "title": "Example",
        "code": "import mymodule as mxa = mx.person1[\"age\"]print(a)"
      },
      {
        "type": "header",
        "level": 2,
        "text": "Built-in Modules"
      },
      {
        "type": "paragraph",
        "text": "There are several built-in modules in Python, which you can import whenever you like."
      },
      {
        "type": "example",
        "title": "Example",
        "code": "import platformx = platform.system()print(x)"
      },
      {
        "type": "header",
        "level": 2,
        "text": "Using the dir() Function"
      },
      {
        "type": "paragraph",
        "text": "There is a built-in function to list all the function names (or variable \nnames) in a module. The <code class=\"w3-codespan\"><a href=\"ref_func_dir.html\">dir()</a></code> function:"
      },
      {
        "type": "example",
        "title": "Example",
        "code": "import platformx = dir(platform)print(x)"
      },
      {
        "type": "note",
        "text": "<p><strong>Note:</strong> The <code class=\"w3-codespan\"><a href=\"ref_func_dir.html\">dir()</a></code> function can be used on <em>all</em> \n  modules, also the ones you create yourself.</p>"
      },
      {
        "type": "header",
        "level": 2,
        "text": "Import From Module"
      },
      {
        "type": "paragraph",
        "text": "You can choose to import only parts from a module, by using the <code class=\"w3-codespan\"><a href=\"ref_keyword_from.html\">from</a></code> keyword."
      },
      {
        "type": "example",
        "title": "Example",
        "code": "def greeting(name):  print(\"Hello, \" + name)person1 \n  = {  \"name\": \"John\",  \"age\": 36,  \"country\": \n  \"Norway\"}"
      },
      {
        "type": "example",
        "title": "Example",
        "code": "from mymodule import person1print (person1[\"age\"])"
      },
      {
        "type": "note",
        "text": "<p><strong>Note:</strong> When importing using the <code class=\"w3-codespan\"><a href=\"ref_keyword_from.html\">from</a></code> \n  keyword, do not use the module name when referring to elements in the module. \n  Example: <code class=\"w3-codespan\">person1[\"age\"]</code>, <strong>not</strong>\n  <code class=\"w3-codespan\"><strike>mymodule.person1[\"age\"]</strike></code></p>"
      }
    ]
  },
  "dates": {
    "title": "Python Datetime",
    "blocks": [
      {
        "type": "header",
        "level": 2,
        "text": "Python Dates"
      },
      {
        "type": "paragraph",
        "text": "A date in Python is not a data type of its own, but we can import a module \nnamed <code class=\"w3-codespan\">datetime</code> to work with dates as date \nobjects."
      },
      {
        "type": "example",
        "title": "Example",
        "code": "import datetimex = datetime.datetime.now()print(x)"
      },
      {
        "type": "header",
        "level": 2,
        "text": "Date Output"
      },
      {
        "type": "paragraph",
        "text": "When we execute the code from the example above the result will be:"
      },
      {
        "type": "paragraph",
        "text": "The date contains year, month, day, hour, minute, second, and microsecond."
      },
      {
        "type": "paragraph",
        "text": "The <code class=\"w3-codespan\">datetime</code> module has many methods to return information about the date \nobject."
      },
      {
        "type": "paragraph",
        "text": "Here are a few examples, you will learn more about them later in this \nchapter:"
      },
      {
        "type": "example",
        "title": "Example",
        "code": "import datetimex = datetime.datetime.now()print(x.year)\n  print(x.strftime(\"%A\"))"
      },
      {
        "type": "header",
        "level": 2,
        "text": "Creating Date Objects"
      },
      {
        "type": "paragraph",
        "text": "To create a date, we can use the <code class=\"w3-codespan\">datetime()</code> class (constructor) of the\n<code class=\"w3-codespan\">datetime</code> module."
      },
      {
        "type": "paragraph",
        "text": "The <code class=\"w3-codespan\">datetime()</code> class requires three parameters to create a date: year, \nmonth, day."
      },
      {
        "type": "example",
        "title": "Example",
        "code": "import datetimex = datetime.datetime(2020, 5, 17)\n  print(x)"
      },
      {
        "type": "paragraph",
        "text": "The <code class=\"w3-codespan\">datetime()</code> class also takes parameters for time and timezone (hour, \nminute, second, microsecond, tzone), but they are optional, and has a default \nvalue of <code class=\"w3-codespan\">0</code>, (<code class=\"w3-codespan\">None</code> for timezone)."
      },
      {
        "type": "header",
        "level": 2,
        "text": "The strftime() Method"
      },
      {
        "type": "paragraph",
        "text": "The <code class=\"w3-codespan\">datetime</code> object has a method for formatting date objects into readable strings."
      },
      {
        "type": "paragraph",
        "text": "The method is called <code class=\"w3-codespan\">strftime()</code>, and takes one parameter, \n<code class=\"w3-codespan\">format</code>, to specify the format of the returned string:"
      },
      {
        "type": "example",
        "title": "Example",
        "code": "import datetimex = datetime.datetime(2018, 6, 1)print(x.strftime(\"%B\"))"
      },
      {
        "type": "paragraph",
        "text": "A reference of all the legal format codes:"
      },
      {
        "type": "table",
        "headers": [
          "Directive",
          "Description",
          "Example",
          "Try it"
        ],
        "rows": [
          [
            "%a",
            "Weekday, short version",
            "Wed",
            "<a target=\"_blank\" class=\"w3-btn btnsmall btnsmall\" href=\"trypythonf158.html?filename=demo_datetime_strftime_a\">Try it »</a>"
          ],
          [
            "%A",
            "Weekday, full version",
            "Wednesday",
            "<a target=\"_blank\" class=\"w3-btn btnsmall btnsmall\" href=\"trypythond7be.html?filename=demo_datetime_strftime_a2\">Try it »</a>"
          ],
          [
            "%w",
            "Weekday as a number 0-6, 0 is Sunday",
            "3",
            "<a target=\"_blank\" class=\"w3-btn btnsmall btnsmall\" href=\"trypythonbf63.html?filename=demo_datetime_strftime_w\">Try it »</a>"
          ],
          [
            "%d",
            "Day of month 01-31",
            "31",
            "<a target=\"_blank\" class=\"w3-btn btnsmall btnsmall\" href=\"trypythonbaaa.html?filename=demo_datetime_strftime_d\">Try it »</a>"
          ],
          [
            "%b",
            "Month name, short version",
            "Dec",
            "<a target=\"_blank\" class=\"w3-btn btnsmall btnsmall\" href=\"trypython2be5.html?filename=demo_datetime_strftime_b\">Try it »</a>"
          ],
          [
            "%B",
            "Month name, full version",
            "December",
            "<a target=\"_blank\" class=\"w3-btn btnsmall btnsmall\" href=\"trypythondf35.html?filename=demo_datetime_strftime_b2\">Try it »</a>"
          ],
          [
            "%m",
            "Month as a number 01-12",
            "12",
            "<a target=\"_blank\" class=\"w3-btn btnsmall btnsmall\" href=\"trypython34aa.html?filename=demo_datetime_strftime_m\">Try it »</a>"
          ],
          [
            "%y",
            "Year, short version, without century",
            "18",
            "<a target=\"_blank\" class=\"w3-btn btnsmall btnsmall\" href=\"trypython21cf.html?filename=demo_datetime_strftime_y\">Try it »</a>"
          ],
          [
            "%Y",
            "Year, full version",
            "2018",
            "<a target=\"_blank\" class=\"w3-btn btnsmall btnsmall\" href=\"trypython8657.html?filename=demo_datetime_strftime_y2\">Try it »</a>"
          ],
          [
            "%H",
            "Hour 00-23",
            "17",
            "<a target=\"_blank\" class=\"w3-btn btnsmall btnsmall\" href=\"trypython6cfc.html?filename=demo_datetime_strftime_h2\">Try it »</a>"
          ],
          [
            "%I",
            "Hour 00-12",
            "05",
            "<a target=\"_blank\" class=\"w3-btn btnsmall btnsmall\" href=\"trypythonf900.html?filename=demo_datetime_strftime_i2\">Try it »</a>"
          ],
          [
            "%p",
            "AM/PM",
            "PM",
            "<a target=\"_blank\" class=\"w3-btn btnsmall btnsmall\" href=\"trypython581f-2.html?filename=demo_datetime_strftime_p\">Try it »</a>"
          ],
          [
            "%M",
            "Minute 00-59",
            "41",
            "<a target=\"_blank\" class=\"w3-btn btnsmall btnsmall\" href=\"trypython470b.html?filename=demo_datetime_strftime_m2\">Try it »</a>"
          ],
          [
            "%S",
            "Second 00-59",
            "08",
            "<a target=\"_blank\" class=\"w3-btn btnsmall btnsmall\" href=\"trypythonc419.html?filename=demo_datetime_strftime_s2\">Try it »</a>"
          ],
          [
            "%f",
            "Microsecond 000000-999999",
            "548513",
            "<a target=\"_blank\" class=\"w3-btn btnsmall btnsmall\" href=\"trypythonccd2.html?filename=demo_datetime_strftime_f\">Try it »</a>"
          ],
          [
            "%z",
            "UTC offset",
            "+0100",
            ""
          ],
          [
            "%Z",
            "Timezone",
            "CST",
            ""
          ],
          [
            "%j",
            "Day number of year 001-366",
            "365",
            "<a target=\"_blank\" class=\"w3-btn btnsmall btnsmall\" href=\"trypython5403.html?filename=demo_datetime_strftime_j\">Try it »</a>"
          ],
          [
            "%U",
            "Week number of year, Sunday as the first day of week, 00-53",
            "52",
            "<a target=\"_blank\" class=\"w3-btn btnsmall btnsmall\" href=\"trypython2f9f.html?filename=demo_datetime_strftime_u2\">Try it »</a>"
          ],
          [
            "%W",
            "Week number of year, Monday as the first day of week, 00-53",
            "52",
            "<a target=\"_blank\" class=\"w3-btn btnsmall btnsmall\" href=\"trypython0013.html?filename=demo_datetime_strftime_w2\">Try it »</a>"
          ],
          [
            "%c",
            "Local version of date and time",
            "Mon Dec 31 17:41:00 2018",
            "<a target=\"_blank\" class=\"w3-btn btnsmall btnsmall\" href=\"trypythonac10.html?filename=demo_datetime_strftime_c\">Try it »</a>"
          ],
          [
            "%C",
            "Century",
            "20",
            "<a target=\"_blank\" class=\"w3-btn btnsmall btnsmall\" href=\"trypythonf683.html?filename=demo_datetime_strftime_century\">Try it »</a>"
          ],
          [
            "%x",
            "Local version of date",
            "12/31/18",
            "<a target=\"_blank\" class=\"w3-btn btnsmall btnsmall\" href=\"trypython23a6.html?filename=demo_datetime_strftime_x\">Try it »</a>"
          ],
          [
            "%X",
            "Local version of time",
            "17:41:00",
            "<a target=\"_blank\" class=\"w3-btn btnsmall btnsmall\" href=\"trypython7af0.html?filename=demo_datetime_strftime_x2\">Try it »</a>"
          ],
          [
            "%%",
            "A % character",
            "%",
            "<a target=\"_blank\" class=\"w3-btn btnsmall btnsmall\" href=\"trypython851e.html?filename=demo_datetime_strftime_percent\">Try it »</a>"
          ],
          [
            "%G",
            "ISO 8601 year",
            "2018",
            "<a target=\"_blank\" class=\"w3-btn btnsmall btnsmall\" href=\"trypython301b.html?filename=demo_datetime_strftime_g\">Try it »</a>"
          ],
          [
            "%u",
            "ISO 8601 weekday (1-7)",
            "1",
            "<a target=\"_blank\" class=\"w3-btn btnsmall btnsmall\" href=\"trypythonddc5.html?filename=demo_datetime_strftime_u\">Try it »</a>"
          ],
          [
            "%V",
            "ISO 8601 weeknumber (01-53)",
            "01",
            "<a target=\"_blank\" class=\"w3-btn btnsmall btnsmall\" href=\"trypython62cf.html?filename=demo_datetime_strftime_v\">Try it »</a>"
          ]
        ]
      }
    ]
  },
  "math": {
    "title": "Python Math",
    "blocks": [
      {
        "type": "intro",
        "text": "Python has a set of built-in math functions, including an extensive math module, that allows you to perform mathematical tasks on numbers."
      },
      {
        "type": "header",
        "level": 2,
        "text": "Built-in Math Functions"
      },
      {
        "type": "paragraph",
        "text": "The <code class=\"w3-codespan\"><a href=\"ref_func_min.html\">min()</a></code> and <code class=\"w3-codespan\"><a href=\"ref_func_max.html\">max()</a></code> functions can be used to find the lowest or highest value in an iterable:"
      },
      {
        "type": "example",
        "title": "Example",
        "code": "x = min(5, 10, 25)y = max(5, 10, 25)print(x)print(y)"
      },
      {
        "type": "paragraph",
        "text": "The <code class=\"w3-codespan\"><a href=\"ref_func_abs.html\">abs()</a></code> function returns the absolute (positive) value of the specified number:"
      },
      {
        "type": "example",
        "title": "Example",
        "code": "x = abs(-7.25)print(x)"
      },
      {
        "type": "paragraph",
        "text": "The <code class=\"w3-codespan\">pow(<em>x</em>, <em>y</em>)</code> function returns the value of x to the power of y (x<sup>y</sup>)."
      },
      {
        "type": "example",
        "title": "Example",
        "code": "x = pow(4, 3)print(x)"
      },
      {
        "type": "header",
        "level": 2,
        "text": "The Math Module"
      },
      {
        "type": "paragraph",
        "text": "Python has also a built-in module called <code class=\"w3-codespan\">math</code>, which extends the list of mathematical functions."
      },
      {
        "type": "paragraph",
        "text": "To use it, you must import the <code class=\"w3-codespan\">math</code> module:"
      },
      {
        "type": "example",
        "title": "Example",
        "code": "import math"
      },
      {
        "type": "paragraph",
        "text": "When you have imported the <code class=\"w3-codespan\">math</code> module, you \ncan start using methods and constants of the module."
      },
      {
        "type": "paragraph",
        "text": "The <code class=\"w3-codespan\">math.sqrt()</code> method for example, returns the square root of a number:"
      },
      {
        "type": "example",
        "title": "Example",
        "code": "import \n    mathx = math.sqrt(64)print(x)"
      },
      {
        "type": "paragraph",
        "text": "The <code class=\"w3-codespan\">math.ceil()</code> method rounds a number upwards to \nits nearest integer, and the <code class=\"w3-codespan\">math.floor()</code> \nmethod rounds a number downwards to its nearest integer, and returns the result:"
      },
      {
        "type": "example",
        "title": "Example",
        "code": "import \n    mathx = math.ceil(1.4)y = math.floor(1.4)print(x) # \n    returns 2print(y) # returns 1"
      },
      {
        "type": "paragraph",
        "text": "The <code class=\"w3-codespan\">math.pi</code> constant, returns the value of \nPI (3.14...):"
      },
      {
        "type": "example",
        "title": "Example",
        "code": "import \n    mathx = math.piprint(x)"
      },
      {
        "type": "header",
        "level": 2,
        "text": "Complete Math Module Reference"
      },
      {
        "type": "paragraph",
        "text": "In our <a href=\"module_math.html\">Math Module Reference</a> you will \nfind a complete reference of all methods and constants that belongs to the Math module."
      }
    ]
  },
  "json": {
    "title": "Python JSON",
    "blocks": [
      {
        "type": "intro",
        "text": "JSON is a syntax for storing and exchanging data."
      },
      {
        "type": "intro",
        "text": "JSON is text, written with JavaScript object notation."
      },
      {
        "type": "header",
        "level": 2,
        "text": "JSON in Python"
      },
      {
        "type": "paragraph",
        "text": "Python has a built-in package called <code class=\"w3-codespan\">json</code>, which can be used to work with JSON data."
      },
      {
        "type": "example",
        "title": "Example",
        "code": "import json"
      },
      {
        "type": "header",
        "level": 2,
        "text": "Parse JSON - Convert from JSON to Python"
      },
      {
        "type": "paragraph",
        "text": "If you have a JSON string, you can parse it by using the\n<code class=\"w3-codespan\">json.loads()</code> method."
      },
      {
        "type": "note",
        "text": "<p>The result will be a <a href=\"#/python/dictionaries\">Python dictionary</a>.</p>"
      },
      {
        "type": "example",
        "title": "Example",
        "code": "import json# some JSON:x =  '{ \"name\":\"John\", \"age\":30, \"city\":\"New \n  York\"}'# parse x:y = json.loads(x)# the result is a \n  Python dictionary:print(y[\"age\"])"
      },
      {
        "type": "header",
        "level": 2,
        "text": "Convert from Python to JSON"
      },
      {
        "type": "paragraph",
        "text": "If you have a Python object, you can convert it into a JSON string by \nusing the <code class=\"w3-codespan\">json.dumps()</code> method."
      },
      {
        "type": "example",
        "title": "Example",
        "code": "import json# a Python object (dict):x = {  \"name\": \n  \"John\",  \"age\": 30,  \"city\": \"New York\"}# \n  convert into JSON:y = json.dumps(x)# the result is a JSON string:\n  print(y)"
      },
      {
        "type": "paragraph",
        "text": "You can convert Python objects of the following types, into JSON strings:"
      },
      {
        "type": "list",
        "ordered": false,
        "items": [
          "dict",
          "list",
          "tuple",
          "string",
          "int",
          "float",
          "True",
          "False",
          "None"
        ]
      },
      {
        "type": "example",
        "title": "Example",
        "code": "import jsonprint(json.dumps({\"name\": \"John\", \"age\": 30}))print(json.dumps([\"apple\", \n  \"bananas\"]))print(json.dumps((\"apple\", \"bananas\")))\n  print(json.dumps(\"hello\"))print(json.dumps(42))print(json.dumps(31.76))print(json.dumps(True))print(json.dumps(False))print(json.dumps(None))"
      },
      {
        "type": "paragraph",
        "text": "When you convert from Python to JSON, Python objects are converted into the JSON (JavaScript) equivalent:"
      },
      {
        "type": "table",
        "headers": [
          "Python",
          "JSON"
        ],
        "rows": [
          [
            "dict",
            "Object"
          ],
          [
            "list",
            "Array"
          ],
          [
            "tuple",
            "Array"
          ],
          [
            "str",
            "String"
          ],
          [
            "int",
            "Number"
          ],
          [
            "float",
            "Number"
          ],
          [
            "True",
            "true"
          ],
          [
            "False",
            "false"
          ],
          [
            "None",
            "null"
          ]
        ]
      },
      {
        "type": "example",
        "title": "Example",
        "code": "import jsonx = {  \"name\": \n  \"John\",  \"age\": 30,  \"married\": True,  \n  \"divorced\": False,  \"children\": (\"Ann\",\"Billy\"),  \"pets\": \n  None,  \"cars\": [    {\"model\": \"BMW 230\", \"mpg\": \n  27.5},    {\"model\": \"Ford Edge\", \"mpg\": 24.1}  ]\n  }print(json.dumps(x))"
      },
      {
        "type": "header",
        "level": 2,
        "text": "Format the Result"
      },
      {
        "type": "paragraph",
        "text": "The example above prints a JSON string, but it is not very easy to read, with no indentations and line breaks."
      },
      {
        "type": "paragraph",
        "text": "The <code class=\"w3-codespan\">json.dumps()</code> method has parameters to \nmake it easier to read the result:"
      },
      {
        "type": "example",
        "title": "Example",
        "code": "json.dumps(x, indent=4)"
      },
      {
        "type": "paragraph",
        "text": "You can also define the separators, default value is (\", \", \": \"), which \nmeans using a comma and a space to separate each object, and a colon and a space \nto separate keys from values:"
      },
      {
        "type": "example",
        "title": "Example",
        "code": "json.dumps(x, indent=4, separators=(\". \", \" = \"))"
      },
      {
        "type": "header",
        "level": 2,
        "text": "Order the Result"
      },
      {
        "type": "paragraph",
        "text": "The <code class=\"w3-codespan\">json.dumps()</code> method has parameters to \norder the keys in the result:"
      },
      {
        "type": "example",
        "title": "Example",
        "code": "json.dumps(x, indent=4, sort_keys=True)"
      }
    ]
  },
  "regex": {
    "title": "Python RegEx",
    "blocks": [
      {
        "type": "intro",
        "text": "A RegEx, or Regular Expression, is a sequence of characters that forms a search pattern."
      },
      {
        "type": "intro",
        "text": "RegEx can be used to check if a string contains the specified search pattern."
      },
      {
        "type": "header",
        "level": 2,
        "text": "RegEx Module"
      },
      {
        "type": "paragraph",
        "text": "Python has a built-in package called <code class=\"w3-codespan\">re</code>, which can be used to work with \nRegular Expressions."
      },
      {
        "type": "paragraph",
        "text": "Import the <code class=\"w3-codespan\">re</code> module:"
      },
      {
        "type": "example",
        "title": "Example",
        "code": "import re"
      },
      {
        "type": "header",
        "level": 2,
        "text": "RegEx in Python"
      },
      {
        "type": "paragraph",
        "text": "When you have imported the <code class=\"w3-codespan\">re</code> module, you \ncan start using regular expressions:"
      },
      {
        "type": "example",
        "title": "Example",
        "code": "import \n    retxt = \"The rain in Spain\"x = re.search(\"^The.*Spain$\", txt)"
      },
      {
        "type": "header",
        "level": 2,
        "text": "RegEx Functions"
      },
      {
        "type": "paragraph",
        "text": "The <code class=\"w3-codespan\">re</code> module offers a set of functions that allows \nus to search a string for a match:"
      },
      {
        "type": "table",
        "headers": [
          "Function",
          "Description"
        ],
        "rows": [
          [
            "<a href=\"#findall\">findall</a>",
            "Returns a list containing all matches"
          ],
          [
            "<a href=\"#search\">search</a>",
            "Returns a <a href=\"#matchobject\">Match object</a> if there is a match anywhere in the string"
          ],
          [
            "<a href=\"#split\">split</a>",
            "Returns a list where the string has been split at each match"
          ],
          [
            "<a href=\"#sub\">sub</a>",
            "Replaces one or many matches with a string"
          ]
        ]
      },
      {
        "type": "header",
        "level": 2,
        "text": "Metacharacters"
      },
      {
        "type": "paragraph",
        "text": "Metacharacters are characters with a special meaning:"
      },
      {
        "type": "table",
        "headers": [
          "Character",
          "Description",
          "Example",
          "Try it"
        ],
        "rows": [
          [
            "[]",
            "A set of characters",
            "\"[a-m]\"",
            "<a target=\"_blank\" class=\"w3-btn btnsmall btnsmall\" href=\"trypythoncb9d.html?filename=demo_regex_meta1\">Try it »</a>"
          ],
          [
            "\\",
            "Signals a special sequence (can also be used to escape special characters)",
            "\"\\d\"",
            "<a target=\"_blank\" class=\"w3-btn btnsmall btnsmall\" href=\"trypythondc71.html?filename=demo_regex_meta2\">Try it »</a>"
          ],
          [
            ".",
            "Any character (except newline character)",
            "\"he..o\"",
            "<a target=\"_blank\" class=\"w3-btn btnsmall btnsmall\" href=\"trypythone5c5.html?filename=demo_regex_meta3\">Try it »</a>"
          ],
          [
            "^",
            "Starts with",
            "\"^hello\"",
            "<a target=\"_blank\" class=\"w3-btn btnsmall btnsmall\" href=\"trypython99b3.html?filename=demo_regex_meta4\">Try it »</a>"
          ],
          [
            "$",
            "Ends with",
            "\"planet$\"",
            "<a target=\"_blank\" class=\"w3-btn btnsmall btnsmall\" href=\"trypython3d24.html?filename=demo_regex_meta5\">Try it »</a>"
          ],
          [
            "*",
            "Zero or more occurrences",
            "\"he.*o\"",
            "<a target=\"_blank\" class=\"w3-btn btnsmall btnsmall\" href=\"trypythone584.html?filename=demo_regex_meta6\">Try it »</a>"
          ],
          [
            "+",
            "One or more occurrences",
            "\"he.+o\"",
            "<a target=\"_blank\" class=\"w3-btn btnsmall btnsmall\" href=\"trypythond4f5.html?filename=demo_regex_meta7\">Try it »</a>"
          ],
          [
            "?",
            "Zero or one occurrences",
            "\"he.?o\"",
            "<a target=\"_blank\" class=\"w3-btn btnsmall btnsmall\" href=\"trypython7646.html?filename=demo_regex_meta10\">Try it »</a>"
          ],
          [
            "{}",
            "Exactly the specified number of occurrences",
            "\"he.{2}o\"",
            "<a target=\"_blank\" class=\"w3-btn btnsmall btnsmall\" href=\"trypython5b50.html?filename=demo_regex_meta8\">Try it »</a>"
          ],
          [
            "|",
            "Either or",
            "\"falls|stays\"",
            "<a target=\"_blank\" class=\"w3-btn btnsmall btnsmall\" href=\"trypython4f94.html?filename=demo_regex_meta9\">Try it »</a>"
          ],
          [
            "()",
            "Capture and group",
            "&nbsp;",
            "&nbsp;"
          ]
        ]
      },
      {
        "type": "header",
        "level": 2,
        "text": "Flags"
      },
      {
        "type": "paragraph",
        "text": "You can add flags to the pattern when using regular expressions."
      },
      {
        "type": "table",
        "headers": [
          "Flag",
          "Shorthand",
          "Description",
          "Try it"
        ],
        "rows": [
          [
            "re.ASCII",
            "re.A",
            "Returns only ASCII matches",
            "<a target=\"_blank\" class=\"w3-btn btnsmall btnsmall\" href=\"trypythond618.html?filename=demo_regex_ascii\">Try it »</a>"
          ],
          [
            "re.DEBUG",
            "",
            "Returns debug information",
            "<a target=\"_blank\" class=\"w3-btn btnsmall btnsmall\" href=\"trypython5d02.html?filename=demo_regex_debug\">Try it »</a>"
          ],
          [
            "re.DOTALL",
            "re.S",
            "Makes the . character match all characters (including newline character)",
            "<a target=\"_blank\" class=\"w3-btn btnsmall btnsmall\" href=\"trypythondbda.html?filename=demo_regex_dotall\">Try it »</a>"
          ],
          [
            "re.IGNORECASE",
            "re.I",
            "Case-insensitive matching",
            "<a target=\"_blank\" class=\"w3-btn btnsmall btnsmall\" href=\"trypython2827.html?filename=demo_regex_ignorecase\">Try it »</a>"
          ],
          [
            "re.MULTILINE",
            "re.M",
            "Returns only matches at the beginning of each line",
            "<a target=\"_blank\" class=\"w3-btn btnsmall btnsmall\" href=\"trypython2179.html?filename=demo_regex_multiline\">Try it »</a>"
          ],
          [
            "re.NOFLAG",
            "",
            "Specifies that no flag is set for this pattern",
            ""
          ],
          [
            "re.UNICODE",
            "re.U",
            "Returns Unicode matches. This is default from Python 3. For Python 2: use this flag to return only Unicode matches",
            "<a target=\"_blank\" class=\"w3-btn btnsmall btnsmall\" href=\"trypythonacf2.html?filename=demo_regex_unicode\">Try it »</a>"
          ],
          [
            "re.VERBOSE",
            "re.X",
            "Allows whitespaces and comments inside patterns. Makes the pattern more readable",
            "<a target=\"_blank\" class=\"w3-btn btnsmall btnsmall\" href=\"trypython0bae.html?filename=demo_regex_verbose\">Try it »</a>"
          ]
        ]
      },
      {
        "type": "header",
        "level": 2,
        "text": "Special Sequences"
      },
      {
        "type": "paragraph",
        "text": "A special sequence is a <code class=\"w3-codespan\">\\</code> followed by one of the characters in the list below, and has a special meaning:"
      },
      {
        "type": "table",
        "headers": [
          "Character",
          "Description",
          "Example",
          "Try it"
        ],
        "rows": [
          [
            "\\A",
            "Returns a match if the specified characters are at the beginning of the \nstring",
            "\"\\AThe\"",
            "<a target=\"_blank\" class=\"w3-btn btnsmall btnsmall\" href=\"trypythonad04.html?filename=demo_regex_seq1\">Try it »</a>"
          ],
          [
            "\\b",
            "Returns a match where the specified characters are at the beginning or at the \nend of a word<br>(the \"r\" in the beginning is making sure that the string is \nbeing treated as a \"raw string\")",
            "r\"\\bain\"<br><br>r\"ain\\b\"",
            "<a target=\"_blank\" class=\"w3-btn btnsmall btnsmall\" href=\"trypythonb50a.html?filename=demo_regex_seq2\">Try it »</a>\n<br><br>\n<a target=\"_blank\" class=\"w3-btn btnsmall btnsmall\" href=\"trypythonb6e3.html?filename=demo_regex_seq2-2\">Try it »</a>"
          ],
          [
            "\\B",
            "Returns a match where the specified characters are present, but NOT at the beginning \n(or at \nthe end) of a word<br>(the \"r\" in the beginning is making sure that the string \nis being treated as a \"raw string\")",
            "r\"\\Bain\"<br><br>r\"ain\\B\"",
            "<a target=\"_blank\" class=\"w3-btn btnsmall btnsmall\" href=\"trypython00c7.html?filename=demo_regex_seq3\">Try it »</a>\n<br><br>\n<a target=\"_blank\" class=\"w3-btn btnsmall btnsmall\" href=\"trypythona44c.html?filename=demo_regex_seq3-2\">Try it »</a>"
          ],
          [
            "\\d",
            "Returns a match where the string contains digits (numbers from 0-9)",
            "\"\\d\"",
            "<a target=\"_blank\" class=\"w3-btn btnsmall btnsmall\" href=\"trypython3449.html?filename=demo_regex_seq4\">Try it »</a>"
          ],
          [
            "\\D",
            "Returns a match where the string DOES NOT contain digits",
            "\"\\D\"",
            "<a target=\"_blank\" class=\"w3-btn btnsmall btnsmall\" href=\"trypython45d5.html?filename=demo_regex_seq5\">Try it »</a>"
          ],
          [
            "\\s",
            "Returns a match where the string contains a white space character",
            "\"\\s\"",
            "<a target=\"_blank\" class=\"w3-btn btnsmall btnsmall\" href=\"trypython1898.html?filename=demo_regex_seq6\">Try it »</a>"
          ],
          [
            "\\S",
            "Returns a match where the string DOES NOT contain a white space character",
            "\"\\S\"",
            "<a target=\"_blank\" class=\"w3-btn btnsmall btnsmall\" href=\"trypythoneb28.html?filename=demo_regex_seq7\">Try it »</a>"
          ],
          [
            "\\w",
            "Returns a match where the string contains any word characters (characters from \na to Z, digits from 0-9, and the underscore _ character)",
            "\"\\w\"",
            "<a target=\"_blank\" class=\"w3-btn btnsmall btnsmall\" href=\"trypython087d.html?filename=demo_regex_seq8\">Try it »</a>"
          ],
          [
            "\\W",
            "Returns a match where the string DOES NOT contain any word characters",
            "\"\\W\"",
            "<a target=\"_blank\" class=\"w3-btn btnsmall btnsmall\" href=\"trypython9b06.html?filename=demo_regex_seq9\">Try it »</a>"
          ],
          [
            "\\Z",
            "Returns a match if the specified characters are at the end of the string",
            "\"Spain\\Z\"",
            "<a target=\"_blank\" class=\"w3-btn btnsmall btnsmall\" href=\"trypythonac62.html?filename=demo_regex_seq10\">Try it »</a>"
          ]
        ]
      },
      {
        "type": "header",
        "level": 2,
        "text": "Sets"
      },
      {
        "type": "paragraph",
        "text": "A set is a set of characters inside a pair of square brackets <code class=\"w3-codespan\">\n[]</code> with a special meaning:"
      },
      {
        "type": "table",
        "headers": [
          "Set",
          "Description",
          "Try it"
        ],
        "rows": [
          [
            "[arn]",
            "Returns a match where one of the specified characters (<code class=\"w3-codespan\">a</code>,\n<code class=\"w3-codespan\">r</code>, or <code class=\"w3-codespan\">n</code>) is \npresent",
            "<a target=\"_blank\" class=\"w3-btn btnsmall btnsmall\" href=\"trypython6843.html?filename=demo_regex_set1\">Try it »</a>"
          ],
          [
            "[a-n]",
            "Returns a match for any lower case character, alphabetically between\n<code class=\"w3-codespan\">a</code> and <code class=\"w3-codespan\">n</code>",
            "<a target=\"_blank\" class=\"w3-btn btnsmall btnsmall\" href=\"trypython1fad.html?filename=demo_regex_set2\">Try it »</a>"
          ],
          [
            "[^arn]",
            "Returns a match for any character EXCEPT <code class=\"w3-codespan\">a</code>,\n<code class=\"w3-codespan\">r</code>, and <code class=\"w3-codespan\">n</code>",
            "<a target=\"_blank\" class=\"w3-btn btnsmall btnsmall\" href=\"trypythone671.html?filename=demo_regex_set3\">Try it »</a>"
          ],
          [
            "[0123]",
            "Returns a match where any of the specified digits (<code class=\"w3-codespan\">0</code>,\n<code class=\"w3-codespan\">1</code>, <code class=\"w3-codespan\">2</code>, or <code class=\"w3-codespan\">\n3</code>) are \npresent",
            "<a target=\"_blank\" class=\"w3-btn btnsmall btnsmall\" href=\"trypython4240.html?filename=demo_regex_set4\">Try it »</a>"
          ],
          [
            "[0-9]",
            "Returns a match for any digit between\n<code class=\"w3-codespan\">0</code> and <code class=\"w3-codespan\">9</code>",
            "<a target=\"_blank\" class=\"w3-btn btnsmall btnsmall\" href=\"trypythond3e0.html?filename=demo_regex_set5\">Try it »</a>"
          ],
          [
            "[0-5][0-9]",
            "Returns a match for any two-digit numbers from <code class=\"w3-codespan\">00</code> and <code class=\"w3-codespan\">\n59</code>",
            "<a target=\"_blank\" class=\"w3-btn btnsmall btnsmall\" href=\"trypython299b.html?filename=demo_regex_set6\">Try it »</a>"
          ],
          [
            "[a-zA-Z]",
            "Returns a match for any character alphabetically between\n<code class=\"w3-codespan\">a</code> and <code class=\"w3-codespan\">z</code>, lower case OR upper case",
            "<a target=\"_blank\" class=\"w3-btn btnsmall btnsmall\" href=\"trypythoncf21.html?filename=demo_regex_set7\">Try it »</a>"
          ],
          [
            "[+]",
            "In sets, <code class=\"w3-codespan\">+</code>, <code class=\"w3-codespan\">*</code>,\n<code class=\"w3-codespan\">.</code>, <code class=\"w3-codespan\">|</code>,\n<code class=\"w3-codespan\">()</code>, <code class=\"w3-codespan\">$</code>,<code class=\"w3-codespan\">{}</code> \nhas no special meaning, so <code class=\"w3-codespan\">[+]</code> means: return a match for any\n<code class=\"w3-codespan\">+</code> character in the string",
            "<a target=\"_blank\" class=\"w3-btn btnsmall btnsmall\" href=\"trypython7672.html?filename=demo_regex_set8\">Try it »</a>"
          ]
        ]
      },
      {
        "type": "header",
        "level": 2,
        "text": "The findall() Function"
      },
      {
        "type": "paragraph",
        "text": "The <code class=\"w3-codespan\">findall()</code> function returns a list containing all matches."
      },
      {
        "type": "example",
        "title": "Example",
        "code": "import retxt = \"The rain in Spain\"x = re.findall(\"ai\", \n  txt)\n  print(x)"
      },
      {
        "type": "paragraph",
        "text": "The list contains the matches in the order they are found."
      },
      {
        "type": "paragraph",
        "text": "If no matches are found, an empty list is returned:"
      },
      {
        "type": "example",
        "title": "Example",
        "code": "import retxt = \"The rain in Spain\"x = re.findall(\"Portugal\", \n  txt)\n  print(x)"
      },
      {
        "type": "header",
        "level": 2,
        "text": "The search() Function"
      },
      {
        "type": "paragraph",
        "text": "The <code class=\"w3-codespan\">search()</code> function searches the string \nfor a match, and returns a <a href=\"#matchobject\">Match object</a> if there is a \nmatch."
      },
      {
        "type": "paragraph",
        "text": "If there is more than one match, \nonly the first occurrence of the match will be returned:"
      },
      {
        "type": "example",
        "title": "Example",
        "code": "import retxt = \"The rain in Spain\"x = re.search(\"\\s\", \n  txt)\n  print(\"The first white-space character is located in \n  position:\", x.start())"
      },
      {
        "type": "paragraph",
        "text": "If no matches are found, the value <code class=\"w3-codespan\"><a href=\"ref_keyword_none.html\">None</a></code> is returned:"
      },
      {
        "type": "example",
        "title": "Example",
        "code": "import retxt = \"The rain in Spain\"x = re.search(\"Portugal\", \n  txt)\n  print(x)"
      },
      {
        "type": "header",
        "level": 2,
        "text": "The split() Function"
      },
      {
        "type": "paragraph",
        "text": "The <code class=\"w3-codespan\">split()</code> function returns a list where \nthe string has been split at each match:"
      },
      {
        "type": "example",
        "title": "Example",
        "code": "import retxt = \"The rain in Spain\"x = re.split(\"\\s\", \n  txt)\n  print(x)"
      },
      {
        "type": "paragraph",
        "text": "You can control the number of occurrences by specifying the \n<code class=\"w3-codespan\">maxsplit</code> \nparameter:"
      },
      {
        "type": "example",
        "title": "Example",
        "code": "import retxt = \"The rain in Spain\"x = re.split(\"\\s\", \n  txt, \n  1)\n  print(x)"
      },
      {
        "type": "header",
        "level": 2,
        "text": "The sub() Function"
      },
      {
        "type": "paragraph",
        "text": "The <code class=\"w3-codespan\">sub()</code> function replaces the matches with \nthe text of your choice:"
      },
      {
        "type": "example",
        "title": "Example",
        "code": "import retxt = \"The rain in Spain\"x = re.sub(\"\\s\", \n  \"9\", txt)\n  print(x)"
      },
      {
        "type": "paragraph",
        "text": "You can control the number of replacements by specifying the\n<code class=\"w3-codespan\">count</code> \nparameter:"
      },
      {
        "type": "example",
        "title": "Example",
        "code": "import retxt = \"The rain in Spain\"x = re.sub(\"\\s\", \n  \"9\", txt, 2)\n  print(x)"
      },
      {
        "type": "header",
        "level": 2,
        "text": "Match Object"
      },
      {
        "type": "paragraph",
        "text": "A Match Object is an object containing information \nabout the search and the result."
      },
      {
        "type": "note",
        "text": "<p><strong>Note:</strong> If there is no match, the value <code class=\"w3-codespan\"><a href=\"ref_keyword_none.html\">None</a></code> will be \nreturned, instead of the Match Object.</p>"
      },
      {
        "type": "example",
        "title": "Example",
        "code": "import retxt = \"The rain in Spain\"x = re.search(\"ai\", \n  txt)\n  print(x) #this will print an object"
      },
      {
        "type": "paragraph",
        "text": "The Match object has properties and methods used to retrieve information \nabout the search, and the result:"
      },
      {
        "type": "paragraph",
        "text": "<code class=\"w3-codespan\">.span()</code> returns a tuple containing the start-, and end positions of the match.<br>\n<code class=\"w3-codespan\">.string</code> returns the string passed into the function<br>\n<code class=\"w3-codespan\">.group()</code> returns the part of the string where there was a match<br>"
      },
      {
        "type": "example",
        "title": "Example",
        "code": "import re\n    txt = \"The rain in Spain\"\n    x = re.search(r\"\\bS\\w+\", txt)\n    print(x.span())"
      },
      {
        "type": "example",
        "title": "Example",
        "code": "import re\n    txt = \"The rain in Spain\"\n    x = re.search(r\"\\bS\\w+\", txt)\n    print(x.string)"
      },
      {
        "type": "example",
        "title": "Example",
        "code": "import re\n    txt = \"The rain in Spain\"\n    x = re.search(r\"\\bS\\w+\", txt)\n    print(x.group())"
      },
      {
        "type": "note",
        "text": "<p><strong>Note:</strong> If there is no match, the value <code class=\"w3-codespan\"><a href=\"ref_keyword_none.html\">None</a></code> will be \nreturned, instead of the Match Object.</p>"
      }
    ]
  },
  "pip": {
    "title": "Python PIP",
    "blocks": [
      {
        "type": "header",
        "level": 2,
        "text": "What is PIP?"
      },
      {
        "type": "paragraph",
        "text": "PIP is a package manager for Python packages, or modules if you like."
      },
      {
        "type": "note",
        "text": "<p><strong>Note:</strong> If you have Python version 3.4 or later, PIP is included by default.</p>"
      },
      {
        "type": "header",
        "level": 2,
        "text": "What is a Package?"
      },
      {
        "type": "paragraph",
        "text": "A package contains all the files you need for a module."
      },
      {
        "type": "paragraph",
        "text": "Modules are Python code libraries you can include in your project."
      },
      {
        "type": "header",
        "level": 2,
        "text": "Check if PIP is Installed"
      },
      {
        "type": "paragraph",
        "text": "Navigate your command line to the location of Python's script directory, and type the following:"
      },
      {
        "type": "example",
        "title": "Example",
        "code": "C:\\Users\\Your Name\\AppData\\Local\\Programs\\Python\\Python36-32\\Scripts>pip --version"
      },
      {
        "type": "header",
        "level": 2,
        "text": "Install PIP"
      },
      {
        "type": "paragraph",
        "text": "If you do not have PIP installed, you can download and install it from this page:\n<a target=\"_blank\" href=\"https://pypi.org/project/pip/\">https://pypi.org/project/pip/</a>"
      },
      {
        "type": "header",
        "level": 2,
        "text": "Download a Package"
      },
      {
        "type": "paragraph",
        "text": "Downloading a package is very easy."
      },
      {
        "type": "paragraph",
        "text": "Open the command line interface and tell PIP to download the package you \nwant."
      },
      {
        "type": "paragraph",
        "text": "Navigate your command line to the location of Python's script directory, and type the following:"
      },
      {
        "type": "example",
        "title": "Example",
        "code": "C:\\Users\\Your Name\\AppData\\Local\\Programs\\Python\\Python36-32\\Scripts>pip \n  install camelcase"
      },
      {
        "type": "paragraph",
        "text": "Now you have downloaded and installed your first package!"
      },
      {
        "type": "header",
        "level": 2,
        "text": "Using a Package"
      },
      {
        "type": "paragraph",
        "text": "Once the package is installed, it is ready to use."
      },
      {
        "type": "paragraph",
        "text": "Import the \"camelcase\" package into your project."
      },
      {
        "type": "example",
        "title": "Example",
        "code": "import camelcasec = camelcase.CamelCase()txt = \"hello world\"\n  print(c.hump(txt))"
      },
      {
        "type": "header",
        "level": 2,
        "text": "Find Packages"
      },
      {
        "type": "paragraph",
        "text": "Find more packages at <a target=\"_blank\" href=\"https://pypi.org/\">https://pypi.org/</a>."
      },
      {
        "type": "header",
        "level": 2,
        "text": "Remove a Package"
      },
      {
        "type": "paragraph",
        "text": "Use the <code class=\"w3-codespan\">uninstall</code> command to remove a package:"
      },
      {
        "type": "example",
        "title": "Example",
        "code": "C:\\Users\\Your Name\\AppData\\Local\\Programs\\Python\\Python36-32\\Scripts>pip \n  uninstall camelcase"
      },
      {
        "type": "paragraph",
        "text": "The PIP Package Manager will ask you to confirm that you want to remove the \ncamelcase package:"
      },
      {
        "type": "example",
        "title": "Example",
        "code": "Uninstalling camelcase-02.1:  Would remove:    c:\\users\\Your Name\\appdata\\local\\programs\\python\\python36-32\\lib\\site-packages\\camelcase-0.2-py3.6.egg-info    \n  c:\\users\\Your Name\\appdata\\local\\programs\\python\\python36-32\\lib\\site-packages\\camelcase\\*\n  Proceed (y/n)?"
      },
      {
        "type": "paragraph",
        "text": "Press <code class=\"w3-codespan\">y</code> and the package will be removed."
      },
      {
        "type": "header",
        "level": 2,
        "text": "List Packages"
      },
      {
        "type": "paragraph",
        "text": "Use the <code class=\"w3-codespan\">list</code> command to list all the packages installed on your system:"
      },
      {
        "type": "example",
        "title": "Example",
        "code": "C:\\Users\\Your Name\\AppData\\Local\\Programs\\Python\\Python36-32\\Scripts>pip list"
      }
    ]
  },
  "try-except": {
    "title": "Python Try Except",
    "blocks": [
      {
        "type": "intro",
        "text": "The <code class=\"w3-codespan\">try</code> block lets you test a \nblock of code for errors."
      },
      {
        "type": "intro",
        "text": "The <code class=\"w3-codespan\">except</code> block lets you \nhandle the error."
      },
      {
        "type": "intro",
        "text": "The <code class=\"w3-codespan\">else</code> block lets you \nexecute code when there is no error."
      },
      {
        "type": "intro",
        "text": "The <code class=\"w3-codespan\">finally</code> block lets you \nexecute code, regardless of the result of the try- and except blocks."
      },
      {
        "type": "header",
        "level": 2,
        "text": "Exception Handling"
      },
      {
        "type": "paragraph",
        "text": "When an error occurs, or exception as we call it, Python will normally stop and \ngenerate an error message."
      },
      {
        "type": "paragraph",
        "text": "These exceptions can be handled using the <code class=\"w3-codespan\">try</code> statement:"
      },
      {
        "type": "example",
        "title": "Example",
        "code": "try:  print(x)except:  print(\"An exception occurred\")"
      },
      {
        "type": "paragraph",
        "text": "Since the try block raises an error, the except block will be executed."
      },
      {
        "type": "paragraph",
        "text": "Without the try block, the program will crash and raise an error:"
      },
      {
        "type": "example",
        "title": "Example",
        "code": "print(x)"
      },
      {
        "type": "header",
        "level": 2,
        "text": "Many Exceptions"
      },
      {
        "type": "paragraph",
        "text": "You can define as many exception blocks as you want, e.g. if you want to execute a \nspecial block of code for a special kind of error:"
      },
      {
        "type": "example",
        "title": "Example",
        "code": "try:  print(x)except NameError:  print(\"Variable x \n  is not defined\")except:  print(\"Something else went \n  wrong\")"
      },
      {
        "type": "paragraph",
        "text": "See more Error types in our <a href=\"python_ref_exceptions.html\">Python Built-in Exceptions Reference</a>."
      },
      {
        "type": "header",
        "level": 2,
        "text": "Else"
      },
      {
        "type": "paragraph",
        "text": "You can use the <code class=\"w3-codespan\">else</code> keyword to define a \nblock of code to be executed if no errors were raised:"
      },
      {
        "type": "example",
        "title": "Example",
        "code": "try:  print(\"Hello\")except:  print(\"Something went \n  wrong\")else:  print(\"Nothing went wrong\")"
      },
      {
        "type": "header",
        "level": 2,
        "text": "Finally"
      },
      {
        "type": "paragraph",
        "text": "The <code class=\"w3-codespan\">finally</code> block, if specified, will be executed \nregardless if the try block \nraises an error or not."
      },
      {
        "type": "example",
        "title": "Example",
        "code": "try:  print(x)except:  print(\"Something went \n  wrong\")finally:  print(\"The 'try except' is finished\")"
      },
      {
        "type": "paragraph",
        "text": "This can be useful to close objects and clean up resources:"
      },
      {
        "type": "example",
        "title": "Example",
        "code": "try:  f = open(\"demofile.txt\")  try:    \n  f.write(\"Lorum Ipsum\")  except:    \n  print(\"Something went wrong when writing to the file\")  finally:    \n  f.close()except:  print(\"Something went wrong when opening the \n  file\")"
      },
      {
        "type": "paragraph",
        "text": "The program can continue, without leaving the file object open."
      },
      {
        "type": "header",
        "level": 2,
        "text": "Raise an exception"
      },
      {
        "type": "paragraph",
        "text": "As a Python developer you can choose to throw an exception if a condition occurs."
      },
      {
        "type": "paragraph",
        "text": "To throw (or raise) an exception, use the <code class=\"w3-codespan\"><a href=\"ref_keyword_raise.html\">raise</a></code> keyword."
      },
      {
        "type": "example",
        "title": "Example",
        "code": "x = -1if x < 0:  raise Exception(\"Sorry, no numbers below \n    zero\")"
      },
      {
        "type": "paragraph",
        "text": "The <code class=\"w3-codespan\"><a href=\"ref_keyword_raise.html\">raise</a></code> keyword is used to raise an \nexception."
      },
      {
        "type": "paragraph",
        "text": "You can define what kind of error to raise, and the text to print to the user."
      },
      {
        "type": "example",
        "title": "Example",
        "code": "x = \"hello\"if not type(x) is int:  raise TypeError(\"Only \n    integers are allowed\")"
      }
    ]
  },
  "user-input": {
    "title": "Python User Input",
    "blocks": [
      {
        "type": "header",
        "level": 2,
        "text": "User Input"
      },
      {
        "type": "paragraph",
        "text": "Python allows for user input."
      },
      {
        "type": "paragraph",
        "text": "That means we are able to ask the user for input."
      },
      {
        "type": "paragraph",
        "text": "The following example asks for your name, and when you enter a name, it gets printed on the screen:"
      },
      {
        "type": "example",
        "title": "Example",
        "code": "print(\"Enter your name:\")\nname = input()\nprint(f\"Hello {name}\")"
      },
      {
        "type": "note",
        "text": "<p>Python stops executing when it comes to the <code class=\"w3-codespan\"><a href=\"ref_func_input.asp\">input()</a></code> function, and continues \nwhen the user has given some input.</p>"
      },
      {
        "type": "header",
        "level": 2,
        "text": "Using prompt"
      },
      {
        "type": "paragraph",
        "text": "In the example above, the user had to input their name on a new line. The Python <code class=\"w3-codespan\"><a href=\"ref_func_input.asp\">input()</a></code> function has a <code class=\"w3-codespan\">prompt</code> parameter,\nwhich acts as a message you can put in front of the user input, on the same line:"
      },
      {
        "type": "example",
        "title": "Example",
        "code": "name = input(\"Enter your name:\")\nprint(f\"Hello {name}\")"
      },
      {
        "type": "header",
        "level": 2,
        "text": "Multiple Inputs"
      },
      {
        "type": "paragraph",
        "text": "You can add as many inputs as you want, Python will stop executing at each of them,\nwaiting for user input:"
      },
      {
        "type": "example",
        "title": "Example",
        "code": "name = input(\"Enter your name:\")\nprint(f\"Hello {name}\")\nfav1 = input(\"What is your favorite animal:\")\nfav2 = input(\"What is your favorite color:\")\nfav3 = input(\"What is your favorite number:\")\nprint(f\"Do you want a {fav2} {fav1} with {fav3} legs?\")"
      },
      {
        "type": "header",
        "level": 2,
        "text": "Input Number"
      },
      {
        "type": "paragraph",
        "text": "The input from the user is treated as a string. Even if, in the example above, you can input a number,\nthe Python interpreter will still treat it as a string."
      },
      {
        "type": "paragraph",
        "text": "You can convert the input into a number with the <code class=\"w3-codespan\"><a href=\"ref_func_float.html\">float()</a></code>\nfunction:"
      },
      {
        "type": "example",
        "title": "Example",
        "code": "x = input(\"Enter a number:\")\n\n#find the square root of the number:\ny = math.sqrt(float(x))\n\nprint(f\"The square root of {x} is {y}\")"
      },
      {
        "type": "header",
        "level": 2,
        "text": "Validate Input"
      },
      {
        "type": "paragraph",
        "text": "It is a good practice to validate any input from the user. In the example above, \nan error will occur if the user inputs something other than a number."
      },
      {
        "type": "paragraph",
        "text": "To avoid getting an error, we can test the input, and if it is not a number, the user could get a message like \"Wrong input, please try again\",\nand allowed to make a new input:"
      },
      {
        "type": "example",
        "title": "Example",
        "code": "y = True\nwhile y == True:\n  x = input(\"Enter a number:\")\n  try:    x = float(x);    y = False  \n  except:    print(\"Wrong input, please try again.\")\n\nprint(\"Thank you!\")"
      }
    ]
  },
  "string-formatting": {
    "title": "Python String Formatting",
    "blocks": [
      {
        "type": "intro",
        "text": "F-String was introduced in Python 3.6,\nand is now the preferred way of formatting strings."
      },
      {
        "type": "intro",
        "text": "Before Python 3.6 we had to use the <code class=\"w3-codespan\"><a href=\"ref_func_format.html\">format()</a></code> method."
      },
      {
        "type": "header",
        "level": 2,
        "text": "F-Strings"
      },
      {
        "type": "paragraph",
        "text": "F-string allows you to format selected parts of a string."
      },
      {
        "type": "paragraph",
        "text": "To specify a string as an f-string, simply put an <code class=\"w3-codespan\">f</code> in front of the string \nliteral, like this:"
      },
      {
        "type": "example",
        "title": "Example",
        "code": "txt = f\"The price is 49 dollars\"\n\nprint(txt)"
      },
      {
        "type": "header",
        "level": 2,
        "text": "Placeholders and Modifiers"
      },
      {
        "type": "paragraph",
        "text": "To format values in an f-string, add placeholders <code class=\"w3-codespan\">{}</code>, \na placeholder can contain variables,\noperations, functions, and modifiers to format the value."
      },
      {
        "type": "example",
        "title": "Example",
        "code": "price = 59\n\ntxt = f\"The price is {price} dollars\"\n\nprint(txt)"
      },
      {
        "type": "paragraph",
        "text": "A placeholder can also include a <em>modifier </em>to format the value."
      },
      {
        "type": "paragraph",
        "text": "A modifier is included by adding a colon <code class=\"w3-codespan\">:</code> followed by a legal formatting type, like \n<code class=\"w3-codespan\">.2f</code> which means fixed point number with 2 decimals:"
      },
      {
        "type": "example",
        "title": "Example",
        "code": "price = 59\n\ntxt = f\"The price is {price:.2f} dollars\"\n\nprint(txt)"
      },
      {
        "type": "paragraph",
        "text": "You can also format a value directly without keeping it in a variable:"
      },
      {
        "type": "example",
        "title": "Example",
        "code": "txt = f\"The price is {95:.2f} dollars\"\n\nprint(txt)"
      },
      {
        "type": "header",
        "level": 2,
        "text": "Perform Operations in F-Strings"
      },
      {
        "type": "paragraph",
        "text": "You can perform Python operations inside the placeholders."
      },
      {
        "type": "paragraph",
        "text": "You can do math operations:"
      },
      {
        "type": "example",
        "title": "Example",
        "code": "txt = f\"The price is {20 * 59} dollars\"\n\nprint(txt)"
      },
      {
        "type": "paragraph",
        "text": "You can perform math operations on variables:"
      },
      {
        "type": "example",
        "title": "Example",
        "code": "price = 59\ntax = 0.25\ntxt = f\"The price is {price + (price * tax)} dollars\"\n\nprint(txt)"
      },
      {
        "type": "paragraph",
        "text": "You can perform <code class=\"w3-codespan\">if...else</code> statements inside the placeholders:"
      },
      {
        "type": "example",
        "title": "Example",
        "code": "price = 49\ntxt = f\"It is very {'Expensive' if price>50 else 'Cheap'}\"\n\nprint(txt)"
      },
      {
        "type": "header",
        "level": 2,
        "text": "Execute Functions in F-Strings"
      },
      {
        "type": "paragraph",
        "text": "You can execute functions inside the placeholder:"
      },
      {
        "type": "example",
        "title": "Example",
        "code": "fruit = \"apples\"\ntxt = f\"I love {fruit.upper()}\"\n\nprint(txt)"
      },
      {
        "type": "paragraph",
        "text": "The function does not have to be a built-in Python method, you can create your own functions and use them:"
      },
      {
        "type": "example",
        "title": "Example",
        "code": "def myconverter(x):\n  return x * 0.3048\n \ntxt = f\"The plane is flying at a {myconverter(30000)} meter altitude\"\n\nprint(txt)"
      },
      {
        "type": "header",
        "level": 2,
        "text": "More Modifiers"
      },
      {
        "type": "paragraph",
        "text": "At the beginning of this chapter we explained how to use the <code class=\"w3-codespan\">.2f</code> modifier to \nformat a number into a fixed point number with 2 decimals."
      },
      {
        "type": "paragraph",
        "text": "There are several other modifiers that can be used to format values:"
      },
      {
        "type": "example",
        "title": "Example",
        "code": "price = 59000\n\ntxt = f\"The price is {price:,} dollars\"\n\nprint(txt)"
      },
      {
        "type": "paragraph",
        "text": "Here is a list of all the formatting types."
      },
      {
        "type": "table",
        "headers": [
          "Formatting Types"
        ],
        "rows": [
          [
            "<code class=\"w3-codespan\">:&lt;</code>",
            "<a target=\"_blank\" class=\"w3-btn btnsmall\" href=\"trypython56c5.html?filename=demo_fstring_modifier1\">Try it</a>",
            "Left aligns the result (within the available space)"
          ],
          [
            "<code class=\"w3-codespan\">:&gt;</code>",
            "<a target=\"_blank\" class=\"w3-btn btnsmall\" href=\"trypython8208.html?filename=demo_fstring_modifier2\">Try it</a>",
            "Right aligns the result (within the available space)"
          ],
          [
            "<code class=\"w3-codespan\">:^</code>",
            "<a target=\"_blank\" class=\"w3-btn btnsmall\" href=\"trypython50e1.html?filename=demo_fstring_modifier3\">Try it</a>",
            "Center aligns the result (within the available space)"
          ],
          [
            "<code class=\"w3-codespan\">:=</code>",
            "<a target=\"_blank\" class=\"w3-btn btnsmall\" href=\"trypython28c5.html?filename=demo_fstring_modifier4\">Try it</a>",
            "Places the sign to the left most position"
          ],
          [
            "<code class=\"w3-codespan\">:+</code>",
            "<a target=\"_blank\" class=\"w3-btn btnsmall\" href=\"trypython8cec.html?filename=demo_fstring_modifier5\">Try it</a>",
            "Use a plus sign to indicate if the result is positive or negative"
          ],
          [
            "<code class=\"w3-codespan\">:-</code>",
            "<a target=\"_blank\" class=\"w3-btn btnsmall\" href=\"trypython8e35.html?filename=demo_fstring_modifier6\">Try it</a>",
            "Use a minus sign for negative values only"
          ],
          [
            "<code class=\"w3-codespan\">:&nbsp;</code>",
            "<a target=\"_blank\" class=\"w3-btn btnsmall\" href=\"trypython71a1.html?filename=demo_fstring_modifier7\">Try it</a>",
            "Use a space to insert an extra space before positive numbers (and a minus sign \n  before negative numbers)"
          ],
          [
            "<code class=\"w3-codespan\">:,</code>",
            "<a target=\"_blank\" class=\"w3-btn btnsmall\" href=\"trypython7eb2.html?filename=demo_fstring_modifier8\">Try it</a>",
            "Use a comma as a thousand separator"
          ],
          [
            "<code class=\"w3-codespan\">:_</code>",
            "<a target=\"_blank\" class=\"w3-btn btnsmall\" href=\"trypython1c33.html?filename=demo_fstring_modifier9\">Try it</a>",
            "Use a underscore as a thousand separator"
          ],
          [
            "<code class=\"w3-codespan\">:b</code>",
            "<a target=\"_blank\" class=\"w3-btn btnsmall\" href=\"trypython0296.html?filename=demo_fstring_modifier10\">Try it</a>",
            "Binary format"
          ],
          [
            "<code class=\"w3-codespan\">:c</code>",
            "",
            "Converts the value into the corresponding Unicode character"
          ],
          [
            "<code class=\"w3-codespan\">:d</code>",
            "<a target=\"_blank\" class=\"w3-btn btnsmall\" href=\"trypython191d.html?filename=demo_fstring_modifier12\">Try it</a>",
            "Decimal format"
          ],
          [
            "<code class=\"w3-codespan\">:e</code>",
            "<a target=\"_blank\" class=\"w3-btn btnsmall\" href=\"trypythonc4c8.html?filename=demo_fstring_modifier13\">Try it</a>",
            "Scientific format, with a lower case e"
          ],
          [
            "<code class=\"w3-codespan\">:E</code>",
            "<a target=\"_blank\" class=\"w3-btn btnsmall\" href=\"trypythonc432.html?filename=demo_fstring_modifier14\">Try it</a>",
            "Scientific format, with an upper case E"
          ],
          [
            "<code class=\"w3-codespan\">:f</code>",
            "<a target=\"_blank\" class=\"w3-btn btnsmall\" href=\"trypython2854.html?filename=demo_fstring_modifier15\">Try it</a>",
            "Fix point number format"
          ],
          [
            "<code class=\"w3-codespan\">:F</code>",
            "<a target=\"_blank\" class=\"w3-btn btnsmall\" href=\"trypythonc536.html?filename=demo_fstring_modifier16\">Try it</a>",
            "Fix point number format, in uppercase format (show\n  <code class=\"w3-codespan\">inf</code> and\n  <code class=\"w3-codespan\">nan</code> as <code class=\"w3-codespan\">INF</code> \n  and <code class=\"w3-codespan\">NAN</code>)"
          ],
          [
            "<code class=\"w3-codespan\">:g</code>",
            "",
            "General format"
          ],
          [
            "<code class=\"w3-codespan\">:G</code>",
            "",
            "General format (using a upper case E for scientific notations)"
          ],
          [
            "<code class=\"w3-codespan\">:o</code>",
            "<a target=\"_blank\" class=\"w3-btn btnsmall\" href=\"trypython0a21.html?filename=demo_fstring_modifier19\">Try it</a>",
            "Octal format"
          ],
          [
            "<code class=\"w3-codespan\">:x</code>",
            "<a target=\"_blank\" class=\"w3-btn btnsmall\" href=\"trypython5ae6.html?filename=demo_fstring_modifier20\">Try it</a>",
            "Hex format, lower case"
          ],
          [
            "<code class=\"w3-codespan\">:X</code>",
            "<a target=\"_blank\" class=\"w3-btn btnsmall\" href=\"trypython3c9c.html?filename=demo_fstring_modifier21\">Try it</a>",
            "Hex format, upper case"
          ],
          [
            "<code class=\"w3-codespan\">:n</code>",
            "",
            "Number format"
          ],
          [
            "<code class=\"w3-codespan\">:%</code>",
            "<a target=\"_blank\" class=\"w3-btn btnsmall\" href=\"trypython6327.html?filename=demo_fstring_modifier23\">Try it</a>",
            "Percentage format"
          ]
        ]
      },
      {
        "type": "header",
        "level": 2,
        "text": "String format()"
      },
      {
        "type": "paragraph",
        "text": "Before Python 3.6 we used the <code class=\"w3-codespan\"><a href=\"ref_func_format.html\">format()</a></code> method to format strings."
      },
      {
        "type": "paragraph",
        "text": "The <code class=\"w3-codespan\"><a href=\"ref_func_format.html\">format()</a></code> method can still be used,\nbut f-strings are faster and the preferred way to format strings."
      },
      {
        "type": "paragraph",
        "text": "The next examples in this page demonstrates how to format strings with the\n<code class=\"w3-codespan\"><a href=\"ref_func_format.html\">format()</a></code> method."
      },
      {
        "type": "paragraph",
        "text": "The <code class=\"w3-codespan\"><a href=\"ref_func_format.html\">format()</a></code> method also uses curly brackets as placeholders\n<code class=\"w3-codespan\">{}</code>, but the syntax is slightly different:"
      },
      {
        "type": "example",
        "title": "Example",
        "code": "price = 49txt = \"The price is {} dollars\"print(txt.format(price))"
      },
      {
        "type": "paragraph",
        "text": "You can add parameters inside the curly brackets to specify how to convert \nthe value:"
      },
      {
        "type": "example",
        "title": "Example",
        "code": "txt = \"The price is {:.2f} dollars\""
      },
      {
        "type": "paragraph",
        "text": "Check out all formatting types in our <a href=\"ref_string_format.html\">String format() Reference</a>."
      },
      {
        "type": "header",
        "level": 2,
        "text": "Multiple Values"
      },
      {
        "type": "paragraph",
        "text": "If you want to use more values, just add more values to the <code class=\"w3-codespan\"><a href=\"ref_func_format.html\">format()</a></code> method:"
      },
      {
        "type": "example",
        "title": "Example",
        "code": "print(txt.format(price, itemno, count))"
      },
      {
        "type": "paragraph",
        "text": "And add more placeholders:"
      },
      {
        "type": "example",
        "title": "Example",
        "code": "quantity = 3itemno = 567price = 49myorder = \"I want {} pieces of \n  item number {} for {:.2f} dollars.\"print(myorder.format(quantity, itemno, price))"
      },
      {
        "type": "header",
        "level": 2,
        "text": "Index Numbers"
      },
      {
        "type": "paragraph",
        "text": "You can use index numbers (a number inside the curly brackets <code class=\"w3-codespan\">{0}</code>) to be sure the \nvalues are placed \nin the correct placeholders:"
      },
      {
        "type": "example",
        "title": "Example",
        "code": "quantity = 3itemno = 567price = 49myorder = \"I want {0} pieces of \n  item number {1} for {2:.2f} dollars.\"print(myorder.format(quantity, itemno, price))"
      },
      {
        "type": "paragraph",
        "text": "Also, if you want to refer to the same value more than once, use the index number:"
      },
      {
        "type": "example",
        "title": "Example",
        "code": "age = 36name = \"John\"txt = \"His name is {1}. {1} is {0} years old.\"print(txt.format(age, \n  name))"
      },
      {
        "type": "header",
        "level": 2,
        "text": "Named Indexes"
      },
      {
        "type": "paragraph",
        "text": "You can also use named indexes by entering a name inside the curly brackets <code class=\"w3-codespan\">{carname}</code>, \nbut then you must use names when you pass the parameter values\n<code class=\"w3-codespan\">txt.format(carname = \"Ford\")</code>:"
      },
      {
        "type": "example",
        "title": "Example",
        "code": "myorder = \"I have a {carname}, it is a {model}.\"print(myorder.format(carname \n  = \"Ford\", model = \"Mustang\"))"
      }
    ]
  },
  "file-handling": {
    "title": "Python File Open",
    "blocks": [
      {
        "type": "header",
        "level": 2,
        "text": "Open a File on the Server"
      },
      {
        "type": "paragraph",
        "text": "Assume we have the following file, located in the same folder as Python:"
      },
      {
        "type": "example",
        "title": "Example",
        "code": "Hello! Welcome to demofile.txtThis file is for testing purposes.Good \n  Luck!"
      },
      {
        "type": "paragraph",
        "text": "To open the file, use the built-in <code class=\"w3-codespan\"><a href=\"ref_func_open.html\">open()</a></code> function."
      },
      {
        "type": "paragraph",
        "text": "The <code class=\"w3-codespan\"><a href=\"ref_func_open.html\">open()</a></code> function returns a file object, which has a \n<code class=\"w3-codespan\">read()</code> method for reading the content of the file:"
      },
      {
        "type": "example",
        "title": "Example",
        "code": "f = open(\"demofile.txt\")print(f.read())"
      },
      {
        "type": "paragraph",
        "text": "If the file is located in a different location, you will have to specify the file path, \nlike this:"
      },
      {
        "type": "example",
        "title": "Example",
        "code": "f = open(\"D:\\\\myfiles\\welcome.txt\")print(f.read())"
      },
      {
        "type": "header",
        "level": 2,
        "text": "Using the with statement"
      },
      {
        "type": "paragraph",
        "text": "You can also use the <code class=\"w3-codespan\">with</code> statement when opening a file:"
      },
      {
        "type": "example",
        "title": "Example",
        "code": "with open(\"demofile.txt\") as f:\n  print(f.read())"
      },
      {
        "type": "paragraph",
        "text": "Then you do not have to worry about closing your files, the <code class=\"w3-codespan\">with</code> statement takes care of that."
      },
      {
        "type": "header",
        "level": 2,
        "text": "Close Files"
      },
      {
        "type": "paragraph",
        "text": "It is a good practice to always close the file when you are done with it."
      },
      {
        "type": "paragraph",
        "text": "If you are not using the <code class=\"w3-codespan\">with</code> statement, you must write a close statement in order to close the file:"
      },
      {
        "type": "example",
        "title": "Example",
        "code": "f = open(\"demofile.txt\")print(f.readline())\n  f.close()"
      },
      {
        "type": "note",
        "text": "<p><strong>Note:</strong> You should always close your files. In some cases, due to buffering, changes made to a file may not show until you close the file.</p>"
      },
      {
        "type": "header",
        "level": 2,
        "text": "Read Only Parts of the File"
      },
      {
        "type": "paragraph",
        "text": "By default the <code class=\"w3-codespan\">read()</code> method returns the whole text, but you can also specify how many characters you want to return:"
      },
      {
        "type": "example",
        "title": "Example",
        "code": "with open(\"demofile.txt\") as f:  print(f.read(5))"
      },
      {
        "type": "header",
        "level": 2,
        "text": "Read Lines"
      },
      {
        "type": "paragraph",
        "text": "You can return one line by using the <code class=\"w3-codespan\">readline()</code> method:"
      },
      {
        "type": "example",
        "title": "Example",
        "code": "with open(\"demofile.txt\") as f:  print(f.readline())"
      },
      {
        "type": "paragraph",
        "text": "By calling <code class=\"w3-codespan\">readline()</code> two times, you can read the \ntwo first lines:"
      },
      {
        "type": "example",
        "title": "Example",
        "code": "with open(\"demofile.txt\") as f:  print(f.readline())  print(f.readline())"
      },
      {
        "type": "paragraph",
        "text": "By looping through the lines of the file, you can read the whole file, line by line:"
      },
      {
        "type": "example",
        "title": "Example",
        "code": "with open(\"demofile.txt\") as f:  for x in f:    print(x)"
      }
    ]
  }
}