You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

194 lines
4.5 KiB

{
"cells": [
{
"cell_type": "markdown",
"id": "b4fcc94e-6e57-450e-8de7-b757834b6d9f",
"metadata": {},
"source": [
"### Here's a class with an `__init__` thingy and a method"
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "f2c48975-7736-4f72-9e47-7c9df5b534df",
"metadata": {
"editable": true,
"slideshow": {
"slide_type": ""
},
"tags": []
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"This is an oldtimer.\n"
]
}
],
"source": [
"class Car:\n",
" def __init__(self, brand, year):\n",
" self.brand = brand\n",
" self.year = year\n",
"\n",
" currentYear = 2025\n",
"\n",
" def isOldTimer(self):\n",
" age = 2025 - self.year\n",
" if(age > 30):\n",
" print(\"This is an oldtimer.\")\n",
" else:\n",
" print(\"This isn't an oldtimer yet.\")\n",
"\n",
"myCar = Car(\"Bentley\", 1967)\n",
"\n",
"myCar.isOldTimer()"
]
},
{
"cell_type": "markdown",
"id": "7dc40be0-a3af-49cf-93e4-14134c75325a",
"metadata": {
"editable": true,
"slideshow": {
"slide_type": ""
},
"tags": []
},
"source": [
"### Here's what I learned today about `yield`, _comprehension_, and _sets_"
]
},
{
"cell_type": "code",
"execution_count": 4,
"id": "f726fc34-8b87-482a-9100-05d26e2853db",
"metadata": {},
"outputs": [],
"source": [
"bentley = {\"brand\": \"Bentley\", \"category\": \"sporty luxury vehicles\"}\n",
"volkswagen = {\"brand\": \"Volkswagen\", \"category\": \"lackluster utilitarian vehicles\"}\n",
"jaguar = {\"brand\": \"Jaguar\", \"category\": \"sporty luxury vehicles\"}\n",
"koenig = {\"brand\": \"Koenigsegg\"}\n",
"default = {\"category\": \"default vehicle\"}\n",
"\n",
"cars = [bentley, volkswagen, jaguar, koenig]\n",
"\n",
"#A 'comprehension' is a shorthand for defining lists, sets, dictionaries, and tuples\n",
"brands = [car.get(\"brand\") for car in cars if car.get(\"brand\")]\n",
"\n",
"#Here's a comprehension for a set. A set is like a list, but unordered, and it can only have unique values\n",
"categories = {car.get(\"category\") for car in cars if car.get(\"category\")}"
]
},
{
"cell_type": "code",
"execution_count": 5,
"id": "c94a579a-7229-4d19-b445-d70b20dbc731",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"['Bentley', 'Volkswagen', 'Jaguar', 'Koenigsegg']\n"
]
}
],
"source": [
"print(brands)"
]
},
{
"cell_type": "code",
"execution_count": 6,
"id": "1d99825f-4e1c-4846-bc44-3001ea85df75",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"{'lackluster utilitarian vehicles', 'sporty luxury vehicles'}\n"
]
}
],
"source": [
"print(categories)"
]
},
{
"cell_type": "code",
"execution_count": 10,
"id": "59fd4d0b-c9de-44a9-8205-8b8353940481",
"metadata": {
"editable": true,
"slideshow": {
"slide_type": ""
},
"tags": []
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Bentley\n",
"Jaguar\n",
"Koenigsegg\n"
]
}
],
"source": [
"#'yield' is comparable to 'return' with the difference that it doesn't load entire lists to memory\n",
"#btw, 'from' allows for a more condensed way of a 'for x in y' statement\n",
"import time\n",
"\n",
"def listBrands():\n",
" yield from [brand for brand in brands if not brand.startswith('V')]\n",
"\n",
"for brand in listBrands():\n",
" print(brand)\n",
" time.sleep(1)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "6c52158e-1786-4638-a7c7-add61d932459",
"metadata": {
"editable": true,
"slideshow": {
"slide_type": ""
},
"tags": []
},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.11.11"
}
},
"nbformat": 4,
"nbformat_minor": 5
}