Miscellaneous
Type Checking
type()
Returns the type of an object.
Syntax:
type(object)
Example:
print(type("hello")) # Output: <class 'str'>
print(type(123)) # Output: <class 'int'>
isinstance()
Checks if an object is an instance or subclass of a class or a tuple of classes.
Syntax:
isinstance(object, classinfo)
Example:
print(isinstance("hello", str)) # Output: True
print(isinstance(123, (int, float))) # Output: True
Input and Output
print()
Prints objects to the console.
Syntax:
print(*objects[, sep=' ', end='\n', file=sys.stdout, flush=False])
Example:
print("Hello", "world") # Output: Hello world
input()
Reads a line from input, converts it to a string, and returns it.
Syntax:
input([prompt])
Example:
name = input("Enter your name: ")
print(f"Hello, {name}!")
Utility Functions
id()
Returns the identity (unique identifier) of an object.
Syntax:
id(object)
Example:
x = 10
print(id(x)) # Output: A unique identifier for x
callable()
Checks if an object appears to be callable (i.e., if it can be called as a function).
Syntax:
callable(object)
Example:
print(callable(print)) # Output: True
print(callable(123)) # Output: False
eval()
Evaluates a given expression from a string-based input.
Syntax:
eval(expression[, globals[, locals]])
Example:
result = eval("2 + 3")
print(result) # Output: 5
exec()
Executes a dynamically created Python code which can be a single statement, multiple statements, or a block.
Syntax:
exec(object[, globals[, locals]])
Example:
exec("for i in range(3): print(i)")
Output:
0
1
2
format()
Formats a value according to a format specification.
Syntax:
format(value[, format_spec])
Example:
print(format(1234.567, ".2f")) # Output: 1234.57