- "markdown": "---\ntitle: \"Sample Programs\"\nformat: html\nfilters:\n - pyodide\njupyter: python3\nexecute: \n enabled: true\n---\n\n\n\n\n\n\n\n\n## Introduction to Python\n\nIn this tutorial, we will cover the basics of Python programming, including data types, keywords, variables, input/output statements, operators, arithmetic expressions, operator precedence, and evaluation of expressions.\n\n## Data Types\n\nPython supports several built-in data types. Let's explore some of the most common ones:\n\n- **Integer (`int`)**: Represents whole numbers.\n- **Floating Point (`float`)**: Represents decimal numbers.\n- **String (`str`)**: Represents sequences of characters.\n- **Boolean (`bool`)**: Represents `True` or `False`.\n\n>**Example 1**\n\n:::{.panel-tabset}\n\n### Static Template\n\n::: {#62476cde .cell execution_count=1}\n``` {.python .cell-code}\n# Demonstrating different data types\n\n# Integer\na = 10\nprint(\"Integer:\", a, type(a))\n\n# Float\nb = 3.14\nprint(\"Float:\", b, type(b))\n\n\n# String\nc = \"Hello, Python!\"\nprint(\"String:\", c, type(c))\n\n# Boolean\nd = True\nprint(\"Boolean:\", d, type(d))\n```\n\n::: {.cell-output .cell-output-stdout}\n```\nInteger: 10 <class 'int'>\nFloat: 3.14 <class 'float'>\nString: Hello, Python! <class 'str'>\nBoolean: True <class 'bool'>\n```\n:::\n:::\n\n\n### Interactive Cell\n\n```{pyodide-python}\n# Demonstrating different data types\n\n# Integer\na = 10\nprint(\"Integer:\", a, type(a))\n\n# Float\nb = 3.14\nprint(\"Float:\", b, type(b))\n\n\n# String\nc = \"Hello, Python!\"\nprint(\"String:\", c, type(c))\n\n# Boolean\nd = True\nprint(\"Boolean:\", d, type(d))\n```\n\n:::\n\n### Variables\nVariables are used to store data in memory. A variable is created when you assign a value to it using the = operator.\n```{pyodide-python}\n# Variable assignment\n\nx = 5\ny = 2.5\nz = x + y\n\nprint(\"x =\", x)\nprint(\"y =\", y)\nprint(\"z =\", z)\n\n\n```\n### Input and Output Statements\nPython provides the input() function to take user input and the print() function to display output.\n```{pyodide-python}\n# Input and Output\nname=\"justin\"\nage=32\n#name = input(\"Enter your name: \")\n#age = int(input(\"Enter your age: \"))\n\nprint(f\"Hello, {name}! You are {age} years old.\")\n\n\n```\n### Operators\nOperators are special symbols used to perform operations on variables and values. Python supports several types of operators:\n\nArithmetic Operators: +, -, *, /, //, %, **\nComparison Operators: ==, !=, >, <, >=, <=\nLogical Operators: and, or, not\nAssignment Operators: =, +=, -=, *=, /=, //=, %=, **=\n```{pyodide-python}\n# Arithmetic Operations\n\na = 15\nb = 4\n\naddition = a + b\nsubtraction = a - b\nmultiplication = a * b\ndivision = a / b\nfloor_division = a // b\nmodulus = a % b\nexponentiation = a ** b\n\nprint(\"Addition:\", addition)\nprint(\"Subtraction:\", subtraction)\nprint(\"Multiplication:\", multiplication)\nprint(\"Division:\", division)\nprint(\"Floor Division:\", floor_division)\nprint(\"Modulus:\", modulus)\nprint(\"Exponentiation:\", exponentiation)\n\n```\n### Arithmetic Expressions\nAn arithmetic expression is a combination of numbers, operators, and variables that evaluates to a value.\n\n```{pyodide-python}\n# Evaluating arithmetic expressions\n\nexpression = (5 + 2) * (10 - 3) / 2 ** 2\nprint(\"Expression Result:\", expression)\n```\n### Operator Precedence\nOperator precedence determines the order in which operations are performed in an expression. The following list shows the precedence from highest to lowest:\n\n** (Exponentiation)\n*, /, //, % (Multiplication, Division, Floor Division, Modulus)\n+, - (Addition, Subtraction)\n```{pyodide-python}\n# Operator precedence\n\nresult = 5 + 3 * 2 ** 2 - 1\nprint(\"Operator Precedence Result:\", result)\n\n```\n\n\n### Evaluation of Expressions\nPython evaluates expressions from left to right, following the precedence rules.\n\n```{pyodide-python}\n# Evaluation of expressions\n\nvalue = (10 + 5) * 2 - 3 / 3\nprint(\"Evaluation Result:\", value)\n\n```\n### Conditional Statements in Python\n\nConditional statements in Python allow the execution of specific code blocks based on whether a condition is true or false. Let's explore various types of conditional statements.\n\n#### The `if` Statement\n\nThe `if` statement tests a specific condition. If the condition is true, the code block under the `if` statement is executed.\n\nExample\n\n```{pyodide-python}\n# Example of an if statement\n\nnumber = 10\n\nif number > 0:\n print(f\"{number} is a positive number.\")\n```\nExplanation\nThe above program checks if number is greater than 0. Since 10 is greater than 0, the condition is true, and the message is printed.\n\n#### The if-else Statement\nThe if-else statement allows you to execute one block of code if the condition is true and another block if it is false.\n```{pyodide-python}\n# Example of an if-else statement\n\nnumber = -5\n\nif number >= 0:\n print(f\"{number} is a non-negative number.\")\nelse:\n print(f\"{number} is a negative number.\")\n\n```\nExplanation\nIn this example, the program checks if number is greater than or equal to 0. If true, it prints that the number is non-negative. Otherwise, it prints that the number is negative.\nExample 2\n\n```{pyodide-python}\nage = 20\n\nif age >= 18:\n print(\"You are eligible to vote.\")\nelse:\n print(\"You are not eligible to vote.\")\n```\nExplanation\nThis program checks if a person’s age is greater than or equal to 18. If true, it prints that the person is eligible to vote. Otherwise, it states they are not eligible to vote.\n#### The elif Statement\nThe elif statement, short for \"else if,\" allows you to check multiple conditions sequentially. If one of the conditions is true, the corresponding block of code is executed.\n```{pyodide-python}\n# Example of an elif statement\n\nnumber = 0\n\nif number > 0:\n print(f\"{number} is a positive number.\")\nelif number == 0:\n print(f\"{number} is zero.\")\nelse:\n print(f\"{number} is a negative number.\")\n\n```\nExplanation\nHere, the program checks three conditions: whether the number is positive, zero, or negative. The elif statement handles the case where number is exactly 0.\n\n```{pyodide-python}\n# Another example of an elif statement\n\nmarks = 85\n\nif marks >= 90:\n grade = 'A'\nelif marks >= 80:\n grade = 'B'\nelif marks >= 70:\n grade = 'C'\nelse:\n grade = 'F'\n\nprint(f\"Your grade is {grade}.\")\n\n```\nExplanation\nThis program assigns a grade based on the marks obtained. Depending on the range in which the marks fall, the corresponding grade is assigned and printed.\n\n#### Nested if-else Statements\nNested if-else statements allow you to include an if-else statement inside another if-else block for handling more complex conditions.\n```{pyodide-python}\n# Example of nested if-else statements\n\nnumber = 25\n\nif number > 0:\n if number % 2 == 0:\n print(f\"{number} is a positive even number.\")\n else:\n print(f\"{number} is a positive odd number.\")\n\n```\nExplanation\nThis example checks if a number is positive and then further checks whether it is even or odd using nested if-else statements.\n\n```{pyodide-python}\n# Another example of nested if-else statements\n\nscore = 92\n\nif score >= 50:\n if score >= 90:\n print(\"Excellent!\")\n else:\n print(\"Good job!\")\nelse:\n print(\"Better luck next time.\")\n\n```\nExplanation\nThis program checks if a score is at least 50. If true, it further checks if the score is 90 or above, printing \"Excellent!\" if it is, and \"Good job!\" if it isn’t. If the score is below 50, it prints \"Better luck next time.\"\n\n",
0 commit comments