Image: McGeddon, CC BY-SA 4.0
total = 0
for power in range(64):
total += 2 ** power
print(total)
import math
print(math.factorial(1000))
t = 1_800_000_000
print(t)
print(t + 0.1)
print(t + 0.000000001)
from decimal import Decimal
t = Decimal(1_800_000_000)
print(t)
print(t + Decimal("0.1"))
print(t + Decimal("0.000000001"))
a, b = 1, 2
a, b = b, a
if "@" in email:
send_email(email)
text = Path("notes.txt").read_text()
if 4 <= age <= 99:
print("This LEGO set is for you")
exists = any(user.email == email for user in users)
total = 0
for power in range(64):
total += 2 ** power
print(total)
print(sum(2 ** power for power in range(64)))
users = [
User(id=1, name="Andrey Gein"),
User(id=2, name="Ada Lovelace"),
User(id=3, name="Guido van Rossum"),
]
names_by_id = {user.id: user.name for user in users}
scores = {
"Alice": 82,
"Bob": 47,
"Charlie": 91,
"Diana": 58,
}
passed = {
name: score
for name, score in scores.items()
if score >= 80
}
print(passed)
Introduced in Python 3.6, 25 years after Python first appeared
Heavily improved in Python 3.12, three years ago
completed = 73
total = 120
# Before Python 3.6
print("Completed: {} out of {}. Progress: {:.1%}".format(completed, total, completed / total))
# Since Python 3.6
print(f"Completed: {completed} out of {total}. Progress: {completed / total:.1%}")
It means the loop finished without a break
for meeting in meetings:
if meeting.overlaps(new_meeting):
print(f"Conflict with {meeting.title}")
break
else:
meetings.append(new_meeting)
print("Meeting scheduled")
Three similar species of Iris are distinguished by the length and width of their sepals and petals. Ronald Fisher described them in his 1936 paper
sklearn, also known as scikit-learn, is a Python library with machine learning algorithms
from sklearn import datasets, ensemble
X, y = datasets.load_iris(return_X_y=True)
model = ensemble.RandomForestClassifier()
model.fit(X, y)
print(model.predict([[5.1, 3.5, 1.4, 0.2]]))
import pandas as pd
import matplotlib.pyplot as plt
data = pd.read_csv("temperature.csv")
data.plot(x="date", y="temperature")
plt.show()
pip install jupyterlab
match and casematch point:
case (0, 0):
print("Origin")
case (0, y):
print(f"On the Y axis: {y}")
case (x, 0):
print(f"On the X axis: {x}")
case (x, y):
print(f"Point: {x}, {y}")
return abs(point_1.x - point_2.x)
^^^^^^^^^
AttributeError: 'NoneType' object has no attribute 'x'
"Hello!".split(max_split=1)
^^^^^^^^^
TypeError: got an unexpected keyword argument 'max_split'
Did you mean: 'maxsplit'?
itertools.batchedfrom itertools import batched
for email_batch in batched(subscribers, 100):
newsletter.send(to=email_batch)
python3.13t
https://docs.python.org/3.12/library/itertools.html#itertools.batched · https://peps.python.org/pep-0703/
Like f-strings, but preserve static text and interpolated values
variety = "Stilton"
template = t"Try some {variety} cheese!"
print(template)
print(template.strings)
print(template.values)
function_name, ClassName, CONSTANT_NAMEtotal = price * quantity, not total=price*quantity