Python codes for practice - What is the code that matches the instruction?
Removes whitespace from either side of the string specified
' X '.strip()
batRegex = re.compile(r'Bat(man|mobile|copter|bat)')
Define a set of key words to search for that follow a prefix
import re phoneNumRegex = re.compile(r'\d\d\d-\d\d\d-\d\d\d\d')
Define a string structure that can be called when searching for a set of letters/numbers/symbols that match the structure
islower()
Returns True if all characters are lowercase letters
isdecimal()
Returns True of the string contains only numbers
'name_of_string'.title()
Returns the string in title form. Example: 'This Is Title Case"
isalnum()
Returns true (a nonzero number) if the argument is a letter of the alphabet or a digit. Otherwise it returns 0.
isspace()
Returns true (a nonzero number) if the argument is a whitespace character
istitle()
Returns true if the contents of the string given are in title case
isalpha()
Returns true if the string contains only alphabetic letters and is at least one character in length. Returns false otherwise.
mo = batRegex.search('Batmobile lost a wheel and the Joker got away') mo.group()
Search a string for a regular expression based on a pre-defined structure
Create another version of a list stored
cheese = copy.deepcopy(spam)
Grab from a key dictionary
d = {'k1':[1,2,3,{'tricky':['oh','man','inception',{'target':[1,2,3,'hello']}]}]} d['k1'][3]['tricky'][3]['target'][3]
You are driving a little too fast, and a police officer stops you. Write a function to return one of 3 possible results
def caught_speeding(speed, is_birthday): if is_birthday: speeding = speed - 5 else: speeding = speed if speeding > 80: return 'Big Ticket' elif speeding > 60: return 'Small Ticket' else: return 'No Ticket' caught_speeding(81,True)
Code that counts the number of times a word appears in a string
def countDog(st): count = 0 for word in st.lower().split(): if word == 'dog': count += 1 return count countDog('This dog runs faster than the other dog dude!')
Create a function that grabs the email website domain from a string in the form
def domainGet(email): return email.split('@')[-1]
Returns True if the string contains a certain word
def findDog(st): return 'dog' in st.lower().split()
Perform an operation on a variable for as long as the variable is within a pre-specified range/value
i = 1 while i < 5: print('i is: {}'.format(i)) i = i+1
Statement defining conditions based on a category or attribute of a value assigned
if 1 == 2: print('first') elif 3 == 3: print('middle') else: print('Last')
Use indexing to grab an element from a list
lst = [1,2,[3,4],[5,[100,200,['hello']],23,11],1,7] lst[3][1][2][0]
Print a string using pre-assigned variables
planet = "Earth" diameter = 12742 print("The diameter of {} is {} kilometers.".format(planet,diameter))
string.lower()
returns a lower case version of the string
name_of_string.upper()
returns an upper case version of the string
Split a string
s = 'Hi there Sam!' s.split()
Filter out words that don't start with an "s"
seq = ['soup','dog','salad','cat','great'] list(filter(lambda word: word[0]=='s',seq))
Define a list and return a value based on the contents of that list
seq = [1,2,3,4,5] for item in seq: print('Yep')
Define a list and return double each value of the contents of that list
seq = [1,2,3,4,5] for jelly in seq: print(jelly+jelly)