Skip to content

Commit 878eb1d

Browse files
authored
Merge branch 'master' into python-copy-update
2 parents 825fe1a + 13dd736 commit 878eb1d

32 files changed

+465
-3
lines changed

python-break/README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# How to Exit Loops Early With the Python Break Keyword
2+
3+
This folder provides the code examples for the Real Python tutorial [How to Exit Loops Early With the Python Break Keyword](https://realpython.com/python-break/).
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
for number in range(10):
2+
if number > 5:
3+
break
4+
print(number)
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
for index in range(6):
2+
if index % 2 == 0:
3+
continue
4+
print(index)
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
scores = [90, 30, 50, 70, 85, 35]
2+
3+
num_failed_scores = 0
4+
failed_score = 60
5+
for score in scores:
6+
if score < failed_score:
7+
num_failed_scores += 1
8+
print(f"Number of failed tests: {num_failed_scores}")
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
scores = [90, 30, 50, 70, 85, 35]
2+
3+
num_failed_scores = 0
4+
failed_score = 60
5+
needs_tutoring = "No"
6+
for score in scores:
7+
if score < failed_score:
8+
num_failed_scores += 1
9+
if num_failed_scores >= 2:
10+
needs_tutoring = "Yes"
11+
break
12+
13+
print(f"Does the student need tutoring? {needs_tutoring}")
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
scores = [[90, 30, 80, 100], [100, 80, 95, 87], [75, 84, 77, 50]]
2+
3+
failed_score = 60
4+
num_failed_students = 0
5+
for student_score_list in scores:
6+
for score in student_score_list:
7+
if score < failed_score:
8+
num_failed_students += 1
9+
break
10+
11+
print(f"Number of students who failed a test: {num_failed_students}")

python-break/user_input_example.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import random
2+
3+
guesses_left = 4
4+
random_number = random.randint(1, 10)
5+
6+
while True:
7+
if guesses_left <= 0:
8+
print(
9+
f"You ran out of guesses! The correct number was {random_number}"
10+
)
11+
break
12+
guess = input("Guess a number between 1 and 10, or enter q to quit: ")
13+
if guess == "q":
14+
print("Successfully exited game.")
15+
break
16+
elif not (guess.isnumeric()):
17+
print("Please enter a valid value.")
18+
elif int(guess) == random_number:
19+
print("Congratulations, you picked the correct number!")
20+
break
21+
else:
22+
print("Sorry, your guess was incorrect.")
23+
guesses_left -= 1
24+
print(f"You have {guesses_left} guesses left.")

python-dict-attribute/README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# Using Python's `.__dict__` to Work With Attributes
2+
3+
This folder provides the code examples for the Real Python tutorial [Using Python's `.__dict__` to Work With Attributes](https://realpython.com/python-dict-attribute/).
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
class Parent:
2+
def __init__(self):
3+
self.parent_attr = "parent"
4+
5+
6+
class Child(Parent):
7+
def __init__(self):
8+
super().__init__()
9+
self.child_attr = "child"
10+
11+
12+
parent = Parent()
13+
print(parent.__dict__)
14+
15+
child = Child()
16+
print(child.__dict__)

python-dict-attribute/config_v1.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
class Config:
2+
def __init__(self, **kwargs):
3+
self.__dict__.update(kwargs)
4+
5+
update = __init__
6+
7+
def __str__(self):
8+
return str(self.__dict__)
9+
10+
11+
config = Config(theme="light", font_size=12, language="English")
12+
print(config)
13+
14+
user = {"theme": "dark", "font_size": 14, "language": "Spanish"}
15+
config.update(**user)
16+
print(config)

0 commit comments

Comments
 (0)