This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def first_and_last(message): | |
if not message or message[0] == message[len(message)-1]: | |
return True | |
else: | |
return False |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def initials(phrase): | |
words = phrase.split() | |
result = "" | |
for word in words: | |
result += word[0].upper() | |
return result | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def student_grade(name, grade): | |
return "{} received {}% on the exam".format(name, grade) | |
print(student_grade("Reed", 80)) | |
print(student_grade("Paige", 92)) | |
print(student_grade("Jesse", 85)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
String operations | |
len(string) Returns the length of the string | |
for character in string Iterates over each character in the string | |
if substring in string Checks whether the substring is part of the string | |
string[i] Accesses the character at index i of the string, starting at zero | |
string[i:j] Accesses the substring starting at index i, ending at index j-1. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def replace_ending(sentence, old, new): | |
# Check if the old string is at the end of the sentence | |
if sentence.endswith(old): | |
# Using i as the slicing index, combine the part | |
# of the sentence up to the matched string at the | |
# end with the new string | |
i = len(old) | |
new_sentence = sentence[:-i]+new | |
return new_sentence | |
# Return the original sentence if there is no match |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def is_palindrome(input_string): | |
# We'll create two strings, to compare them | |
new_string = "" | |
reverse_string = "" | |
# Traverse through each letter of the input string | |
for letter in input_string.strip(): | |
# Add any non-blank letters to the | |
# end of one string, and to the front | |
# of the other string. | |
new_string = new_string+letter.replace(" ","") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def skip_elements(elements): | |
# Initialize variables | |
new_list = [] | |
i = 0 | |
# Iterate through the list by value and not index | |
for element in elements: | |
# Does this element belong in the resulting list? Check on index | |
if i % 2 == 0: | |
# Add this element to the resulting list |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def skip_elements(elements): | |
# code goes here | |
new_list = [] | |
for index, element in enumerate(elements): | |
if index % 2 == 0: | |
new_list.append(element) | |
return new_list | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Common sequence operations | |
len(sequence) Returns the length of the sequence | |
for element in sequence Iterates over each element in the sequence | |
if element in sequence Checks whether the element is part of the sequence | |
sequence[i] Accesses the element at index i of the sequence, starting at zero |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def group_list(group, users): | |
members = [ user for user in users] | |
return "{}: {}".format(group, members) | |
print(group_list("Marketing", ["Mike", "Karen", "Jake", "Tasha"])) # Should be "Marketing: Mike, Karen, Jake, Tasha" | |
print(group_list("Engineering", ["Kim", "Jay", "Tom"])) # Should be "Engineering: Kim, Jay, Tom" | |
print(group_list("Users", "")) # Should be "Users:" |
OlderNewer