functions

Beginner-friendly tutorials on defining and calling functions in Python.

Python: Type Conversion, Implicit vs Explicit with Gotchas

Python type conversion in plain words: turn an int into a str, a str into a float, and back again, using int(), float(), str(), and bool(). Every path is shown with tested output and the catches that trip up beginners. Here is the problem. A user, say a fellow named Anvay, types his age into your program and you get back the text "28", not the number 28. You try to check if "28" > 18 and Python throws an ... Read More

Python Variables: Naming, Assignment, and the Memory Model

Change a list through one name and watch a completely different name show the change too. That bug bites nearly every beginner, and it happens because Python variables are names attached to objects, not boxes holding values. This post covers naming rules, assignment, dynamic typing, id() and is vs ==, plus the integer caching quirk that surprises even experienced developers. “Names are not the things they refer to. A variable in Python is a name that refers to an object. ... Read More

Python: Data Types (int, float, str, bool, complex, bytes, bytearray, None)

Ask Python what 28 is and it answers int. Wrap the same characters in quotes and they become str, and suddenly + means glue instead of add. Python data types decide what every value can and cannot do. This guide covers all 8 built-in types, from int and float to bytes and None, with tested examples and the traps between them. “Programs must be written for people to read, and only incidentally for machines to execute.” Harold Abelson, SICP Last ... Read More