class Rocket(): # Rocket simulates a rocket ship for a game, # or a physics simulation. def __init__(self): # Each rocket has an (x,y) position. self.x = 0 self.y = 0 ###highlight=[11,12,13] class Rocket(): # Rocket simulates a rocket ship for a game, # or a physics simulation. def __init__(self): # Each rocket has an (x,y) position. self.x = 0 self.y = 0 def move_up(self): # Increment the y-position of the rocket. self.y += 1 ###highlight=[15,16, 17] class Rocket(): # Rocket simulates a rocket ship for a game, # or a physics simulation. def __init__(self): # Each rocket has an (x,y) position. self.x = 0 self.y = 0 def move_up(self): # Increment the y-position of the rocket. self.y += 1 # Create a Rocket object. my_rocket = Rocket() print(my_rocket) ###highlight=[15,16,17,18,19,20,21,22,23] class Rocket(): # Rocket simulates a rocket ship for a game, # or a physics simulation. def __init__(self): # Each rocket has an (x,y) position. self.x = 0 self.y = 0 def move_up(self): # Increment the y-position of the rocket. self.y += 1 # Create a Rocket object, and have it start to move up. my_rocket = Rocket() print("Rocket altitude:", my_rocket.y) my_rocket.move_up() print("Rocket altitude:", my_rocket.y) my_rocket.move_up() print("Rocket altitude:", my_rocket.y) ###highlight=[15,16,17,18,19,20,21,22,23] class Rocket(): # Rocket simulates a rocket ship for a game, # or a physics simulation. def __init__(self): # Each rocket has an (x,y) position. self.x = 0 self.y = 0 def move_up(self): # Increment the y-position of the rocket. self.y += 1 # Create a fleet of 5 rockets, and store them in a list. my_rockets = [] for x in range(0,5): new_rocket = Rocket() my_rockets.append(new_rocket) # Show that each rocket is a separate object. for rocket in my_rockets: print(rocket) ###highlight=[16] class Rocket(): # Rocket simulates a rocket ship for a game, # or a physics simulation. def __init__(self): # Each rocket has an (x,y) position. self.x = 0 self.y = 0 def move_up(self): # Increment the y-position of the rocket. self.y += 1 # Create a fleet of 5 rockets, and store them in a list. my_rockets = [Rocket() for x in range(0,5)] # Show that each rocket is a separate object. for rocket in my_rockets: print(rocket) ###highlight=[18,19,20,21,22,23] class Rocket(): # Rocket simulates a rocket ship for a game, # or a physics simulation. def __init__(self): # Each rocket has an (x,y) position. self.x = 0 self.y = 0 def move_up(self): # Increment the y-position of the rocket. self.y += 1 # Create a fleet of 5 rockets, and store them in a list. my_rockets = [Rocket() for x in range(0,5)] # Move the first rocket up. my_rockets[0].move_up() # Show that only the first rocket has moved. for rocket in my_rockets: print("Rocket altitude:", rocket.y) class Rocket(): # Rocket simulates a rocket ship for a game, # or a physics simulation. def __init__(self): # Each rocket has an (x,y) position. self.x = 0 self.y = 0 ###highlight=[11,12,13] class Rocket(): # Rocket simulates a rocket ship for a game, # or a physics simulation. def __init__(self): # Each rocket has an (x,y) position. self.x = 0 self.y = 0 def move_up(self): # Increment the y-position of the rocket. self.y += 1 ###highlight=[15,16,17,18,19,20,21,22,23] class Rocket(): # Rocket simulates a rocket ship for a game, # or a physics simulation. def __init__(self): # Each rocket has an (x,y) position. self.x = 0 self.y = 0 def move_up(self): # Increment the y-position of the rocket. self.y += 1 # Create a Rocket object, and have it start to move up. my_rocket = Rocket() print("Rocket altitude:", my_rocket.y) my_rocket.move_up() print("Rocket altitude:", my_rocket.y) my_rocket.move_up() print("Rocket altitude:", my_rocket.y) ###highlight=[15,16,17,18,19,20,21,22,23] class Rocket(): # Rocket simulates a rocket ship for a game, # or a physics simulation. def __init__(self): # Each rocket has an (x,y) position. self.x = 0 self.y = 0 def move_up(self): # Increment the y-position of the rocket. self.y += 1 # Create a fleet of 5 rockets, and store them in a list. my_rockets = [] for x in range(0,5): new_rocket = Rocket() my_rockets.append(new_rocket) # Show that each rocket is a separate object. for rocket in my_rockets: print(rocket) ###highlight=[15,16,17,18] class Rocket(): # Rocket simulates a rocket ship for a game, # or a physics simulation. def __init__(self): # Each rocket has an (x,y) position. self.x = 0 self.y = 0 def move_up(self): # Increment the y-position of the rocket. self.y += 1 # Create a fleet of 5 rockets, and store them in a list. my_rockets = [] for x in range(0,5): my_rockets.append(Rocket()) # Show that each rocket is a separate object. for rocket in my_rockets: print(rocket) ###highlight=[6,7,8,9] class Rocket(): # Rocket simulates a rocket ship for a game, # or a physics simulation. def __init__(self): # Each rocket has an (x,y) position. self.x = 0 self.y = 0 def move_up(self): # Increment the y-position of the rocket. self.y += 1 ###highlight=[6,7,8,9] class Rocket(): # Rocket simulates a rocket ship for a game, # or a physics simulation. def __init__(self, x=0, y=0): # Each rocket has an (x,y) position. self.x = x self.y = y def move_up(self): # Increment the y-position of the rocket. self.y += 1 ###highlight=[15,16,17,18,19,20,21,22,23] class Rocket(): # Rocket simulates a rocket ship for a game, # or a physics simulation. def __init__(self, x=0, y=0): # Each rocket has an (x,y) position. self.x = x self.y = y def move_up(self): # Increment the y-position of the rocket. self.y += 1 # Make a series of rockets at different starting places. rockets = [] rockets.append(Rocket()) rockets.append(Rocket(0,10)) rockets.append(Rocket(100,0)) # Show where each rocket is. for index, rocket in enumerate(rockets): print("Rocket %d is at (%d, %d)." % (index, rocket.x, rocket.y)) ###highlight=[11,12,13,14,15] class Rocket(): # Rocket simulates a rocket ship for a game, # or a physics simulation. def __init__(self, x=0, y=0): # Each rocket has an (x,y) position. self.x = x self.y = y def move_rocket(self, x_increment=0, y_increment=1): # Move the rocket according to the paremeters given. # Default behavior is to move the rocket up one unit. self.x += x_increment self.y += y_increment ###highlight=[17,18,19,20,21,22,23,24,25,26,27] class Rocket(): # Rocket simulates a rocket ship for a game, # or a physics simulation. def __init__(self, x=0, y=0): # Each rocket has an (x,y) position. self.x = x self.y = y def move_rocket(self, x_increment=0, y_increment=1): # Move the rocket according to the paremeters given. # Default behavior is to move the rocket up one unit. self.x += x_increment self.y += y_increment # Create three rockets. rockets = [Rocket() for x in range(0,3)] # Move each rocket a different amount. rockets[0].move_rocket() rockets[1].move_rocket(10,10) rockets[2].move_rocket(-10,0) # Show where each rocket is. for index, rocket in enumerate(rockets): print("Rocket %d is at (%d, %d)." % (index, rocket.x, rocket.y)) ###highlight=[19,20,21,22,23,24,25,26,27,28,29,30,31] from math import sqrt class Rocket(): # Rocket simulates a rocket ship for a game, # or a physics simulation. def __init__(self, x=0, y=0): # Each rocket has an (x,y) position. self.x = x self.y = y def move_rocket(self, x_increment=0, y_increment=1): # Move the rocket according to the paremeters given. # Default behavior is to move the rocket up one unit. self.x += x_increment self.y += y_increment def get_distance(self, other_rocket): # Calculates the distance from this rocket to another rocket, # and returns that value. distance = sqrt((self.x-other_rocket.x)**2+(self.y-other_rocket.y)**2) return distance # Make two rockets, at different places. rocket_0 = Rocket() rocket_1 = Rocket(10,5) # Show the distance between them. distance = rocket_0.get_distance(rocket_1) print("The rockets are %f units apart." % distance) ###highlight=[25,26,27,28,29,30,31,32,33,34] from math import sqrt class Rocket(): # Rocket simulates a rocket ship for a game, # or a physics simulation. def __init__(self, x=0, y=0): # Each rocket has an (x,y) position. self.x = x self.y = y def move_rocket(self, x_increment=0, y_increment=1): # Move the rocket according to the paremeters given. # Default behavior is to move the rocket up one unit. self.x += x_increment self.y += y_increment def get_distance(self, other_rocket): # Calculates the distance from this rocket to another rocket, # and returns that value. distance = sqrt((self.x-other_rocket.x)**2+(self.y-other_rocket.y)**2) return distance class Shuttle(Rocket): # Shuttle simulates a space shuttle, which is really # just a reusable rocket. def __init__(self, x=0, y=0, flights_completed=0): super().__init__(x, y) self.flights_completed = flights_completed shuttle = Shuttle(10,0,3) print(shuttle) class NewClass(ParentClass): ###highlight=[5] class NewClass(ParentClass): def __init__(self, arguments_new_class, arguments_parent_class): super().__init__(arguments_parent_class) # Code for initializing an object of the new class. ###highlight=[7] class Shuttle(Rocket): # Shuttle simulates a space shuttle, which is really # just a reusable rocket. def __init__(self, x=0, y=0, flights_completed=0): Rocket.__init__(self, x, y) self.flights_completed = flights_completed ###highlight=[3, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65] from math import sqrt from random import randint class Rocket(): # Rocket simulates a rocket ship for a game, # or a physics simulation. def __init__(self, x=0, y=0): # Each rocket has an (x,y) position. self.x = x self.y = y def move_rocket(self, x_increment=0, y_increment=1): # Move the rocket according to the paremeters given. # Default behavior is to move the rocket up one unit. self.x += x_increment self.y += y_increment def get_distance(self, other_rocket): # Calculates the distance from this rocket to another rocket, # and returns that value. distance = sqrt((self.x-other_rocket.x)**2+(self.y-other_rocket.y)**2) return distance class Shuttle(Rocket): # Shuttle simulates a space shuttle, which is really # just a reusable rocket. def __init__(self, x=0, y=0, flights_completed=0): super().__init__(x, y) self.flights_completed = flights_completed # Create several shuttles and rockets, with random positions. # Shuttles have a random number of flights completed. shuttles = [] for x in range(0,3): x = randint(0,100) y = randint(1,100) flights_completed = randint(0,10) shuttles.append(Shuttle(x, y, flights_completed)) rockets = [] for x in range(0,3): x = randint(0,100) y = randint(1,100) rockets.append(Rocket(x, y)) # Show the number of flights completed for each shuttle. for index, shuttle in enumerate(shuttles): print("Shuttle %d has completed %d flights." % (index, shuttle.flights_completed)) print("\n") # Show the distance from the first shuttle to all other shuttles. first_shuttle = shuttles[0] for index, shuttle in enumerate(shuttles): distance = first_shuttle.get_distance(shuttle) print("The first shuttle is %f units away from shuttle %d." % (distance, index)) print("\n") # Show the distance from the first shuttle to all other rockets. for index, rocket in enumerate(rockets): distance = first_shuttle.get_distance(rocket) print("The first shuttle is %f units away from rocket %d." % (distance, index)) ###highlight=[5] class NewClass(ParentClass): def __init__(self, arguments_new_class, arguments_parent_class): super(NewClass, self).__init__(arguments_parent_class) # Code for initializing an object of the new class. ###highlight=[25,26,27,28,29,30,31,32,33,34] from math import sqrt class Rocket(): # Rocket simulates a rocket ship for a game, # or a physics simulation. def __init__(self, x=0, y=0): # Each rocket has an (x,y) position. self.x = x self.y = y def move_rocket(self, x_increment=0, y_increment=1): # Move the rocket according to the paremeters given. # Default behavior is to move the rocket up one unit. self.x += x_increment self.y += y_increment def get_distance(self, other_rocket): # Calculates the distance from this rocket to another rocket, # and returns that value. distance = sqrt((self.x-other_rocket.x)**2+(self.y-other_rocket.y)**2) return distance class Shuttle(Rocket): # Shuttle simulates a space shuttle, which is really # just a reusable rocket. def __init__(self, x=0, y=0, flights_completed=0): super(Shuttle, self).__init__(x, y) self.flights_completed = flights_completed shuttle = Shuttle(10,0,3) print(shuttle) ###highlight=[2] # Save as rocket.py from math import sqrt class Rocket(): # Rocket simulates a rocket ship for a game, # or a physics simulation. def __init__(self, x=0, y=0): # Each rocket has an (x,y) position. self.x = x self.y = y def move_rocket(self, x_increment=0, y_increment=1): # Move the rocket according to the paremeters given. # Default behavior is to move the rocket up one unit. self.x += x_increment self.y += y_increment def get_distance(self, other_rocket): # Calculates the distance from this rocket to another rocket, # and returns that value. distance = sqrt((self.x-other_rocket.x)**2+(self.y-other_rocket.y)**2) return distance ###highlight=[2] # Save as rocket_game.py from rocket import Rocket rocket = Rocket() print("The rocket is at (%d, %d)." % (rocket.x, rocket.y)) ###highlight=[27,28,29,30,31,32,33] # Save as rocket.py from math import sqrt class Rocket(): # Rocket simulates a rocket ship for a game, # or a physics simulation. def __init__(self, x=0, y=0): # Each rocket has an (x,y) position. self.x = x self.y = y def move_rocket(self, x_increment=0, y_increment=1): # Move the rocket according to the paremeters given. # Default behavior is to move the rocket up one unit. self.x += x_increment self.y += y_increment def get_distance(self, other_rocket): # Calculates the distance from this rocket to another rocket, # and returns that value. distance = sqrt((self.x-other_rocket.x)**2+(self.y-other_rocket.y)**2) return distance class Shuttle(Rocket): # Shuttle simulates a space shuttle, which is really # just a reusable rocket. def __init__(self, x=0, y=0, flights_completed=0): super().__init__(x, y) self.flights_completed = flights_completed ###highlight=[3,8,9,10] # Save as rocket_game.py from rocket import Rocket, Shuttle rocket = Rocket() print("The rocket is at (%d, %d)." % (rocket.x, rocket.y)) shuttle = Shuttle() print("\nThe shuttle is at (%d, %d)." % (shuttle.x, shuttle.y)) print("The shuttle has completed %d flights." % shuttle.flights_completed) from module_name import ClassName # Save as rocket_game.py import rocket rocket_0 = rocket.Rocket() print("The rocket is at (%d, %d)." % (rocket_0.x, rocket_0.y)) shuttle_0 = rocket.Shuttle() print("\nThe shuttle is at (%d, %d)." % (shuttle_0.x, shuttle_0.y)) print("The shuttle has completed %d flights." % shuttle_0.flights_completed) import module_name module_name.ClassName import module_name as local_module_name # Save as rocket_game.py import rocket as rocket_module rocket = rocket_module.Rocket() print("The rocket is at (%d, %d)." % (rocket.x, rocket.y)) shuttle = rocket_module.Shuttle() print("\nThe shuttle is at (%d, %d)." % (shuttle.x, shuttle.y)) print("The shuttle has completed %d flights." % shuttle.flights_completed) import rocket as rocket_module import rocket as r from module_name import * # Save as multiplying.py def double(x): return 2*x def triple(x): return 3*x def quadruple(x): return 4*x ###highlight=[2] from multiplying import double, triple, quadruple print(double(5)) print(triple(5)) print(quadruple(5)) ###highlight=[2] import multiplying print(multiplying.double(5)) print(multiplying.triple(5)) print(multiplying.quadruple(5)) ###highlight=[2] import multiplying as m print(m.double(5)) print(m.triple(5)) print(m.quadruple(5)) ###highlight=[2] from multiplying import * print(double(5)) print(triple(5)) print(quadruple(5)) # this import sys import os # not this import sys, os from rocket import Rocket, Shuttle