

{"id":123241,"date":"2023-11-02T16:39:22","date_gmt":"2023-11-02T11:09:22","guid":{"rendered":"https:\/\/data-flair.training\/blogs\/?p=123241"},"modified":"2024-02-28T11:39:24","modified_gmt":"2024-02-28T06:09:24","slug":"python-program-on-lambda-function","status":"publish","type":"post","link":"https:\/\/data-flair.training\/blogs\/python-program-on-lambda-function\/","title":{"rendered":"Python Program on Lambda Function"},"content":{"rendered":"<p>In this article, we will explore a Python program that utilizes lambda functions to perform basic mathematical operations. Lambda functions, also known as anonymous functions, provide a concise way to define small, one-line functions without the need for a formal function definition. This program demonstrates the use of lambda functions for operations such as squaring, addition, subtraction, multiplication, division, and finding the greater of two numbers.<\/p>\n<h2>Prerequisites<\/h2>\n<ul>\n<li>Familiarity with lambda functions.<\/li>\n<li>Understanding of basic mathematical operations.<\/li>\n<li>Knowledge of taking user input in Python.<\/li>\n<\/ul>\n<h3>Topic Explanation<\/h3>\n<p>This program defines five lambda functions, each tailored for basic mathematical operations: squaring, addition, subtraction, multiplication, and integer division. It collects user input for two numbers and employs these lambda functions to perform the specified mathematical computations, subsequently displaying the results.<\/p>\n<p>By encapsulating these fundamental arithmetic operations within lambda functions, the program provides a practical illustration of their flexibility in tackling mathematical tasks. Users receive hands-on exposure to the application of lambda functions, enhancing their proficiency in utilizing these tools for efficient calculations within<\/p>\n<p><strong>Code:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\"># Define lambda functions for basic operations\r\nsquare = lambda x: x * x  # Lambda function to calculate the square of a number\r\nadd = lambda a, b: a + b  # Lambda function to add two numbers\r\nsubtract = lambda a, b: a - b  # Lambda function to subtract the second number from the first\r\nmultiply = lambda a, b: a * b  # Lambda function to multiply two numbers\r\ndivide = lambda a, b: a \/\/ b  # Lambda function to integer divide the first number by the second\r\ngreater = lambda a, b: a if a &gt; b else b  # Lambda function to find the greater of two numbers\r\n\r\n# Input\r\nm = int(input(\"Enter first number: \"))  # User input for the first number\r\nn = int(input(\"Enter second number: \"))  # User input for the second number\r\n\r\n# Calculate and output\r\nprint(\"Square of the first number is:\", square(m))  # Output the square of the first number\r\nprint(\"Sum of the two numbers is:\", add(m, n))  # Output the sum of the two numbers\r\nprint(\"Difference between the two numbers is:\", subtract(m, n))  # Output the difference between the two numbers\r\nprint(\"Product of the two numbers is:\", multiply(m, n))  # Output the product of the two numbers\r\n\r\n# Check and output greater number\r\nprint(\"Greater number is:\", greater(m, n))  # Output the greater of the two numbers<\/pre>\n<div class=\"df-code-out\">\n<p><strong>Output:<\/strong><\/p>\n<p>Enter first number: <strong>5<\/strong><br \/>\nEnter second number: <strong>6<\/strong><br \/>\nSquare of the first number is: <strong>25<\/strong><br \/>\nSum of the two numbers is: <strong>11<\/strong><br \/>\nDifference between the two numbers is: <strong>-1<\/strong><br \/>\nThe product of the two numbers is:<strong> 30<\/strong><br \/>\nGreater number is:<strong> 6<\/strong><\/p>\n<\/div>\n<h4>Code Explanation:<\/h4>\n<ul>\n<li>Defines 6 lambda functions for basic math operations like square, add, subtract, multiply, divide and finding greater of two numbers<\/li>\n<li>square lambda function takes one argument x and returns x*x<\/li>\n<li>add lambda function takes two arguments a and b and returns their sum<\/li>\n<li>subtract lambda function takes two arguments a and b and returns their difference<\/li>\n<li>multiply lambda function takes two arguments a and b and returns their product<\/li>\n<li>divide lambda function takes two arguments a and b and returns their integer quotient<\/li>\n<li>greater lambda function takes two arguments a and b, compares them and returns the greater of the two<\/li>\n<li>Takes two integer inputs m and n from user<\/li>\n<li>Calls the defined lambda functions by passing m and n and prints the results<\/li>\n<li>Calculates and prints &#8211; square of m, sum of m and n, difference between m and n, product of m and n and greater of m and n<\/li>\n<\/ul>\n<h3>Summary<\/h3>\n<p>In summary, this Python program effectively utilizes concise lambda functions to execute a variety of fundamental mathematical operations, such as squaring, addition, subtraction, multiplication, division, and determining the larger of two numbers. Its user-friendly design facilitates easy interaction by collecting user input. This makes it a versatile and practical tool for performing elementary arithmetic operations in Python. Whether for educational purposes or quick calculations, this program provides a convenient and efficient solution for users seeking to perform basic math computations with ease.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this article, we will explore a Python program that utilizes lambda functions to perform basic mathematical operations. Lambda functions, also known as anonymous functions, provide a concise way to define small, one-line functions&#46;&#46;&#46;<\/p>\n","protected":false},"author":581,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[46],"tags":[28660,28659,10333,28658,28626,28661],"class_list":["post-123241","post","type-post","status-publish","format-standard","hentry","category-python","tag-lambda-function","tag-lambda-function-in-python","tag-python","tag-python-lambda-function","tag-python-practical","tag-python-program-on-lambda-function"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.8 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Python Program on Lambda Function - DataFlair<\/title>\n<meta name=\"description\" content=\"Lambda functions, also known as anonymous functions, provide a concise way to define small, one-line functions without the need for a formal function definition.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/data-flair.training\/blogs\/python-program-on-lambda-function\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Python Program on Lambda Function - DataFlair\" \/>\n<meta property=\"og:description\" content=\"Lambda functions, also known as anonymous functions, provide a concise way to define small, one-line functions without the need for a formal function definition.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/data-flair.training\/blogs\/python-program-on-lambda-function\/\" \/>\n<meta property=\"og:site_name\" content=\"DataFlair\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/DataFlairWS\/\" \/>\n<meta property=\"article:published_time\" content=\"2023-11-02T11:09:22+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2024-02-28T06:09:24+00:00\" \/>\n<meta name=\"author\" content=\"DataFlair Team\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@DataFlairWS\" \/>\n<meta name=\"twitter:site\" content=\"@DataFlairWS\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"DataFlair Team\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"2 minutes\" \/>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Python Program on Lambda Function - DataFlair","description":"Lambda functions, also known as anonymous functions, provide a concise way to define small, one-line functions without the need for a formal function definition.","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:\/\/data-flair.training\/blogs\/python-program-on-lambda-function\/","og_locale":"en_US","og_type":"article","og_title":"Python Program on Lambda Function - DataFlair","og_description":"Lambda functions, also known as anonymous functions, provide a concise way to define small, one-line functions without the need for a formal function definition.","og_url":"https:\/\/data-flair.training\/blogs\/python-program-on-lambda-function\/","og_site_name":"DataFlair","article_publisher":"https:\/\/www.facebook.com\/DataFlairWS\/","article_published_time":"2023-11-02T11:09:22+00:00","article_modified_time":"2024-02-28T06:09:24+00:00","author":"DataFlair Team","twitter_card":"summary_large_image","twitter_creator":"@DataFlairWS","twitter_site":"@DataFlairWS","twitter_misc":{"Written by":"DataFlair Team","Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/data-flair.training\/blogs\/python-program-on-lambda-function\/#article","isPartOf":{"@id":"https:\/\/data-flair.training\/blogs\/python-program-on-lambda-function\/"},"author":{"name":"DataFlair Team","@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/person\/c187795dc82ab948373cca526df7c445"},"headline":"Python Program on Lambda Function","datePublished":"2023-11-02T11:09:22+00:00","dateModified":"2024-02-28T06:09:24+00:00","mainEntityOfPage":{"@id":"https:\/\/data-flair.training\/blogs\/python-program-on-lambda-function\/"},"wordCount":448,"commentCount":0,"publisher":{"@id":"https:\/\/data-flair.training\/blogs\/#organization"},"keywords":["lambda function","lambda function in python","Python","python lambda function","python practical","python program on lambda function"],"articleSection":["Python Tutorials"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/data-flair.training\/blogs\/python-program-on-lambda-function\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/data-flair.training\/blogs\/python-program-on-lambda-function\/","url":"https:\/\/data-flair.training\/blogs\/python-program-on-lambda-function\/","name":"Python Program on Lambda Function - DataFlair","isPartOf":{"@id":"https:\/\/data-flair.training\/blogs\/#website"},"datePublished":"2023-11-02T11:09:22+00:00","dateModified":"2024-02-28T06:09:24+00:00","description":"Lambda functions, also known as anonymous functions, provide a concise way to define small, one-line functions without the need for a formal function definition.","breadcrumb":{"@id":"https:\/\/data-flair.training\/blogs\/python-program-on-lambda-function\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/data-flair.training\/blogs\/python-program-on-lambda-function\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/data-flair.training\/blogs\/python-program-on-lambda-function\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Blog Home","item":"https:\/\/data-flair.training\/blogs\/"},{"@type":"ListItem","position":2,"name":"Python Tutorials","item":"https:\/\/data-flair.training\/blogs\/category\/python\/"},{"@type":"ListItem","position":3,"name":"Python Program on Lambda Function"}]},{"@type":"WebSite","@id":"https:\/\/data-flair.training\/blogs\/#website","url":"https:\/\/data-flair.training\/blogs\/","name":"DataFlair","description":"Learn Today. Lead Tomorrow.","publisher":{"@id":"https:\/\/data-flair.training\/blogs\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/data-flair.training\/blogs\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/data-flair.training\/blogs\/#organization","name":"DataFlair","url":"https:\/\/data-flair.training\/blogs\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/logo\/image\/","url":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2016\/07\/Data-Flair.png","contentUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2016\/07\/Data-Flair.png","width":106,"height":48,"caption":"DataFlair"},"image":{"@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/DataFlairWS\/","https:\/\/x.com\/DataFlairWS","https:\/\/www.linkedin.com\/company\/dataflair-web-services-pvt-ltd\/","https:\/\/www.youtube.com\/user\/DataFlairWS"]},{"@type":"Person","@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/person\/c187795dc82ab948373cca526df7c445","name":"DataFlair Team","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/2302ebc438084d2f1f993edc1996a0aae01332e81f3227cba8df0c48ec010ca4?s=96&d=mm&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/2302ebc438084d2f1f993edc1996a0aae01332e81f3227cba8df0c48ec010ca4?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/2302ebc438084d2f1f993edc1996a0aae01332e81f3227cba8df0c48ec010ca4?s=96&d=mm&r=g","caption":"DataFlair Team"},"description":"DataFlair Team provides high-impact content on programming, Java, Python, C++, DSA, AI, ML, data Science, Android, Flutter, MERN, Web Development, and technology. We make complex concepts easy to grasp, helping learners of all levels succeed in their tech careers.","url":"https:\/\/data-flair.training\/blogs\/author\/dfteam6\/"}]}},"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts\/123241","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/users\/581"}],"replies":[{"embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/comments?post=123241"}],"version-history":[{"count":3,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts\/123241\/revisions"}],"predecessor-version":[{"id":134129,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts\/123241\/revisions\/134129"}],"wp:attachment":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/media?parent=123241"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/categories?post=123241"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/tags?post=123241"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}