Python Must-Know Built-in Functions
Learning Python Day 4
--
There are several built-in functions in Python that may be used to carry out a variety of tasks. No of your level of expertise as a developer, it is imperative that you understand these fundamental built-in functionalities.
You may easily do basic tasks with their help, including printing output, managing user input, modifying data, calculating values, iterating over sequences, and more.
print()
name = "John"
print("Hello, " + name + "!") # Output: Hello, John!
The print()
function is used to display output to the console.
input()
age = input("Enter your age: ")
print("You are " + age + " years old.") # Output: You are 25 years old.
The input()
function reads user input from the console.
len()
numbers = [1, 2, 3, 4, 5]
length = len(numbers)
print("The length of the list is:", length) # Output: The length of the list is: 5
The len()
function returns the number of items in an object. In this example, it calculates the length of the numbers
list and prints the result.
type()
value = 10
data_type = type(value)
print("The type of the value is:", data_type) # Output: The type of the value is: <class 'int'>
The type()
function returns the type of an object. In this example, it returns the type of the value
variable and prints the result.
int(), float(), str()
number = "10"
integer = int(number)
floating = float(number)
text = str(integer)
print(number, integer, floating, text) # Output: 10 10 10.0 10
These functions are used for type conversion. In this example, the int()
function converts the string "10"
to an integer.
range()
for i in range(5):
print(i) # Output: 0 1 2 3 4
The range()
function generates a sequence of numbers In this example, it generates numbers from 0 to 4, and the loop prints each number.
max(), min()
numbers = [5, 2, 8, 1, 9]
maximum = max(numbers)
minimum = min(numbers)
print("Maximum number:", maximum) # Output: Maximum number: 9
print("Minimum number:", minimum) # Output: Minimum number: 1
These functions are used to find the maximum and minimum values froma sequence or multiple arguments.
sum()
numbers = [1, 2, 3, 4, 5]
total = sum(numbers)
print("Sum of numbers:", total) # Output: Sum of numbers: 15
The sum()
function calculates the sum of a sequence of numbers.
abs()
number = -10
absolute_value = abs(number)
print("Absolute value:", absolute_value) # Output: Absolute value: 10
The abs()
function returns the absolute value of a number.
round()
pi = 3.14159
rounded_number = round(pi, 2)
print("Rounded number:", rounded_number) # Output: Rounded number: 3.14
The round()
funciton rounds a number to a specified precision.
sorted()
numbers = [3, 1, 4, 2, 5]
sorted_list = sorted(numbers)
print("Sorted list:", sorted_list) # Output: Sorted list: [1, 2, 3, 4, 5]
The sorted()
function returns a new list with the items from an iterable sorted in ascending order.
enumerate()
fruits = ["apple", "banana", "cherry"]
for index, fruit in enumerate(fruits):
print(index, fruit)
# Output:
# 0 apple
# 1 banana
# 2 cherry
The enumerate()
function returns an iterator that pairs elements with their index.
zip()
numbers = [1, 2, 3]
letters = ['a', 'b', 'c']
for num, letter in zip(numbers, letters):
print(num, letter)
# Output:
# 1 a
# 2 b
# 3 c
The zip()
function takes multiple iterables and returns an iterator of tuples, pairing corresponding elements.
any(), all()
numbers = [0, 1, 2, 3]
any_true = any(numbers)
all_true = all(numbers)
print("Any true:", any_true) # Output: Any true: True
print("All true:", all_true) # Output: All true: False
The any()
function returns True
if any element in an iterable evaluates to True
, and the all()
function returns True
if all elements evaluate to True
.
help()
help(print)
The help()
function displays information about an object, including its docstring. In this example, it provides help and documentation for the print()
function.