{"id":1817,"date":"2019-09-16T06:28:47","date_gmt":"2019-09-16T05:28:47","guid":{"rendered":"https:\/\/learnscripting.org\/?p=1817"},"modified":"2019-09-16T06:28:47","modified_gmt":"2019-09-16T05:28:47","slug":"python-control-flow","status":"publish","type":"post","link":"https:\/\/learnscripting.org\/python-control-flow\/","title":{"rendered":"Python Control Flow"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">This tutorial will discuss how the python interpreter shares the processing among the source code. To prioritize the control python used below keywords to direct the control flow.<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>1.\u00a0<code>if<\/code>\u00a0Statements<\/li><li>2.\u00a0<code>for<\/code>\u00a0Statements<\/li><li>3. The\u00a0<code>range()<\/code>\u00a0Function<\/li><li>4.\u00a0<code>break<\/code>\u00a0and\u00a0<code>continue<\/code>\u00a0Statements, and\u00a0<code>else<\/code>\u00a0Clauses on Loops<\/li><li>5.\u00a0<code>pass<\/code>\u00a0Statements<\/li><li>6. Defining Functions<\/li><li>7. More on Defining Functions<\/li><li>7.1. Default Argument Values<\/li><li>7.2. Keyword Arguments<\/li><li>7.3. Arbitrary Argument Lists<\/li><li>7.4. Unpacking Argument Lists<\/li><li>7.5. Lambda Expressions<\/li><li>7.6. Documentation Strings<\/li><li>7.7. Function Annotations <\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">1.\u00a0<code>if<\/code>\u00a0Statements<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Perhaps the most well-known statement type is the&nbsp;<a href=\"https:\/\/docs.python.org\/3\/reference\/compound_stmts.html#if\"><code>if<\/code><\/a>&nbsp;statement. For example:&gt;&gt;&gt;<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><strong>&gt;&gt;&gt; <\/strong>x = int(input(\"Please enter an integer: \"))\nPlease enter an integer: 42\n<strong>&gt;&gt;&gt; <\/strong><strong>if<\/strong> x &lt; 0:\n<strong>... <\/strong>    x = 0\n<strong>... <\/strong>    print('Negative changed to zero')\n<strong>... <\/strong><strong>elif<\/strong> x == 0:\n<strong>... <\/strong>    print('Zero')\n<strong>... <\/strong><strong>elif<\/strong> x == 1:\n<strong>... <\/strong>    print('Single')\n<strong>... <\/strong><strong>else<\/strong>:\n<strong>... <\/strong>    print('More')\n<strong>...<\/strong>\nMore\n<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">There can be zero or more\u00a0<a href=\"https:\/\/docs.python.org\/3\/reference\/compound_stmts.html#elif\"><code>elif<\/code><\/a>\u00a0parts, and the\u00a0<a href=\"https:\/\/docs.python.org\/3\/reference\/compound_stmts.html#else\"><code>else<\/code><\/a>\u00a0part is optional. The keyword \u2018<code>elif<\/code>\u2019 is short for \u2018else if\u2019, and is useful to avoid excessive indentation. An\u00a0<code>if<\/code>\u00a0\u2026\u00a0<code>elif<\/code>\u00a0\u2026\u00a0<code>elif<\/code>\u00a0\u2026 sequence is a substitute for the\u00a0<code>switch<\/code>\u00a0or\u00a0<code>case<\/code>\u00a0statements found in other languages.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">2.\u00a0<code>for<\/code>\u00a0Statements<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">The&nbsp;<a href=\"https:\/\/docs.python.org\/3\/reference\/compound_stmts.html#for\"><code>for<\/code><\/a>&nbsp;statement in Python differs a bit from what you may be used to in C or Pascal. Rather than always iterating over an arithmetic progression of numbers (like in Pascal), or giving the user the ability to define both the iteration step and halting condition (as C), Python\u2019s&nbsp;<code>for<\/code>&nbsp;statement iterates over the items of any sequence (a list or a string), in the order that they appear in the sequence. For example (no pun intended):&gt;&gt;&gt;<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><strong>&gt;&gt;&gt; <\/strong><em># Measure some strings:<\/em>\n<strong>... <\/strong>words = ['cat', 'window', 'defenestrate']\n<strong>&gt;&gt;&gt; <\/strong><strong>for<\/strong> w <strong>in<\/strong> words:\n<strong>... <\/strong>    print(w, len(w))\n<strong>...<\/strong>\ncat 3\nwindow 6\ndefenestrate 12\n<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">If you need to modify the sequence you are iterating over while inside the loop (for example to duplicate selected items), it is recommended that you first make a copy. Iterating over a sequence does not implicitly make a copy. The slice notation makes this especially convenient:&gt;&gt;&gt;<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><strong>&gt;&gt;&gt; <\/strong><strong>for<\/strong> w <strong>in<\/strong> words[:]:  <em># Loop over a slice copy of the entire list.<\/em>\n<strong>... <\/strong>    <strong>if<\/strong> len(w) &gt; 6:\n<strong>... <\/strong>        words.insert(0, w)\n<strong>...<\/strong>\n<strong>&gt;&gt;&gt; <\/strong>words\n['defenestrate', 'cat', 'window', 'defenestrate']\n<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">With\u00a0<code>for\u00a0w\u00a0in\u00a0words:<\/code>, the example would attempt to create an infinite list, inserting\u00a0<code>defenestrate<\/code>\u00a0over and over again.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">3. The\u00a0<code>range()<\/code>\u00a0Function <\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">If you do need to iterate over a sequence of numbers, the built-in function\u00a0<code>range()<\/code>\u00a0comes in handy. It generates arithmetic progressions:>>><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><strong>&gt;&gt;&gt; <\/strong><strong>for<\/strong> i <strong>in<\/strong> range(5):\n<strong>... <\/strong>    print(i)\n<strong>...<\/strong>\n0\n1\n2\n3\n4\n<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">The given end point is never part of the generated sequence;&nbsp;<code>range(10)<\/code>&nbsp;generates 10 values, the legal indices for items of a sequence of length 10. It is possible to let the range start at another number, or to specify a different increment (even negative; sometimes this is called the \u2018step\u2019):<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">range(5, 10)\n   5, 6, 7, 8, 9\n\nrange(0, 10, 3)\n   0, 3, 6, 9\n\nrange(-10, -100, -30)\n  -10, -40, -70\n<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">To iterate over the indices of a sequence, you can combine\u00a0<code>range()<\/code>\u00a0and\u00a0<code>len(<\/code><a href=\"https:\/\/docs.python.org\/3\/library\/functions.html#len\"><code>)<\/code><\/a>\u00a0as follows:>>><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><strong>&gt;&gt;&gt; <\/strong>a = ['Mary', 'had', 'a', 'little', 'lamb']\n<strong>&gt;&gt;&gt; <\/strong><strong>for<\/strong> i <strong>in<\/strong> range(len(a)):\n<strong>... <\/strong>    print(i, a[i])\n<strong>...<\/strong>\n0 Mary\n1 had\n2 a\n3 little\n4 lamb\n<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">In most such cases, however, it is convenient to use the\u00a0<code>enumerate()<\/code>\u00a0function, see\u00a0Looping Techniques.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">A strange thing happens if you just print a range:&gt;&gt;&gt;<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><strong>&gt;&gt;&gt; <\/strong>print(range(10))\nrange(0, 10)\n<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">In many ways the object returned by\u00a0<code>range<\/code><a href=\"https:\/\/docs.python.org\/3\/library\/stdtypes.html#range\"><code>()<\/code><\/a>\u00a0behaves as if it is a list, but in fact it isn\u2019t. It is an object which returns the successive items of the desired sequence when you iterate over it, but it doesn\u2019t really make the list, thus saving space.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">We say such an object is\u00a0<em>iterable<\/em>, that is, suitable as a target for functions and constructs that expect something from which they can obtain successive items until the supply is exhausted. We have seen that the\u00a0<a href=\"https:\/\/docs.python.org\/3\/reference\/compound_stmts.html#for\"><code>for<\/code><\/a>\u00a0statement is such an\u00a0<em>iterator<\/em>. The function\u00a0<code>list<\/code><a href=\"https:\/\/docs.python.org\/3\/library\/stdtypes.html#list\"><code>()<\/code><\/a>\u00a0is another; it creates lists from iterables:>>><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><strong>&gt;&gt;&gt; <\/strong>list(range(5))\n[0, 1, 2, 3, 4]\n<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Later we will see more functions that return iterables and take iterables as argument.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">4.\u00a0<code>break<\/code>\u00a0and\u00a0<code>continue<\/code>\u00a0Statements, and\u00a0<code>else<\/code>\u00a0Clauses on Loops<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">The&nbsp;<a href=\"https:\/\/docs.python.org\/3\/reference\/simple_stmts.html#break\"><code>break<\/code><\/a>&nbsp;statement, like in C, breaks out of the innermost enclosing&nbsp;<a href=\"https:\/\/docs.python.org\/3\/reference\/compound_stmts.html#for\"><code>for<\/code><\/a>&nbsp;or&nbsp;<a href=\"https:\/\/docs.python.org\/3\/reference\/compound_stmts.html#while\"><code>while<\/code><\/a>&nbsp;loop.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Loop statements may have an&nbsp;<code>else<\/code>&nbsp;clause; it is executed when the loop terminates through exhaustion of the list (with&nbsp;<a href=\"https:\/\/docs.python.org\/3\/reference\/compound_stmts.html#for\"><code>for<\/code><\/a>) or when the condition becomes false (with&nbsp;<a href=\"https:\/\/docs.python.org\/3\/reference\/compound_stmts.html#while\"><code>while<\/code><\/a>), but not when the loop is terminated by a&nbsp;<a href=\"https:\/\/docs.python.org\/3\/reference\/simple_stmts.html#break\"><code>break<\/code><\/a>&nbsp;statement. This is exemplified by the following loop, which searches for prime numbers:&gt;&gt;&gt;<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><strong>&gt;&gt;&gt; <\/strong><strong>for<\/strong> n <strong>in<\/strong> range(2, 10):\n<strong>... <\/strong>    <strong>for<\/strong> x <strong>in<\/strong> range(2, n):\n<strong>... <\/strong>        <strong>if<\/strong> n % x == 0:\n<strong>... <\/strong>            print(n, 'equals', x, '*', n\/\/x)\n<strong>... <\/strong>            <strong>break<\/strong>\n<strong>... <\/strong>    <strong>else<\/strong>:\n<strong>... <\/strong>        <em># loop fell through without finding a factor<\/em>\n<strong>... <\/strong>        print(n, 'is a prime number')\n<strong>...<\/strong>\n2 is a prime number\n3 is a prime number\n4 equals 2 * 2\n5 is a prime number\n6 equals 2 * 3\n7 is a prime number\n8 equals 2 * 4\n9 equals 3 * 3\n<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">(Yes, this is the correct code. Look closely: the&nbsp;<code>else<\/code>&nbsp;clause belongs to the&nbsp;<a href=\"https:\/\/docs.python.org\/3\/reference\/compound_stmts.html#for\"><code>for<\/code><\/a>&nbsp;loop,&nbsp;<strong>not<\/strong>&nbsp;the&nbsp;<a href=\"https:\/\/docs.python.org\/3\/reference\/compound_stmts.html#if\"><code>if<\/code><\/a>&nbsp;statement.)<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">When used with a loop, the&nbsp;<code>else<\/code>&nbsp;clause has more in common with the&nbsp;<code>else<\/code>&nbsp;clause of a&nbsp;<a href=\"https:\/\/docs.python.org\/3\/reference\/compound_stmts.html#try\"><code>try<\/code><\/a>&nbsp;statement than it does that of&nbsp;<a href=\"https:\/\/docs.python.org\/3\/reference\/compound_stmts.html#if\"><code>if<\/code><\/a>&nbsp;statements: a&nbsp;<code>try<\/code>&nbsp;statement\u2019s&nbsp;<code>else<\/code>&nbsp;clause runs when no exception occurs, and a loop\u2019s&nbsp;<code>else<\/code>&nbsp;clause runs when no&nbsp;<code>break<\/code>&nbsp;occurs. For more on the&nbsp;<code>try<\/code>&nbsp;statement and exceptions, see&nbsp;<a href=\"https:\/\/docs.python.org\/3\/tutorial\/errors.html#tut-handling\">Handling Exceptions<\/a>.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The&nbsp;<a href=\"https:\/\/docs.python.org\/3\/reference\/simple_stmts.html#continue\"><code>continue<\/code><\/a>&nbsp;statement, also borrowed from C, continues with the next iteration of the loop:&gt;&gt;&gt;<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><strong>&gt;&gt;&gt; <\/strong><strong>for<\/strong> num <strong>in<\/strong> range(2, 10):\n<strong>... <\/strong>    <strong>if<\/strong> num % 2 == 0:\n<strong>... <\/strong>        print(\"Found an even number\", num)\n<strong>... <\/strong>        <strong>continue<\/strong>\n<strong>... <\/strong>    print(\"Found a number\", num)\nFound an even number 2\nFound a number 3\nFound an even number 4\nFound a number 5\nFound an even number 6\nFound a number 7\nFound an even number 8\nFound a number 9<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">5.\u00a0<code>pass<\/code>\u00a0Statements<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">The&nbsp;<a href=\"https:\/\/docs.python.org\/3\/reference\/simple_stmts.html#pass\"><code>pass<\/code><\/a>&nbsp;statement does nothing. It can be used when a statement is required syntactically but the program requires no action. For example:&gt;&gt;&gt;<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><strong>&gt;&gt;&gt; <\/strong><strong>while<\/strong> <strong>True<\/strong>:\n<strong>... <\/strong>    <strong>pass<\/strong>  <em># Busy-wait for keyboard interrupt (Ctrl+C)<\/em>\n<strong>...<\/strong>\n<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">This is commonly used for creating minimal classes:&gt;&gt;&gt;<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><strong>&gt;&gt;&gt; <\/strong><strong>class<\/strong> <strong>MyEmptyClass<\/strong>:\n<strong>... <\/strong>    <strong>pass<\/strong>\n<strong>...<\/strong>\n<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Another place&nbsp;<a href=\"https:\/\/docs.python.org\/3\/reference\/simple_stmts.html#pass\"><code>pass<\/code><\/a>&nbsp;can be used is as a place-holder for a function or conditional body when you are working on new code, allowing you to keep thinking at a more abstract level. The&nbsp;<code>pass<\/code>&nbsp;is silently ignored:&gt;&gt;&gt;<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><strong>&gt;&gt;&gt; <\/strong><strong>def<\/strong> initlog(*args):\n<strong>... <\/strong>    <strong>pass<\/strong>   <em># Remember to implement this!<\/em>\n<strong>...<\/strong>\n<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">6. Defining Functions<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">We can create a function that writes the Fibonacci series to an arbitrary boundary:&gt;&gt;&gt;<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><strong>&gt;&gt;&gt; <\/strong><strong>def<\/strong> fib(n):    <em># write Fibonacci series up to n<\/em>\n<strong>... <\/strong>    <em>\"\"\"Print a Fibonacci series up to n.\"\"\"<\/em>\n<strong>... <\/strong>    a, b = 0, 1\n<strong>... <\/strong>    <strong>while<\/strong> a &lt; n:\n<strong>... <\/strong>        print(a, end=' ')\n<strong>... <\/strong>        a, b = b, a+b\n<strong>... <\/strong>    print()\n<strong>...<\/strong>\n<strong>&gt;&gt;&gt; <\/strong><em># Now call the function we just defined:<\/em>\n<strong>... <\/strong>fib(2000)\n0 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597\n<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">The keyword&nbsp;<a href=\"https:\/\/docs.python.org\/3\/reference\/compound_stmts.html#def\"><code>def<\/code><\/a>&nbsp;introduces a function&nbsp;<em>definition<\/em>. It must be followed by the function name and the parenthesized list of formal parameters. The statements that form the body of the function start at the next line, and must be indented.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The first statement of the function body can optionally be a string literal; this string literal is the function\u2019s documentation string, or&nbsp;<em>docstring<\/em>. (More about docstrings can be found in the section&nbsp;<a href=\"https:\/\/docs.python.org\/3\/tutorial\/controlflow.html#tut-docstrings\">Documentation Strings<\/a>.) There are tools which use docstrings to automatically produce online or printed documentation, or to let the user interactively browse through code; it\u2019s good practice to include docstrings in code that you write, so make a habit of it.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The&nbsp;<em>execution<\/em>&nbsp;of a function introduces a new symbol table used for the local variables of the function. More precisely, all variable assignments in a function store the value in the local symbol table; whereas variable references first look in the local symbol table, then in the local symbol tables of enclosing functions, then in the global symbol table, and finally in the table of built-in names. Thus, global variables and variables of enclosing functions cannot be directly assigned a value within a function (unless, for global variables, named in a&nbsp;<a href=\"https:\/\/docs.python.org\/3\/reference\/simple_stmts.html#global\"><code>global<\/code><\/a>&nbsp;statement, or, for variables of enclosing functions, named in a&nbsp;<a href=\"https:\/\/docs.python.org\/3\/reference\/simple_stmts.html#nonlocal\"><code>nonlocal<\/code><\/a>&nbsp;statement), although they may be referenced.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The actual parameters (arguments) to a function call are introduced in the local symbol table of the called function when it is called; thus, arguments are passed using&nbsp;<em>call by value<\/em>&nbsp;(where the&nbsp;<em>value<\/em>&nbsp;is always an object&nbsp;<em>reference<\/em>, not the value of the object).&nbsp;<a href=\"https:\/\/docs.python.org\/3\/tutorial\/controlflow.html#id2\">1<\/a>&nbsp;When a function calls another function, a new local symbol table is created for that call.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">A function definition introduces the function name in the current symbol table. The value of the function name has a type that is recognized by the interpreter as a user-defined function. This value can be assigned to another name which can then also be used as a function. This serves as a general renaming mechanism:&gt;&gt;&gt;<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><strong>&gt;&gt;&gt; <\/strong>fib\n&lt;function fib at 10042ed0&gt;\n<strong>&gt;&gt;&gt; <\/strong>f = fib\n<strong>&gt;&gt;&gt; <\/strong>f(100)\n0 1 1 2 3 5 8 13 21 34 55 89\n<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Coming from other languages, you might object that&nbsp;<code>fib<\/code>&nbsp;is not a function but a procedure since it doesn\u2019t return a value. In fact, even functions without a&nbsp;<a href=\"https:\/\/docs.python.org\/3\/reference\/simple_stmts.html#return\"><code>return<\/code><\/a>&nbsp;statement do return a value, albeit a rather boring one. This value is called&nbsp;<code>None<\/code>&nbsp;(it\u2019s a built-in name). Writing the value&nbsp;<code>None<\/code>&nbsp;is normally suppressed by the interpreter if it would be the only value written. You can see it if you really want to using&nbsp;<a href=\"https:\/\/docs.python.org\/3\/library\/functions.html#print\"><code>print()<\/code><\/a>:&gt;&gt;&gt;<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><strong>&gt;&gt;&gt; <\/strong>fib(0)\n<strong>&gt;&gt;&gt; <\/strong>print(fib(0))\nNone\n<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">It is simple to write a function that returns a list of the numbers of the Fibonacci series, instead of printing it:&gt;&gt;&gt;<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><strong>&gt;&gt;&gt; <\/strong><strong>def<\/strong> fib2(n):  <em># return Fibonacci series up to n<\/em>\n<strong>... <\/strong>    <em>\"\"\"Return a list containing the Fibonacci series up to n.\"\"\"<\/em>\n<strong>... <\/strong>    result = []\n<strong>... <\/strong>    a, b = 0, 1\n<strong>... <\/strong>    <strong>while<\/strong> a &lt; n:\n<strong>... <\/strong>        result.append(a)    <em># see below<\/em>\n<strong>... <\/strong>        a, b = b, a+b\n<strong>... <\/strong>    <strong>return<\/strong> result\n<strong>...<\/strong>\n<strong>&gt;&gt;&gt; <\/strong>f100 = fib2(100)    <em># call it<\/em>\n<strong>&gt;&gt;&gt; <\/strong>f100                <em># write the result<\/em>\n[0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89]\n<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">This example, as usual, demonstrates some new Python features:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>The&nbsp;<a href=\"https:\/\/docs.python.org\/3\/reference\/simple_stmts.html#return\"><code>return<\/code><\/a>&nbsp;statement returns with a value from a function.&nbsp;<code>return<\/code>&nbsp;without an expression argument returns&nbsp;<code>None<\/code>. Falling off the end of a function also returns&nbsp;<code>None<\/code>.<\/li><li>The statement&nbsp;<code>result.append(a)<\/code>&nbsp;calls a&nbsp;<em>method<\/em>&nbsp;of the list object&nbsp;<code>result<\/code>. A method is a function that \u2018belongs\u2019 to an object and is named&nbsp;<code>obj.methodname<\/code>, where&nbsp;<code>obj<\/code>&nbsp;is some object (this may be an expression), and&nbsp;<code>methodname<\/code>&nbsp;is the name of a method that is defined by the object\u2019s type. Different types define different methods. Methods of different types may have the same name without causing ambiguity. (It is possible to define your own object types and methods, using&nbsp;<em>classes<\/em>, see&nbsp;<a href=\"https:\/\/docs.python.org\/3\/tutorial\/classes.html#tut-classes\">Classes<\/a>) The method&nbsp;<code>append()<\/code>&nbsp;shown in the example is defined for list objects; it adds a new element at the end of the list. In this example it is equivalent to&nbsp;<code>result&nbsp;=&nbsp;result&nbsp;+&nbsp;[a]<\/code>, but more efficient.<\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">7. More on Defining Functions<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">It is also possible to define functions with a variable number of arguments. There are three forms, which can be combined.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">7.1. Default Argument Values<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">The most useful form is to specify a default value for one or more arguments. This creates a function that can be called with fewer arguments than it is defined to allow. For example:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><strong>def<\/strong> ask_ok(prompt, retries=4, reminder='Please try again!'):\n    <strong>while<\/strong> <strong>True<\/strong>:\n        ok = input(prompt)\n        <strong>if<\/strong> ok <strong>in<\/strong> ('y', 'ye', 'yes'):\n            <strong>return<\/strong> <strong>True<\/strong>\n        <strong>if<\/strong> ok <strong>in<\/strong> ('n', 'no', 'nop', 'nope'):\n            <strong>return<\/strong> <strong>False<\/strong>\n        retries = retries - 1\n        <strong>if<\/strong> retries &lt; 0:\n            <strong>raise<\/strong> ValueError('invalid user response')\n        print(reminder)\n<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">This function can be called in several ways:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>giving only the mandatory argument:&nbsp;<code>ask_ok('Do&nbsp;you&nbsp;really&nbsp;want&nbsp;to&nbsp;quit?')<\/code><\/li><li>giving one of the optional arguments:&nbsp;<code>ask_ok('OK&nbsp;to&nbsp;overwrite&nbsp;the&nbsp;file?',&nbsp;2)<\/code><\/li><li>or even giving all arguments:&nbsp;<code>ask_ok('OK&nbsp;to&nbsp;overwrite&nbsp;the&nbsp;file?',&nbsp;2,&nbsp;'Come&nbsp;on,&nbsp;only&nbsp;yes&nbsp;or&nbsp;no!')<\/code><\/li><\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">This example also introduces the&nbsp;<a href=\"https:\/\/docs.python.org\/3\/reference\/expressions.html#in\"><code>in<\/code><\/a>&nbsp;keyword. This tests whether or not a sequence contains a certain value.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The default values are evaluated at the point of function definition in the&nbsp;<em>defining<\/em>&nbsp;scope, so that<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">i = 5\n\n<strong>def<\/strong> f(arg=i):\n    print(arg)\n\ni = 6\nf()\n<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">will print&nbsp;<code>5<\/code>.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Important warning:<\/strong>&nbsp;The default value is evaluated only once. This makes a difference when the default is a mutable object such as a list, dictionary, or instances of most classes. For example, the following function accumulates the arguments passed to it on subsequent calls:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><strong>def<\/strong> f(a, L=[]):\n    L.append(a)\n    <strong>return<\/strong> L\n\nprint(f(1))\nprint(f(2))\nprint(f(3))\n<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">This will print<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">[1]\n[1, 2]\n[1, 2, 3]\n<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">If you don\u2019t want the default to be shared between subsequent calls, you can write the function like this instead:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><strong>def<\/strong> f(a, L=<strong>None<\/strong>):\n    <strong>if<\/strong> L <strong>is<\/strong> <strong>None<\/strong>:\n        L = []\n    L.append(a)\n    <strong>return<\/strong> L\n<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">7.2. Keyword Arguments<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Functions can also be called using&nbsp;<a href=\"https:\/\/docs.python.org\/3\/glossary.html#term-keyword-argument\">keyword arguments<\/a>&nbsp;of the form&nbsp;<code>kwarg=value<\/code>. For instance, the following function:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><strong>def<\/strong> parrot(voltage, state='a stiff', action='voom', type='Norwegian Blue'):\n    print(\"-- This parrot wouldn't\", action, end=' ')\n    print(\"if you put\", voltage, \"volts through it.\")\n    print(\"-- Lovely plumage, the\", type)\n    print(\"-- It's\", state, \"!\")\n<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">accepts one required argument (<code>voltage<\/code>) and three optional arguments (<code>state<\/code>,&nbsp;<code>action<\/code>, and&nbsp;<code>type<\/code>). This function can be called in any of the following ways:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">parrot(1000)                                          <em># 1 positional argument<\/em>\nparrot(voltage=1000)                                  <em># 1 keyword argument<\/em>\nparrot(voltage=1000000, action='VOOOOOM')             <em># 2 keyword arguments<\/em>\nparrot(action='VOOOOOM', voltage=1000000)             <em># 2 keyword arguments<\/em>\nparrot('a million', 'bereft of life', 'jump')         <em># 3 positional arguments<\/em>\nparrot('a thousand', state='pushing up the daisies')  <em># 1 positional, 1 keyword<\/em>\n<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">but all the following calls would be invalid:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">parrot()                     <em># required argument missing<\/em>\nparrot(voltage=5.0, 'dead')  <em># non-keyword argument after a keyword argument<\/em>\nparrot(110, voltage=220)     <em># duplicate value for the same argument<\/em>\nparrot(actor='John Cleese')  <em># unknown keyword argument<\/em>\n<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">In a function call, keyword arguments must follow positional arguments. All the keyword arguments passed must match one of the arguments accepted by the function (e.g.&nbsp;<code>actor<\/code>&nbsp;is not a valid argument for the&nbsp;<code>parrot<\/code>&nbsp;function), and their order is not important. This also includes non-optional arguments (e.g.&nbsp;<code>parrot(voltage=1000)<\/code>&nbsp;is valid too). No argument may receive a value more than once. Here\u2019s an example that fails due to this restriction:&gt;&gt;&gt;<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><strong>&gt;&gt;&gt; <\/strong><strong>def<\/strong> function(a):\n<strong>... <\/strong>    <strong>pass<\/strong>\n<strong>...<\/strong>\n<strong>&gt;&gt;&gt; <\/strong>function(0, a=0)\nTraceback (most recent call last):\n  File \"&lt;stdin&gt;\", line 1, in &lt;module&gt;\nTypeError: function() got multiple values for keyword argument 'a'\n<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">When a final formal parameter of the form&nbsp;<code>**name<\/code>&nbsp;is present, it receives a dictionary (see&nbsp;<a href=\"https:\/\/docs.python.org\/3\/library\/stdtypes.html#typesmapping\">Mapping Types \u2014 dict<\/a>) containing all keyword arguments except for those corresponding to a formal parameter. This may be combined with a formal parameter of the form&nbsp;<code>*name<\/code>&nbsp;(described in the next subsection) which receives a&nbsp;<a href=\"https:\/\/docs.python.org\/3\/tutorial\/datastructures.html#tut-tuples\">tuple<\/a>&nbsp;containing the positional arguments beyond the formal parameter list. (<code>*name<\/code>&nbsp;must occur before&nbsp;<code>**name<\/code>.) For example, if we define a function like this:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><strong>def<\/strong> cheeseshop(kind, *arguments, **keywords):\n    print(\"-- Do you have any\", kind, \"?\")\n    print(\"-- I'm sorry, we're all out of\", kind)\n    <strong>for<\/strong> arg <strong>in<\/strong> arguments:\n        print(arg)\n    print(\"-\" * 40)\n    <strong>for<\/strong> kw <strong>in<\/strong> keywords:\n        print(kw, \":\", keywords[kw])\n<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">It could be called like this:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">cheeseshop(\"Limburger\", \"It's very runny, sir.\",\n           \"It's really very, VERY runny, sir.\",\n           shopkeeper=\"Michael Palin\",\n           client=\"John Cleese\",\n           sketch=\"Cheese Shop Sketch\")\n<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">and of course it would print:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">-- Do you have any Limburger ?\n-- I'm sorry, we're all out of Limburger\nIt's very runny, sir.\nIt's really very, VERY runny, sir.\n----------------------------------------\nshopkeeper : Michael Palin\nclient : John Cleese\nsketch : Cheese Shop Sketch\n<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Note that the order in which the keyword arguments are printed is guaranteed to match the order in which they were provided in the function call.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">7.3. Arbitrary Argument Lists<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Finally, the least frequently used option is to specify that a function can be called with an arbitrary number of arguments. These arguments will be wrapped up in a tuple (see&nbsp;<a href=\"https:\/\/docs.python.org\/3\/tutorial\/datastructures.html#tut-tuples\">Tuples and Sequences<\/a>). Before the variable number of arguments, zero or more normal arguments may occur.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><strong>def<\/strong> write_multiple_items(file, separator, *args):\n    file.write(separator.join(args))\n<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Normally, these&nbsp;<code>variadic<\/code>&nbsp;arguments will be last in the list of formal parameters, because they scoop up all remaining input arguments that are passed to the function. Any formal parameters which occur after the&nbsp;<code>*args<\/code>&nbsp;parameter are \u2018keyword-only\u2019 arguments, meaning that they can only be used as keywords rather than positional arguments.&gt;&gt;&gt;<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><strong>&gt;&gt;&gt; <\/strong><strong>def<\/strong> concat(*args, sep=\"\/\"):\n<strong>... <\/strong>    <strong>return<\/strong> sep.join(args)\n<strong>...<\/strong>\n<strong>&gt;&gt;&gt; <\/strong>concat(\"earth\", \"mars\", \"venus\")\n'earth\/mars\/venus'\n<strong>&gt;&gt;&gt; <\/strong>concat(\"earth\", \"mars\", \"venus\", sep=\".\")\n'earth.mars.venus'\n<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">7.4. Unpacking Argument Lists<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">The reverse situation occurs when the arguments are already in a list or tuple but need to be unpacked for a function call requiring separate positional arguments. For instance, the built-in&nbsp;<a href=\"https:\/\/docs.python.org\/3\/library\/stdtypes.html#range\"><code>range()<\/code><\/a>&nbsp;function expects separate&nbsp;<em>start<\/em>&nbsp;and&nbsp;<em>stop<\/em>&nbsp;arguments. If they are not available separately, write the function call with the&nbsp;<code>*<\/code>&nbsp;operator to unpack the arguments out of a list or tuple:&gt;&gt;&gt;<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><strong>&gt;&gt;&gt; <\/strong>list(range(3, 6))            <em># normal call with separate arguments<\/em>\n[3, 4, 5]\n<strong>&gt;&gt;&gt; <\/strong>args = [3, 6]\n<strong>&gt;&gt;&gt; <\/strong>list(range(*args))            <em># call with arguments unpacked from a list<\/em>\n[3, 4, 5]\n<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">In the same fashion, dictionaries can deliver keyword arguments with the&nbsp;<code>**<\/code>&nbsp;operator:&gt;&gt;&gt;<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><strong>&gt;&gt;&gt; <\/strong><strong>def<\/strong> parrot(voltage, state='a stiff', action='voom'):\n<strong>... <\/strong>    print(\"-- This parrot wouldn't\", action, end=' ')\n<strong>... <\/strong>    print(\"if you put\", voltage, \"volts through it.\", end=' ')\n<strong>... <\/strong>    print(\"E's\", state, \"!\")\n<strong>...<\/strong>\n<strong>&gt;&gt;&gt; <\/strong>d = {\"voltage\": \"four million\", \"state\": \"bleedin' demised\", \"action\": \"VOOM\"}\n<strong>&gt;&gt;&gt; <\/strong>parrot(**d)\n-- This parrot wouldn't VOOM if you put four million volts through it. E's bleedin' demised !\n<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">7.5. Lambda Expressions<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Small anonymous functions can be created with the&nbsp;<a href=\"https:\/\/docs.python.org\/3\/reference\/expressions.html#lambda\"><code>lambda<\/code><\/a>&nbsp;keyword. This function returns the sum of its two arguments:&nbsp;<code>lambda&nbsp;a,&nbsp;b:&nbsp;a+b<\/code>. Lambda functions can be used wherever function objects are required. They are syntactically restricted to a single expression. Semantically, they are just syntactic sugar for a normal function definition. Like nested function definitions, lambda functions can reference variables from the containing scope:&gt;&gt;&gt;<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><strong>&gt;&gt;&gt; <\/strong><strong>def<\/strong> make_incrementor(n):\n<strong>... <\/strong>    <strong>return<\/strong> <strong>lambda<\/strong> x: x + n\n<strong>...<\/strong>\n<strong>&gt;&gt;&gt; <\/strong>f = make_incrementor(42)\n<strong>&gt;&gt;&gt; <\/strong>f(0)\n42\n<strong>&gt;&gt;&gt; <\/strong>f(1)\n43\n<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">The above example uses a lambda expression to return a function. Another use is to pass a small function as an argument:&gt;&gt;&gt;<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><strong>&gt;&gt;&gt; <\/strong>pairs = [(1, 'one'), (2, 'two'), (3, 'three'), (4, 'four')]\n<strong>&gt;&gt;&gt; <\/strong>pairs.sort(key=<strong>lambda<\/strong> pair: pair[1])\n<strong>&gt;&gt;&gt; <\/strong>pairs\n[(4, 'four'), (1, 'one'), (3, 'three'), (2, 'two')]\n<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">7.6. Documentation Strings<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Here are some conventions about the content and formatting of documentation strings.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The first line should always be a short, concise summary of the object\u2019s purpose. For brevity, it should not explicitly state the object\u2019s name or type, since these are available by other means (except if the name happens to be a verb describing a function\u2019s operation). This line should begin with a capital letter and end with a period.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">If there are more lines in the documentation string, the second line should be blank, visually separating the summary from the rest of the description. The following lines should be one or more paragraphs describing the object\u2019s calling conventions, its side effects, etc.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The Python parser does not strip indentation from multi-line string literals in Python, so tools that process documentation have to strip indentation if desired. This is done using the following convention. The first non-blank line&nbsp;<em>after<\/em>&nbsp;the first line of the string determines the amount of indentation for the entire documentation string. (We can\u2019t use the first line since it is generally adjacent to the string\u2019s opening quotes so its indentation is not apparent in the string literal.) Whitespace \u201cequivalent\u201d to this indentation is then stripped from the start of all lines of the string. Lines that are indented less should not occur, but if they occur all their leading whitespace should be stripped. Equivalence of whitespace should be tested after expansion of tabs (to 8 spaces, normally).<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Here is an example of a multi-line docstring:&gt;&gt;&gt;<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><strong>&gt;&gt;&gt; <\/strong><strong>def<\/strong> my_function():\n<strong>... <\/strong>    <em>\"\"\"Do nothing, but document it.<\/em>\n<strong>...<\/strong><em><\/em>\n<strong>... <\/strong><em>    No, really, it doesn't do anything.<\/em>\n<strong>... <\/strong><em>    \"\"\"<\/em>\n<strong>... <\/strong>    <strong>pass<\/strong>\n<strong>...<\/strong>\n<strong>&gt;&gt;&gt; <\/strong>print(my_function.__doc__)\nDo nothing, but document it.\n\n    No, really, it doesn't do anything.\n<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">7.7. Function Annotations<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\"><a href=\"https:\/\/docs.python.org\/3\/reference\/compound_stmts.html#function\">Function annotations<\/a>&nbsp;are completely optional metadata information about the types used by user-defined functions (see&nbsp;<a href=\"https:\/\/www.python.org\/dev\/peps\/pep-3107\"><strong>PEP 3107<\/strong><\/a>&nbsp;and&nbsp;<a href=\"https:\/\/www.python.org\/dev\/peps\/pep-0484\"><strong>PEP 484<\/strong><\/a>&nbsp;for more information).<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><a href=\"https:\/\/docs.python.org\/3\/glossary.html#term-function-annotation\">Annotations<\/a>&nbsp;are stored in the&nbsp;<code>__annotations__<\/code>&nbsp;attribute of the function as a dictionary and have no effect on any other part of the function. Parameter annotations are defined by a colon after the parameter name, followed by an expression evaluating to the value of the annotation. Return annotations are defined by a literal&nbsp;<code>-&gt;<\/code>, followed by an expression, between the parameter list and the colon denoting the end of the&nbsp;<a href=\"https:\/\/docs.python.org\/3\/reference\/compound_stmts.html#def\"><code>def<\/code><\/a>&nbsp;statement. The following example has a positional argument, a keyword argument, and the return value annotated:&gt;&gt;&gt;<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><strong>&gt;&gt;&gt; <\/strong><strong>def<\/strong> f(ham: str, eggs: str = 'eggs') -&gt; str:\n<strong>... <\/strong>    print(\"Annotations:\", f.__annotations__)\n<strong>... <\/strong>    print(\"Arguments:\", ham, eggs)\n<strong>... <\/strong>    <strong>return<\/strong> ham + ' and ' + eggs\n<strong>...<\/strong>\n<strong>&gt;&gt;&gt; <\/strong>f('spam')\nAnnotations: {'ham': &lt;class 'str'&gt;, 'return': &lt;class 'str'&gt;, 'eggs': &lt;class 'str'&gt;}\nArguments: spam eggs\n'spam and eggs'\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>This tutorial will discuss how the python interpreter shares the processing among the source code. To prioritize the control python used below keywords to direct the control flow. 1.\u00a0if\u00a0Statements 2.\u00a0for\u00a0Statements 3. The\u00a0range()\u00a0Function 4.\u00a0break\u00a0and\u00a0continue\u00a0Statements, and\u00a0else\u00a0Clauses on Loops 5.\u00a0pass\u00a0Statements 6. Defining Functions 7. More on Defining Functions 7.1. Default Argument Values 7.2. Keyword Arguments 7.3. Arbitrary Argument &hellip;<\/p>\n","protected":false},"author":1,"featured_media":1818,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"rop_custom_images_group":[],"rop_custom_messages_group":[],"rop_publish_now":"initial","rop_publish_now_accounts":{"facebook_2613112545524186_272996453239123":"","twitter_aTo5NzY3MTIyNTIwMzEwNTM4MjQ7_976712252031053800":""},"rop_publish_now_history":[],"rop_publish_now_status":"pending","footnotes":""},"categories":[24],"tags":[],"class_list":["post-1817","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-python"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v28.1 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Python Control Flow - Learn Scripting<\/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:\/\/learnscripting.org\/python-control-flow\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Python Control Flow - Learn Scripting\" \/>\n<meta property=\"og:description\" content=\"This tutorial will discuss how the python interpreter shares the processing among the source code. To prioritize the control python used below keywords to direct the control flow. 1.\u00a0if\u00a0Statements 2.\u00a0for\u00a0Statements 3. The\u00a0range()\u00a0Function 4.\u00a0break\u00a0and\u00a0continue\u00a0Statements, and\u00a0else\u00a0Clauses on Loops 5.\u00a0pass\u00a0Statements 6. Defining Functions 7. More on Defining Functions 7.1. Default Argument Values 7.2. Keyword Arguments 7.3. Arbitrary Argument &hellip;\" \/>\n<meta property=\"og:url\" content=\"https:\/\/learnscripting.org\/python-control-flow\/\" \/>\n<meta property=\"og:site_name\" content=\"Learn Scripting\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/profile.php?id=100064270142636\" \/>\n<meta property=\"article:published_time\" content=\"2019-09-16T05:28:47+00:00\" \/>\n<meta name=\"author\" content=\"Shakti Das\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Shakti Das\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"19 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/learnscripting.org\\\/python-control-flow\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/learnscripting.org\\\/python-control-flow\\\/\"},\"author\":{\"name\":\"Shakti Das\",\"@id\":\"https:\\\/\\\/learnscripting.org\\\/#\\\/schema\\\/person\\\/71045e0c38b97ea7f76363d6effa320c\"},\"headline\":\"Python Control Flow\",\"datePublished\":\"2019-09-16T05:28:47+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/learnscripting.org\\\/python-control-flow\\\/\"},\"wordCount\":2552,\"publisher\":{\"@id\":\"https:\\\/\\\/learnscripting.org\\\/#organization\"},\"image\":{\"@id\":\"https:\\\/\\\/learnscripting.org\\\/python-control-flow\\\/#primaryimage\"},\"thumbnailUrl\":\"\",\"articleSection\":[\"Python\"],\"inLanguage\":\"en-US\"},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/learnscripting.org\\\/python-control-flow\\\/\",\"url\":\"https:\\\/\\\/learnscripting.org\\\/python-control-flow\\\/\",\"name\":\"Python Control Flow - Learn Scripting\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/learnscripting.org\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/learnscripting.org\\\/python-control-flow\\\/#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/learnscripting.org\\\/python-control-flow\\\/#primaryimage\"},\"thumbnailUrl\":\"\",\"datePublished\":\"2019-09-16T05:28:47+00:00\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/learnscripting.org\\\/python-control-flow\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/learnscripting.org\\\/python-control-flow\\\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/learnscripting.org\\\/python-control-flow\\\/#primaryimage\",\"url\":\"\",\"contentUrl\":\"\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/learnscripting.org\\\/python-control-flow\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/learnscripting.org\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Python Control Flow\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/learnscripting.org\\\/#website\",\"url\":\"https:\\\/\\\/learnscripting.org\\\/\",\"name\":\"Learn Scripting\",\"description\":\"Coding Knowledge Unveiled: Empower Yourself\",\"publisher\":{\"@id\":\"https:\\\/\\\/learnscripting.org\\\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/learnscripting.org\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\\\/\\\/learnscripting.org\\\/#organization\",\"name\":\"Learn Scripting\",\"url\":\"https:\\\/\\\/learnscripting.org\\\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/learnscripting.org\\\/#\\\/schema\\\/logo\\\/image\\\/\",\"url\":\"https:\\\/\\\/i0.wp.com\\\/learnscripting.org\\\/wp-content\\\/uploads\\\/2022\\\/03\\\/cropped-ls-600x600-1-512x512.jpg?fit=512%2C512&ssl=1\",\"contentUrl\":\"https:\\\/\\\/i0.wp.com\\\/learnscripting.org\\\/wp-content\\\/uploads\\\/2022\\\/03\\\/cropped-ls-600x600-1-512x512.jpg?fit=512%2C512&ssl=1\",\"width\":512,\"height\":512,\"caption\":\"Learn Scripting\"},\"image\":{\"@id\":\"https:\\\/\\\/learnscripting.org\\\/#\\\/schema\\\/logo\\\/image\\\/\"},\"sameAs\":[\"https:\\\/\\\/www.facebook.com\\\/profile.php?id=100064270142636\"]},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/learnscripting.org\\\/#\\\/schema\\\/person\\\/71045e0c38b97ea7f76363d6effa320c\",\"name\":\"Shakti Das\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/5492ae25bd62294799695759ba85aaa28ae3d1de6b30be7cbdc377abc2ea0aac?s=96&d=mm&r=g\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/5492ae25bd62294799695759ba85aaa28ae3d1de6b30be7cbdc377abc2ea0aac?s=96&d=mm&r=g\",\"contentUrl\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/5492ae25bd62294799695759ba85aaa28ae3d1de6b30be7cbdc377abc2ea0aac?s=96&d=mm&r=g\",\"caption\":\"Shakti Das\"},\"sameAs\":[\"https:\\\/\\\/learnscripting.org\"],\"url\":\"https:\\\/\\\/learnscripting.org\\\/author\\\/53kgl\\\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Python Control Flow - Learn Scripting","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:\/\/learnscripting.org\/python-control-flow\/","og_locale":"en_US","og_type":"article","og_title":"Python Control Flow - Learn Scripting","og_description":"This tutorial will discuss how the python interpreter shares the processing among the source code. To prioritize the control python used below keywords to direct the control flow. 1.\u00a0if\u00a0Statements 2.\u00a0for\u00a0Statements 3. The\u00a0range()\u00a0Function 4.\u00a0break\u00a0and\u00a0continue\u00a0Statements, and\u00a0else\u00a0Clauses on Loops 5.\u00a0pass\u00a0Statements 6. Defining Functions 7. More on Defining Functions 7.1. Default Argument Values 7.2. Keyword Arguments 7.3. Arbitrary Argument &hellip;","og_url":"https:\/\/learnscripting.org\/python-control-flow\/","og_site_name":"Learn Scripting","article_publisher":"https:\/\/www.facebook.com\/profile.php?id=100064270142636","article_published_time":"2019-09-16T05:28:47+00:00","author":"Shakti Das","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Shakti Das","Est. reading time":"19 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/learnscripting.org\/python-control-flow\/#article","isPartOf":{"@id":"https:\/\/learnscripting.org\/python-control-flow\/"},"author":{"name":"Shakti Das","@id":"https:\/\/learnscripting.org\/#\/schema\/person\/71045e0c38b97ea7f76363d6effa320c"},"headline":"Python Control Flow","datePublished":"2019-09-16T05:28:47+00:00","mainEntityOfPage":{"@id":"https:\/\/learnscripting.org\/python-control-flow\/"},"wordCount":2552,"publisher":{"@id":"https:\/\/learnscripting.org\/#organization"},"image":{"@id":"https:\/\/learnscripting.org\/python-control-flow\/#primaryimage"},"thumbnailUrl":"","articleSection":["Python"],"inLanguage":"en-US"},{"@type":"WebPage","@id":"https:\/\/learnscripting.org\/python-control-flow\/","url":"https:\/\/learnscripting.org\/python-control-flow\/","name":"Python Control Flow - Learn Scripting","isPartOf":{"@id":"https:\/\/learnscripting.org\/#website"},"primaryImageOfPage":{"@id":"https:\/\/learnscripting.org\/python-control-flow\/#primaryimage"},"image":{"@id":"https:\/\/learnscripting.org\/python-control-flow\/#primaryimage"},"thumbnailUrl":"","datePublished":"2019-09-16T05:28:47+00:00","breadcrumb":{"@id":"https:\/\/learnscripting.org\/python-control-flow\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/learnscripting.org\/python-control-flow\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/learnscripting.org\/python-control-flow\/#primaryimage","url":"","contentUrl":""},{"@type":"BreadcrumbList","@id":"https:\/\/learnscripting.org\/python-control-flow\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/learnscripting.org\/"},{"@type":"ListItem","position":2,"name":"Python Control Flow"}]},{"@type":"WebSite","@id":"https:\/\/learnscripting.org\/#website","url":"https:\/\/learnscripting.org\/","name":"Learn Scripting","description":"Coding Knowledge Unveiled: Empower Yourself","publisher":{"@id":"https:\/\/learnscripting.org\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/learnscripting.org\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/learnscripting.org\/#organization","name":"Learn Scripting","url":"https:\/\/learnscripting.org\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/learnscripting.org\/#\/schema\/logo\/image\/","url":"https:\/\/i0.wp.com\/learnscripting.org\/wp-content\/uploads\/2022\/03\/cropped-ls-600x600-1-512x512.jpg?fit=512%2C512&ssl=1","contentUrl":"https:\/\/i0.wp.com\/learnscripting.org\/wp-content\/uploads\/2022\/03\/cropped-ls-600x600-1-512x512.jpg?fit=512%2C512&ssl=1","width":512,"height":512,"caption":"Learn Scripting"},"image":{"@id":"https:\/\/learnscripting.org\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/profile.php?id=100064270142636"]},{"@type":"Person","@id":"https:\/\/learnscripting.org\/#\/schema\/person\/71045e0c38b97ea7f76363d6effa320c","name":"Shakti Das","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/5492ae25bd62294799695759ba85aaa28ae3d1de6b30be7cbdc377abc2ea0aac?s=96&d=mm&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/5492ae25bd62294799695759ba85aaa28ae3d1de6b30be7cbdc377abc2ea0aac?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/5492ae25bd62294799695759ba85aaa28ae3d1de6b30be7cbdc377abc2ea0aac?s=96&d=mm&r=g","caption":"Shakti Das"},"sameAs":["https:\/\/learnscripting.org"],"url":"https:\/\/learnscripting.org\/author\/53kgl\/"}]}},"_links":{"self":[{"href":"https:\/\/learnscripting.org\/wp-json\/wp\/v2\/posts\/1817","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/learnscripting.org\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/learnscripting.org\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/learnscripting.org\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/learnscripting.org\/wp-json\/wp\/v2\/comments?post=1817"}],"version-history":[{"count":0,"href":"https:\/\/learnscripting.org\/wp-json\/wp\/v2\/posts\/1817\/revisions"}],"wp:attachment":[{"href":"https:\/\/learnscripting.org\/wp-json\/wp\/v2\/media?parent=1817"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/learnscripting.org\/wp-json\/wp\/v2\/categories?post=1817"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/learnscripting.org\/wp-json\/wp\/v2\/tags?post=1817"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}