{"id":913,"date":"2024-09-15T16:11:06","date_gmt":"2024-09-15T16:11:06","guid":{"rendered":"https:\/\/www.zframez.com\/articles\/?p=913"},"modified":"2024-10-26T04:39:52","modified_gmt":"2024-10-26T04:39:52","slug":"chapter-10-python-functions-variable-scopes-and-lambda-expressions","status":"publish","type":"post","link":"https:\/\/www.zframez.com\/articles\/python-tutorials\/chapter-10-python-functions-variable-scopes-and-lambda-expressions","title":{"rendered":"Chapter 10: Python Functions, Variable Scopes, and Lambda Expressions"},"content":{"rendered":"<body>\n<div class=\"table-of-contents\" style=\"background-color: #066095; color: white; padding: 10px; border-radius: 10px; margin-top: 20px; display: flex; justify-content: space-between; align-items: center; font-weight: 600;\">\n  <span style=\"flex: 1; text-align: left;\">\n    <a href=\"https:\/\/www.zframez.com\/articles\/python-tutorials\/chapter-9-python-dictionaries-operations-and-methods\" style=\"color: white; text-decoration: underline;\">\u25c0 Previous<\/a>\n  <\/span>\n  \n  <span style=\"flex: 1; text-align: center;\">\n    <a href=\"https:\/\/www.zframez.com\/articles\/python-tutorials\" style=\"color: white; text-decoration: underline;\">Python Tutorials<\/a>\n  <\/span>\n  \n  <span style=\"flex: 1; text-align: right;\">\n    <a href=\"https:\/\/www.zframez.com\/articles\/python-tutorials\/chapter-11-python-file-handling-reading-writing-and-managing-files\" style=\"color: white; text-decoration: underline;\">Next \u25b6<\/a>\n  <\/span>\n<\/div>\n\n\n\n<h1 class=\"wp-block-heading has-x-large-font-size\"><strong>Understanding Functions, Variable Scopes, and Lambda Functions in Python<\/strong><\/h1>\n\n\n\n<p>In this chapter, we\u2019ll go through functions, one of the core building blocks in Python. Functions allow you to organize code, reuse logic, and make your programs cleaner and more efficient. In this chapter, we\u2019ll explore how to define functions with and without return statements, understand the differences between local and global variables, and how Python handles variable scopes. We\u2019ll also go over more advanced topics like default arguments, keyword arguments, and passing a variable number of arguments. To wrap things up, we\u2019ll take a look at lambda functions, which are anonymous functions that can be used in situations where you need simple one-liner functions. By the end of this chapter, you\u2019ll have a solid grasp of how to use functions effectively in Python.<\/p>\n\n\n\n<h2 class=\"wp-block-heading has-large-font-size\"><strong>Functions in Python<\/strong><\/h2>\n\n\n\n<p>Example 1: <strong>Defining a Function Without a Return Statement<\/strong>:<\/p>\n\n\n\n<pre class=\"wp-block-code has-background-color has-foreground-background-color has-text-color has-background has-link-color wp-elements-d8444735beb457808fb8038f5e78abef\"><code>def table(a):\n    b = 1\n    while b &lt;= 5:\n        print(\"%d * %d = %d\" % (a, b, b * a))\n        b += 1\n        \ntable(5)\n<\/code><\/pre>\n\n\n\n<p>Output:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted has-background-color has-foreground-background-color has-text-color has-background has-link-color wp-elements-d80a9f82a5c538c9afa9bae488b30642\">5 * 1 = 5<br>5 * 2 = 10<br>5 * 3 = 15<br>5 * 4 = 20<br>5 * 5 = 25<\/pre>\n\n\n\n<p><strong>Explanation<\/strong>:<br>This function, <code>table(a)<\/code>, takes a single parameter \u201c<code><strong>a<\/strong><\/code>\u201d and prints the multiplication table for \u201c<code><strong>a<\/strong>\"<\/code> from 1 to 5. It does not return any value because it directly prints the result inside the loop. The loop runs as long as \u201c<code><strong>b<\/strong>\"<\/code> is less than or equal to 5, and on each iteration, it prints the result of <strong><code>a * b<\/code>.<\/strong><\/p>\n\n\n\n<p>Example 2: <strong>Defining a Function with a Return Statement<\/strong>:<\/p>\n\n\n\n<pre class=\"wp-block-code has-background-color has-foreground-background-color has-text-color has-background has-link-color wp-elements-782907ede44e8fd8041d86e1d094a0fc\"><code>def average(a, b, c):\n    d = (a + b + c) \/ 3\n    return d\n\nresult = average(10, 20, 30)\nprint(result)\n\n#<strong>Output :<\/strong> 20.0 <\/code><\/pre>\n\n\n\n<p><strong>Explanation<\/strong>:<br>Here, the function <code>average(a, b, c)<\/code> calculates the average of three numbers. It returns the result, which is captured in the variable <code><strong>result<\/strong><\/code>. The function uses the <strong><code>return<\/code> <\/strong>statement to pass back the value of \u201c<code><strong>d<\/strong>\"<\/code>, making it available outside the function. The key difference from the previous example is that this function returns a value rather than just printing the result.<\/p>\n\n\n\n<h2 class=\"wp-block-heading has-large-font-size\"><strong>Variable Scope in Python: Local and Global Variables<\/strong><\/h2>\n\n\n\n<p><strong>Local Variables<\/strong> : Variables declared inside a function are <strong>local variables<\/strong>, which means they only exist within the function and cannot be accessed outside it.<\/p>\n\n\n\n<pre class=\"wp-block-code has-background-color has-foreground-background-color has-text-color has-background has-link-color wp-elements-e94c96a75dd2dfc628f9ba2736d714c3\"><code>def average(a, b, c):\n    d = (a + b + c) \/ 3\n    return d\n\nresult = average(10, 20, 30)\nprint(d)  # This will cause an error\n\n#Output : NameError: name 'd' is not defined<\/code><\/pre>\n\n\n\n<p>In this code, \u201c<code><strong>d<\/strong>\"<\/code> is a local variable inside the <code>average()<\/code> function. Trying to print \u201c<code><strong>d<\/strong>\"<\/code> outside the function causes a <code>NameError<\/code> because the variable \u201c<code><strong>d<\/strong>\"<\/code> does not exist outside the scope of the function. This illustrates the concept of local variables being confined to the function where they are defined.<\/p>\n\n\n\n<p><strong>Global Variables<\/strong> : Global variables are defined outside any function and can be accessed within functions unless explicitly overridden.<\/p>\n\n\n\n<pre class=\"wp-block-code has-background-color has-foreground-background-color has-text-color has-background has-link-color wp-elements-cbb397e348a62641203936e04915d74b\"><code>a = 10\n\ndef average(b, c):\n    print(\"a is\", a)\n    d = (a + b + c) \/ 3\n    return d\n\nprint(average(20, 30))\n\n#Output :\na is 10\n20.0<\/code><\/pre>\n\n\n\n<p><strong>Explanation<\/strong>:<br>Here, the variable \u201c<code><strong>a<\/strong>\"<\/code> is declared globally. When the function <code>average()<\/code> is called, it can access the global variable \u201c<code><strong>a<\/strong>\"<\/code> without needing to pass it as an argument. The global variable \u201c<code><strong>a<\/strong>\"<\/code> is used within the function, and no local variable \u201c<code>a\"<\/code> is created.<\/p>\n\n\n\n<p><strong>Local Variable with Same Name as Global<\/strong> :<\/p>\n\n\n\n<pre class=\"wp-block-code has-background-color has-foreground-background-color has-text-color has-background has-link-color wp-elements-fe2b84212b5ffe5ef658fb9fc3fc9d9c\"><code>a = 10\n\ndef average(b, c):\n    a = 40\n    d = (a + b + c) \/ 3\n    print(\"local variable 'a' is\", a)\n    \naverage(20, 30)\nprint(\"global variable 'a' is still\", a)\n\n#Output:\nlocal variable 'a' is 40\nglobal variable 'a' is still 10\n<\/code><\/pre>\n\n\n\n<p><strong>Explanation<\/strong>:<br>In this example, even though there is a global variable \u201c<code><strong>a<\/strong>\"<\/code>, the function <code>average()<\/code> declares a new local variable \u201c<code><strong>a<\/strong>\"<\/code> with the value 40. This local variable is used inside the function, and the global \u201c<code><strong>a<\/strong>\"<\/code> remains unchanged. Python prioritizes local variables over global ones within a function when the names are the same.<\/p>\n\n\n\n<p><strong>Using \u201c<code>global\"<\/code> to Modify Global Variables<\/strong> :<\/p>\n\n\n\n<pre class=\"wp-block-code has-background-color has-foreground-background-color has-text-color has-background has-link-color wp-elements-886cddca97c5dfe96a86ea116f2a428e\"><code>a = 10\n\ndef average(b, c):\n    global a\n    print(\"variable 'a' inside function is\", a)\n    d = (a + b + c) \/ 3\n    a = 40  # This modifies the global variable 'a'\n    print(\"average is\", d)\n\naverage(20, 30)\nprint(\"global variable 'a' is now\", a)\n\n#Output :\nvariable 'a' inside function is 10\naverage is 20.0\nglobal variable 'a' is now 40<\/code><\/pre>\n\n\n\n<p><strong>Explanation<\/strong>:<br>Using the \u201c<code>global\"<\/code> keyword, the function <code>average()<\/code> can modify the global variable \u201c<code><strong>a<\/strong>\"<\/code>. Without \u201c<code>global\"<\/code>, the assignment <code>a = 40<\/code> would create a local variable \u201c<code><strong>a<\/strong>\"<\/code>, but here it modifies the global \u201c<code><strong>a<\/strong>\"<\/code> directly. This is useful when you need to change the value of a global variable within a function.<\/p>\n\n\n\n<h2 class=\"wp-block-heading has-large-font-size\"><strong>Call by Object Reference<\/strong><\/h2>\n\n\n\n<p>When passing a mutable object like a list to a function, changes made to the object inside the function are reflected outside the function as well.<\/p>\n\n\n\n<pre class=\"wp-block-code has-background-color has-foreground-background-color has-text-color has-background has-link-color wp-elements-87f9432f83c9d41480989816293a7558\"><code>def add_to_list(x, y):\n    x.append(y)\n\na = []\nadd_to_list(a, 10)\nadd_to_list(a, 20)\nadd_to_list(a, 30)\nprint(a)\n\n#Output:\n[10, 20, 30]\n<\/code><\/pre>\n\n\n\n<p><strong>Explanation<\/strong>:<br>In this example, the function <code>add_to_list(x, y)<\/code> modifies the list <code>x<\/code> by appending the value <code>y<\/code>. Since lists are mutable, the changes made to <code>x<\/code> inside the function affect the list <code>a<\/code> outside the function. This behavior is known as <strong>call by object reference<\/strong> in Python.<\/p>\n\n\n\n<h2 class=\"wp-block-heading has-large-font-size\"><strong>Default Argument Values<\/strong><\/h2>\n\n\n\n<pre class=\"wp-block-code has-background-color has-foreground-background-color has-text-color has-background has-link-color wp-elements-6e24ccbfea09f79410606c851f3a1396\"><code>def average(a, b, c=30):\n    print((a + b + c) \/ 3)\n\naverage(10, 20)\naverage(10, 20, 60)\n\n#Output :\n20.0\n30.0<\/code><\/pre>\n\n\n\n<p><strong>Explanation<\/strong>:<br>In this function, the third parameter \u201c<code>c\"<\/code> has a default value of 30. When the function is called with only two arguments (<code>10<\/code> and <code>20<\/code>), it uses the default value of \u201c<code>c\"<\/code> as 30. However, when the function is called with three arguments (<code>10, 20, 60<\/code>), it overrides the default value with 60.<\/p>\n\n\n\n<h2 class=\"wp-block-heading has-large-font-size\"><strong>Keyword Arguments<\/strong><\/h2>\n\n\n\n<pre class=\"wp-block-code has-background-color has-foreground-background-color has-text-color has-background has-link-color wp-elements-d20ce9b75746b9435f2d0f914181bee7\"><code>def repeat(string, count):\n    a = 1\n    while a &lt; count:\n        print(string)\n        a += 1\n\nrepeat(count=3, string=\"python\")\n\n#Output :\npython\npython\npython\n<\/code><\/pre>\n\n\n\n<p><strong>Explanation<\/strong>:<br>Python allows you to pass arguments by specifying the parameter name. This flexibility is called <strong>keyword arguments<\/strong>, allowing you to pass values in any order by specifying the parameter name. In this example, the <code>repeat()<\/code> function is called using <code>count=3<\/code> and <code>string=\"python\"<\/code>.<\/p>\n\n\n\n<h2 class=\"wp-block-heading has-large-font-size\"><strong>Variable Number of Arguments<\/strong><\/h2>\n\n\n\n<pre class=\"wp-block-code has-background-color has-foreground-background-color has-text-color has-background has-link-color wp-elements-d8bea0efe531834b0286b48801e6f520\"><code>def sum(a, b, *c):\n    result = a + b\n    for tmp in c:\n        result += tmp\n    print(result)\n\nsum(10, 20)\nsum(10, 20, 30)\nsum(10, 20, 30, 40)\n\n#Output:\n30\n60\n100\n<\/code><\/pre>\n\n\n\n<p><strong>Explanation<\/strong>:<br>In Python, the <code>*c<\/code> in the function definition allows the function to accept an arbitrary number of additional arguments. These extra arguments are collected into a tuple and can be accessed inside the function. In this example, the <code>sum()<\/code> function sums all the arguments passed to it, regardless of how many there are.<\/p>\n\n\n\n<h2 class=\"wp-block-heading has-large-font-size\"><strong>Lambda Functions in Python<\/strong><\/h2>\n\n\n\n<p>Lambda functions are anonymous functions that can take any number of arguments but have only one expression. They are commonly used for short, simple functions.<\/p>\n\n\n\n<pre class=\"wp-block-code has-background-color has-foreground-background-color has-text-color has-background has-link-color wp-elements-a0c49fe74412f0fb6f7fe88940f3cf23\"><code>cube = lambda x: x ** 3\nprint(cube(5))  # 125\n\neven = lambda x: True if x % 2 == 0 else False\nprint(even(10))  # True\n\nsum = lambda x, y: x + y\nprint(sum(10, 5))  # 15\n<\/code><\/pre>\n\n\n\n<p><strong>Explanation<\/strong>:<br>Lambda functions are useful for writing small, one-liner functions. In the examples above, <code>cube<\/code> calculates the cube of a number, <code>even<\/code> checks if a number is even, and <code>sum<\/code> adds two numbers. Lambda functions don\u2019t require the use of the <code>def<\/code> keyword and are often used when a function object is needed temporarily.<\/p>\n\n\n\n<h2 class=\"wp-block-heading has-large-font-size\"><strong>Using Lambda Functions with <code>filter()<\/code>, <code>map()<\/code>, and <code>reduce()<\/code><\/strong><\/h2>\n\n\n\n<pre class=\"wp-block-code has-background-color has-foreground-background-color has-text-color has-background has-link-color wp-elements-603661e2c45c18cffb5b3c47158a21eb\"><code>a = [1, 2, 3, 4, 5]\n\n# Using map with lambda\ncube = lambda x: x ** 3\nprint(list(map(cube, a)))  # [1, 8, 27, 64, 125]\n\n# Using filter with lambda\neven = lambda x: x % 2 == 0\nprint(list(filter(even, a)))  # [2, 4]\n\n# Using reduce with lambda\nfrom functools import reduce\nsum = lambda x, y: x + y\nprint(reduce(sum, a))  # 15\n<\/code><\/pre>\n\n\n\n<p><strong>Explanation<\/strong>:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>map()<\/code> applies the lambda function to each element of the list <code>a<\/code>.<\/li>\n\n\n\n<li><code>filter()<\/code> returns only the elements of the list <code>a<\/code> that satisfy the condition in the lambda function.<\/li>\n\n\n\n<li><code>reduce()<\/code> reduces the list <code>a<\/code> by applying the lambda function cumulatively, in this case, summing all the elements.<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\">\n\n\n\n<h2 class=\"wp-block-heading has-large-font-size\"><strong>Programming Exercises :<\/strong><\/h2>\n\n\n\n<ol class=\"wp-block-list\">\n<li>Create a function to check whether a given number is prime. Use this function with a list of numbers to print only the prime numbers from that list.<\/li>\n\n\n\n<li>Write a Python program to find the factorial of a number using a recursive function.<\/li>\n\n\n\n<li>\n<ol class=\"wp-block-list\">\n<li>Create a function to find and return the smallest element from a given list.<\/li>\n\n\n\n<li>Create another function to return the position of a given element in a list.<\/li>\n\n\n\n<li>Create a function to delete the element at a given position in the list.<\/li>\n\n\n\n<li>Using the above three functions, repeatedly find, print, and remove the smallest number from the input list. Continue this process until the input list is empty, and this will effectively sort the numbers in ascending order.<\/li>\n<\/ol>\n<\/li>\n\n\n\n<li>Create a function to check if a given year is a leap year or not. Use this function to print only the leap years from a list of years.<\/li>\n\n\n\n<li>Write a Python program that defines a function to calculate the sum of squares of numbers in a list. Use this function to return the sum of squares for any given list of numbers.<\/li>\n\n\n\n<li>Create a function that takes a list of numbers and returns the average of the numbers in the list.<\/li>\n\n\n\n<li>Write a Python program to define a function that counts the number of vowels in a given string. Use this function to count and print the vowels from a list of strings.<\/li>\n\n\n\n<li>Create a function that accepts two lists and returns a new list containing only the elements that are common between the two lists.<\/li>\n\n\n\n<li>Write a Python program that defines a function to reverse a given string. Use this function to reverse a list of strings and print the result.<\/li>\n\n\n\n<li>Create a function that takes a list of numbers and returns the maximum and minimum numbers in the list.<\/li>\n\n\n\n<li>Write a Python program that defines a function to calculate the greatest common divisor (GCD) of two numbers using recursion.<\/li>\n<\/ol>\n\n\n\n<p><\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity is-style-wide\">\n\n\n\n<div class=\"table-of-contents\" style=\"background-color: #066095; color: white; padding: 10px; border-radius: 10px; margin-top: 20px; display: flex; justify-content: space-between; align-items: center; font-weight: 600;\">\n  <span style=\"flex: 1; text-align: left;\">\n    <a href=\"https:\/\/www.zframez.com\/articles\/python-tutorials\/chapter-9-python-dictionaries-operations-and-methods\" style=\"color: white; text-decoration: underline;\">\u25c0 Previous<\/a>\n  <\/span>\n  \n  <span style=\"flex: 1; text-align: center;\">\n    <a href=\"https:\/\/www.zframez.com\/articles\/python-tutorials\" style=\"color: white; text-decoration: underline;\">Python Tutorials<\/a>\n  <\/span>\n  \n  <span style=\"flex: 1; text-align: right;\">\n    <a href=\"https:\/\/www.zframez.com\/articles\/python-tutorials\/chapter-11-python-file-handling-reading-writing-and-managing-files\" style=\"color: white; text-decoration: underline;\">Next \u25b6<\/a>\n  <\/span>\n<\/div>\n<\/body>","protected":false},"excerpt":{"rendered":"<p>\u25c0 Previous Python Tutorials Next \u25b6 Understanding Functions, Variable Scopes, and Lambda Functions in Python In this chapter, we\u2019ll go through functions, one of the core building blocks in Python. Functions allow you to organize code, reuse logic, and make your programs cleaner and more efficient. In this chapter, we\u2019ll explore how to define functions [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"wp-custom-template-post-with-sidebar2","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[46],"tags":[313,314,275,310,311,50,280,312],"class_list":["post-913","post","type-post","status-publish","format-standard","hentry","category-python-tutorials","tag-default-arguments-in-python","tag-global-vs-local-variables","tag-python-functions","tag-python-keyword-arguments","tag-python-lambda-functions","tag-python-programming","tag-python-tutorials","tag-variable-scopes-in-python"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.4 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Chapter 10: Python Functions, Variable Scopes, and Lambda Expressions - Tutorials<\/title>\n<meta name=\"description\" content=\"Learn about functions in Python, variable scopes, default arguments, keyword arguments, and lambda functions. This chapter covers key concepts and examples for efficient coding.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.zframez.com\/articles\/python-tutorials\/chapter-10-python-functions-variable-scopes-and-lambda-expressions\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Chapter 10: Python Functions, Variable Scopes, and Lambda Expressions\" \/>\n<meta property=\"og:description\" content=\"Explore how to work with functions in Python, understand variable scopes, and use lambda functions for quick one-liner operations. A comprehensive guide to functions in Python.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.zframez.com\/articles\/python-tutorials\/chapter-10-python-functions-variable-scopes-and-lambda-expressions\" \/>\n<meta property=\"og:site_name\" content=\"Tutorials\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/zframez\/\" \/>\n<meta property=\"article:published_time\" content=\"2024-09-15T16:11:06+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2024-10-26T04:39:52+00:00\" \/>\n<meta name=\"author\" content=\"sajith achipra\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@zframez\" \/>\n<meta name=\"twitter:site\" content=\"@zframez\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"sajith achipra\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"6 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/www.zframez.com\\\/articles\\\/python-tutorials\\\/chapter-10-python-functions-variable-scopes-and-lambda-expressions#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.zframez.com\\\/articles\\\/python-tutorials\\\/chapter-10-python-functions-variable-scopes-and-lambda-expressions\"},\"author\":{\"name\":\"sajith achipra\",\"@id\":\"https:\\\/\\\/www.zframez.com\\\/articles\\\/#\\\/schema\\\/person\\\/8b3b88007644501771d2452d3cc80f41\"},\"headline\":\"Chapter 10: Python Functions, Variable Scopes, and Lambda Expressions\",\"datePublished\":\"2024-09-15T16:11:06+00:00\",\"dateModified\":\"2024-10-26T04:39:52+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/www.zframez.com\\\/articles\\\/python-tutorials\\\/chapter-10-python-functions-variable-scopes-and-lambda-expressions\"},\"wordCount\":1191,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\\\/\\\/www.zframez.com\\\/articles\\\/#organization\"},\"keywords\":[\"default arguments in Python\",\"global vs local variables\",\"Python functions\",\"Python keyword arguments\",\"Python lambda functions\",\"python programming\",\"Python tutorials\",\"variable scopes in Python\"],\"articleSection\":[\"python tutorials\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/www.zframez.com\\\/articles\\\/python-tutorials\\\/chapter-10-python-functions-variable-scopes-and-lambda-expressions#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/www.zframez.com\\\/articles\\\/python-tutorials\\\/chapter-10-python-functions-variable-scopes-and-lambda-expressions\",\"url\":\"https:\\\/\\\/www.zframez.com\\\/articles\\\/python-tutorials\\\/chapter-10-python-functions-variable-scopes-and-lambda-expressions\",\"name\":\"Chapter 10: Python Functions, Variable Scopes, and Lambda Expressions - Tutorials\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.zframez.com\\\/articles\\\/#website\"},\"datePublished\":\"2024-09-15T16:11:06+00:00\",\"dateModified\":\"2024-10-26T04:39:52+00:00\",\"description\":\"Learn about functions in Python, variable scopes, default arguments, keyword arguments, and lambda functions. This chapter covers key concepts and examples for efficient coding.\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/www.zframez.com\\\/articles\\\/python-tutorials\\\/chapter-10-python-functions-variable-scopes-and-lambda-expressions#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/www.zframez.com\\\/articles\\\/python-tutorials\\\/chapter-10-python-functions-variable-scopes-and-lambda-expressions\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/www.zframez.com\\\/articles\\\/python-tutorials\\\/chapter-10-python-functions-variable-scopes-and-lambda-expressions#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Tutorials\",\"item\":\"https:\\\/\\\/www.zframez.com\\\/articles\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Chapter 10: Python Functions, Variable Scopes, and Lambda Expressions\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/www.zframez.com\\\/articles\\\/#website\",\"url\":\"https:\\\/\\\/www.zframez.com\\\/articles\\\/\",\"name\":\"zframez tutorials\",\"description\":\"Learn networking bit by bit\",\"publisher\":{\"@id\":\"https:\\\/\\\/www.zframez.com\\\/articles\\\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/www.zframez.com\\\/articles\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\\\/\\\/www.zframez.com\\\/articles\\\/#organization\",\"name\":\"zframez technologies\",\"url\":\"https:\\\/\\\/www.zframez.com\\\/articles\\\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/www.zframez.com\\\/articles\\\/#\\\/schema\\\/logo\\\/image\\\/\",\"url\":\"https:\\\/\\\/i0.wp.com\\\/www.zframez.com\\\/articles\\\/wp-content\\\/uploads\\\/2024\\\/07\\\/zframez-logo.jpg?fit=864%2C864&ssl=1\",\"contentUrl\":\"https:\\\/\\\/i0.wp.com\\\/www.zframez.com\\\/articles\\\/wp-content\\\/uploads\\\/2024\\\/07\\\/zframez-logo.jpg?fit=864%2C864&ssl=1\",\"width\":864,\"height\":864,\"caption\":\"zframez technologies\"},\"image\":{\"@id\":\"https:\\\/\\\/www.zframez.com\\\/articles\\\/#\\\/schema\\\/logo\\\/image\\\/\"},\"sameAs\":[\"https:\\\/\\\/www.facebook.com\\\/zframez\\\/\",\"https:\\\/\\\/x.com\\\/zframez\",\"https:\\\/\\\/www.instagram.com\\\/zframez_technologies\\\/\"]},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/www.zframez.com\\\/articles\\\/#\\\/schema\\\/person\\\/8b3b88007644501771d2452d3cc80f41\",\"name\":\"sajith achipra\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/3d9f27c5311500982b6f19d03d0506f1c328f30f51d8d5f73f46577687fd81f8?s=96&d=mm&r=g\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/3d9f27c5311500982b6f19d03d0506f1c328f30f51d8d5f73f46577687fd81f8?s=96&d=mm&r=g\",\"contentUrl\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/3d9f27c5311500982b6f19d03d0506f1c328f30f51d8d5f73f46577687fd81f8?s=96&d=mm&r=g\",\"caption\":\"sajith achipra\"},\"sameAs\":[\"http:\\\/\\\/www.zframez.com\"]}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Chapter 10: Python Functions, Variable Scopes, and Lambda Expressions - Tutorials","description":"Learn about functions in Python, variable scopes, default arguments, keyword arguments, and lambda functions. This chapter covers key concepts and examples for efficient coding.","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:\/\/www.zframez.com\/articles\/python-tutorials\/chapter-10-python-functions-variable-scopes-and-lambda-expressions","og_locale":"en_US","og_type":"article","og_title":"Chapter 10: Python Functions, Variable Scopes, and Lambda Expressions","og_description":"Explore how to work with functions in Python, understand variable scopes, and use lambda functions for quick one-liner operations. A comprehensive guide to functions in Python.","og_url":"https:\/\/www.zframez.com\/articles\/python-tutorials\/chapter-10-python-functions-variable-scopes-and-lambda-expressions","og_site_name":"Tutorials","article_publisher":"https:\/\/www.facebook.com\/zframez\/","article_published_time":"2024-09-15T16:11:06+00:00","article_modified_time":"2024-10-26T04:39:52+00:00","author":"sajith achipra","twitter_card":"summary_large_image","twitter_creator":"@zframez","twitter_site":"@zframez","twitter_misc":{"Written by":"sajith achipra","Est. reading time":"6 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.zframez.com\/articles\/python-tutorials\/chapter-10-python-functions-variable-scopes-and-lambda-expressions#article","isPartOf":{"@id":"https:\/\/www.zframez.com\/articles\/python-tutorials\/chapter-10-python-functions-variable-scopes-and-lambda-expressions"},"author":{"name":"sajith achipra","@id":"https:\/\/www.zframez.com\/articles\/#\/schema\/person\/8b3b88007644501771d2452d3cc80f41"},"headline":"Chapter 10: Python Functions, Variable Scopes, and Lambda Expressions","datePublished":"2024-09-15T16:11:06+00:00","dateModified":"2024-10-26T04:39:52+00:00","mainEntityOfPage":{"@id":"https:\/\/www.zframez.com\/articles\/python-tutorials\/chapter-10-python-functions-variable-scopes-and-lambda-expressions"},"wordCount":1191,"commentCount":0,"publisher":{"@id":"https:\/\/www.zframez.com\/articles\/#organization"},"keywords":["default arguments in Python","global vs local variables","Python functions","Python keyword arguments","Python lambda functions","python programming","Python tutorials","variable scopes in Python"],"articleSection":["python tutorials"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/www.zframez.com\/articles\/python-tutorials\/chapter-10-python-functions-variable-scopes-and-lambda-expressions#respond"]}]},{"@type":"WebPage","@id":"https:\/\/www.zframez.com\/articles\/python-tutorials\/chapter-10-python-functions-variable-scopes-and-lambda-expressions","url":"https:\/\/www.zframez.com\/articles\/python-tutorials\/chapter-10-python-functions-variable-scopes-and-lambda-expressions","name":"Chapter 10: Python Functions, Variable Scopes, and Lambda Expressions - Tutorials","isPartOf":{"@id":"https:\/\/www.zframez.com\/articles\/#website"},"datePublished":"2024-09-15T16:11:06+00:00","dateModified":"2024-10-26T04:39:52+00:00","description":"Learn about functions in Python, variable scopes, default arguments, keyword arguments, and lambda functions. This chapter covers key concepts and examples for efficient coding.","breadcrumb":{"@id":"https:\/\/www.zframez.com\/articles\/python-tutorials\/chapter-10-python-functions-variable-scopes-and-lambda-expressions#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.zframez.com\/articles\/python-tutorials\/chapter-10-python-functions-variable-scopes-and-lambda-expressions"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.zframez.com\/articles\/python-tutorials\/chapter-10-python-functions-variable-scopes-and-lambda-expressions#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Tutorials","item":"https:\/\/www.zframez.com\/articles"},{"@type":"ListItem","position":2,"name":"Chapter 10: Python Functions, Variable Scopes, and Lambda Expressions"}]},{"@type":"WebSite","@id":"https:\/\/www.zframez.com\/articles\/#website","url":"https:\/\/www.zframez.com\/articles\/","name":"zframez tutorials","description":"Learn networking bit by bit","publisher":{"@id":"https:\/\/www.zframez.com\/articles\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.zframez.com\/articles\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/www.zframez.com\/articles\/#organization","name":"zframez technologies","url":"https:\/\/www.zframez.com\/articles\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.zframez.com\/articles\/#\/schema\/logo\/image\/","url":"https:\/\/i0.wp.com\/www.zframez.com\/articles\/wp-content\/uploads\/2024\/07\/zframez-logo.jpg?fit=864%2C864&ssl=1","contentUrl":"https:\/\/i0.wp.com\/www.zframez.com\/articles\/wp-content\/uploads\/2024\/07\/zframez-logo.jpg?fit=864%2C864&ssl=1","width":864,"height":864,"caption":"zframez technologies"},"image":{"@id":"https:\/\/www.zframez.com\/articles\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/zframez\/","https:\/\/x.com\/zframez","https:\/\/www.instagram.com\/zframez_technologies\/"]},{"@type":"Person","@id":"https:\/\/www.zframez.com\/articles\/#\/schema\/person\/8b3b88007644501771d2452d3cc80f41","name":"sajith achipra","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/3d9f27c5311500982b6f19d03d0506f1c328f30f51d8d5f73f46577687fd81f8?s=96&d=mm&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/3d9f27c5311500982b6f19d03d0506f1c328f30f51d8d5f73f46577687fd81f8?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/3d9f27c5311500982b6f19d03d0506f1c328f30f51d8d5f73f46577687fd81f8?s=96&d=mm&r=g","caption":"sajith achipra"},"sameAs":["http:\/\/www.zframez.com"]}]}},"jetpack_featured_media_url":"","jetpack-related-posts":[{"id":83,"url":"https:\/\/www.zframez.com\/articles\/python-tutorials\/chapter-1-introduction-to-python-programming","url_meta":{"origin":913,"position":0},"title":"Chapter 1: Introduction to Python Programming","author":"sajith achipra","date":"September 4, 2024","format":false,"excerpt":"\u25c0 Previous Python Tutorials Next \u25b6 In this Python tutorial, we'll explore Python's origins, key milestones, and its standout features and benefits. Whether you're new to programming or looking to expand your skills, this guide will provide you with a solid foundation in Python. If you want to understand why\u2026","rel":"","context":"In &quot;python tutorials&quot;","block_context":{"text":"python tutorials","link":"https:\/\/www.zframez.com\/articles\/category\/python-tutorials"},"img":{"alt_text":"Table filled with Python-related materials, including a computer displaying Python code, a Python textbook, and notes about Python","src":"https:\/\/i0.wp.com\/www.zframez.com\/articles\/wp-content\/uploads\/2024\/09\/working-with-python.webp?fit=1024%2C1024&ssl=1&resize=350%2C200","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/www.zframez.com\/articles\/wp-content\/uploads\/2024\/09\/working-with-python.webp?fit=1024%2C1024&ssl=1&resize=350%2C200 1x, https:\/\/i0.wp.com\/www.zframez.com\/articles\/wp-content\/uploads\/2024\/09\/working-with-python.webp?fit=1024%2C1024&ssl=1&resize=525%2C300 1.5x, https:\/\/i0.wp.com\/www.zframez.com\/articles\/wp-content\/uploads\/2024\/09\/working-with-python.webp?fit=1024%2C1024&ssl=1&resize=700%2C400 2x"},"classes":[]},{"id":98,"url":"https:\/\/www.zframez.com\/articles\/python-tutorials\/chapter-3-working-with-variables-in-python","url_meta":{"origin":913,"position":1},"title":"Chapter 3: Working with Variables in Python","author":"sajith achipra","date":"September 4, 2024","format":false,"excerpt":"\u25c0 Previous Python Tutorials Next \u25b6 In Python, variables are one of the fundamental concepts you'll work with. They allow you to store data that your programs can manipulate and use. In this chapter, we\u2019ll explore what variables are, how to check the Python version you\u2019re using, and the different\u2026","rel":"","context":"In &quot;python tutorials&quot;","block_context":{"text":"python tutorials","link":"https:\/\/www.zframez.com\/articles\/category\/python-tutorials"},"img":{"alt_text":"Computer displaying a program, with a book and pen on the table, representing learning and coding in Python.","src":"https:\/\/i0.wp.com\/www.zframez.com\/articles\/wp-content\/uploads\/2024\/09\/Understanding-Python-variables.jpg?fit=684%2C457&ssl=1&resize=350%2C200","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/www.zframez.com\/articles\/wp-content\/uploads\/2024\/09\/Understanding-Python-variables.jpg?fit=684%2C457&ssl=1&resize=350%2C200 1x, https:\/\/i0.wp.com\/www.zframez.com\/articles\/wp-content\/uploads\/2024\/09\/Understanding-Python-variables.jpg?fit=684%2C457&ssl=1&resize=525%2C300 1.5x"},"classes":[]},{"id":904,"url":"https:\/\/www.zframez.com\/articles\/python-tutorials\/chapter-7-working-with-strings-in-python","url_meta":{"origin":913,"position":2},"title":"Chapter 7: Working with Strings in Python","author":"sajith achipra","date":"September 15, 2024","format":false,"excerpt":"\u25c0 Previous Python Tutorials Next \u25b6 Python String Manipulation: Methods and Examples In Chapter 7, we\u2019ll be looking at how to work with strings in Python. Strings are used everywhere in programming, and Python provides a lot of methods to make string manipulation easy. In this chapter, we\u2019ll explore methods\u2026","rel":"","context":"In &quot;python tutorials&quot;","block_context":{"text":"python tutorials","link":"https:\/\/www.zframez.com\/articles\/category\/python-tutorials"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":908,"url":"https:\/\/www.zframez.com\/articles\/python-tutorials\/chapter-8-python-lists-and-tuples-operations-and-methods","url_meta":{"origin":913,"position":3},"title":"Chapter 8: Python Lists and Tuples -Operations and Methods","author":"sajith achipra","date":"September 15, 2024","format":false,"excerpt":"\u25c0 Previous Python Tutorials Next \u25b6 Working with Lists and Tuples in Python In this chapter, we\u2019ll explore two important data structures in Python: lists and tuples. Lists allow you to store a collection of items that can be modified, while tuples are immutable, meaning their elements cannot be changed.\u2026","rel":"","context":"In &quot;python tutorials&quot;","block_context":{"text":"python tutorials","link":"https:\/\/www.zframez.com\/articles\/category\/python-tutorials"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":901,"url":"https:\/\/www.zframez.com\/articles\/python-tutorials\/chapter-6-python-while-and-for-loops-examples-and-explanations","url_meta":{"origin":913,"position":4},"title":"Chapter 6: Python While and For Loops: Examples and Explanations","author":"sajith achipra","date":"September 15, 2024","format":false,"excerpt":"\u25c0 Previous Python Tutorials Next \u25b6 Python While and For Loops In this chapter of our Python tutorial series, we will explore two essential looping structures in Python: while loops and for loops. Loops are a fundamental part of programming, allowing you to execute a block of code repeatedly under\u2026","rel":"","context":"In &quot;python tutorials&quot;","block_context":{"text":"python tutorials","link":"https:\/\/www.zframez.com\/articles\/category\/python-tutorials"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":751,"url":"https:\/\/www.zframez.com\/articles\/python-tutorials\/chapter-5-python-conditional-statements-if-else-and-elif","url_meta":{"origin":913,"position":5},"title":"Chapter 5:Python Conditional Statements: if, else, and elif","author":"sajith achipra","date":"September 11, 2024","format":false,"excerpt":"\u25c0 Previous Python Tutorials Next \u25b6 Table of Contents Introduction to Python `if` and `else` Understanding Python `if` Syntax Multiple Lines of Code in an `if` Statement Using `else` with `if` How does Python know when the `if` code block is finished? Using the `pass` Statement Checking Odd and Even\u2026","rel":"","context":"In &quot;python tutorials&quot;","block_context":{"text":"python tutorials","link":"https:\/\/www.zframez.com\/articles\/category\/python-tutorials"},"img":{"alt_text":"Block diagram illustrating an if expression and corresponding code block in Python, with a Python icon in the top right corner.","src":"https:\/\/i0.wp.com\/www.zframez.com\/articles\/wp-content\/uploads\/2024\/09\/Python-conditional-statments-if-else.jpg?fit=1200%2C675&ssl=1&resize=350%2C200","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/www.zframez.com\/articles\/wp-content\/uploads\/2024\/09\/Python-conditional-statments-if-else.jpg?fit=1200%2C675&ssl=1&resize=350%2C200 1x, https:\/\/i0.wp.com\/www.zframez.com\/articles\/wp-content\/uploads\/2024\/09\/Python-conditional-statments-if-else.jpg?fit=1200%2C675&ssl=1&resize=525%2C300 1.5x, https:\/\/i0.wp.com\/www.zframez.com\/articles\/wp-content\/uploads\/2024\/09\/Python-conditional-statments-if-else.jpg?fit=1200%2C675&ssl=1&resize=700%2C400 2x, https:\/\/i0.wp.com\/www.zframez.com\/articles\/wp-content\/uploads\/2024\/09\/Python-conditional-statments-if-else.jpg?fit=1200%2C675&ssl=1&resize=1050%2C600 3x"},"classes":[]}],"jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/www.zframez.com\/articles\/wp-json\/wp\/v2\/posts\/913","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.zframez.com\/articles\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.zframez.com\/articles\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.zframez.com\/articles\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.zframez.com\/articles\/wp-json\/wp\/v2\/comments?post=913"}],"version-history":[{"count":4,"href":"https:\/\/www.zframez.com\/articles\/wp-json\/wp\/v2\/posts\/913\/revisions"}],"predecessor-version":[{"id":1111,"href":"https:\/\/www.zframez.com\/articles\/wp-json\/wp\/v2\/posts\/913\/revisions\/1111"}],"wp:attachment":[{"href":"https:\/\/www.zframez.com\/articles\/wp-json\/wp\/v2\/media?parent=913"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.zframez.com\/articles\/wp-json\/wp\/v2\/categories?post=913"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.zframez.com\/articles\/wp-json\/wp\/v2\/tags?post=913"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}