Only the required built-in functions for S1 are mentioned here.
Converts an integer to a binary string.
print(bin(10)) # Output: '0b1010'Returns the string representing a character for the given Unicode code.
print(chr(97)) # Output: 'a'complex
Section titled “complex”Creates a complex number.
print(complex(1, 2)) # Output: (1+2j)Creates a dictionary.
print(dict(a=1, b=2)) # Output: {'a': 1, 'b': 2}Converts a number or string to a float.
print(float("3.14")) # Output: 3.14Converts an integer to a hexadecimal string.
print(hex(255)) # Output: '0xff'Returns the identity of an object.
print(id(5)) # Output: (an integer)Converts a number or string to an integer.
print(int("10")) # Output: 10Used to return the number of items in an object. Commonly used with sequences (like lists, tuples, and strings) and collections (like dictionaries and sets).
# Example with a listfruits = ["apple", "banana", "cherry"]print(len(fruits)) # Output: 3
# Example with a stringname = "Alice"print(len(name)) # Output: 5Returns the largest item.
print(max(1, 2, 3)) # Output: 3Returns the smallest item.
print(min(1, 2, 3)) # Output: 1Opens a file and returns a file object. The open function is fundamental for
file operations in Python.
Basic syntax: open(filename, mode='r', encoding=None)
Common modes:
'r': Read (default)'w': Write (overwrites)'a': Append'b': Binary mode
# Reading a filewith open('file.txt', 'r') as f: content = f.read() print(content)
# Writing to a filewith open('output.txt', 'w') as f: f.write('Hello, World!')
# Appending to a filewith open('log.txt', 'a') as f: f.write('New log entry\n')When with statement is used, the file will be automatically closed after use.
If not, the file has to be closed using f.close().
Returns the Unicode code for a given character.
print(ord('a')) # Output: 97Returns the power of a number.
print(pow(2, 3)) # Output: 8Prints to the console.
print("Hello, World!") # Output: Hello, World!Used to generate a sequence of numbers. Commonly used in for loops to iterate
over a sequence of numbers. The range function can take 1, or 2, or 3
arguments, similar to
string slicing.
range(stop): Generates numbers from 0 tostop - 1.range(start, stop): Generates numbers fromstarttostop - 1.range(start, stop, step): Generates numbers fromstarttostop - 1, incrementing bystep.
Here are some examples:
# Using range with one argumentfor i in range(5): print(i) # Output: 0, 1, 2, 3, 4
# Using range with two argumentsfor i in range(2, 5): print(i) # Output: 2, 3, 4
# Using range with three argumentsfor i in range(1, 10, 2): print(i) # Output: 1, 3, 5, 7, 9The range function returns an immutable sequence type, which can be converted
to a list if needed:
numbers = list(range(5))print(numbers) # Output: [0, 1, 2, 3, 4]reversed
Section titled “reversed”Returns a reversed iterator.
print(list(reversed([1, 2, 3]))) # Output: [3, 2, 1]Rounds a number to a specified number of digits.
print(round(3.14159, 2)) # Output: 3.14Creates a set.
print(set([1, 2, 3])) # Output: {1, 2, 3}Converts an object to a string.
print(str(123)) # Output: '123'Returns the sum of a collection.
print(sum([1, 2, 3])) # Output: 6Creates a tuple.
print(set([1, 2, 3])) # Output: {1, 2, 3}Returns the type of an object.
print(type(123)) # Output: <class 'int'>