print(s, ..., sep=' ', end='\n', file=sys.stdout, flush=False)
return
) its result to the caller
total = min(12, 5, 7)
min(3, 4, 2)
a_string = 'Python'; a_string.upper()
text = 'a,b,c'; text.split(',')
# ...delta_x, delta_y are defined delta_x2 = delta_x * delta_x delta_y2 = delta_y * delta_y distance = math.sqrt(delta_x2 + delta_y2) # vs. import math distance = math.hypot(delta_x, delta_y)
def name(arg1, arg2, ..., argN): """ Textual explanation of what the function is supposed to do. """ # calculate result... return result
def power(base, exp = 2): return base ** exp
#!/usr/bin/env python3 def interest(capital, rate, years=1): """ Return the total interest of investing `capital` at `rate`. """ value = capital * (1 + rate) ** years return value - capital print(interest(100, 0.03)) result = interest(100, 0.03, 3) profit = interest(1300, 0.025, 5)
#!/usr/bin/env python3 def print_uppercase(text): """ Print the passed text as upper case. """ print(text.upper()) a_text = "Programming is fun!" result = print_uppercase(a_text) def print_sorted_list(l): l.sort() print(l) a_list = [5, 3, 7, 1, 4] another_result = print_sorted_list(a_list)
#!/usr/bin/env python3 def interest(capital=1000, rate=0.04, years=1): """ Return the total interest of investing `capital` at `rate`. """ value = capital * (1 + rate) ** years return value - capital print(interest(100, 0.03)) profit = interest(capital=1300, rate=0.025, years=5) profit = interest(years=5) profit = interest(1300, years=5) result = interest(1000 + interest(rate=0.06), 0.03, 3)
if __name__ == "__main__": sys.exit(main())
#!/usr/bin/env python3 """ Lambda functions are expressions that can be created at their point of use. They are limited in comparison to regular functions, but useful as arguments, e.g. for sorting. """ interest = lambda c, r, y: c * (1 + r) ** y - c print(interest(100, 0.03, 1)) result = interest(100, 0.03, 3) profit = interest(1300, 0.025, 5) a_list = [3, 4, 19, 11, 1, 4, 6, 8, 5, 2, 0, 12, 13, 3] a_list.sort(key=lambda x: x % 3) # sort by remainder of division by 3 print(a_list)
sin(sqrt(12))
method chaining is done usingtext.upper().split()
Having enough eyes, all bugs seem shallow.– Linus Torvalds)
None
(comp. void type) can be returned