Running Python Programs on macOS
On macOS, you can create a shell script to run your Python scripts by creating a text file with the .command file extension. Create a new file in a text editor such as TextEdit and add the following content:
#!/usr/bin/env bash
python3 /path/to/your/pythonScript.py
Save this file with the .command file extension in your home folder (for example, on my computer it’s /Users/al). In a terminal window, make this shell script executable by running chmod u+x yourScript.command. Now you will be able to click the Spotlight icon (or press -SPACE) and enter yourScript.command to run the shell script, which in turn will run your Python script.
identifiers
type()
class()
id()
dir()
id(car1) 139771129539104
strings
split string
my_string[::-1]
i.e. mystring[start:stop:step_size
chain operations
'hello world'.replace('world', 'place').upper() 'HELLO PLACE'
my_string='hello world' my_string.replace('world', 'big city').upper() 'HELLO BIG CITY'
format strings
f'3 + 4 = {3+4}'
or
my_age = 40 f'My age is {my_age}'
including variables
name = "joe" age = 48 hello = "Hi!, my name is %s and I'm %s years old..." % (name, age) print(hello) "Hi!, my name is joe and I'm 48 years old..."
or
f'My name is {name}. Next year I will be {age + 1}.' 'My name is Al. Next year I will be 4001.'
normalising input
print("How are you today?") answer = input() if answer.lower() == "great": print("Me too! hav fun y'all..!") else: print("I hope your day improves...")
and normalise by force and then check for a Boolean
>>> 'HELLO'.lower().islower() True
isX()
isalpha() Returns True if the string consists only of letters and isn’t blank
isalnum() Returns True if the string consists only of letters and numbers and is not blank
isdecimal() Returns True if the string consists only of numeric characters and is not blank
isspace() Returns True if the string consists only of spaces, tabs, and newlines and is not blank
istitle() Returns True if the string consists only of words that begin with an uppercase letter followed by only lowercase letters
join
’, ‘.join([‘cats’, ‘rats’, ‘bats’])
‘cats, rats, bats’
’ ‘.join([‘My’, ‘name’, ‘is’, ‘Simon’])
‘My name is Simon’
‘ABC’.join([‘My’, ‘name’, ‘is’, ‘Simon’])
‘MyABCnameABCisABCSimon’
split
‘My name is Simon’.split()
[‘My’, ‘name’, ‘is’, ‘Simon’]
‘MyABCnameABCisABCSimon’.split(‘ABC’)
[‘My’, ‘name’, ‘is’, ‘Simon’]
‘My name is Simon’.split(‘m’)
[‘My na’, ‘e is Si’, ‘on’]
for loops
for <variable> in <iterable>: ... do something with variable
On each iteration, an element from iterable is assigned to variable. This variable exists and can be used only inside the loop. Once there is nothing more left, the loop stops and the program continues with the next lines of code
for letter in 'Hello': print(letter)
lists
iterating through lists
supplies = ['pens', 'staplers', 'flamethrowers', 'binders']
**for i in range(len(supplies)):**
print('Index ' + str(i) + ' in supplies is: ' + supplies[i])
Index 0 in supplies is: pens
Index 1 in supplies is: staplers
Index 2 in supplies is: flamethrowers
Index 3 in supplies is: binders
Using range(len(supplies)) in the previously shown for loop is handy because the code in the loop can access the index (as the variable i) and the value at that index (as supplies[i]). Best of all, range(len(supplies)) will iterate through all the indexes of supplies, no matter how many items it contains.
hello = ['name', 'list', 'of', 'lists'] for i in range(len(hello)): print(hello[i])
assignment trick
cat = ['fat', 'gray', 'loud'] size, color, disposition, name = cat print(color) brown
insert
list = ['0', '1', '2', '3', '4', '5',] list ['0', '1', '2', '3', '4', '5'] list.insert(3, "hello") list ['0', '1', '2', 'hello', '3', '4', '5']
index, remove, append, del
bacon.index('cat') bacon.append(99) bacon.remove('cat') del bacon[0]
replication and concatenation
[1, 2, 3] + ['A', 'B', 'C'] [1, 2, 3, 'A', 'B', 'C'] ['X', 'Y', 'Z'] * 3 ['X', 'Y', 'Z', 'X', 'Y', 'Z', 'X', 'Y', 'Z']
converting tuples <> lists
tuple(['cat', 'dog', 5]) ('cat', 'dog', 5) >>> list(('cat', 'dog', 5)) ['cat', 'dog', 5] >>> list('hello') ['h', 'e', 'l', 'l', 'o']
dictionaries
Does a value exist?
spam = {'name': 'Zophie', 'age': 7} 'name' in spam.keys() True
functions
nice link that explains difference between methods and functions but with a nice function example