|
| 1 | +{ |
| 2 | + "cells": [ |
| 3 | + { |
| 4 | + "cell_type": "markdown", |
| 5 | + "id": "5a0a7331", |
| 6 | + "metadata": {}, |
| 7 | + "source": [ |
| 8 | + "# First Class Functions in Python\n", |
| 9 | + "<br>\n", |
| 10 | + "--> First Class Objects in a language are handled uniformly throughout.<br><br>\n", |
| 11 | + "--> They may be stored in data structures,passed as arguments, or used in control structures.<br><br>\n", |
| 12 | + "--> A Programming Language is said to support First Class functions if it treats functions as First Class objects.<br><br>\n", |
| 13 | + "--> Python supports the concept of First Class functions.<br>" |
| 14 | + ] |
| 15 | + }, |
| 16 | + { |
| 17 | + "cell_type": "markdown", |
| 18 | + "id": "a531863c", |
| 19 | + "metadata": {}, |
| 20 | + "source": [ |
| 21 | + "# Properties of First Class Functions\n", |
| 22 | + "<br>\n", |
| 23 | + "--> A function is an instance of the object type.<br><br>\n", |
| 24 | + "--> Function can be stored in a variable.<br><br>\n", |
| 25 | + "--> Function can be passed as a parameter to another function.<br><br>\n", |
| 26 | + "--> Function can be returned from a function<br><br>\n", |
| 27 | + "--> Function can be stored in data structures such as hash tables,lists.<br>" |
| 28 | + ] |
| 29 | + }, |
| 30 | + { |
| 31 | + "cell_type": "code", |
| 32 | + "execution_count": 8, |
| 33 | + "id": "7d21f43d", |
| 34 | + "metadata": {}, |
| 35 | + "outputs": [ |
| 36 | + { |
| 37 | + "name": "stdout", |
| 38 | + "output_type": "stream", |
| 39 | + "text": [ |
| 40 | + "First Class Function in Python\n", |
| 41 | + "Function to convert string to Uppercase\n", |
| 42 | + "Uppercase string from function is: HELLO\n", |
| 43 | + "Uppercase string from variable is: HELLO\n" |
| 44 | + ] |
| 45 | + } |
| 46 | + ], |
| 47 | + "source": [ |
| 48 | + "# Example of First Class Functions with one argument\n", |
| 49 | + "\n", |
| 50 | + "print(\"First Class Function in Python\")\n", |
| 51 | + "print(\"Function to convert string to Uppercase\")\n", |
| 52 | + "\n", |
| 53 | + "# Creating a function to convert string to uppercase\n", |
| 54 | + "def shout(text):\n", |
| 55 | + " return text.upper()\n", |
| 56 | + "\n", |
| 57 | + "# Calling function\n", |
| 58 | + "print(\"Uppercase string from function is:\",shout(\"Hello\"))\n", |
| 59 | + "\n", |
| 60 | + "# Storing in variable\n", |
| 61 | + "yell=shout\n", |
| 62 | + "\n", |
| 63 | + "# Calling function using variable\n", |
| 64 | + "print(\"Uppercase string from variable is:\",yell(\"Hello\"))" |
| 65 | + ] |
| 66 | + }, |
| 67 | + { |
| 68 | + "cell_type": "code", |
| 69 | + "execution_count": 9, |
| 70 | + "id": "c95f28d7", |
| 71 | + "metadata": {}, |
| 72 | + "outputs": [ |
| 73 | + { |
| 74 | + "name": "stdout", |
| 75 | + "output_type": "stream", |
| 76 | + "text": [ |
| 77 | + "First Class Function in Python\n", |
| 78 | + "Function to convert string to Uppercase\n", |
| 79 | + "Concatenated string from function is: Hello,How are You?\n", |
| 80 | + "Concatenated string from variable is: Hello,How are You?\n" |
| 81 | + ] |
| 82 | + } |
| 83 | + ], |
| 84 | + "source": [ |
| 85 | + "# Example of First Class Functions with three arguments\n", |
| 86 | + "\n", |
| 87 | + "print(\"First Class Function in Python\")\n", |
| 88 | + "print(\"Function to convert string to Uppercase\")\n", |
| 89 | + "\n", |
| 90 | + "# Creating a function to add string\n", |
| 91 | + "def calc(a,b,c):\n", |
| 92 | + " return a+b+c\n", |
| 93 | + "\n", |
| 94 | + "# Calling function\n", |
| 95 | + "print(\"Concatenated string from function is:\",calc(\"Hello\",\",\",\"How are You?\"))\n", |
| 96 | + "\n", |
| 97 | + "# Storing in variable\n", |
| 98 | + "c=calc\n", |
| 99 | + "\n", |
| 100 | + "# Calling function using variable\n", |
| 101 | + "print(\"Concatenated string from variable is:\",calc(\"Hello\",\",\",\"How are You?\"))" |
| 102 | + ] |
| 103 | + } |
| 104 | + ], |
| 105 | + "metadata": { |
| 106 | + "kernelspec": { |
| 107 | + "display_name": "Python 3 (ipykernel)", |
| 108 | + "language": "python", |
| 109 | + "name": "python3" |
| 110 | + }, |
| 111 | + "language_info": { |
| 112 | + "codemirror_mode": { |
| 113 | + "name": "ipython", |
| 114 | + "version": 3 |
| 115 | + }, |
| 116 | + "file_extension": ".py", |
| 117 | + "mimetype": "text/x-python", |
| 118 | + "name": "python", |
| 119 | + "nbconvert_exporter": "python", |
| 120 | + "pygments_lexer": "ipython3", |
| 121 | + "version": "3.9.12" |
| 122 | + } |
| 123 | + }, |
| 124 | + "nbformat": 4, |
| 125 | + "nbformat_minor": 5 |
| 126 | +} |
0 commit comments