Indexing and range (starting from 0 by default)
○ [start (inclusive) : end (exclusive) : stepsize (optional)] = [0, 1, 2, 3] = [-4, -3, -2, -1]. Tip: end - start is the number of elements returned.
○ Range(start (inclusive), stop (exclusive), stepsize). Range(3): 0, 1, 2. The start, stop and step parameters work the same as in list slicing.
○ Set function is used to create a set, particularly useful for removing duplicates from a list or character returns as { }. There is no order.
○ 1: 8: 3 is going to give us: 1, 4 and 7!
Scope (local and global names)
○ Global variables are defined outside functions and are accessible everywhere, but a local variable with the same name inside a function take
precedence (dominates). If a variable isn’t found locally, only then Python looks in the global scope. The global keyword allows modifying
global variables within functions.
String methods (immutable, all of these methods are non-mutating string methods)
○ Escape characters can also be used to include quotes or specific characters: \\ = \, \’ = ‘, \” = “, \n = go to the next line, \t = insert a tab.
○ The individual character(s) of a string can be accessed directly using zero based indexing just like with lists.
○ str.index(value, start, end) – returns the index of the first occurrence of the specified value within the slice (works with strings too)
○ len(x) – returns the number of elements in a list or the number of characters in a string, including spaces, punctuation and other characters.
○ sorted(x) – returns a new list containing all items from x in ascending order. When reverse = True it returns a new list containing all items
from x in descending order. Sorted( ) does not change the original list itself. Works with strings too.
○ str.capitalize( ) – returns a copy of the string with only the first character capitalized and the rest lowercase. It doesn’t change the string, but
returns a new string. You must give that new string a name else you cannot use it in the print statement.
○ str.upper( ) – returns a copy of the string with all alphabetic characters converted to uppercase, leaving the original string unchanged.
○ str.lower( ) – returns a copy of all the characters of the string converted to lowercase.
○ str.isupper( ) – returns True if all characters in the string are uppercase, otherwise it returns False, str.islower( ) it is vice versa.