Fundamentals 6 min read

Python Built-in Functions: int(), float(), str(), list(), tuple(), dict(), set(), bool() and Best Practices

This article introduces Python's core built-in conversion functions—including int(), float(), str(), list(), tuple(), dict(), set() and bool()—explaining their syntax, parameters, example usages, and key best‑practice considerations for reliable type conversion in programming.

Test Development Learning Exchange
Test Development Learning Exchange
Test Development Learning Exchange
Python Built-in Functions: int(), float(), str(), list(), tuple(), dict(), set(), bool() and Best Practices

int() converts a string or numeric value to an integer and optionally accepts a base argument to specify the numeral system.

int([x[, base]])

Parameters: x (optional) – the object to convert; base (optional) – numeric base, default is 10.

Examples:

print(int("123"))      # Output: 123
print(int(12.34))      # Output: 12
print(int("1010", 2)) # Output: 10

float() converts a string or numeric value to a floating‑point number.

float([x])

Parameter: x (optional) – the object to convert.

Examples:

print(float("123.45"))  # Output: 123.45
print(float(123))       # Output: 123.0

str() converts a value of any type to its string representation.

str(object='')

Parameter: object (optional) – the value to convert.

Examples:

print(str(123))        # Output: "123"
print(str(12.34))      # Output: "12.34"
print(str([1, 2, 3]))  # Output: "[1, 2, 3]"

list() creates a list from an iterable such as a string, tuple, or set.

list([iterable])

Parameter: iterable (optional) – the source iterable.

Examples:

print(list("hello"))          # Output: ['h', 'e', 'l', 'l', 'o']
print(list((1, 2, 3)))        # Output: [1, 2, 3]

tuple() creates a tuple from an iterable.

tuple([iterable])

Parameter: iterable (optional) – the source iterable.

Examples:

print(tuple("hello"))        # Output: ('h', 'e', 'l', 'l', 'o')
print(tuple([1, 2, 3]))       # Output: (1, 2, 3)

dict() creates a new dictionary from mappings, key‑value pair sequences, or keyword arguments.

dict(**kwarg)
 dict(mapping, **kwarg)
 dict(iterable, **kwarg)

Parameters:

mapping : another mapping object whose items are copied.

iterable : an iterable of two‑element iterables representing key‑value pairs.

kwarg : key‑value pairs passed as keyword arguments.

Examples:

print(dict([('one', 1), ('two', 2)]))  # Output: {'one': 1, 'two': 2}
print(dict(one=1, two=2))              # Output: {'one': 1, 'two': 2}

set() creates a set, an unordered collection of unique elements, optionally from an iterable.

set([iterable])

Parameter: iterable (optional) – source iterable.

Examples:

print(set("hello"))          # Output: {'h', 'e', 'l', 'o'}
print(set([1, 2, 2, 3]))      # Output: {1, 2, 3}

bool() converts a value to a Boolean, returning True or False . Values considered false include empty sequences, numeric zero, and None ; all other values are true.

bool([x])

Parameter: x (optional) – the object to evaluate.

Examples:

print(bool(0))          # Output: False
print(bool(1))          # Output: True
print(bool([]))         # Output: False
print(bool(['a']))      # Output: True

Best Practices and Considerations

Handle invalid input for int() and float() with try/except to avoid ValueError .

Be aware that int() truncates decimals without rounding, which may cause precision loss.

Leverage the flexibility of dict() by choosing the most appropriate input form (mapping, iterable of pairs, or keyword arguments).

Use set() for quick deduplication because it automatically discards duplicate elements.

Pythonprogramming fundamentalsType Conversionexamplesbuilt-in-functions
Test Development Learning Exchange
Written by

Test Development Learning Exchange

Test Development Learning Exchange

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.