|
| 1 | +# Here are some simple return function examples to |
| 2 | +# practice with. |
| 3 | + |
| 4 | +def addition(num1,num2): |
| 5 | + return num1+num2 |
| 6 | +def subtraction(num1,num2): |
| 7 | + return num1-num2 |
| 8 | +def multiplication(num1,num2): |
| 9 | + return num1*num2 |
| 10 | +def square(num1,num2): |
| 11 | + return num1**num2 |
| 12 | +def division(num1,num2): |
| 13 | + return num1/num2 |
| 14 | +def name(first_name,last_name,mc2=18600**2): |
| 15 | + return first_name+last_name+str(mc2) |
| 16 | + |
| 17 | +a=addition(8,2) |
| 18 | +s=subtraction(8,2) |
| 19 | +m=multiplication(8,2) |
| 20 | +d=division(8,2) |
| 21 | +e=square(8,2) |
| 22 | + |
| 23 | +nums=int(a+s+m+d+e) |
| 24 | + |
| 25 | +name=name('Albert ','Einstein = ',nums) |
| 26 | + |
| 27 | +# remove the 'nums' variable and see what happens when |
| 28 | +# you re-execute/run the above Python program example. |
| 29 | + |
| 30 | +print(name) |
| 31 | +'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' |
| 32 | +# How Parentheses, Square Brackets and Curly Braces |
| 33 | +# work in Python. |
| 34 | + |
| 35 | +# Parentheses: '()' |
| 36 | +# Parentheses are used for 'Tuples', along with other uses, |
| 37 | +# such as 'print' functions and functions alike. |
| 38 | + |
| 39 | +# Square Brackets: '[]' |
| 40 | +# Square Brackets are used for 'lists' and '2d lists', along |
| 41 | +# with other uses, such as indexing character strings and |
| 42 | +# values alike. |
| 43 | + |
| 44 | +# Curly Braces: '{}' |
| 45 | +# Curly Braces are used for 'sets' and 'dictionaries', along |
| 46 | +# with other uses, such as formatted character strings. |
| 47 | + |
| 48 | +# Here is a simple 'tuple' example: |
| 49 | +# names=('John','Ron','Tom','Bob') |
| 50 | + |
| 51 | +# Here is a simple 'list' example: |
| 52 | +# names=['John','Ron','Tom','Bob'] |
| 53 | + |
| 54 | +# Here is a simple 'dictionary' example: |
| 55 | +# names={1:'John',2:'Ron',3:'Tom',4:'Bob'} |
| 56 | + |
| 57 | +# Here is a simple 'set' example: |
| 58 | +# names={'John','Ron','Tom','Bob'} |
| 59 | +'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' |
| 60 | +# Variable Scope: |
| 61 | + |
| 62 | +# L= Local |
| 63 | +# E= Enclosing |
| 64 | +# G= Global |
| 65 | +# B=Built-in |
| 66 | +'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' |
| 67 | +# predefined returned values to arguments example: |
| 68 | + |
| 69 | +def values_example(value0,value1,value2,value3): |
| 70 | + return 0,1,2,3 |
| 71 | + |
| 72 | +print(values_example('Value0','Value1','Value2','Value3')[2]) |
| 73 | + |
| 74 | +# undefined returned values to arguments example: |
| 75 | + |
| 76 | +# If you aren't sure how many returned values to variables |
| 77 | +# are needed, use the '*args' function instead. You can name |
| 78 | +# the word 'args' to any name you like, but the (*) is needed. |
| 79 | +# For example: '*get_any_number_of_returned_values' works. |
| 80 | +# However in python, programmers use the standard as '*args' |
| 81 | +# short for (arguments). Use '*args' if you want to update the |
| 82 | +# function's returned values, without the worry of how many |
| 83 | +# actual argument variables are needed inside the 'print' |
| 84 | +# statement, such as the example above illustrates. |
| 85 | + |
| 86 | +# Example 1: |
| 87 | + |
| 88 | +def args_example(*args): |
| 89 | + return args[0] |
| 90 | + |
| 91 | +print(args_example(0,1,2,3,4,5,6,7,8,9)) |
| 92 | + |
| 93 | +# Example 2: |
| 94 | + |
| 95 | +def args_example(*args): |
| 96 | + return 0,1,2,3,4,5,6,7,8,9 |
| 97 | + |
| 98 | +print(args_example()[1]) |
| 99 | +'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' |
| 100 | +# undefined returned values to keyword arguments example: |
| 101 | + |
| 102 | +# If you aren't sure how many returned values to variables are |
| 103 | +# needed, use the '**kwargs' function instead. You can name the |
| 104 | +# word 'kwargs' to any name you like, but the (**) is needed. For |
| 105 | +# example: '**get_any_number_of_returned_values' works. However |
| 106 | +# in python, programmers use the standard as '**kwargs' short for |
| 107 | +# (keyword arguments). Use '**kwargs' if you want to update the |
| 108 | +# function's returned values, without the worry of how many actual |
| 109 | +# keyword argument variables are needed inside the 'return' |
| 110 | +# statement. |
| 111 | + |
| 112 | +def kwargs_example(**kwargs): |
| 113 | + return 0,1,2,3,4,5,6,7,8,9 |
| 114 | + |
| 115 | +print(kwargs_example()[2]) |
| 116 | +'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' |
| 117 | +# Here are the very same examples below, but with hard line |
| 118 | +# breaks. In most cases, you must use parenthesis '()' to surround |
| 119 | +# hard line breaks, such as these examples illustrate. |
| 120 | + |
| 121 | +# Example 1: |
| 122 | + |
| 123 | +def args_example(*args): |
| 124 | + return args[0] |
| 125 | + |
| 126 | +print(args_example( # insert a hard line break if you like. |
| 127 | + 'Test with numbers',0,1,2,3,4,5,6,7,8,9,'Example.')) |
| 128 | + |
| 129 | +# Example 2: |
| 130 | + |
| 131 | +def args_example(*args): |
| 132 | + return( # insert a hard line break if you like. |
| 133 | + 'Test with numbers',0,1,2,3,4,5,6,7,8,9,'Example.') |
| 134 | + |
| 135 | +print(args_example()[0]) |
| 136 | +'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' |
| 137 | +def kwargs_example(**kwargs): |
| 138 | + return( # insert a hard line break if you like. |
| 139 | + 'Test with numbers',0,1,2,3,4,5,6,7,8,9,'Example.') |
| 140 | + |
| 141 | +print(kwargs_example()[0]) |
| 142 | +'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' |
| 143 | +# Here are the very same examples again, but with the use of |
| 144 | +# variables to shorten our code a bit in the 'print' statements. |
| 145 | + |
| 146 | +def args_example(*args): |
| 147 | + return args |
| 148 | + |
| 149 | +args=args_example( # insert a hard line break if you like. |
| 150 | + 'Test with numbers',0,1,2,3,4,5,6,7,8,9,'Example.') |
| 151 | + |
| 152 | +print(args[0]) |
| 153 | + |
| 154 | +# Example 2: |
| 155 | + |
| 156 | +def args_example(*args): |
| 157 | + return( # insert a hard line break if you like. |
| 158 | + 'Test with numbers',0,1,2,3,4,5,6,7,8,9,'Example.') |
| 159 | + |
| 160 | +args=args_example() |
| 161 | + |
| 162 | +print(args[0]) |
| 163 | +'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' |
| 164 | +def kwargs_example(**kwargs): |
| 165 | + return( # insert a hard line break if you like. |
| 166 | + 'Test with numbers',0,1,2,3,4,5,6,7,8,9,'Example.') |
| 167 | + |
| 168 | +kwargs=kwargs_example() |
| 169 | + |
| 170 | +print(kwargs[0]) |
| 171 | +'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' |
| 172 | +# Using the knowledge we've learnt so far, let's create an |
| 173 | +# arguments variable list loop using a for-loop. |
| 174 | + |
| 175 | +def args_example(*args): |
| 176 | + return args |
| 177 | + |
| 178 | +args=args_example( # insert a hard line break if you like. |
| 179 | + 'Test with numbers',0,1,2,3,4,5,6,7,8,9,'Example.') |
| 180 | + |
| 181 | +for i in args: |
| 182 | + print(i,end=' ') # add the 'end=' function to create single-line text output. |
| 183 | +'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' |
| 184 | +# Using the knowledge we've learnt so far, let's create a |
| 185 | +# keyword arguments variable list loop using a for-loop. |
| 186 | + |
| 187 | +def kwargs_example(**kwargs): |
| 188 | + return( # insert a hard line break if you like. |
| 189 | + 'Test with numbers',0,1,2,3,4,5,6,7,8,9,'Example.') |
| 190 | + |
| 191 | +kwargs=kwargs_example() |
| 192 | + |
| 193 | +for i in kwargs: |
| 194 | + print(i,end=' ') # add the 'end=' function to create single-line text output. |
0 commit comments