[AI] [PY] [1] Python Class

Absolutely, I’d be happy to provide you with an in-depth lesson on Python’s built-in functions!

1. print()

The print() function is used to display content in the console. It can take one or more arguments, which can be strings, variables, or expressions. It’s commonly used for debugging and displaying information to users.

Example:

print("Hello, world!")

2. input()

The input() function allows you to get user input from the console. It displays a prompt to the user and waits for them to enter a value, which is then returned as a string.

Example:

name = input("Please enter your name: ")
print("Hello, " + name)

3. len()

The len() function returns the length (number of elements) of an iterable, such as a string, list, tuple, or dictionary.

Example:

word = "Python"
print(len(word))  # Output: 6

4. type()

The type() function is used to determine the data type of an object.

Example:

num = 42
print(type(num))  # Output: <class 'int'>

5. Type Conversion Functions: int(), float(), str(), bool()

These functions allow you to convert values from one data type to another.
Int is an integer, Float is a decimal, Str is a string, and Bool is a boolean value.

Example:

num_str = "42"
num_int = int(num_str)
print(type(num_int))  # Output: <class 'int'>
dec_string = "4.2"
dec_float = float(dec_string)
print(type(num_int))  # Output: <class 'float'>

6. range()

The range() function generates a sequence of numbers within a specified range. It can take up to three arguments: start, stop, and step.

Example:

for i in range(1, 6, 2):
    print(i)  # Output: 1, 3, 5

7. max(), min(), sum()

These functions operate on iterables to find the maximum, minimum, and sum of their elements, respectively.

Example:

numbers = [4, 8, 2, 10]
print(max(numbers))  # Output: 10
print(min(numbers))  # Output: 2
print(sum(numbers))  # Output: 24

8. abs()

The abs() function returns the absolute value of a number.

Example:

num = -5
abs_num = abs(num)
print(abs_num)  # Output: 5

9. round()

The round() function is used to round a floating-point number to a specified number of decimal places.

Example:

pi = 3.14159
rounded_pi = round(pi, 2)
print(rounded_pi)  # Output: 3.14

These are just some of the fundamental built-in functions in Python. Understanding and using these functions effectively can greatly enhance your programming capabilities. Keep exploring the Python documentation for more functions and their use cases!

1 Like