Skip to content

Commit c07eabf

Browse files
authored
Merge pull request #679 from realpython/type-hint-arrow-samples
Type hint arrow samples
2 parents df6b8e1 + 087c2ca commit c07eabf

8 files changed

+60
-0
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
This folder provides the code examples for the Real Python tutorial [What Does -> Mean in Python Function Definitions?](https://realpython.com/what-does-mean-in-python-function-definitions/)
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
game_stock = {"Dragon Quest": 5, "Final Fantasy": 1, "Age of Empires": 5}
2+
3+
4+
def get_highest_stock_games(game_stock: dict[str, int]) -> dict[str, int]:
5+
max_quantity = max(game_stock.values())
6+
return {
7+
game: quantity
8+
for game, quantity in game_stock.items()
9+
if quantity == max_quantity
10+
}
11+
12+
13+
print(f"Games with the highest stock: {get_highest_stock_games(game_stock)}")
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
my_number = 32
2+
my_number = "32"
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
class Game:
2+
def __init__(self, name: str, genre: str, price: float) -> None:
3+
self.name = name
4+
self.genre = genre
5+
self.price = price
6+
7+
def get_name(self) -> str:
8+
return self.name
9+
10+
def get_genre(self) -> str:
11+
return self.genre
12+
13+
def get_price(self) -> float:
14+
return self.price
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
def get_number_of_titles(titles: list[str]) -> str:
2+
return len(titles)
3+
4+
5+
games = ["Dragon Quest", "Final Fantasy", "Age of Empires"]
6+
print(f"Number of titles: {get_number_of_titles(games)}")
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
def find_keyword_in_titles(titles: list[str], keyword: str) -> None:
2+
print(f"Titles that contain the keyword {keyword}")
3+
for game_title in titles:
4+
if keyword in game_title:
5+
print(f"{game_title}")
6+
7+
8+
games = ["Dragon Quest", "Final Fantasy", "Age of Empires"]
9+
find_keyword_in_titles(games, "Final")
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
def get_number_of_titles(titles: list[str]) -> int:
2+
return len(titles)
3+
4+
5+
games = ["Dragon Quest", "Final Fantasy", "Age of Empires"]
6+
print(f"Number of titles: {get_number_of_titles(games)}")
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import random
2+
3+
4+
def get_game_recommendation(titles: list[str]) -> str:
5+
return random.choice(titles)
6+
7+
8+
games = ["Dragon Quest", "Final Fantasy", "Age of Empires"]
9+
print(f"Random recommendation: {get_game_recommendation(games)}")

0 commit comments

Comments
 (0)