{"id":1697,"date":"2023-04-03T01:53:09","date_gmt":"2023-04-03T01:53:09","guid":{"rendered":"https:\/\/datmt.com\/?p=1697"},"modified":"2023-07-31T13:46:10","modified_gmt":"2023-07-31T13:46:10","slug":"day-2-control-structures-and-functions","status":"publish","type":"post","link":"https:\/\/datmt.com\/python\/day-2-control-structures-and-functions\/","title":{"rendered":"Day 2: Control Structures and Functions"},"content":{"rendered":"\n<p>Welcome back to our Python learning journey! Yesterday, we covered the basics of Python programming, including data types, variables, and input\/output. Today, we&#8217;ll dive into control structures and functions.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Control Structures<\/h2>\n\n\n\n<p>Control structures are constructs that allow us to control the flow of our program based on certain conditions. The most common control structures are <code>if<\/code> statements and loops.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">If Statements<\/h3>\n\n\n\n<p><code>if<\/code> statements allow us to execute code only if a certain condition is true. Here&#8217;s the basic syntax:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;python&quot;,&quot;mime&quot;:&quot;text\/x-python&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:true,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;&quot;,&quot;language&quot;:&quot;Python&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;python&quot;}\">if condition:\n    # code to execute if condition is true\n<\/pre><\/div>\n\n\n\n<p>The <code>condition<\/code> is an expression that evaluates to either <code>True<\/code> or <code>False<\/code>. If it&#8217;s <code>True<\/code>, the code inside the <code>if<\/code> statement will be executed. If it&#8217;s <code>False<\/code>, the code will be skipped.<\/p>\n\n\n\n<p>Let&#8217;s look at an example:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;python&quot;,&quot;mime&quot;:&quot;text\/x-python&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:true,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;&quot;,&quot;language&quot;:&quot;Python&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;python&quot;}\">age = 18\n\nif age &gt;= 18:\n    print(&quot;You are an adult.&quot;)\n\n<\/pre><\/div>\n\n\n\n<p>In this example, we&#8217;re checking if <code>age<\/code> is greater than or equal to 18. If it is, we print out a message saying that the user is an adult. If <code>age<\/code> is less than 18, the message will not be printed.<\/p>\n\n\n\n<p>We can also add an <code>else<\/code> clause to execute code if the condition is <code>False<\/code>:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;python&quot;,&quot;mime&quot;:&quot;text\/x-python&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:true,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;&quot;,&quot;language&quot;:&quot;Python&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;python&quot;}\">age = 16\n\nif age &gt;= 18:\n    print(&quot;You are an adult.&quot;)\nelse:\n    print(&quot;You are not yet an adult.&quot;)\n\n<\/pre><\/div>\n\n\n\n<p>In this example, if <code>age<\/code> is greater than or equal to 18, we print out a message saying that the user is an adult. Otherwise, we print out a message saying that the user is not yet an adult.<\/p>\n\n\n\n<p>Let&#8217;s improve the program by taking input from the user:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;python&quot;,&quot;mime&quot;:&quot;text\/x-python&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:true,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;&quot;,&quot;language&quot;:&quot;Python&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;python&quot;}\">age = int(input(&quot;Hi there, please enter your age: &quot;))\n\nif age &gt;= 18:\n    print(&quot;You are an adult.&quot;)\nelse:\n    print(&quot;You are not yet an adult.&quot;)\n\n<\/pre><\/div>\n\n\n\n<p>When you run it, you can enter any number and you will get the result:<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"678\" height=\"178\" sizes=\"auto, (max-width: 678px) 100vw, 678px\" src=\"https:\/\/datmt.com\/wp-content\/uploads\/2023\/04\/image-2.png\" alt=\"Control structure in python example\" class=\"wp-image-1720\" srcset=\"https:\/\/datmt.com\/wp-content\/uploads\/2023\/04\/image-2.png 678w, https:\/\/datmt.com\/wp-content\/uploads\/2023\/04\/image-2-300x79.png 300w\" \/><\/figure>\n\n\n\n<p>As you can see, there is no limit on the value you can enter. In reality, age couldn&#8217;t be &lt; 0 or greater than 200 (for now). Can you add more checks to make sure invalid values are not allowed?<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Loops<\/h3>\n\n\n\n<p>Loops allow us to execute a block of code repeatedly. The two most common types of loops are <code>for<\/code> loops and <code>while<\/code> loops.<\/p>\n\n\n\n<h5 class=\"wp-block-heading\">For Loops<\/h5>\n\n\n\n<p><code>for<\/code> loops allow us to iterate over a sequence of values. Here&#8217;s the basic syntax:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;python&quot;,&quot;mime&quot;:&quot;text\/x-python&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:true,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;&quot;,&quot;language&quot;:&quot;Python&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;python&quot;}\">for variable in sequence:\n    # code to execute for each value in sequence\n<\/pre><\/div>\n\n\n\n<p>The <code>variable<\/code> is a temporary variable that takes on each value in the <code>sequence<\/code> one at a time. The code inside the loop is executed once for each value in the sequence.<\/p>\n\n\n\n<p>Let&#8217;s look at an example:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;python&quot;,&quot;mime&quot;:&quot;text\/x-python&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:true,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;&quot;,&quot;language&quot;:&quot;Python&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;python&quot;}\">fruits = [&quot;apple&quot;, &quot;banana&quot;, &quot;cherry&quot;]\n\nfor fruit in fruits:\n    print(fruit)\n\n<\/pre><\/div>\n\n\n\n<p>In this example, we&#8217;re iterating over a list of fruits and printing out each one.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"660\" height=\"200\" sizes=\"auto, (max-width: 660px) 100vw, 660px\" src=\"https:\/\/datmt.com\/wp-content\/uploads\/2023\/04\/image-3.png\" alt=\"looping through a list\" class=\"wp-image-1721\" srcset=\"https:\/\/datmt.com\/wp-content\/uploads\/2023\/04\/image-3.png 660w, https:\/\/datmt.com\/wp-content\/uploads\/2023\/04\/image-3-300x91.png 300w\" \/><\/figure>\n\n\n\n<p><\/p>\n\n\n\n<h5 class=\"wp-block-heading\">While Loops<\/h5>\n\n\n\n<p><code>while<\/code> loops allow us to execute a block of code as long as a certain condition is true. Here&#8217;s the basic syntax:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;python&quot;,&quot;mime&quot;:&quot;text\/x-python&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:true,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;&quot;,&quot;language&quot;:&quot;Python&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;python&quot;}\">while condition:\n    # code to execute while condition is true<\/pre><\/div>\n\n\n\n<p>The <code>condition<\/code> is an expression that evaluates to either <code>True<\/code> or <code>False<\/code>. The code inside the loop is executed repeatedly as long as the condition is <code>True<\/code>.<\/p>\n\n\n\n<p>Let&#8217;s look at an example:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;python&quot;,&quot;mime&quot;:&quot;text\/x-python&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:true,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;&quot;,&quot;language&quot;:&quot;Python&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;python&quot;}\">count = 0\n\nwhile count &lt; 5:\n    print(count)\n    count += 1\n<\/pre><\/div>\n\n\n\n<p>In this example, we&#8217;re using a <code>while<\/code> loop to print out the numbers from 0 to 4.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"846\" height=\"298\" sizes=\"auto, (max-width: 846px) 100vw, 846px\" src=\"https:\/\/datmt.com\/wp-content\/uploads\/2023\/04\/image-4.png\" alt=\"While loop in python\" class=\"wp-image-1722\" srcset=\"https:\/\/datmt.com\/wp-content\/uploads\/2023\/04\/image-4.png 846w, https:\/\/datmt.com\/wp-content\/uploads\/2023\/04\/image-4-300x106.png 300w, https:\/\/datmt.com\/wp-content\/uploads\/2023\/04\/image-4-768x271.png 768w\" \/><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\">Functions<\/h3>\n\n\n\n<p>Functions are blocks of code that can be called from other parts of the program. They allow us to organize our code into reusable chunks and make our code more modular.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Defining Functions<\/h4>\n\n\n\n<p>To define a function, we use the <code>def<\/code> keyword followed by the name of the function and its parameters (if any):<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;python&quot;,&quot;mime&quot;:&quot;text\/x-python&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:true,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;&quot;,&quot;language&quot;:&quot;Python&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;python&quot;}\">def greet(name):\n  print(f&quot;Hello, {name}!&quot;)\n    \ndef greet_general():\n  print(&quot;Hello, hi&quot;)\n<\/pre><\/div>\n\n\n\n<p>In this example, we&#8217;ve defined a function called <code>greet<\/code> that takes one parameter <code>name<\/code>. The function prints out a greeting message using the value of the <code>name<\/code> parameter.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Calling Functions<\/h4>\n\n\n\n<p>To call a function, we simply use its name followed by its arguments (if any):<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;python&quot;,&quot;mime&quot;:&quot;text\/x-python&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:true,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;&quot;,&quot;language&quot;:&quot;Python&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;python&quot;}\">greet(&quot;Dat&quot;)\ngreet_general()\n<\/pre><\/div>\n\n\n\n<p>This will call the <code>greet<\/code> and <code>greet_general<\/code> functions. Here is the output:<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"818\" height=\"158\" sizes=\"auto, (max-width: 818px) 100vw, 818px\" src=\"https:\/\/datmt.com\/wp-content\/uploads\/2023\/04\/image-5.png\" alt=\"Define and call functions in python\" class=\"wp-image-1723\" srcset=\"https:\/\/datmt.com\/wp-content\/uploads\/2023\/04\/image-5.png 818w, https:\/\/datmt.com\/wp-content\/uploads\/2023\/04\/image-5-300x58.png 300w, https:\/\/datmt.com\/wp-content\/uploads\/2023\/04\/image-5-768x148.png 768w\" \/><\/figure>\n\n\n\n<h4 class=\"wp-block-heading\">Returning Values<\/h4>\n\n\n\n<p>Functions can also return values using the <code>return<\/code> keyword. Here&#8217;s an example:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;python&quot;,&quot;mime&quot;:&quot;text\/x-python&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:true,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;&quot;,&quot;language&quot;:&quot;Python&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;python&quot;}\">def square(x):\n    return x * x<\/pre><\/div>\n\n\n\n<p>In this example, we&#8217;ve defined a function called <code>square<\/code> that takes one parameter <code>x<\/code>. The function returns the square of <code>x<\/code>.<\/p>\n\n\n\n<p>We can call this function and use its return value like this:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;python&quot;,&quot;mime&quot;:&quot;text\/x-python&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:true,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;&quot;,&quot;language&quot;:&quot;Python&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;python&quot;}\">result = square(5)\nprint(result) # prints 25\n<\/pre><\/div>\n\n\n\n<p><\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Extra tasks<\/h3>\n\n\n\n<p>Now that we&#8217;ve covered the basics of control structures and functions, it&#8217;s time to put them to the test with some programming challenges.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Write a function that takes a number as an argument and returns <code>\"odd\"<\/code> or <code>\"even\"<\/code> depending on whether the number is odd or even.<\/h3>\n\n\n\n<p>This is quite a simple assignment. The only thing you may not know beforehand is how to check if a number is odd or even. The trick is to use the modulus operator. <\/p>\n\n\n\n<p>Here is the code:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;python&quot;,&quot;mime&quot;:&quot;text\/x-python&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:true,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;&quot;,&quot;language&quot;:&quot;Python&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;python&quot;}\">number = int(input(&quot;Please input a number: &quot;))\n\nif number % 2 == 0:\n    print(&quot;The number is even&quot;)\nelse:\n    print(&quot;The number is odd&quot;)\n<\/pre><\/div>\n\n\n\n<p>Running the program would produce the following output:<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"840\" height=\"286\" sizes=\"auto, (max-width: 840px) 100vw, 840px\" src=\"https:\/\/datmt.com\/wp-content\/uploads\/2023\/04\/image.png\" alt=\"\" class=\"wp-image-1718\" srcset=\"https:\/\/datmt.com\/wp-content\/uploads\/2023\/04\/image.png 840w, https:\/\/datmt.com\/wp-content\/uploads\/2023\/04\/image-300x102.png 300w, https:\/\/datmt.com\/wp-content\/uploads\/2023\/04\/image-768x261.png 768w\" \/><\/figure>\n\n\n\n<p><\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Write a function that takes a list of numbers as an argument and returns the sum of all the numbers in the list.<\/h3>\n\n\n\n<p>This practice helps you reinforce your knowledge of list. Here is the code for this program:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;python&quot;,&quot;mime&quot;:&quot;text\/x-python&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:true,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;&quot;,&quot;language&quot;:&quot;Python&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;python&quot;}\">def sum_list(numbers):\n    # initialize a variable to store the sum\n    total = 0\n\n    # loop through the list and add each number to the total\n    for number in numbers:\n        total += number\n\n    # return the final total\n    return total<\/pre><\/div>\n\n\n\n<p>Let&#8217;s break down what the code does:<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>We define a function called <code>sum_list<\/code> that takes one parameter <code>numbers<\/code>. This function returns the sum of all the numbers in the <code>numbers<\/code> list.<\/li>\n\n\n\n<li>Inside the <code>sum_list<\/code> function, we initialize a variable called <code>total<\/code> to <code>0<\/code>. This variable will store the sum of all the numbers in the list.<\/li>\n\n\n\n<li>We use a <code>for<\/code> loop to iterate through each number in the <code>numbers<\/code> list. Inside the loop, we add each number to the <code>total<\/code> variable.<\/li>\n\n\n\n<li>After looping through all the numbers in the list, we return the final <code>total<\/code> value.<\/li>\n<\/ol>\n\n\n\n<p>Let&#8217;s try the function with some list:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;python&quot;,&quot;mime&quot;:&quot;text\/x-python&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:true,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;&quot;,&quot;language&quot;:&quot;Python&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;python&quot;}\">print(&quot;Sum of list from 1 to 5: &quot; + str(sum_list([1, 2, 3, 4, 5])))\nprint(&quot;Sum of list from 5 to 10: &quot; + str(sum_list([5, 6, 7, 8, 9, 10])))<\/pre><\/div>\n\n\n\n<p>The output is exactly as you might expect:<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"662\" height=\"168\" sizes=\"auto, (max-width: 662px) 100vw, 662px\" src=\"https:\/\/datmt.com\/wp-content\/uploads\/2023\/04\/image-1.png\" alt=\"Sumlist output\" class=\"wp-image-1719\" srcset=\"https:\/\/datmt.com\/wp-content\/uploads\/2023\/04\/image-1.png 662w, https:\/\/datmt.com\/wp-content\/uploads\/2023\/04\/image-1-300x76.png 300w\" \/><\/figure>\n\n\n\n<p><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Conclusion<\/h2>\n\n\n\n<p>In this post, you&#8217;ve learned about control structures (if and while) and how to define and user functions. These building blocks are vital for any program you write in the future. In the next lesson, you will learn about String and List. <\/p>\n\n\n\n<p>In the meantime, keep practicing! If you need the code, it&#8217;s available on <a href=\"https:\/\/github.com\/datmt\/30-days-of-python\/tree\/main\/day-2\" target=\"_blank\" rel=\"noreferrer noopener\">GitHub<\/a>.<\/p>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Welcome back to our Python learning journey! Yesterday, we covered the basics of Python programming, including data types, variables, and input\/output. Today, we&#8217;ll dive into control structures and functions. Control Structures Control structures are constructs that allow us to control the flow of our program based on certain conditions. The most common control structures are &#8230; <a title=\"Day 2: Control Structures and Functions\" class=\"read-more\" href=\"https:\/\/datmt.com\/python\/day-2-control-structures-and-functions\/\" aria-label=\"Read more about Day 2: Control Structures and Functions\">Read more<\/a><\/p>\n","protected":false},"author":1,"featured_media":1725,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"inline_featured_image":false,"footnotes":""},"categories":[248],"tags":[252,253,254],"class_list":["post-1697","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-python","tag-python-control-structure","tag-python-function","tag-python-list"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.1.1 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Day 2: Control Structures and Functions - datmt<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/datmt.com\/python\/day-2-control-structures-and-functions\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Day 2: Control Structures and Functions - datmt\" \/>\n<meta property=\"og:description\" content=\"Welcome back to our Python learning journey! Yesterday, we covered the basics of Python programming, including data types, variables, and input\/output. Today, we&#8217;ll dive into control structures and functions. Control Structures Control structures are constructs that allow us to control the flow of our program based on certain conditions. The most common control structures are ... Read more\" \/>\n<meta property=\"og:url\" content=\"https:\/\/datmt.com\/python\/day-2-control-structures-and-functions\/\" \/>\n<meta property=\"og:site_name\" content=\"datmt\" \/>\n<meta property=\"article:published_time\" content=\"2023-04-03T01:53:09+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2023-07-31T13:46:10+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/datmt.com\/wp-content\/uploads\/2023\/04\/control-structure-python.png\" \/>\n\t<meta property=\"og:image:width\" content=\"1280\" \/>\n\t<meta property=\"og:image:height\" content=\"720\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/png\" \/>\n<meta name=\"author\" content=\"\u0110\u1ea1t Tr\u1ea7n\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"\u0110\u1ea1t Tr\u1ea7n\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"5 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/datmt.com\/python\/day-2-control-structures-and-functions\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/datmt.com\/python\/day-2-control-structures-and-functions\/\"},\"author\":{\"name\":\"\u0110\u1ea1t Tr\u1ea7n\",\"@id\":\"https:\/\/datmt.com\/#\/schema\/person\/7736566e3758f424a11fd53f581c4b5e\"},\"headline\":\"Day 2: Control Structures and Functions\",\"datePublished\":\"2023-04-03T01:53:09+00:00\",\"dateModified\":\"2023-07-31T13:46:10+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/datmt.com\/python\/day-2-control-structures-and-functions\/\"},\"wordCount\":878,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\/\/datmt.com\/#\/schema\/person\/7736566e3758f424a11fd53f581c4b5e\"},\"image\":{\"@id\":\"https:\/\/datmt.com\/python\/day-2-control-structures-and-functions\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/datmt.com\/wp-content\/uploads\/2023\/04\/control-structure-python.png\",\"keywords\":[\"python control structure\",\"python function\",\"python list\"],\"articleSection\":[\"python\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/datmt.com\/python\/day-2-control-structures-and-functions\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/datmt.com\/python\/day-2-control-structures-and-functions\/\",\"url\":\"https:\/\/datmt.com\/python\/day-2-control-structures-and-functions\/\",\"name\":\"Day 2: Control Structures and Functions - datmt\",\"isPartOf\":{\"@id\":\"https:\/\/datmt.com\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/datmt.com\/python\/day-2-control-structures-and-functions\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/datmt.com\/python\/day-2-control-structures-and-functions\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/datmt.com\/wp-content\/uploads\/2023\/04\/control-structure-python.png\",\"datePublished\":\"2023-04-03T01:53:09+00:00\",\"dateModified\":\"2023-07-31T13:46:10+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/datmt.com\/python\/day-2-control-structures-and-functions\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/datmt.com\/python\/day-2-control-structures-and-functions\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/datmt.com\/python\/day-2-control-structures-and-functions\/#primaryimage\",\"url\":\"https:\/\/datmt.com\/wp-content\/uploads\/2023\/04\/control-structure-python.png\",\"contentUrl\":\"https:\/\/datmt.com\/wp-content\/uploads\/2023\/04\/control-structure-python.png\",\"width\":1280,\"height\":720,\"caption\":\"Control Structures and Functions\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/datmt.com\/python\/day-2-control-structures-and-functions\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/datmt.com\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Day 2: Control Structures and Functions\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/datmt.com\/#website\",\"url\":\"https:\/\/datmt.com\/\",\"name\":\"datmt\",\"description\":\"Hands on projects\",\"publisher\":{\"@id\":\"https:\/\/datmt.com\/#\/schema\/person\/7736566e3758f424a11fd53f581c4b5e\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/datmt.com\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":[\"Person\",\"Organization\"],\"@id\":\"https:\/\/datmt.com\/#\/schema\/person\/7736566e3758f424a11fd53f581c4b5e\",\"name\":\"\u0110\u1ea1t Tr\u1ea7n\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/datmt.com\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/datmt.com\/wp-content\/uploads\/2023\/03\/cropped-profile.jpg\",\"contentUrl\":\"https:\/\/datmt.com\/wp-content\/uploads\/2023\/03\/cropped-profile.jpg\",\"width\":512,\"height\":512,\"caption\":\"\u0110\u1ea1t Tr\u1ea7n\"},\"logo\":{\"@id\":\"https:\/\/datmt.com\/#\/schema\/person\/image\/\"},\"description\":\"I build softwares that solve problems. I also love writing\/documenting things I learn\/want to learn.\",\"sameAs\":[\"https:\/\/datmt.com\"],\"url\":\"https:\/\/datmt.com\/author\/mtdat171_c\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Day 2: Control Structures and Functions - datmt","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/datmt.com\/python\/day-2-control-structures-and-functions\/","og_locale":"en_US","og_type":"article","og_title":"Day 2: Control Structures and Functions - datmt","og_description":"Welcome back to our Python learning journey! Yesterday, we covered the basics of Python programming, including data types, variables, and input\/output. Today, we&#8217;ll dive into control structures and functions. Control Structures Control structures are constructs that allow us to control the flow of our program based on certain conditions. The most common control structures are ... Read more","og_url":"https:\/\/datmt.com\/python\/day-2-control-structures-and-functions\/","og_site_name":"datmt","article_published_time":"2023-04-03T01:53:09+00:00","article_modified_time":"2023-07-31T13:46:10+00:00","og_image":[{"width":1280,"height":720,"url":"https:\/\/datmt.com\/wp-content\/uploads\/2023\/04\/control-structure-python.png","type":"image\/png"}],"author":"\u0110\u1ea1t Tr\u1ea7n","twitter_card":"summary_large_image","twitter_misc":{"Written by":"\u0110\u1ea1t Tr\u1ea7n","Est. reading time":"5 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/datmt.com\/python\/day-2-control-structures-and-functions\/#article","isPartOf":{"@id":"https:\/\/datmt.com\/python\/day-2-control-structures-and-functions\/"},"author":{"name":"\u0110\u1ea1t Tr\u1ea7n","@id":"https:\/\/datmt.com\/#\/schema\/person\/7736566e3758f424a11fd53f581c4b5e"},"headline":"Day 2: Control Structures and Functions","datePublished":"2023-04-03T01:53:09+00:00","dateModified":"2023-07-31T13:46:10+00:00","mainEntityOfPage":{"@id":"https:\/\/datmt.com\/python\/day-2-control-structures-and-functions\/"},"wordCount":878,"commentCount":0,"publisher":{"@id":"https:\/\/datmt.com\/#\/schema\/person\/7736566e3758f424a11fd53f581c4b5e"},"image":{"@id":"https:\/\/datmt.com\/python\/day-2-control-structures-and-functions\/#primaryimage"},"thumbnailUrl":"https:\/\/datmt.com\/wp-content\/uploads\/2023\/04\/control-structure-python.png","keywords":["python control structure","python function","python list"],"articleSection":["python"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/datmt.com\/python\/day-2-control-structures-and-functions\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/datmt.com\/python\/day-2-control-structures-and-functions\/","url":"https:\/\/datmt.com\/python\/day-2-control-structures-and-functions\/","name":"Day 2: Control Structures and Functions - datmt","isPartOf":{"@id":"https:\/\/datmt.com\/#website"},"primaryImageOfPage":{"@id":"https:\/\/datmt.com\/python\/day-2-control-structures-and-functions\/#primaryimage"},"image":{"@id":"https:\/\/datmt.com\/python\/day-2-control-structures-and-functions\/#primaryimage"},"thumbnailUrl":"https:\/\/datmt.com\/wp-content\/uploads\/2023\/04\/control-structure-python.png","datePublished":"2023-04-03T01:53:09+00:00","dateModified":"2023-07-31T13:46:10+00:00","breadcrumb":{"@id":"https:\/\/datmt.com\/python\/day-2-control-structures-and-functions\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/datmt.com\/python\/day-2-control-structures-and-functions\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/datmt.com\/python\/day-2-control-structures-and-functions\/#primaryimage","url":"https:\/\/datmt.com\/wp-content\/uploads\/2023\/04\/control-structure-python.png","contentUrl":"https:\/\/datmt.com\/wp-content\/uploads\/2023\/04\/control-structure-python.png","width":1280,"height":720,"caption":"Control Structures and Functions"},{"@type":"BreadcrumbList","@id":"https:\/\/datmt.com\/python\/day-2-control-structures-and-functions\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/datmt.com\/"},{"@type":"ListItem","position":2,"name":"Day 2: Control Structures and Functions"}]},{"@type":"WebSite","@id":"https:\/\/datmt.com\/#website","url":"https:\/\/datmt.com\/","name":"datmt","description":"Hands on projects","publisher":{"@id":"https:\/\/datmt.com\/#\/schema\/person\/7736566e3758f424a11fd53f581c4b5e"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/datmt.com\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":["Person","Organization"],"@id":"https:\/\/datmt.com\/#\/schema\/person\/7736566e3758f424a11fd53f581c4b5e","name":"\u0110\u1ea1t Tr\u1ea7n","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/datmt.com\/#\/schema\/person\/image\/","url":"https:\/\/datmt.com\/wp-content\/uploads\/2023\/03\/cropped-profile.jpg","contentUrl":"https:\/\/datmt.com\/wp-content\/uploads\/2023\/03\/cropped-profile.jpg","width":512,"height":512,"caption":"\u0110\u1ea1t Tr\u1ea7n"},"logo":{"@id":"https:\/\/datmt.com\/#\/schema\/person\/image\/"},"description":"I build softwares that solve problems. I also love writing\/documenting things I learn\/want to learn.","sameAs":["https:\/\/datmt.com"],"url":"https:\/\/datmt.com\/author\/mtdat171_c\/"}]}},"_links":{"self":[{"href":"https:\/\/datmt.com\/wp-json\/wp\/v2\/posts\/1697","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/datmt.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/datmt.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/datmt.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/datmt.com\/wp-json\/wp\/v2\/comments?post=1697"}],"version-history":[{"count":2,"href":"https:\/\/datmt.com\/wp-json\/wp\/v2\/posts\/1697\/revisions"}],"predecessor-version":[{"id":1724,"href":"https:\/\/datmt.com\/wp-json\/wp\/v2\/posts\/1697\/revisions\/1724"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/datmt.com\/wp-json\/wp\/v2\/media\/1725"}],"wp:attachment":[{"href":"https:\/\/datmt.com\/wp-json\/wp\/v2\/media?parent=1697"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/datmt.com\/wp-json\/wp\/v2\/categories?post=1697"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/datmt.com\/wp-json\/wp\/v2\/tags?post=1697"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}