write-only mode ("w")read-only mode ("r")read and write mode ("r+")append mode ("a"), which adds any new data you write to the file to the end of the file. my_file = open("output.txt", "r")print my_file.read()my_file.close() my_file = open("text.txt", "r")print my_file.readline()print my_file.readline()print my_file.readline()my_file.close() with open("text.txt", "w") as textfile: textfile.write..
class Fruit(object): """A class that makes various tasty fruits.""" def __init__(self, name, color, flavor, poisonous): self.name = name self.color = color self.flavor = flavor self.poisonous = poisonous def description(self): print "I'm a %s %s and I taste %s." % (self.color, self.name, self.flavor) def is_edible(self): if not self.poisonous: print "Yep! I'm edible." else: print "Don't eat me! ..
garbled = "IXXX aXXmX aXXXnXoXXXXXtXhXeXXXXrX sXXXXeXcXXXrXeXt mXXeXsXXXsXaXXXXXXgXeX!XX" message = filter(lambda x: x!="X", garbled) print message #I am another secret message!letters = ['A', 'B', 'C', 'D', 'E'] print letters[::-1] #Output: ['E', 'D', 'C', 'B', 'A']
grades = [100, 100, 90, 40, 80, 100, 85, 70, 90, 65, 90, 85, 50.5] def print_grades(grades_input): for grade in grades_input: print grade def grades_sum(scores): total = 0 for score in scores: total += score return total def grades_average(grades_input): sum_of_grades = grades_sum(grades_input) average = sum_of_grades / float(len(grades_input)) return average def grades_variance(scores): average..
count = 0 while count = 10: break // from random import randint # Generates a number from 1 through 10 inclusive random_number = randint(1, 10) guesses_left = 3 # Start your game! while guesses_left > 0: guess = int(raw_input("Your guess: ")) if guess == random_number: print "Yo..
range(6) # => [0, 1, 2, 3, 4, 5] range(1, 6) # => [1, 2, 3, 4, 5] range(1, 6, 3) # => [1, 4] for loop Method 1 - for item in list:for item in list: print itemMethod 2 - iterate through indexes:for i in range(len(list)): print list[i] nested n = [[1, 2, 3], [4, 5, 6, 7, 8, 9]] # Add your function here def flatten(lists): result = [] for numbers in lists: for number in numbers: result.append(numbe..
get score letter fromstudent list lloyd = { "name": "Lloyd", "homework": [90.0, 97.0, 75.0, 92.0], "quizzes": [88.0, 40.0, 94.0], "tests": [75.0, 90.0] } alice = { "name": "Alice", "homework": [100.0, 92.0, 98.0, 100.0], "quizzes": [82.0, 83.0, 91.0], "tests": [89.0, 97.0] } tyler = { "name": "Tyler", "homework": [0.0, 87.0, 75.0, 22.0], "quizzes": [0.0, 75.0, 78.0], "tests": [100.0, 100.0] } # ..
Cut Listsuitcase = ["sunglasses", "hat", "passport", "laptop", "suit", "shoes"] # The first and second items (index zero and one) first = suitcase[0:2] # Third and fourth items (index two and three) middle = suitcase[2:4] # The last two items (index four and five) last = suitcase[4:6] Cut stringanimals = "catdogfrog" # The first three characters of animals cat = animals[:3] # The fourth through ..
def speak(message): return message if happy(): speak("I'm happy!") elif sad(): speak("I'm sad.") else: speak("I don't know what I'm feeling.") import math print math.sqrt(13689) def is_numeric(num): return type(num) == int or type(num) == float: max(2, 3, 4) # 4 min(2, 3, 4) # 2 abs(2) # 2 abs(-2) # 2 def hotel_cost (nights): return 140 * nights def plane_ride_cost(city): if city == "Charlotte":..