Python

Tuple

a = 12
b = 7
*# Call divmod.*
x = **divmod**(a, b)
*# The first part.*
print(x[0])
*# The second part (remainder).*
print(x[1])1
5

Recursion

def make_change(cents):
*# Use modulo 25 to find quarter count.*
parts = **divmod**(cents, 25)
quarters = parts[0]
*# Use modulo 5 on remainder to find nickel count.*
cents_remaining = parts[1]
parts = **divmod**(cents_remaining, 5)
nickels = parts[0]
*# Pennies are the remainder.*
cents_remaining = parts[1]
*# Display the results.*
print("Argument:", cents)
print("Quarters:", quarters)
print("Nickels:", nickels)
print("Pennies:", cents_remaining)
*# Test with 81 cents.*
make_change(81)('Argument:', 81)
('Quarters:', 3)
('Nickels:', 1)
('Pennies:', 1)

def even(number):
*# Even numbers have no remainder when divided by 2.*
return (number % 2) == 0
def odd(number):
*# Odd numbers have 1 or -1 remainder when divided by 2.*
return (number % 2) != 0
*# Test even and odd methods.*
print("#", "Even?", "Odd?")
for value in range(-3, 3):
print(value, even(value), odd(value))
('#', 'Even?', 'Odd?')
(-3, False, True)
(-2, True, False)
(-1, False, True)
(0, True, False)
(1, False, True)
(2, True, False)

Prime Number

Dot Net Perls is a collection of tested code examples. Pages are continually updated to stay current, with code correctness a top priority.

Sam Allen is passionate about computer languages. In the past, his work has been recommended by Apple and Microsoft and he has studied computers at a selective university in the United States.

No updates found for this page.