Skip to content

Learning Python: Day 28

Inheritance The __init__() method for a child class electric_car.py class Car: ….”””A simple attempt to represent a car.””” ….def __init__(self, make, model, year): ……..”””Initialize attributes to describe a car.””” ……..self.make = make ……..self.model = model ……..self.year = year ……..self.odometer_reading =… Read More »Learning Python: Day 28

Learning Python: Day 27

Modifying Attribute Values Modifying an attribute’s value directly class Car: ….–snip– my_new_car = Car(‘audi’, ‘a4’, 2024) print(my_new_car.get_descriptive_name()) my_new_car.odometer_reading = 23 my_new_car.read_odometer() 2024 Audi A4 This car has 23 miles on it. Modifying an attribute’s value through a method class Car:… Read More »Learning Python: Day 27

Learning Python: Day 26

Setting a default value for an attribute class Car: ….def __init__(self, make, model, year): ……..”””Initialize attributes to describe a car.””” ……..self.make = make ……..self.model = model ……..self.year = year ……..self.odometer_reading = 0 ….def get_descriptive_name(self): ……..–snip– ….def read_odometer(self): ……..”””Print a statement… Read More »Learning Python: Day 26

Learning Python: Day 25

Working with classes and instances The car class car.py class Car: ….”””A simple attempt to represent a car.””” def __init__(self, make, model, year): ….”””Initialize attributes to describe a car.””” ….self.make = make ….self.model = model ….self.year = year def get_descriptive_name(self):… Read More »Learning Python: Day 25

Learning Python: Day 24

The __init__() method Making an instance from a class class Dog: ….–snip– my_dog = Dog(‘Willie’, 6) print(f”My dog’s name is {my_dog.name}.”) print(f”My dog is {my_dog.age} years old.”) Accessing attributes my_dog.name my_dog: My dog’s name is Willie. My dog is 6… Read More »Learning Python: Day 24

Learning Python: Day 23

Chapter 9 – Classes Creating and using a class Creating the dog class dog.py class Dog: ….”””A simple attempt to model a dog.””” ….def_init_(self, name, age): ……..”””Initialize name and age attributes.””” ……..self.name = name ……..self.age = age ….def sit(self): ……..”””Simulate… Read More »Learning Python: Day 23

Learning Python: Day 22

Importing specific functions from module_name import function_name from module_name import function_0, function_1, function_2 from pizza import make_pizza make_pizza(16, ‘pepperoni’) make_pizza(12, ‘mushrooms’, ‘green peppers’, ‘extra cheese’) Using as to give a function an alias from pizza import make_pizza as mp mp(16,… Read More »Learning Python: Day 22

Learning Python: Day 21

Storing your functions in modules Importing an entire module pizza.py def make_pizza(size, *toppings): ….”””Summarize the pizza we are about to make.””” ….print(f”\nMaking a {size}-inch pizza with the following toppings:”) ….for topping in toppings: ……..print(f”-{topping}”) making_pizzas.py import pizza pizza.make_pizza(16, ‘pepperoni’) pizza.make_pizza(12,… Read More »Learning Python: Day 21

Learning Python: Day 20

Passing an arbitrary number of arguments pizza.py def make_pizza(*toppings): ….”””Print the list of toppings that have been requested.””” ….print(toppings) make_pizza(‘pepperoni’) make_pizza(‘mushrooms’, ‘green peppers’, ‘extra cheese’) (‘pepperoni’,) (‘mushrooms’, ‘green peppers’, ‘extra cheese’) def make_pizza(*toppings): …”””Summarize the pizza we are about to… Read More »Learning Python: Day 20

Learning Python: Day 19

Chapter 8 – Functions Defining a function greeter.py def greet_user(): ….”””Display a simple greeting.””” ….print*”Hello!”) greet_user() Passing information to a function def greet_user(username): ….”””Display a simple greeting.””” ….print(f”Hello, {username.title()}!””) greet_user(‘jesse’) Hello, Jesse! Arguments and parameters Passing arguments Positional arguments pets.py… Read More »Learning Python: Day 19