Python Crash Course: A Hands-On, Project-Based Introduction to Programming
Rate it:
29%
Flag icon
Sometimes you’ll want to prevent a function from modifying a
30%
Flag icon
You can send a copy of a list to a function like this:
30%
Flag icon
function_name(list_name[:])
30%
Flag icon
The slice notation [:]
30%
Flag icon
def make_pizza(*toppings):
30%
Flag icon
def build_profile(first, last, **user_info):
30%
Flag icon
follow. You can go a step further by storing your functions in a separate file called a module and then importing that module into your main
30%
Flag icon
A module is a file ending in .py
31%
Flag icon
Importing Specific Functions
31%
Flag icon
from module_name import function_name
31%
Flag icon
Using as to Give a Function an Alias
31%
Flag icon
If the name of a function you’re importing might conflict with an existing name in your program or
31%
Flag icon
Using as to Give a Module an Alias
31%
Flag icon
Making an object from a class is called instantiation,
32%
Flag icon
Creating the Dog Class
32%
Flag icon
➌     def __init__(self, name, age):            """Initialize name and age
32%
Flag icon
__init__() Method
32%
Flag icon
When Python reads this line, it calls the __init__() method
33%
Flag icon
➊         self.odometer_reading = 0
34%
Flag icon
the class you’re writing is a specialized version of another class you
34%
Flag icon
inherits from another,
34%
Flag icon
➊ class Car():        """A simple attempt to represent a car."""
34%
Flag icon
class ElectricCar(Car):
34%
Flag icon
super().__init__(make, model, year)
34%
Flag icon
The name of the parent class must be included in parentheses in the definition of the child class.
34%
Flag icon
The super() function at ➍ is a special function that helps Python make connections between the parent and child class.
34%
Flag icon
The name super comes from a convention of calling the parent class a superclass
34%
Flag icon
class Car(object):
34%
Flag icon
class ElectricCar(Car):
34%
Flag icon
def __init__(self, make, model, year):
34%
Flag icon
super(ElectricCar, self).__init__(make...
This highlight has been truncated due to consecutive passage length restrictions.
34%
Flag icon
super() function needs two arguments: a reference to the child class and the self object.
34%
Flag icon
make proper connections between the parent and child classes.
34%
Flag icon
object syntax as well.
34%
Flag icon
Overriding
34%
Flag icon
def fill_gas_tank(self):         """Electric cars don't have gas tanks."""         print("This car doesn't need a gas tank!")
34%
Flag icon
the ElectricCar class, we might notice that we’re adding many attributes and methods specific to the car’s battery.
35%
Flag icon
Importing Classes
35%
Flag icon
from car import Car
36%
Flag icon
Storing Multiple Classes in a Module You can store as many classes as you need in a single module, although each class in a module should be related somehow.
36%
Flag icon
from car import Car, ElectricCar
36%
Flag icon
Importing an Entire Module
36%
Flag icon
➊ import car
36%
Flag icon
from module_name import *
36%
Flag icon
When you’re starting out, keep your code structure simple. Try doing everything in one file and moving your classes to separate modules once everything is working.
36%
Flag icon
The Python Standard Library
36%
Flag icon
Python standard library is a set of modules included with every Python installation.
37%
Flag icon
Styling Classes
37%
Flag icon
in CamelCaps.
37%
Flag icon
with open('pi_digits.txt') as file_object: