This file contains hidden or 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
L = [[5, 8, 7], ['hello', 'hi', 'hola'], [6.6, 1.54, 3.99], ['small', 'large']] | |
# Test if 'hola' is in the list L. Save to variable name test1 | |
if 'hola' in L: | |
test1 = True | |
else: | |
test1 = False | |
# Test if [5, 8, 7] is in the list L. Save to variable name test2 | |
if [5,8,7] in L: |
This file contains hidden or 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
lst = [['apple', 'orange', 'banana'], [5, 6, 7, 8, 9.9, 10], ['green', 'yellow', 'purple', 'red']] | |
#Test to see if 'yellow' is in the third list of lst. Save to variable ``yellow`` | |
if 'yellow' in lst[2]: | |
yellow = True | |
else: | |
yellow = False | |
#Test to see if 4 is in the second list of lst. Save to variable ``four`` |
This file contains hidden or 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
nested = [['dog', 'cat', 'horse'], ['frog', 'turtle', 'snake', 'gecko'], ['hamster', 'gerbil', 'rat', 'ferret']] | |
output = nested[1][2] |
This file contains hidden or 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 length(lst): | |
if len(lst) >= 5: | |
return("Longer than 5") | |
else: | |
return("Less than 5") |
This file contains hidden or 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
# Write a function called change that takes any string, adds “Nice to meet you!” to the end of the argument given, and returns that new string. | |
def change(string): | |
Str = "{}Nice to meet you!".format(string) | |
return Str |
This file contains hidden or 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
#Write code to find out how many lines are in the file emotion_words.txt as shown above. Save this value to the variable num_lines. Do not use the len method. | |
file = open("emotion_word.txt","r") | |
count = 0 | |
for aline in file.readlines(): | |
count += 1 | |
num_lines = count |
This file contains hidden or 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
#Find the number of lines in the file, travel_plans2.txt, and assign it to the variable num_lines. | |
file = open("travel_plans2.txt","r") | |
count = file.readlines() | |
num_lines = len(count) |
This file contains hidden or 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 string called first_forty that is comprised of the first 40 characters of emotion_words2.txt. | |
file = open("emotion_words2.txt","r") | |
const = file.read() | |
first_forty = const[:40] |
This file contains hidden or 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 the file school_prompt2.txt, find the number of characters in the file and assign that value to the variable num_char. | |
fil = open("school_prompt2.txt","r") | |
consts = fil.read() | |
num_char = len(consts) | |
fil.close() |
This file contains hidden or 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
# wap to read last n files from a file | |
f = open("output.txt", "r") | |
n = int(input("Enter No. of lines:")) | |
lst = [] | |
for x in f: | |
l1 = f.readlines() | |
lst = lst +l1 | |
print(lst[n:]) | |
f.close() |