-
1. Introduction to Python
-
2. Python Basics
-
3. Working with Data Structures
-
4. Functions and Modules
-
5. Object-Oriented Programming (OOP)
-
6. File Handling
-
7. Error and Exception Handling
-
8. Python for Data Analysis
-
9. Advanced Topics in Python
-
10. Working with APIs
-
11. Python for Automation
-
12. Capstone Projects
- 13. Final Assessment and Quizzes
4.2.1 Overview of commonly used built-in functions
Python provides a wide variety of built-in functions that help you perform common operations efficiently. These functions are part of Python’s standard library and are available for immediate use without needing to import any additional modules. Below is an overview of some of the most commonly used built-in functions:
1. Functions for Output and Input
1.1 print()
- Prints output to the console.
Example:
print("Hello, World!")
1.2 input()
- Reads user input from the console. The value is returned as a string.
Example:
name = input("Enter your name: ") print(f"Hello, {name}!")
2. Functions for Data Types and Type Conversion
2.1 type()
- Returns the type of an object (e.g., integer, string, etc.).
Example:
x = 42 print(type(x)) # Output: <class 'int'>
2.2 int(), float(), str()
- Converts data to integers, floating-point numbers, or strings respectively.
Example:
x = "123" print(int(x)) # Output: 123 y = "3.14" print(float(y)) # Output: 3.14 z = 42 print(str(z)) # Output: '42'
3. Functions for Mathematical Operations
3.1 abs()
- Returns the absolute value of a number.
Example:
num = -5 print(abs(num)) # Output: 5
3.2 max() and min()
- Returns the largest or smallest item in an iterable or from a set of values.
Example:
numbers = [1, 2, 3, 4] print(max(numbers)) # Output: 4 print(min(numbers)) # Output: 1
3.3 sum()
- Returns the sum of all elements in an iterable (e.g., list, tuple).
Example:
numbers = [1, 2, 3] print(sum(numbers)) # Output: 6
3.4 round()
- Rounds a floating-point number to the specified number of decimal places.
Example:
pi = 3.14159 print(round(pi, 2)) # Output: 3.14
4. Functions for String Manipulation
4.1 len()
- Returns the length of a string or any iterable.
Example:
text = "Hello" print(len(text)) # Output: 5
4.2 str()
- Converts any object to a string representation.
Example:
x = 10 print(str(x)) # Output: '10'
4.3 upper() and lower()
- Converts a string to uppercase or lowercase.
Example:
text = "hello" print(text.upper()) # Output: 'HELLO' print(text.lower()) # Output: 'hello'
5. Functions for Handling Collections (Lists, Tuples, Sets)
5.1 list(), tuple(), and set()
- Converts an iterable (like a string or a dictionary) into a list, tuple, or set.
Example:
my_tuple = (1, 2, 3) print(list(my_tuple)) # Output: [1, 2, 3] my_list = [1, 2, 3] print(tuple(my_list)) # Output: (1, 2, 3) my_list = [1, 2, 3, 3] print(set(my_list)) # Output: {1, 2, 3}
6. Functions for Collection Operations
6.1 sorted()
- Returns a sorted list from the elements of any iterable.
Example:
numbers = [4, 2, 3, 1] print(sorted(numbers)) # Output: [1, 2, 3, 4]
6.2 reversed()
- Returns a reversed iterator of the given sequence.
Example:
numbers = [1, 2, 3, 4] print(list(reversed(numbers))) # Output: [4, 3, 2, 1]
7. Functions for File Handling
7.1 open()
- Opens a file and returns a file object, which can be used to read or write data.
Example:
file = open("example.txt", "w") file.write("Hello, file!") file.close()
7.2 read() and readlines()
- read() reads the entire content of a file, while readlines() reads the file line by line.
Example:
file = open("example.txt", "r") content = file.read() print(content) # Output: 'Hello, file!' file.close()
8. Functions for Logical and Comparison Operations
8.1 all() and any()
- all() returns True if all elements of an iterable are true.
- any() returns True if at least one element of the iterable is true.
Example:
nums = [1, 2, 3] print(all(nums)) # Output: True (since all elements are non-zero) print(any(nums)) # Output: True (since there are non-zero elements)
8.2 isinstance()
- Checks if an object is an instance of a specified class or data type.
Example:
x = 42 print(isinstance(x, int)) # Output: True
9. Functions for Iteration
9.1 map()
- Applies a given function to all items in an iterable and returns a map object (iterator).
Example:
numbers = [1, 2, 3] squared = map(lambda x: x ** 2, numbers) print(list(squared)) # Output: [1, 4, 9]
9.2 filter()
- Filters elements in an iterable based on a function that returns True or False.
Example:
numbers = [1, 2, 3, 4, 5] even_numbers = filter(lambda x: x % 2 == 0, numbers) print(list(even_numbers)) # Output: [2, 4]
Conclusion
Python’s built-in functions provide a rich set of tools for performing a wide variety of tasks, from mathematical operations to data manipulation and input/output handling. By using these functions, you can write more concise, readable, and efficient code. Familiarizing yourself with these commonly used functions is crucial for becoming proficient in Python programming.
Commenting is not enabled on this course.