|
| 1 | +# Almost three and a half years later, and I'm still learning |
| 2 | +# how to master Python. I'm always trying new ideas through |
| 3 | +# others so I can learn what Python is truly all about. |
| 4 | + |
| 5 | +# Use the enumerate() function to loop through a list, using |
| 6 | +# only two lines of code; one for the for-index enumerate() |
| 7 | +# function and the other for the 'print' statement. |
| 8 | + |
| 9 | +name_list=['John','Bob','Rob','Tom'] |
| 10 | + |
| 11 | +# Here is a simple for-loop that will loop through the name_list |
| 12 | +# values starting with index 0, followed by index 1 and then |
| 13 | +# index 2, and finally index 3. |
| 14 | + |
| 15 | +for index in name_list: |
| 16 | + print(index) |
| 17 | + |
| 18 | +# The for-loop example above is fine, but it has its limitations |
| 19 | +# when it comes to multi indexing through a tuple or list alike. |
| 20 | +# With the enumerate() function, such things are possible. |
| 21 | +# Try these enumerate() function Python program examples |
| 22 | +# below and see what happens when you experiment with them. |
| 23 | + |
| 24 | +for index,name in enumerate(name_list): |
| 25 | + print(index) |
| 26 | + |
| 27 | +for index,name in enumerate(name_list): |
| 28 | + print(name) |
| 29 | + |
| 30 | +for index,name in enumerate(name_list): |
| 31 | + print(index,name) |
| 32 | + |
| 33 | +for index,name in enumerate(name_list,start=1): |
| 34 | + print(index,name) |
| 35 | + |
| 36 | +name=['John','Bob','Rob','Tom'] |
| 37 | +pet=['Dog','Cat','Bird','Fish'] |
| 38 | +computer=['Desktop','Laptop','Cellphone','Notebook'] |
| 39 | + |
| 40 | +# Note: the zip() function only goes to the shortest length |
| 41 | +# in a multi list. However, you can simply keep them the |
| 42 | +# same size such as the list examples above, which shows |
| 43 | +# three lists called name, pet and computer. Each list has |
| 44 | +# four values in them. This way, every value gets called inside |
| 45 | +# one, single 'print' statement. Try these different examples |
| 46 | +# below. Note: you can rename the words 'index1, index2 and |
| 47 | +# index3' to any names you wish. You can also rename the |
| 48 | +# name variable if you like. |
| 49 | + |
| 50 | +for index1,index2,index3 in zip(name,pet,computer): |
| 51 | + print(index1) |
| 52 | + |
| 53 | +for index1,index2,index3 in zip(name,pet,computer): |
| 54 | + print(index2) |
| 55 | + |
| 56 | +for index1,index2,index3 in zip(name,pet,computer): |
| 57 | + print(index3) |
| 58 | + |
| 59 | +for index1,index2,index3 in zip(name,pet,computer): |
| 60 | + print(index1,index2,index3) |
| 61 | + |
| 62 | +# Let's try the enumerate() function with a 2d-list. |
| 63 | + |
| 64 | +my_2d_list=[ |
| 65 | + ['John','Bob','Rob','Tom'], |
| 66 | + ['Desktop','Laptop','Cellphone','Notebook']] |
| 67 | + |
| 68 | +for index,name in enumerate(my_2d_list): |
| 69 | + print(index) |
| 70 | + |
| 71 | +for index,name in enumerate(my_2d_list): |
| 72 | + print(name[0]) |
| 73 | + |
| 74 | +for index,name in enumerate(my_2d_list): |
| 75 | + print(index,name[0]) |
| 76 | + |
| 77 | +for index,name in enumerate(my_2d_list,start=1): |
| 78 | + print(index,name[0]) |
| 79 | + |
| 80 | +# Let's try the zip() function with a 2d-list. |
| 81 | + |
| 82 | +my_2d_list=[ |
| 83 | + ['John','Bob','Rob','Tom'], |
| 84 | + ['Desktop','Laptop','Cellphone','Notebook']] |
| 85 | + |
| 86 | +for index in zip(my_2d_list): |
| 87 | + print(index[0][0]) |
| 88 | + |
| 89 | +for index in zip(my_2d_list): |
| 90 | + print(index[0][0],index[0][1],index[0][2],index[0][3]) |
| 91 | + |
| 92 | +# Let's try some fun experiment examples with some of what |
| 93 | +# we've learned so far about the enumerate() function. Let's |
| 94 | +# create a program that uses a sentence for each value in the |
| 95 | +# fun_list1, fun_list2 and fun_list3 lists. Let's use the f' format |
| 96 | +# to make string concatenations much easier to create. |
| 97 | + |
| 98 | +fun_list1=['John','Bob','Rob','Tom'] |
| 99 | +fun_list2=['Dog','Cat','Bird','Fish'] |
| 100 | +fun_list3=['Desktop','Laptop','Cellphone','Notebook'] |
| 101 | + |
| 102 | +for index,name in enumerate(fun_list1): |
| 103 | + print(f"My name is {name}. I'm the value from the fun_list1, position {index}") |
| 104 | + |
| 105 | +for index,name in enumerate(fun_list2): |
| 106 | + print(f"I am a {name}. I'm the value from the fun_list2, position {index}") |
| 107 | + |
| 108 | +for index,name in enumerate(fun_list3): |
| 109 | + print(f"I am a {name}. I'm the value from the fun_list3, position {index}") |
| 110 | + |
| 111 | +# These enumerate() function examples are great, but let's beef it up just a lot |
| 112 | +# more with the zip() function, so we can create complex actions with all our |
| 113 | +# fun_lists combined into complete, separate sentences, just simply using two |
| 114 | +# lines of code. See what happens when you type and execute/run this Python |
| 115 | +# program example below: |
| 116 | + |
| 117 | +for list1,list2,list3 in zip(fun_list1,fun_list2,fun_list3): |
| 118 | + print(f"My name is {list1} and I have a {list2} picture on my {list3} screen.") |
| 119 | + |
| 120 | +# The zip() function is very useful, but it can only reach as far as its shortest |
| 121 | +# list length. That means, if you have two, three or more lists, the shortest list |
| 122 | +# out of the three or more lists values will be cut off from the rest if one or more |
| 123 | +# lists have extra values inside them. To avoid this from occurring, make all your |
| 124 | +# lists the same size in each of their values. take a look at the example below: |
| 125 | + |
| 126 | +fun_list1=['John','Bob','Rob','Tom'] # four values |
| 127 | +fun_list2=['Dog','Cat','Bird','Fish'] # four values |
| 128 | +fun_list3=['Desktop','Laptop','Cellphone','Notebook'] # four values |
| 129 | + |
| 130 | +# The zip() function is sometimes better than a simple for-loop or a simple |
| 131 | +# enumerate() function, in that it uses less lines of code and it can also achieve |
| 132 | +# a far better programming style approach over program code redundancy on |
| 133 | +# the programmer's part. |
| 134 | + |
| 135 | +# Let's try one more example to prove this to be true. let's create another |
| 136 | +# fun_list, zip() function Python program example. Type and execute/run |
| 137 | +# this Python program below and see what happens with the output. |
| 138 | + |
| 139 | +fun_list1=['John','Bob','Rob','Tom'] |
| 140 | +fun_list2=['Dog','Cat','Bird','Fish'] |
| 141 | +fun_list3=['Desktop','Laptop','Cellphone','Notebook'] |
| 142 | +fun_list4=['loves my','hates my','found my','lost my'] |
| 143 | +fun_list5=['fed his',"didn't feed his",'plays with his',"doesn't play with his"] |
| 144 | + |
| 145 | +for list1,list2,list3,list4,list5 in zip(fun_list1,fun_list2,fun_list3,fun_list4,fun_list5): |
| 146 | + print(f'{list1} {list4} {list3} and {list5} {list2}.') |
| 147 | + |
| 148 | +# Well, I think we pretty much learned what the enumerate() and zip() functions |
| 149 | +# do. Now, it's practice, practice and more practice, practice... |
| 150 | +'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' |
| 151 | +# Just a little something else to gnaw on, while you practice. |
| 152 | + |
| 153 | +# Use underscores _ to create readable numbers. |
| 154 | + |
| 155 | +num1=10_000_000_000 |
| 156 | +num2=100_000_000 |
| 157 | + |
| 158 | +total=num1+num2 |
| 159 | + |
| 160 | +# Use :, to create readable output. |
| 161 | + |
| 162 | +print(f'{total:,}') # output: 10,100,000,000 |
| 163 | +'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' |
| 164 | +# which of these two Python programs use less lines of code? |
| 165 | + |
| 166 | +george_boole=True |
| 167 | + |
| 168 | +if george_boole: |
| 169 | + x=1 |
| 170 | +else: |
| 171 | + x=0 |
| 172 | +print(x) |
| 173 | + |
| 174 | +# This one uses far less lines of code, yet both do the very same thing. |
| 175 | + |
| 176 | +george_boole=True |
| 177 | + |
| 178 | +x=1 if george_boole else 0 |
| 179 | +print(x) |
0 commit comments