def increment(number, by=1):
return number + by
# Keyword arguments
increment(2, by=1)
# Variable number of arguments
def multiply(*numbers):
for number in numbers:
print number
multiply(1, 2, 3, 4)
# Variable number of keyword arguments
def save_user(**user):
...
save_user(id=1, name="Mosh")
point = {"x": 1, "y": 2}
point = dict(x=1, y=2)
point["z"] = 3
if "a" in point:
...
point.get("a", 0) # 0
del point["x"]
for key, value in point.items():
...
# Dictionary comprehensions
values = {x: x * 2 for x in range(5)}
first = {1, 2, 3, 4}
second = {1, 5}
first | second # {1, 2, 3, 4, 5}
first & second # {1}
first - second # {2, 3, 4}
first ^ second # {2, 3, 4, 5}
if 1 in first:
...
if x == 1:
print(“a”)
elif x == 2:
print(“b”)
else:
print(“c”)
# Ternary operator
x = “a” if n > 1 else “b”
# Chaining comparison operators
if 18 <= age < 65: