βΉοΈ 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 argumentsmin()- Returns the smallest item in an iterable or the smallest of two or more argumentssum()- Returns the sum of all items in an iterablesorted()- Returns a new sorted list from the items in an iterableround()- Returns the round figured value of a numberabs()- Returns the absolute value of a numberpow()- Returns the value of a number raised to the power of another number
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 as0,False, or anempty 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.
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