List comprehensions =================== Fibonacci sequence v1 --------------------- .. interactive_code_block:: :caption: Shortest version def fibonacci(length): # write Fibonacci series up to n a, b = 0, 1 for i in range(length): print(a, end=' ') a, b = b, a + b fibonacci(15) .. interactive_code_block:: :caption: !!! def fibonacci(length): sequence = [] for number in range(0, length): if len(sequence) < 2: sequence.append(number) else: sequence.append(sequence[-2] + sequence[-1]) return sequence print(fibonacci(15)) .. interactive_code_block:: :caption: !!! length= 15 sequence = [] [sequence.append(number) if len(sequence) < 2 else sequence.append(sequence[-2] + sequence[-1]) for number in list(range(0, length))] print(sequence) .. interactive_code_block:: :caption: !!! l = 15 s = [] [s.append(n) if len(s) < 2 else s.append(s[-2] + s[-1]) for n in list(range(0, l))] print(s) Fibonacci sequence v2 (Binet formula) ------------------------------------- .. interactive_code_block:: :caption: !!! square_root_of_5 = 5**0.5 phi = (1 + square_root_of_5) / 2 def fibonacci_number(index): return int((phi**index - (1 - phi)**index) / square_root_of_5) fibonacci_sequence = [fibonacci_number(index) for index in range(0, 15)] print(fibonacci_sequence) .. interactive_code_block:: :caption: !!! square_root_of_5 = 5**0.5 phi = (1 + square_root_of_5) / 2 sequence = [int((phi**n - (1 - phi)**n) / square_root_of_5) for n in range(0, 15)] print(sequence) Months with an 'r' ------------------ .. interactive_code_block:: :caption: Only eat oysters in months with an 'r' months = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'] months_without_r = [month for month in months if 'r' not in month] print('Don\\'t eat oysters between {} and {}.'.format(months_without_r[0], months_without_r[-1])) Abbreviated name ---------------- .. interactive_code_block:: :caption: Only eat oysters in months with an 'r' weekdays = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday'] abbreviated_weekdays = [weekday[:3] for weekday in weekdays] print(abbreviated_weekdays) Abbreviated name 2 ------------------ .. interactive_code_block:: :caption: Only eat oysters in months with an 'r' term = 'Light Amplification by Stimulated Emission of Radiation' abbreviated_term = [character for character in term if character.isupper()] print(''.join(abbreviated_term)) List of leap years ------------------ .. interactive_code_block:: :caption: Generate a list of leap years using a for loop # Definition of a leap year from: # https://github.com/python/cpython/blob/3.9/Lib/calendar.py#L100 def isleap(year): """Return True for leap years, False for non-leap years.""" return year % 4 == 0 and (year % 100 != 0 or year % 400 == 0) leap_years = [] for year in range(1895, 1916): if isleap(year): leap_years.append(year) print(leap_years) .. interactive_code_block:: :caption: Generate a list of leap years using a list comprehension # Definition of a leap year from: # https://github.com/python/cpython/blob/3.9/Lib/calendar.py#L100 def isleap(year): """Return True for leap years, False for non-leap years.""" return year % 4 == 0 and (year % 100 != 0 or year % 400 == 0) leap_years = [year for year in range(1895, 1915) if isleap(year)] print(leap_years) .. interactive_code_block:: :caption: Generate a list of leap years using a list comprehension leap_years = [year for year in range(1895, 1915) if year % 4 == 0 and (year % 100 != 0 or year % 400 == 0)] print(leap_years) .. https://www.programiz.com/python-programming/list-comprehension https://www.py4u.net/discuss/191916 https://en.wikipedia.org/wiki/Square_root_of_5 https://www.mathsisfun.com/numbers/fibonacci-sequence.html