Skip to content

Instantly share code, notes, and snippets.

@svpino
Last active September 2, 2020 06:36
Show Gist options
  • Save svpino/d159d0205ab3ea0350b93407ef0f4b04 to your computer and use it in GitHub Desktop.
Save svpino/d159d0205ab3ea0350b93407ef0f4b04 to your computer and use it in GitHub Desktop.
It's different in Python
# Using Python's enumerate built-in function
# Let's assume we want to print every day from the following
# list together with its position in the list (starting at 1.)
days = ["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"]
""" Solution 1. Works, but Python has a better way """
# We can use a control variable that we increment for every
# value in the list.
index = 1
for day in days:
print(index, day)
# Let's increment our control variable so the next day
# we print has the proper index.
index += 1
""" Solution 2. In Python, it looks much better this way """
# Using the built-in enumerate function we can loop over
# the list and have an automatic counter.
for index, day in enumerate(days, 1):
print(index, day)
# Using Python's list comprehensions
# Let's assume we want to create a list with every number
# divisible by 3 within 0 and 99.
""" Solution 1. Works, but Python has a better way """
# We need to initialize our list so we can add any relevant
# values to it later.
result = []
# Let's now loop through the first 100 values (from 0 to 99)
# and add any value divisible by 3 to our result list.
for value in range(100):
if value % 3 == 0:
result.append(value)
print(result)
""" Solution 2. In Python, it looks much better this way """
# Using a list comprehension, we can create our result in
# a single line by combining our loop with a condition.
result = [value for value in range(100) if value % 3 == 0]
print(result)
""" Bonus: You don't need to include a condition """
# Assuming you don't need a condition, you can use the list
# comprenhension without it. Here is an example to print
# the square of every value from 0 to 9.
print([value ** 2 for value in range(10)]
# Using Python's Comparison Operator Chaining
>>> x = 10
# We can ask whether our x variable is within a specific
# range by chaining comparison operators:
>>> 1 <= x <= 10
True
# The above expression is equivalent to the following:
>>> 1 <= x and x <= 10
True
# We can chain different operators together:
>>> x < 20 <= x * 3 > 18
True
# The above expression is equivalent to the following:
>>> x < 20 and 20 <= x * 3 and x * 3 > 18
True
# Using Python’s for/else loops
# Let's create a function that prints out the first even
# number in the supplied array, or "Nothing" if there
# aren't any even numbers.
def even(array):
for i in array:
if i % 2 == 0:
# If we found an even number, let's print it and
# break the loop.
print("Even number found:", i)
break
else:
# This else condition will be executed only if the for
# loop doesn't end with a break statement.
print("Nothing")
# If we call our function with an array that contains even
# numbers, we should see the first one printed on the screen:
>>> even([1, 4, 5, 8, 7])
Even number found: 4
# If we call our function with an array that doesn't contain
# even numbers, the else clause of the for loop will run
# printing the word "Nothing" on the screen:
>>> even([1, 3, 5, 7, 7])
Nothing
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment