Skip to Content
πŸŽ‰ Welcome to my notes πŸŽ‰
Python6. Built-in Functions

⏹️ Built-in Functions, Keywords and Math Module:

πŸ—οΈ Built-in Functions in Python

Python provides many built-in functions that can be used to perform operations on data types. Some common ones include:

  • len() - Returns the length of an object (e.g., string, list)
  • max() - Returns the largest item in an iterable or the largest of two or more arguments
  • min() - Returns the smallest item in an iterable or the smallest of two or more arguments
  • sum() - Returns the sum of all items in an iterable
  • sorted() - Returns a new sorted list from the items in an iterable
  • round() - Returns the round figured value of a number
  • abs() - Returns the absolute value of a number
  • pow() - Returns the value of a number raised to the power of another number
built_in_functions.py
numbers = [1, 2, 3, 4, 5] print("Length:", len(numbers)) # Output: 5 print("Max:", max(numbers)) # Output: 5 print("Min:", min(numbers)) # Output: 1 print("Sum:", sum(numbers)) # Output: 15 print("Sorted:", sorted(numbers)) # Output: [1, 2, 3, 4, 5] print("Round:", round(3.14159, 2)) # Output: 3.14 print("Absolute:", abs(-10)) # Output: 10 print("Power:", pow(2, 3)) # Output: 8

πŸ—οΈ Helpful Keywords in Python

Python has several keywords that are reserved for specific purposes. Here are some of the most commonly used keywords:

  • None: It is a special constant representing the absence of a value or a null value. It is an object of its own data type, NoneType. None is not the same as 0, False, or an empty string; it specifically indicates that a variable or object does not have an assigned value.

  • pass: It is a null operation; when it is executed, nothing happens. It is used as a placeholder where a statement is syntactically required, but no code needs to be executed.

  • in: It checks if a value is present in a sequence (e.g., list, string).

  • not in: It checks if a value is not present in a sequence (e.g., list, string).

  • is: It checks if two variables point to the same object in memory.

  • is not: It checks if two variables do not point to the same object in memory.

πŸ“ Math module

It provides access to mathematical functions like sqrt(), sin(), cos(), etc.

math_module.py
import math print("Square root:", math.sqrt(16)) # Output: 4.0 print("Sine:", math.sin(math.pi / 2)) # Output: 1.0 print("Cosine:", math.cos(0)) # Output: 1.0 print("Tangent:", math.tan(math.pi / 4)) # Output: 1.0 print("Factorial:", math.factorial(5)) # Output: 120 print("Pi:", math.pi) # Output: 3.141592653589793
Last updated on