Python

Loops

Files in a directory

import os

directory = os.fsencode(directory_in_str)

for file in os.listdir(directory):
    filename = os.fsdecode(file)
    if filename.endswith(".txt"): 
        print("Found a text file"))
        continue
    else:
        continue

Lines in a file

file = open("input.txt", "r")
for line in file:
    print(line)

Lambda Functions

A lambda function does not have a name (therefore refered to as anonymous). Use them for when you need a function to do something quick and simple, but don't plan on reusing it elsewhere.

General syntax

lambda arguments: expression

Example

sum = lambda x, y: x + y

Types

Check if string is a valid integer

"14".isdigit()

Lists

Test is one list is in another list

any(map(lambda v: v in list2, list1))

Count how many items of a list equal a value

count_of_ones = my_list.count(1)