Quick Overview
Python integers have no fixed width, so n + 1 never overflows the way a 32-bit int would. The value simply grows to whatever memory allows.
Create a function addition(num) that takes a number as an argument, increments the number by +1, and returns the result.
Incrementing values is fundamental for counters, loop trackers, pagination, and game scoring mechanisms.
Add 1 to the input parameter:
def addition(num):
return num + 1
# Testing our function
print(addition(0)) # Output: 1
print(addition(9)) # Output: 10
print(addition(-3)) # Output: -2
Inside functions or loop variables, you can also use the shorthand augmented assignment operator += 1:
counter = 5
counter += 1 # Equivalent to counter = counter + 1 (now 6)
num++ post-increment operator! Writing num++ in Python raises a SyntaxError. Always use num + 1 or num += 1.Use num + 1 to increment values. Next, let's compute remainders using the Modulo operator!
A function with no return hands back None, so a forgotten one surfaces as a TypeError at the call site rather than at the line that actually caused it.
minutes * 60 returns an int when given an int, while / always produces a float regardless of the operands. Multiplication preserves the type; division does not.
base * height / 2 always yields a float in Python 3 because / never returns an int, even for whole numbers. // floors instead, which is almost never what an area wants.