<jupyter_start><jupyter_text>Example 1<jupyter_code>colors = ['red', 'green', 'blue', 'yellow']
print(colors[-4])<jupyter_output>red<jupyter_text>Regular Way<jupyter_code>for i in range(len(colors)):
    print(colors[i])
for i in range(len(colors)-1, -1, -1):
    print(colors[i])<jupyter_output>yellow
blue
green
red<jupyter_text>Python Way<jupyter_code>for color in reversed(colors):
    print(color)<jupyter_output>yellow
blue
green
red<jupyter_text>Regular Way<jupyter_code>for i in range(len(colors)):
    print(i, '-->', colors[i])<jupyter_output>0 --> red
1 --> green
2 --> blue
3 --> yellow<jupyter_text>Python Way<jupyter_code>for i, color in enumerate(colors):
    print(i, '-->', colors[i])<jupyter_output>0 --> red
1 --> green
2 --> blue
3 --> yellow<jupyter_text>Example 2<jupyter_code>names = ['raymod', 'rachel', 'mattew']
colors = ['red', 'green', 'blue', 'yellow']<jupyter_output><empty_output><jupyter_text>Regular Way<jupyter_code>n = min(len(names), len(colors))
for i in range(n):
    print(names[i], '-->', colors[i])<jupyter_output>raymod --> red
rachel --> green
mattew --> blue<jupyter_text>Python Way<jupyter_code>for name, color, in zip(names, colors):
    print(name, '-->', color)
for color in sorted(colors):
    print(color)
for color in sorted(colors, reverse=True):
    print(color)
print(sorted(colors, key=len))
print(sorted(colors, key=len, reverse=True))<jupyter_output>['yellow', 'green', 'blue', 'red']<jupyter_text>Example 3<jupyter_code># Looping over a dictionary key
d = {'matthew': 'blue', 'rachel': 'green', 'raymond':'red'}<jupyter_output><empty_output><jupyter_text>Regular Way<jupyter_code>for k in d:
    print(k)
for k in d:
    print(k, '-->', d[k])
# Construct a dictionary from pairs
names = {'raymod', 'rachel', 'matthew'}
colors = {'red', 'green', 'blue'}<jupyter_output><empty_output><jupyter_text>Python Way<jupyter_code>d = dict(zip(names, colors))
print(d)
# Counting with dictionaries
colors = {'red', 'green', 'red', 'blue', 'green', 'red'}
d = {}
for color in colors:
    d[color] = d.get(color, 0) + 1
print(d)<jupyter_output>{'blue': 1, 'red': 1, 'green': 1}<jupyter_text>Example 4   Regular Way<jupyter_code>list_1 = list()
for element in range(20):
  if not(element % 2): # If number is even
    list_1.append(element)
    
print(list_1)
# We may also employ a lambda function
list_1 = list(filter(lambda element: not(element % 2), range(20)))
print(list_1)<jupyter_output>[0, 2, 4, 6, 8, 10, 12, 14, 16, 18]<jupyter_text>Python Way<jupyter_code>list_1 = [element for element in range(20) if not(element % 2)]
print(list_1)<jupyter_output>[0, 2, 4, 6, 8, 10, 12, 14, 16, 18]<jupyter_text>Regular Way<jupyter_code>list_1 = list(range(20))
index = 0
while index < len(list_1):
  print('Index: ',index,'Value: ',list_1[index])
  index += 1<jupyter_output>Index:  0 Value:  0
Index:  1 Value:  1
Index:  2 Value:  2
Index:  3 Value:  3
Index:  4 Value:  4
Index:  5 Value:  5
Index:  6 Value:  6
Index:  7 Value:  7
Index:  8 Value:  8
Index:  9 Value:  9
Index:  10 Value:  10
Index:  11 Value:  11
Index:  12 Value:  12
Index:  13 Value:  13
Index:  14 Value:  14
Index:  15 Value:  15
Index:  16 Value:  16
Index:  17 Value:  17
Index:  18 Value:  18
Index:  19 Value:  19<jupyter_text>Python Way<jupyter_code># We can use enumerate method to list
list_1 = list(range(20))
for index, value in enumerate(list_1):
    # Use format for cleaner code
    print('Index: {}, Value: {}'.format(value, index))<jupyter_output>Index: 0, Value: 0
Index: 1, Value: 1
Index: 2, Value: 2
Index: 3, Value: 3
Index: 4, Value: 4
Index: 5, Value: 5
Index: 6, Value: 6
Index: 7, Value: 7
Index: 8, Value: 8
Index: 9, Value: 9
Index: 10, Value: 10
Index: 11, Value: 11
Index: 12, Value: 12
Index: 13, Value: 13
Index: 14, Value: 14
Index: 15, Value: 15
Index: 16, Value: 16
Index: 17, Value: 17
Index: 18, Value: 18
Index: 19, Value: 19<jupyter_text>Example 5<jupyter_code>list_1 = [1, 2, 3, 4]
list_2 = [4, 5, 6, 7]<jupyter_output><empty_output><jupyter_text>Regular Way<jupyter_code>list_1_and_2 = []
for element in list_1:
    if element in list_2:
        list_1_and_2.append(element)
print(list_1_and_2)<jupyter_output>[4]<jupyter_text>Python Way<jupyter_code># Using set
list_1_and_2 = list(set(list_1) & set(list_2)) 
print(list_1_and_2### Regular Way)<jupyter_output>[4]<jupyter_text>Regular Way<jupyter_code>elements = [1, 3, 5, 6, 7, 9, 2, 7, 8, 4, 2, 1, 9]
unique_elements = set()

for element in elements:
    unique_elements.add(element)
print(unique_elements)<jupyter_output>{1, 2, 3, 4, 5, 6, 7, 8, 9}<jupyter_text>Python Way<jupyter_code>elements = [1, 3, 5, 6, 7, 9, 2, 7, 8, 4, 2, 1, 9]

unique_elements = {element for element in elements}
print(unique_elements)<jupyter_output>{1, 2, 3, 4, 5, 6, 7, 8, 9}<jupyter_text>Regular Way<jupyter_code>if user:
  print('------------------------------')
  print(user)
  print('------------------------------')<jupyter_output>------------------------------
email
------------------------------<jupyter_text>Python Way<jupyter_code>if user:
  print('{0}\n{1}\n{0}'.format('-'*30, user))<jupyter_output>------------------------------
email
------------------------------
{'fraction_non_alphanumeric': 0.19361702127659575, 'fraction_numerical': 0.05299806576402321, 'mean_word_length': 4.45464135021097, 'pattern_counts': {'":': 0, '<': 72, '<?xml version=': 0, '>': 87, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '22', 'dir': 'jupyter-structured-clean-dedup', 'id': '23373', 'license': 'MIT', 'n_tokens_mistral': 2245, 'n_tokens_neox': 2023, 'n_words': 526, 'path': 'idiomatic/.ipynb_checkpoints/IdiomaticPython-checkpoint.ipynb', 'repo_name': 'rudyhendrawn/python-basic', 'size': '5170'}