python

enumerate()

python

Similar to using range(len(someList)) (See: For loops ) you can enumerate() over a list like so: >>> supplies = ['pens', 'staplers', 'flamethrowers', 'binders'] >>> for index, item in enumerate(supplies): ... print('Index ' + str(index) + ' in supplies is: ' + item) ... Index 0 in supplies is: pens Index 1 in supplies is: staplers Index 2 in supplies is: flamethrowers Index 3 in supplies is: binders

Escape Characters

python

An escape character allows use of characters that otherwise would be impossible to use in a string. >>> spam = 'Say hi to Bob\'s mother.' >>> spam "Say hi to Bob's mother." Escape Characters # Escape character Prints as \' Single quote \" Double quote \t Tab \n Newline (line break) \\ Backslash >>> print("Hello there! ...

Exception Handling

python

Errors can be handled with try and except statements. # zeroDivide.py def spam(divideBy): try: return 42 / divideBy except ZeroDivisionError: print('Error: Invalid argument.') print(spam(2)) print(spam(12)) print(spam(0)) print(spam(1)) [akraker@localhost python]$ python zeroDivide.py 21.0 3.5 Error: Invalid argument. None 42.0

f-strings

python

Similar to string interpolation you can use f-strings to insert values into a string. >>> name = 'Al' >>> age = 4000 >>> f'My name is {name}. Next year I will be {age + 1}.' 'My name is Al. Next year I will be 4001.'

Food Log Program

python

I want to create a CLI application that tracks food transactions much like ledger/hledger does for plain text accounting . It will need to read and write to a text file. It will need to know about where the text file lives. We can setup an environment variable like hledger does. Some examples that already exist # hranoprovod-cli Quantified self tool written in Go An example of using Ledger to track calories: ...

For Loops

python

for loops use the range() function and iterate over whatever items are in the range. for loops can also iterate over items in a list. for statement: for i in range(n): for loops contain the following: The for keyword A variable name The in keyword A call to the range() method with up to 3 integers passed to it A colon Starting on a newline, an indented for clause Examples # print('My name is') for i in range(5): print('Jimmy Five Times (' + srt(i) + ')') Equivalent while loop: ...

Functions

python

Function (aka method) is a user defined miniprogram within a program. Instead of writing the same code multiple times a function can be defined, and code-reuse is possible. Example: def hello(): print('Howdy!') hello() First line is a def statement , the code block following is the function body and hello() is a function call . See also # DRY

get()

python

You can use the get() method to retrieve a value or return a default value if the key does not exist. >>> picnicItems = {'apples': 5, 'cups': 2} >>> 'I am bringing ' + str(picnicItems.get('cups', 0)) + ' cups.' 'I am bringing 2 cups.' >>> 'I am bringing ' + str(picnicItems.get('eggs', 0)) + ' eggs.' 'I am bringing 0 eggs.'