Fundamentals 5 min read

Common Python Built‑in Functions with Example Code

This article introduces a collection of essential Python built‑in functions such as print, len, type, conversion utilities, data‑structure constructors, iteration helpers, and numeric helpers, each illustrated with concise example code snippets to demonstrate their typical usage.

Test Development Learning Exchange
Test Development Learning Exchange
Test Development Learning Exchange
Common Python Built‑in Functions with Example Code

print() – prints information to the console. print("Hello, World!")

len() – returns the length of a collection. my_list = [1, 2, 3, 4, 5] print(len(my_list)) # 5

type() – obtains the type of an object. var = 10 print(type(var)) #

int(), float(), str() – convert between string, integer and float types. num_str = "123" num_int = int(num_str) print(num_int) # 123 num_float_str = "123.45" num_float = float(num_float_str) print(num_float) # 123.45 num = 123 num_str_converted = str(num) print(num_str_converted) # "123"

list(), tuple(), set() – create or convert to common containers. my_tuple = (1, 2, 3) my_list_from_tuple = list(my_tuple) print(my_list_from_tuple) # [1, 2, 3] my_list = [1, 2, 2, 3, 4] my_set_from_list = set(my_list) print(my_set_from_list) # {1, 2, 3, 4} my_dict = dict([('a', 1), ('b', 2)]) print(my_dict) # {'a': 1, 'b': 2}

range(), sum(), min(), max() – generate numeric sequences and compute aggregates. numbers = range(1, 6) print(sum(numbers)) # 15 print(min(numbers)) # 1 print(max(numbers)) # 5

input() – read user input from the console. user_input = input("请输入您的名字: ") print("你好," + user_input + "!")

round() – round a floating‑point number to a given precision. rounded_num = round(3.14159, 2) print(rounded_num) # 3.14

enumerate() – iterate with index and value. fruits = ['apple', 'banana', 'cherry'] for index, fruit in enumerate(fruits): print(index, fruit)

zip() – iterate over multiple iterables in parallel. names = ['Alice', 'Bob', 'Charlie'] ages = [24, 30, 22] for name, age in zip(names, ages): print(name, "is", age, "years old.")

sorted() – return a sorted list. unsorted_list = [3, 1, 4, 1, 5, 9, 2, 6] sorted_list = sorted(unsorted_list) print(sorted_list) # [1, 1, 2, 3, 4, 5, 6, 9]

any(), all() – logical checks over iterables. bool_list = [True, False, True] print(any(bool_list)) # True print(all(bool_list)) # False

bin(), oct(), hex() – convert an integer to binary, octal, or hexadecimal string. decimal_number = 10 print(bin(decimal_number)) # 0b1010 print(oct(decimal_number)) # 0o12 print(hex(decimal_number)) # 0xA

chr(), ord() – convert between Unicode code points and characters. char = chr(97) # 'a' print(char) # a unicode_point = ord('€') print(unicode_point) # 8364

help() and dir() – inspect objects and obtain documentation. print(dir(list)) help(list.append)

input() with type conversion – read and convert user input. age = int(input("请输入您的年龄: ")) print("您输入的年龄是:", age)

round() for floating‑point precision – another example. number = 3.14159 rounded = round(number, 3) print(rounded) # 3.142

range() in a loop – iterate over a sequence of numbers. for i in range(5): print(i, end=' ') # Output: 0 1 2 3 4

pythonProgrammingTutorialFundamentalsbuilt-in-functionscode-examples
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.