Created
April 30, 2018 11:14
-
-
Save CavaTrendy/5a15a1ef018f62a3444ba20771dde330 to your computer and use it in GitHub Desktop.
Introduction to Python - Unit 2 Fundamentals
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
# [ ] review and run example - note the first element is always index = 0 | |
student_name = "Alton" | |
print(student_name[0], "<-- first character at index 0") | |
print(student_name[1]) | |
print(student_name[2]) | |
print(student_name[3]) | |
print(student_name[4]) | |
# [ ] review and run example | |
student_name = "Jin" | |
if student_name[0].lower() == "a": | |
print('Winner! Name starts with A:', student_name) | |
elif student_name[0].lower() == "j": | |
print('Winner! Name starts with J:', student_name) | |
else: | |
print('Not a match, try again tomorrow:', student_name) | |
# [ ] review and run example | |
student_name = "Joana" | |
# get last letter | |
end_letter = student_name[-1] | |
print(student_name,"ends with", "'" + end_letter + "'") | |
# [ ] review and run example | |
# get second to last letter | |
second_last_letter = student_name[-2] | |
print(student_name,"has 2nd to last letter of", "'" + second_last_letter + "'") | |
# [ ] review and run example | |
# you can get to the same letter with index counting + or - | |
print("for", student_name) | |
print("index 3 =", "'" + student_name[3] + "'") | |
print("index -2 =","'" + student_name[-2] + "'") |
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
# [ ] review and run example | |
# assign string to student_name | |
student_name = "Colette" | |
# addressing the 3rd, 4th and 5th characters using a slice | |
print("slice student_name[2:5]:",student_name[2:5]) | |
# [ ] review and run example | |
# assign string to student_name | |
student_name = "Colette" | |
# addressing the 3rd, 4th and 5th characters individually | |
print("index 2, 3 & 4 of student_name:", student_name[2] + student_name[3] + student_name[4]) | |
# [ ] review and run example | |
long_word = 'Acknowledgement' | |
print(long_word[2:11]) | |
print(long_word[2:11], "is the 3rd char through the 11th char") | |
print(long_word[2:11], "is the index 2, \"" + long_word[2] + "\",", "through index 10, \"" + long_word[10] + "\"") |
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
# [ ] print the first 4 letters of long_word | |
long_word = "timeline" | |
print(long_word [0:4]) | |
# [ ] print the first 4 letters of long_word in reverse | |
long_word = "timeline" | |
print(long_word [-8:-4]) | |
# [ ] print the last 4 letters of long_word in reverse | |
long_word = "timeline" | |
print(long_word [-4:]) | |
# [ ] print the letters spanning indexes 3 to 6 of long_word in Reverse | |
long_word = "timeline" | |
print(long_word [ -3 : -4 : -6]) |
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
# [ ] review and run example | |
word = "cello" | |
for letter in word: | |
print(letter) | |
# [ ] review and run example | |
# note: the variable 'letter' changed to 'item' | |
word = "trumpet" | |
for item in word: | |
print(item) | |
# [ ] review and run example | |
# note: variable is now 'xyz' | |
# using 'xyz', 'item' or 'letter' are all the same result | |
word = "piano" | |
for xyz in word: | |
print(xyz) | |
# [ ] review and run example | |
# creates a new string (new_name) adding a letter (ltr) each loop | |
# Q?: how many times will the loop run? | |
student_name = "Skye" | |
new_name = "" | |
for ltr in student_name: | |
if ltr.lower() == "y": | |
new_name += ltr.upper() | |
else: | |
new_name += ltr | |
print(student_name,"to",new_name) |
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
# [ ] review and run example | |
student_name = "Skye" | |
for letter in student_name[:3]: | |
print(letter) | |
# Iterate BACKWARDS | |
# [ ] review and run example | |
print() | |
student_name = "Skye" | |
# start at "y" (student_name[2]), iterate backwards | |
for letter in student_name[2::-1]: | |
print(letter) |
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
#len() | |
work_tip = "save your code" | |
# number of characters | |
len(work_tip) | |
# letter "e" occurrences | |
work_tip.count("e") | |
# find the index of the first space | |
work_tip.find(" ") | |
# find the index of "u" searching a slice work_tip[3:6] | |
work_tip.find("u",3,6) | |
#These methods return information that we can use to sort or manipulate strings | |
# [ ] review and run example | |
work_tip = "save your code" | |
print("number of characters in string") | |
print(len(work_tip),"\n")# [ ] create team_names list and populate with 3-5 team name strings | |
team_name = ["Inter" , "Milan", "Juve" , "Napoli"] | |
# [ ] print the list | |
print ("Le prime cinque squadre italiane sono: " , team_name) | |
# define list of strings | |
ft_bones = ["calcaneus", "talus", "cuboid", "navicular", "lateral cuneiform", "intermediate cuneiform", "medial cuneiform"] | |
# display type information | |
print("ft_bones: ", type(ft_bones)) | |
# print the list | |
print(ft_bones) | |
# define list of mixed data type | |
mixed_list = [1, 34, 0.999, "dog", "cat", ft_bones, age_survey] | |
# display type information | |
print("mixed_list: ", type(mixed_list)) | |
# print the list | |
print(mixed_list) | |
print('letter "e" occurrences') | |
print(work_tip.count("e"),"\n") | |
print("find the index of the first space") | |
print(work_tip.find(" "),"\n") | |
print('find the index of "u" searching a slice work_tip[3:9] -', work_tip[3:9]) | |
print(work_tip.find("u",3,9),"\n") | |
print('find the index of "e" searching a slice work_tip[4:] -', work_tip[4:]) | |
print(work_tip.find("e",4)) | |
# [ ] review and run example | |
work_tip = "good code is commented code" | |
print("The sentence: \"" + work_tip + "\" has character length = ", len(work_tip) ) | |
# [ ] review and run example | |
# find the middle index | |
work_tip = "good code is commented code" | |
mid_pt = int(len(work_tip)/2) | |
# print 1st half of sentence | |
print(work_tip[:mid_pt]) | |
# print the 2nd half of sentence | |
print(work_tip[mid_pt:]) | |
#.count() | |
#returns number of times a character or sub-string occur | |
# [ ] review and run example | |
print(work_tip) | |
print("how many w's? ", work_tip.count("w")) | |
print("how many o's? ", work_tip.count("o")) | |
print("uses 'code', how many times? ", work_tip.count("code")) | |
# [ ] review and run example | |
print(work_tip[:mid_pt]) | |
print("# o's in first half") | |
print(work_tip[:mid_pt].count("o")) | |
print() | |
print(work_tip[mid_pt:]) | |
print("# o's in second half") | |
print(work_tip[mid_pt:].count("o")) | |
#.find(string) | |
#returns index of first character or sub-string match, returns -1 if no match found | |
#.find(string, start index, end index), same as above .find() but searches from optional start and to optional end index | |
# [ ] review and run example | |
work_tip = "good code has meaningful variable names" | |
print(work_tip) | |
# index where first instance of "code" starts | |
code_here = work_tip.find("code") | |
print(code_here, '= starting index for "code"') | |
# [ ] review and run example | |
# set start index = 13 and end index = 33 | |
print('search for "meaning" in the sub-string:', work_tip[13:33],"\n") | |
meaning_here = work_tip.find("meaning",13,33) | |
print('"meaning" found in work_tip[13:33] sub-string search at index', meaning_here) | |
# [ ] review and run example | |
# if .find("o") has No Match, -1 is returned | |
print ("work_tip:" , work_tip) | |
location = work_tip.find("o") | |
# keeps looping until location = -1 (no "o" found) | |
while location >= 0: | |
print("'o' at index =", location) | |
# find("o", location + 1) looks for a "o" after index the first "o" was found | |
location = work_tip.find("o", location + 1) | |
print("no more o's") |
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
word = input ("Write a quote: ") | |
new_word = "" | |
for character in word: | |
if character.lower() >= "k": | |
print (new_word.upper() ) | |
elif character.isalpha: | |
new_word += character | |
print (new_word.lower() ) | |
else: | |
break |
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
# [ ] create team_names list and populate with 3-5 team name strings | |
team_name = ["Inter" , "Milan", "Juve" , "Napoli"] | |
# [ ] print the list | |
print ("Le prime cinque squadre italiane sono: " , team_name) | |
# define list of strings | |
ft_bones = ["calcaneus", "talus", "cuboid", "navicular", "lateral cuneiform", "intermediate cuneiform", "medial cuneiform"] | |
# display type information | |
print("ft_bones: ", type(ft_bones)) | |
# print the list | |
print(ft_bones) | |
# define list of mixed data type | |
mixed_list = [1, 34, 0.999, "dog", "cat", ft_bones, age_survey] | |
# display type information | |
print("mixed_list: ", type(mixed_list)) | |
# print the list | |
print(mixed_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
# [ ] review and run example | |
print(ft_bones[0], "is the 1st bone on the list") | |
print(ft_bones[2], "is the 3rd bone on the list") | |
print(ft_bones[-1], "is the last bone on the list") | |
# [ ] review and run example | |
print(ft_bones[1], "is connected to the",ft_bones[3]) | |
# [ ] review and run example | |
three_ages_sum = age_survey[0] + age_survey[1] + age_survey[2] | |
print("The first three ages total", three_ages_sum) |
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
#while loop .append() | |
#define an empty list: bday_survey | |
#get user input, bday, asking for the day of the month they were born (1-31) or "q" to finish | |
#using a while loop (while user not entering "quit") | |
#append the bday input to the bday_survey list | |
#get user input, bday, asking for the day of the month they were born (1-31) or "q" to finish | |
#print bday_survey list | |
# [ ] complete the Birthday Survey task above | |
bday_survey = [ ] | |
while True: | |
user = input ( "Your date of birth or quit: " ) | |
if user.isdigit(): | |
bday_survey.append ( user ) | |
elif user.startswith ( "q" ): | |
break | |
print("the survey is " , bday_survey ) |
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
ft_bones = ["calcaneus", "talus", "cuboid", "navicular", "lateral cuneiform", | |
"intermediate cuneiform", "medial cuneiform"] | |
# [ ] print ft_bones list | |
print (ft_bones) | |
# [ ] delete "cuboid" from ft_bones | |
del ft_bones [ 3 ] | |
# [ ] delete "navicular" from list | |
del ft_bones [ 5 ] | |
# [ ] reprint list | |
print (ft_bones) | |
# [ ] check for deletion of "cuboid" and "navicular" | |
new_bones = ft_bones.find ( "cuboid" , "navicular" ) | |
print (new_bones) |
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
# [ ] pop() and print the first and last items from the ft_bones list | |
ft_bones = ["calcaneus", "talus", "cuboid", "navicular", "lateral cuneiform", | |
"intermediate cuneiform", "medial cuneiform"] | |
ft_bones. pop ( 0 ) | |
ft_bones. pop ( -1 ) | |
# [ ] print the remaining list | |
print ("New list " , ft_bones) |
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
# create a list of purchased item empity | |
purchase_amounts = [ ] | |
#while loop to add item if done is entered stop | |
while True: | |
amounts = input("How much purchase or quit ") | |
purchase_amounts += amounts | |
if amounts.startswith ("q" ): | |
#print the list | |
print(purchase_amounts) | |
break | |
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 and return, using print output, input, if, in (something is in the list)keywords, .append(), .pop(), .remove() list methods. | |
animals = [ "cat" , "dog", "horse" ] | |
new_animals = input ("enter the name of an animal or quit: ") | |
def mammals (the_animals): | |
#delete an animal | |
if new_animals in animals: | |
animals. remove ( new_animals) | |
return "1 instance of" , new_animals, "removed from " , animals | |
# pop an animal | |
elif new_animals == "": | |
popped = animals.pop() | |
return popped, "popped from " , animals | |
#quit function | |
elif new_animals.startswith("q") : | |
return"Goodbye" , animals | |
# append an animal | |
elif new_animals not in animals: | |
animals.append ( new_animals) | |
return "1 instance of " , new_animals , "appended to " , animals | |
animals_a = mammals | |
print(animals_a) |
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 and return, using print output, input, if, in (something is in the list)keywords, .append(), .pop(), .remove() list methods. | |
#list of animals | |
animals = [ "cat" , "dog", "horse" ] | |
new_animals = input ("enter the name of an animal or quit: ") | |
#delete an animal | |
if new_animals in animals: | |
animals. remove ( new_animals) | |
print ("1 instance of" , new_animals, "removed from list") | |
print(animals) | |
# pop an animal | |
elif new_animals == " ": | |
popped = animals.pop() | |
print (popped, "popped from list") | |
print(animals) | |
#quit function | |
elif new_animals.startswith("q") : | |
print ( "Goodbye") | |
print(animals) | |
# append an animal | |
elif new_animals not in animals: | |
animals.append ( new_animals) | |
print ("1 instance of " , new_animals , "appended to list" ) | |
print(animals) | |
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
# create a list of 4 to 6 strings: birds | |
birds = [ "falcon" , "pigeon" , "albatros" , "dove"] | |
# print each bird in the list | |
for bird in birds: | |
print(bird) | |
# [ ] create a list of 7 integers: player_points | |
player_points = [ 8 , 9 , 10 , 11 , 12 , 13 , 14 ,15 ] | |
double_point = 0 | |
# [ ] print double the points for each point value | |
for point in player_points: | |
double_point = point * 2 | |
print(double_point) |
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
# [ ] Using cities, from the example in previous page. iterate through the list using "for"/"in" | |
cities = ["New York", "Shanghai", "Munich", "Tokyo", "Dubai", "Mexico City", "São Paulo", "Hyderabad"] | |
# [ ] sort into lists with "A" in the city name and without "A" in the name: a_city & no_a_city | |
a_city = [ ] | |
no_a_city = [ ] | |
letter = "a" | |
for cities_a in cities: | |
if letter in cities_a: | |
a_city.append(cities_a) | |
else: | |
no_a_city.append(cities_a) | |
print("City with an A" , a_city) | |
print("City without an A" , no_a_city) |
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
#while loop to add animal | |
add_animals = [ ] | |
while True: | |
new_animals = input ("add an animal: ") | |
if new_animals not in add_animals: | |
add_animals.append(new_animals) | |
elif new_animals != new_animals.isalpha (): | |
break | |
print (add_animals) | |
#animal list | |
animals = ["Chimpanzee", "Panther", "Wolf", "Armadillo"] | |
#merge the two list | |
animals.extend(add_animals) | |
print(animals) | |
#merge the list and puted in reversed and sorted | |
animals.reverse() | |
print("reversed " , animals) | |
animals.sort() | |
print("sorted" , animals) |
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
# if, input, def, return, for/in keywords, .lower() and .upper() method, .append, .pop, .split methods, range and len functions | |
def poem (word_list): | |
word_list = input("Please input your poem: ") | |
word_list.split() | |
len(word_list) | |
if word in word_list: | |
word <= str(3) | |
word.lower() | |
return "lower " , word_list | |
if word in word_list: | |
word >= str(7) | |
word.upper() | |
return "upper " , word_list | |
while word_list >= str(5): | |
range(-5,0,1).pop() | |
word_list.pop(-5) | |
new_words.append(word_list) | |
word_list.pop(1) | |
new_words.append(word_list) | |
word_list.pop() | |
new_words.append(word_list) | |
return "popped" , word_list | |
word_list = input("Please input your poem: ") | |
print("lower " , word_list + "\n" + "upper" ,word_list + "\n" +"popped" , word_list ) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment