Adding some more arithmetic operator examples
This commit is contained in:
parent
8a62a49f36
commit
7af513d8db
|
@ -24,4 +24,31 @@ print("Your age is:", age)
|
||||||
age = age + 1
|
age = age + 1
|
||||||
# Age is now equal to 33
|
# Age is now equal to 33
|
||||||
# Someone had a birthday!
|
# Someone had a birthday!
|
||||||
print("Happy Birthday! You are now", age, "years old!")
|
print("Happy Birthday! You are now", age, "years old!")
|
||||||
|
|
||||||
|
# Simple math operators can be combined for the sake of brevity.
|
||||||
|
# These are called "compound" operators
|
||||||
|
# age = age + 1
|
||||||
|
# is the same as
|
||||||
|
# age += 1
|
||||||
|
#
|
||||||
|
# Basic compound operators: += -= *= /= (/= is a floating point operator)
|
||||||
|
# (A special //= which returns integer)
|
||||||
|
unused_age = 100
|
||||||
|
print(f"Unused Age: {unused_age}")
|
||||||
|
unused_age -= 1 # unused_age is now 99
|
||||||
|
print(f"Unused Age: {unused_age}")
|
||||||
|
unused_age += 3 # unused_age is now 102
|
||||||
|
print(f"Unused Age: {unused_age}")
|
||||||
|
unused_age *= 4 # unused_age is now 408
|
||||||
|
print(f"Unused Age: {unused_age}")
|
||||||
|
unused_age //= 2 # unused_age is now 204
|
||||||
|
print(f"Unused Age: {unused_age}")
|
||||||
|
|
||||||
|
# There is a common operator used in programming called the "modulo" or "modulus".
|
||||||
|
# This operator is a division operator that returns the remainder after
|
||||||
|
# the division is complete.
|
||||||
|
unused_age %= 100
|
||||||
|
print(f"Unused Age: {unused_age}")
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user