GGistDev

Values and Types in Python

Python is dynamically typed; everything is an object. Only False, None, numeric zero (0, 0.0, 0j), and empty containers ("", [], {}, set(), etc.) are falsy.

Core values

Integer, float, and complex are built‑ins; numeric literals support bases and underscores for readability.

True, False, None
42        # int (arbitrary precision)
3.14      # float (IEEE 754)
1+2j      # complex (a+bj)
0b1010    # binary 10
0xFF      # hex 255
1_000_000 # underscores for readability

Strings

Strings are immutable Unicode sequences. Choose single, double, or triple quotes for convenience; raw strings (r"…") skip backslash escapes.

s = "hello\nworld"
raw = r"path\to\file"
name = "Python"
print(f"Hi {name}")
multiline = """Line 1\nLine 2"""

Collections

Python’s main built‑in collections cover lists, tuples, sets, and dicts.

lst = [1, 2, 3]                     # list (mutable, ordered)
tpl = (1, 2, 3)                     # tuple (immutable, ordered)
set_ = {1, 2, 3}                     # set (unique, unordered)
dct = {"lang": "py", "v": 3.12}   # dict (mapping)

Common operations:

lst.append(4); lst[0] = 9
2 in set_           # membership
len(dct), dct.keys(), dct.values()

Truthiness

Python converts objects to bool via __bool__ or length (__len__). Use this to write idiomatic conditions.

if items:             # non‑empty
    ...
if not config:        # empty mapping is falsy
    ...

Conversions

Coerce types explicitly to avoid surprises.

int("10"), float("3.1")
str(123), list("abc"), tuple([1,2])

Identity vs equality

== compares values; is checks object identity (same object in memory).

x = 256; y = 256
x == y   # True (equal value)
x is y   # Implementation detail for small ints; do not rely on it generally

Summary

  • Core numerics, strings, and versatile collections
  • Truthiness drives concise conditions; convert explicitly
  • Distinguish equality (==) from identity (is)