|
| 1 | +____________________________ |
| 2 | + |
| 3 | +Day 14:Lecture 1 |
| 4 | +Content: Function Passing arguments |
| 5 | +Author:Ravishankar Chavare |
| 6 | +Date:13-06-2019 |
| 7 | +LinkedIn:https://www.linkedin.com/in/ravishankar-chavare-84474a102 |
| 8 | +_______________________________ |
| 9 | + |
| 10 | + |
| 11 | +*Function arguments* |
| 12 | + |
| 13 | +*What is mean by argument's* |
| 14 | +A value passed to a function (or method) when calling the function called as arguments. |
| 15 | + |
| 16 | +Python support following types of arguments for function |
| 17 | + |
| 18 | +1.positional arguments |
| 19 | +2.Default arguments |
| 20 | +3.Keyword arguments |
| 21 | +4.Arbitary arguments |
| 22 | + |
| 23 | + |
| 24 | +*1.Positional arguments* |
| 25 | + |
| 26 | +- An argument that is not a keyword argument. Positional arguments can appear at the beginning of an argument list and/or be passed as elements of an iterablepreceded by *. |
| 27 | + |
| 28 | +- In positional arguments we need to maintain sequence as we calling area. |
| 29 | +Syntax: |
| 30 | +Functionname(element1,element2) |
| 31 | + |
| 32 | +Or |
| 33 | + |
| 34 | +Functionname(*(element1,element2)) |
| 35 | + |
| 36 | + |
| 37 | +Example |
| 38 | +def Addition(a,b): |
| 39 | + return a+b |
| 40 | +res=Addition(5,6) |
| 41 | +print(res) |
| 42 | + |
| 43 | +#Result:11 |
| 44 | + |
| 45 | +______________________________________ |
| 46 | + |
| 47 | +*2.Keyword arguments* |
| 48 | +- An argument preceded by an identifier (e.g. name=) in a function call or passed as a value in a dictionary preceded by ** |
| 49 | + |
| 50 | +- Here we don't need to maintain position because we are defining element with one keyword. |
| 51 | + |
| 52 | + |
| 53 | +How to define keyword arguments |
| 54 | + |
| 55 | +Syntax: |
| 56 | +Functionname(key1=value,key2=value2) |
| 57 | + |
| 58 | +Or |
| 59 | + |
| 60 | +functionname(**{'key1':value1,'key2':value2}) |
| 61 | + |
| 62 | +How to access elements in function |
| 63 | +Syntax: |
| 64 | +def functionname(key1,key2): |
| 65 | + #function code block |
| 66 | + |
| 67 | +Example: |
| 68 | +def Addition(b,a): |
| 69 | + return a+b |
| 70 | + |
| 71 | +#keyword using |
| 72 | +res=Addition(a=5,b=7) |
| 73 | + |
| 74 | +#Using keywords with dictionary |
| 75 | +result= Addition(**{'a'=5,'b'=7}) |
| 76 | + |
| 77 | +print(res) |
| 78 | +#Result:12 |
| 79 | + |
| 80 | +print(result) |
| 81 | +#Result:12 |
| 82 | + |
| 83 | +______________________________________ |
| 84 | + |
| 85 | +*3.Defualt parameter* |
| 86 | +- Python allows function arguments to have default values. |
| 87 | +- If the function is called without the argument, the argument gets its default value. |
| 88 | + |
| 89 | +The default value is assigned by using assignment(=) operator of the |
| 90 | + |
| 91 | +Syntax: |
| 92 | +keywordname=value. |
| 93 | + |
| 94 | +Example 1: |
| 95 | +def Adition(a,b=7): |
| 96 | + #here default of b is set to 7 if b is not passed |
| 97 | + return a+b |
| 98 | + |
| 99 | +res=Addition(5) |
| 100 | +# here we have pass only a value |
| 101 | +print(res) |
| 102 | + |
| 103 | +#Result:12 |
| 104 | + |
| 105 | +Example 2: |
| 106 | +def Adition(a,b=7): |
| 107 | + #here default value 7 is not set because we have passed a,b from calling function |
| 108 | + return a+b |
| 109 | + |
| 110 | +res=Addition(5,9) |
| 111 | +# here we have pass only a,b value |
| 112 | +print(res) |
| 113 | + |
| 114 | +#Result:14 |
| 115 | + |
| 116 | +______________________________________ |
| 117 | + |
| 118 | +*4.Arbitary arguments* |
| 119 | +- In Python, we can pass a variable(dynamic length) number of arguments to a function using special symbols. |
| 120 | + |
| 121 | +There are two special symbols: |
| 122 | +1. *args(Non Keyword Arguments) |
| 123 | +2. **kwargs (Keyword Arguments) |
| 124 | + |
| 125 | +------------------------------------------------------------- |
| 126 | + |
| 127 | +*1. *args* |
| 128 | +- Python has *args which allow us to pass the variable number of non keyword arguments to function. |
| 129 | +- Use an asterisk * before the parameter name to pass variable length arguments. |
| 130 | +-The arguments are passed as a tuple and these passed arguments make tuple inside the function with same name as the parameter excluding asterisk * |
| 131 | + |
| 132 | + |
| 133 | +Syntax: |
| 134 | +When calling function |
| 135 | +functionname(V1,V2,V3.....) |
| 136 | + |
| 137 | + |
| 138 | +Inside Function |
| 139 | +def functioname(*varname): |
| 140 | + for i in varname: |
| 141 | + #do your computation on i variable |
| 142 | + |
| 143 | + |
| 144 | + |
| 145 | +Example: |
| 146 | +def Addition(*numb): |
| 147 | + add=0 |
| 148 | + for i in numb: |
| 149 | + add=add+i |
| 150 | + |
| 151 | + return add |
| 152 | + |
| 153 | +#send 2 arguments |
| 154 | +res=Addition(5,6) |
| 155 | +print(res) |
| 156 | +#Result:11 |
| 157 | + |
| 158 | +#send 3 arguments |
| 159 | +res=Addition(5,6,7) |
| 160 | +print(res) |
| 161 | +#Result:18 |
| 162 | + |
| 163 | + |
| 164 | +#send 4 arguments |
| 165 | +res=Addition(5,6,7,1) |
| 166 | +print(res) |
| 167 | +#Result:19 |
| 168 | + |
| 169 | +------------------------------------------------------------- |
| 170 | + |
| 171 | +2.**kwargs |
| 172 | +- **kwargs, it allows us to pass the variable length of keyword arguments to the function. |
| 173 | +- The arguments are passed as a dictionary and these arguments make a dictionary inside function with name same as the parameter excluding double asterisk **. |
| 174 | + |
| 175 | +Syntax: |
| 176 | +Syntax: |
| 177 | +When calling function |
| 178 | +functionname(k1=V1,k2=V2,k3=V3.....) |
| 179 | + |
| 180 | + |
| 181 | +Inside Function |
| 182 | +def functioname(**varname): |
| 183 | + for key,value in varname.items(): |
| 184 | + # do operation on key and value |
| 185 | + |
| 186 | +Example: |
| 187 | +def Addition(**numb): |
| 188 | + add=0 |
| 189 | + for key,value in numb: |
| 190 | + add=add+value |
| 191 | + return add |
| 192 | + |
| 193 | +#send 2 arguments |
| 194 | +res=Addition(a=5,b=6) |
| 195 | +print(res) |
| 196 | +#Result:11 |
| 197 | + |
| 198 | +#send 3 arguments |
| 199 | +res=Addition(a=5,b=6,c=7) |
| 200 | +print(res) |
| 201 | +#Result:18 |
| 202 | + |
0 commit comments