|
| 1 | +''' |
| 2 | +Let's learn what *args are all about. The word 'args' simply means the word |
| 3 | +'arguments' for short. One asterisk * is used for *args. Use *args when you |
| 4 | +don't know how many argument variables you want within your define function |
| 5 | +parameters. Note: you do not need to call the word '*args' as args. However, |
| 6 | +you will need to invoke the asterisk * to make *args work. Programmers |
| 7 | +know the word as *args by standard definition, but you can use your own words. |
| 8 | +''' |
| 9 | +# HIGHLIGHT AND COPY CODE, THEN PASTE INTO YOUR PREFERABLE PYTHON APP/IDLE |
| 10 | + |
| 11 | +def book(*args): # one argument variable |
| 12 | + print('Python Programmer\'s Glossary Bible\nby Joseph C. Richardson') |
| 13 | + |
| 14 | +book('unknown number of argument values.','add another unknown argument value.') # two argument values |
| 15 | + |
| 16 | +# Create your own *args function parameter variable as shown below. |
| 17 | + |
| 18 | +def book(*my_unknown_num_arguments): # one argument variable |
| 19 | + print('Python Programmer\'s Glossary Bible\nby Joseph C. Richardson') |
| 20 | + |
| 21 | +book('unknown number of argument values.','add another unknown argument value.') # two argument values |
| 22 | +''' |
| 23 | +As shown in the other define function() examples above, how we needed the exact |
| 24 | +number of argument values to the exact number of argument variables. However, |
| 25 | +with *args you no longer have to worry about how many argument values you will |
| 26 | +need to satisfy the number of argument variables within the define function parameters. |
| 27 | +''' |
| 28 | +# Let's do some more *args with return functions |
| 29 | + |
| 30 | +def book(*args): # one argumant variable |
| 31 | + return 'Python Programmer\'s Glossary Bible' |
| 32 | + |
| 33 | +print(book('by Joseph C. Richardson','add another unknown argument value.')) # two argument values |
| 34 | + |
| 35 | +def nums(*args): # one argument variable |
| 36 | + return args |
| 37 | + |
| 38 | +print(nums(2,3)) # two argument values |
| 39 | +'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' |
| 40 | +''' |
| 41 | +Let's learn what **kwargs are all about. The word 'kwargs' simply means the words |
| 42 | +'keyword arguments' for short. Two asterisks ** are used for **kwargs. Use **kwargs |
| 43 | +when you don't know how many keyword argument variables you want within your |
| 44 | +define function parameters. Note: you do not need to call the word '**kwargs' as kwargs. |
| 45 | +However, you will need to invoke two asterisks ** to make **kwargs work. Programmers |
| 46 | +know the word as **kwargs by standard definition, but you can use your own words. |
| 47 | +''' |
| 48 | +def book(**kwargs): # one keyword argument variable |
| 49 | + print('Python Programmer\'s Glossary Bible\nby Joseph C. Richardson') |
| 50 | + |
| 51 | +book(keyword='keyword',argument='argument') # two keyword argument values |
| 52 | + |
| 53 | +# This example is without any **kwargs at all; we have to name our keyword arguments. |
| 54 | + |
| 55 | +def book(keyword_arg1,keyword_arg2): # two keyword argument variables |
| 56 | + print('Python Programmer\'s Glossary Bible\nby Joseph C. Richardson') |
| 57 | + |
| 58 | +book(keyword_arg1='keyword',keyword_arg2='argument') # two keyword argument values |
| 59 | +''' |
| 60 | +As shown in the define function() example above, how we needed the exact number of keyword |
| 61 | +argument values to the exact number of keyword argument variables. However, with **kwargs |
| 62 | +you no longer have to worry about how many keyword argument values you will need to satisfy |
| 63 | +the number of keyword argument variables within the define function parameters. |
| 64 | +''' |
| 65 | +# Let's create some define functions that act like subroutines. |
| 66 | +''' |
| 67 | +Since there are no line numbers in Python, also means that we cannot create any such 'go to', |
| 68 | +or 'go sub' commands at all with Python. So how can we create subroutines with Python?. How |
| 69 | +can we create subroutines without making them jump to line numbers, like we did in the old days? |
| 70 | +Well the answer is quite simple. Let's use define functions() with a while loop to create our subroutine |
| 71 | +examples. |
| 72 | +''' |
| 73 | +def subroutine1(): |
| 74 | + print('You picked subroutine1') |
| 75 | + |
| 76 | +def subroutine2(): |
| 77 | + print('You picked subroutine2') |
| 78 | + |
| 79 | +def subroutine3(): |
| 80 | + print('You picked subroutine3') |
| 81 | + |
| 82 | +while True: |
| 83 | + message=input('Please type 1, 2 or 3 to select the subroutine you wish to \ |
| 84 | +display or type (q) to quit: ').strip() |
| 85 | + |
| 86 | + if message=='q': |
| 87 | + break |
| 88 | + while True: |
| 89 | + if message=='1': |
| 90 | + subroutine1() |
| 91 | + break |
| 92 | + elif message=='2': |
| 93 | + subroutine2() |
| 94 | + break |
| 95 | + elif message=='3': |
| 96 | + subroutine3() |
| 97 | + break |
| 98 | + else: |
| 99 | + print('Sorry! No subroutine for that.') |
| 100 | + break |
0 commit comments