Reading-Notes

View the Project on GitHub

Game of Greed 3

List Comprehensions

Basically Python list comprehensions powerful and essential method to use, they are used to make your code more readable and eaiser to understand.

Syntax

my_new_list = [ expression for item in list ]

Range() method

lower() and upper() methods

isdigit() method

Parsing files

Using list comprehension, we can iterate through the lines of text in the file and store their contents in a new list.

file = open("dreams.txt", 'r')
poem = [ line for line in file ]

for line in poem:
    print(line)

output –> Hold fast to dreams

For if dreams die

Life is a broken-winged bird

That cannot fly.

-Langston Hughs

Functions

nums = [double(x) for x in range(1,10)] print(nums)

- we can add a condition to the previous for doubling only even numbers:

even_nums = [double(x) for x in range(1,10) if x%2 == 0]

- Additional arguments can be added to create more complex logic:

nums = [x+y for x in [1,2,3] for y in [10,20,30]] ``` output –> [11, 21, 31, 12, 22, 32, 13, 23, 33]

More

Primer on Python Decorators

Decorators provide a simple syntax for calling higher-order functions.

More on decorators