|
| 1 | +# Let's create a dictionary that uses strings as keys and values, instead of |
| 2 | +# of actual text, like we did before. Let's create two, simple tuples; one for |
| 3 | +# the key tuple and one for the value tuple. We can also create them with or |
| 4 | +# without parentheses, but a '\' backslash must be implemented in place of the |
| 5 | +# parentheses. However, the Python programming standard shows only the |
| 6 | +# constant use of parentheses, not backslashes, as you can see in the next |
| 7 | +# example below. |
| 8 | + |
| 9 | +key='dog','cat','mouse','bird','fish' # tuple by default |
| 10 | + |
| 11 | +value=( |
| 12 | + 'Grey Wolf','Huge Tigger', |
| 13 | + 'Black Rat','Macaw Parrot', |
| 14 | + 'Great White Shark') # create a tuple with '()' parentheses. |
| 15 | + |
| 16 | +# Why use '()' parentheses when you can simply use the '\' backslash instead. |
| 17 | +# Note: '\' is not the usual Python programming standard, but it works. Now, |
| 18 | +# however, this only acts like a tuple by default, not a list as one would think. |
| 19 | +# You cannot change or modify tuple values at all; they are immutable, not |
| 20 | +# mutable like lists. Even though this works, it's not viable, especially when |
| 21 | +# you need to create a mutable list, not an immutable tuple, as this example |
| 22 | +# does by default. You must use either '()' parentheses for tuples, '[]' square |
| 23 | +# brackets for lists and '{}' curly braces for dictionaries and sets alike. |
| 24 | + |
| 25 | +key='dog','cat','mouse','bird','fish' # tuple by default |
| 26 | + |
| 27 | +value=\ |
| 28 | + 'Grey Wolf','Huge Tigger',\ |
| 29 | + 'Black Rat','Macaw Parrot',\ |
| 30 | + 'Great White Shark' # tuple by default |
| 31 | + |
| 32 | +dictionary={ # dictionary |
| 33 | + key[0]:value[0], |
| 34 | + key[1]:value[1], |
| 35 | + key[2]:value[2], |
| 36 | + key[3]:value[3], |
| 37 | + key[4]:value[4] |
| 38 | + } |
| 39 | + |
| 40 | +# Non formatted examples with commas ',' and plus '+' signs |
| 41 | + |
| 42 | +for keys,values in dictionary.items(): |
| 43 | + print('My '+keys+' is really a '+values+'.') |
| 44 | + |
| 45 | +for keys,values in dictionary.items(): |
| 46 | + print('My',keys,'is really a',values+'.') |
| 47 | + |
| 48 | +# Old formatted example: now depreciated in Python 3 and up. |
| 49 | +# Can still be used in Python 3, thus far. |
| 50 | + |
| 51 | +for keys,values in dictionary.items(): |
| 52 | + print('My {} is really a {}.'.format(keys,values)) |
| 53 | + |
| 54 | +# New formatted example: Python 3 and up. |
| 55 | + |
| 56 | +for keys,values in dictionary.items(): |
| 57 | + print(f'My {keys} is really a {values}.') |
| 58 | +'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' |
| 59 | +# Things you can do with tuples and lists. |
| 60 | + |
| 61 | +# Tuple Example: |
| 62 | + |
| 63 | +variable_1='dog','cat','bird','guppy' |
| 64 | + |
| 65 | +variable_2=( |
| 66 | + 'Grey Wolf','Huge Tigger', |
| 67 | + 'Macaw Parrot','Great White Shark') |
| 68 | + |
| 69 | +variable_3='John','Rob','Ron','Bob' |
| 70 | + |
| 71 | +variable_4='Mom','Dad','Brother','Sister' |
| 72 | + |
| 73 | +variable_5='friend','girlfriend','boyfriend','neighbour' |
| 74 | + |
| 75 | +for var1,var2,var3,var4,var5 in zip(variable_1,variable_2,variable_3,variable_4,variable_5): |
| 76 | + print(f'My {var4} and my {var5} {var3} says my {var1} is really a {var2}.') |
| 77 | + |
| 78 | +# List Example: |
| 79 | + |
| 80 | +variable_1=['dog','cat','bird','guppy'] |
| 81 | + |
| 82 | +variable_2=[ |
| 83 | + 'Grey Wolf','Huge Tigger', |
| 84 | + 'Macaw Parrot','Great White Shark'] |
| 85 | + |
| 86 | +variable_3=['John','Rob','Ron','Bob'] |
| 87 | + |
| 88 | +variable_4=['Mom','Dad','Brother','Sister'] |
| 89 | + |
| 90 | +variable_5=['friend','girlfriend','boyfriend','neighbour'] |
| 91 | + |
| 92 | +for var1,var2,var3,var4,var5 in zip(variable_1,variable_2,variable_3,variable_4,variable_5): |
| 93 | + print(f'My {var4} and my {var5} {var3} says my {var1} is really a {var2}.') |
| 94 | +'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' |
| 95 | +# Test whether a value is true or false using the logical operators: |
| 96 | + |
| 97 | +# " ==, <, >, <=, >=, != " |
| 98 | + |
| 99 | +a=1;b=1 |
| 100 | + |
| 101 | +print(a==a) # True |
| 102 | +print(a<a) # False |
| 103 | +print(a>a) # False |
| 104 | +print(a<=a) # True |
| 105 | +print(a>=a) # True |
| 106 | +print(a!=a) # False |
| 107 | + |
| 108 | +print(b==b) # True |
| 109 | +print(b<b) # False |
| 110 | +print(b>b) # False |
| 111 | +print(b<=b) # True |
| 112 | +print(b>=b) # True |
| 113 | +print(b!=b) # False |
| 114 | + |
| 115 | +print(a==b) # True |
| 116 | +print(a<b) # False |
| 117 | +print(a>b) # False |
| 118 | +print(a<=b) # True |
| 119 | +print(a>=b) # True |
| 120 | +print(a!=b) # False |
| 121 | + |
| 122 | +print(b==a) # True |
| 123 | +print(b<a) # False |
| 124 | +print(b>a) # False |
| 125 | +print(b<=a) # True |
| 126 | +print(b>=a) # True |
| 127 | +print(b!=a) # False |
| 128 | + |
| 129 | +# Test whether a value is true or false using the Boolean conditionals: |
| 130 | + |
| 131 | +# " True, False, and, or, not " |
| 132 | + |
| 133 | +a=True;b=False |
| 134 | + |
| 135 | +print(a and a) # True |
| 136 | +print(b and b) # False |
| 137 | +print(a and b) # False |
| 138 | +print(b and a) # False |
| 139 | + |
| 140 | +print(a and not a) # False |
| 141 | +print(b and not b) # False |
| 142 | +print(a and not b) # True |
| 143 | +print(b and not a) # False |
| 144 | + |
| 145 | +print(a or a) # True |
| 146 | +print(b or b) # False |
| 147 | +print(a or b) # True |
| 148 | +print(b or a) # True |
| 149 | + |
| 150 | +print(a or not a) # True |
| 151 | +print(b or not b) # True |
| 152 | +print(a or not b) # True |
| 153 | +print(b or not a) # False |
| 154 | + |
| 155 | +print(a is a) # True |
| 156 | +print(b is b) # True |
| 157 | +print(a is b) # False |
| 158 | +print(b is a) # False |
| 159 | + |
| 160 | +print(a is not a) # False |
| 161 | +print(b is not b) # False |
| 162 | +print(a is not b) # True |
| 163 | +print(b is not a) # True |
| 164 | + |
| 165 | +# Check to see if a variable contains a value. |
| 166 | + |
| 167 | +numbers=1,2,3,4,5,6,7,8,9,10 |
| 168 | + |
| 169 | +print(1 in numbers) # True |
| 170 | +print(9 in numbers) # True |
| 171 | +print(11 in numbers) # False |
| 172 | +print(11 not in numbers) # True |
0 commit comments