by yuan meng
def pay_for_a_vehicle():
# enter plate number
plate = input("enter plate number: ")
# enter parking duration
duration = int(input("enter duration (min): "))
# print confirmation
print(f"Confirmation: {plate} is paid for {duration} min")
# return inputs for later use
return plate, duration
use the input() function in jupyter
store each user input in a variable
when you run it, you'll be asked to type inputs
park multiple cars until you don't want to? 👉 use break in while-loop
while True:
# enter information for one vehicle
plate, duration = pay_for_a_vehicle()
# ask whether to pay for another
another = int(input("Park another vehicle? (1: Yes; 0: No):"))
# if not, break
if another == 0:
break
find patterns in strings (tutorial)
example: extract the email in "address colorless green ideas to chomsky1928@mit.edu"
# use built-in "re" library
import re
# test case
test = "address colorless green ideas to chomsky1928@mit.edu"
# write out pattern in 2 groups
pattern = r"(\w+\d+)@(\w+.\w+)"
# search for pattern in example
match = re.search(pattern, test)
# print results
if not match:
print("can't find emails")
else:
print(f"email found: {match.group()}")
groups can be retrieved separately: match.group(1) is "chomsky1928" and match.group(2) is "mit@edu"
() denote a group
email found: chomsky1928@mit.edu
example answers
# example 1: special -> exclude the word itself
re.search(r"(?<=ate).*$", "i ate an apple")
# example 2: special -> multi-line pattern
suffixes = re.compile(
r"""(,? inc.?| limited| unlimited| corporation| corp| llp.?| llc.?| ltd.?|
|\s?company| companies| & company| & co| and company| & son| holdings| solutions|
| partners| holdings| technologies| tech| infotech| informationnology| group| consulting|
| productions| digital| services| usa| us|\s?united states| global| worldwide| international)""",
re.MULTILINE,
)
# full name of company
full_name = "Apple Inc."
# replace suffix with empty string
simplified = suffixes.sub("", full_name.lower())
(?<=abc)def: this pattern is called "positive lookbehind" 👉 looks for "abc" and returns "def" that's behind
cumbersome code i wrote for a data etl pipeline
you guessed it... there are also lookahead and lookaround... (learn more)