Below are 100 Python functions along with their uses and simple examples:
print()
: Displays text or variables. Example:print("Hello, World!")
input()
: Accepts user input. Example:name = input("Enter your name: ")
len()
: Returns the length of a string or list. Example:length = len("Python")
type()
: Returns the data type of a variable. Example:data_type = type(42)
int()
: Converts a value to an integer. Example:number = int("5")
float()
: Converts a value to a floating-point number. Example:decimal = float("3.14")
str()
: Converts a value to a string. Example:text = str(42)
list()
: Converts a sequence to a list. Example:my_list = list("Python")
tuple()
: Converts a sequence to a tuple. Example:my_tuple = tuple("Python")
set()
: Converts a sequence to a set. Example:my_set = set("Python")
dict()
: Creates a dictionary. Example:my_dict = dict(key1="value1", key2="value2")
range()
: Generates a sequence of numbers. Example:numbers = range(1, 6)
zip()
: Combines two or more sequences. Example:zipped = zip(["a", "b"], [1, 2])
sum()
: Adds up the elements of a sequence. Example:total = sum([1, 2, 3])
min()
: Returns the smallest element in a sequence. Example:minimum = min([4, 2, 7])
max()
: Returns the largest element in a sequence. Example:maximum = max([4, 2, 7])
abs()
: Returns the absolute value of a number. Example:absolute_value = abs(-5)
round()
: Rounds a number to a specified number of decimal places. Example:rounded = round(3.14159, 2)
sorted()
: Returns a sorted list from a sequence. Example:sorted_list = sorted([3, 1, 4])
reversed()
: Returns a reversed version of a sequence. Example:reversed_list = list(reversed([1, 2, 3]))
any()
: Returns True if at least one element in a sequence is true. Example:result = any([True, False, False])
all()
: Returns True if all elements in a sequence are true. Example:result = all([True, True, True])
filter()
: Filters elements in a sequence based on a function. Example:filtered = filter(lambda x: x > 0, [-1, 2, -3])
map()
: Applies a function to all elements in a sequence. Example:squared = map(lambda x: x**2, [1, 2, 3])
enumerate()
: Returns index and value pairs from a sequence. Example:pairs = list(enumerate(["a", "b", "c"]))
bool()
: Converts a value to a boolean. Example:is_true = bool("Hello")
abs()
: Returns the absolute value of a number. Example:absolute_value = abs(-5)
pow()
: Raises a number to a specified power. Example:result = pow(2, 3)
slice()
: Extracts a portion of a sequence. Example:portion = slice(1, 4)
chr()
: Returns a string representing a character with a given Unicode code. Example:character = chr(65)
ord()
: Returns an integer representing the Unicode character. Example:code = ord("A")
format()
: Formats a specified value. Example:formatted = format(3.14159, ".2f")
capitalize()
: Converts the first character of a string to uppercase. Example:text = "hello".capitalize()
lower()
: Converts a string to lowercase. Example:text = "HELLO".lower()
upper()
: Converts a string to uppercase. Example:text = "hello".upper()
strip()
: Removes leading and trailing whitespaces from a string. Example:text = " Hello ".strip()
replace()
: Replaces occurrences of a substring in a string. Example:new_text = "hello".replace("e", "a")
count()
: Returns the number of occurrences of a substring in a string. Example:count = "hello".count("l")
find()
: Returns the index of the first occurrence of a substring in a string. Example:index = "hello".find("l")
startswith()
: Checks if a string starts with a specified prefix. Example:result = "hello".startswith("he")
endswith()
: Checks if a string ends with a specified suffix. Example:result = "hello".endswith("lo")
isdigit()
: Checks if all characters in a string are digits. Example:result = "123".isdigit()
isalpha()
: Checks if all characters in a string are alphabetic. Example:result = "abc".isalpha()
isalnum()
: Checks if all characters in a string are alphanumeric. Example:result = "abc123".isalnum()
isspace()
: Checks if all characters in a string are whitespaces. Example:result = " ".isspace()
isupper()
: Checks if all characters in a string are uppercase. Example:result = "HELLO".isupper()
islower()
: Checks if all characters in a string are lowercase. Example:result = "hello".islower()
join()
: Joins elements of a sequence into a string. Example: `joined =
split()
: Splits a string into a list of substrings based on a delimiter. Example:words = "Hello,World".split(",")
isnumeric()
: Checks if all characters in a string are numeric. Example:result = "123".isnumeric()
isdecimal()
: Checks if all characters in a string are decimals. Example:result = "123.45".isdecimal()
isidentifier()
: Checks if a string is a valid identifier. Example:result = "variable_1".isidentifier()
title()
: Converts the first character of each word to uppercase in a string. Example:text = "hello world".title()
isprintable()
: Checks if all characters in a string are printable. Example:result = "Hello\nWorld".isprintable()
capitalize()
: Converts the first character of a string to uppercase. Example:text = "hello".capitalize()
rjust()
: Right-justifies a string in a field of a specified width. Example:justified = "hello".rjust(10)
ljust()
: Left-justifies a string in a field of a specified width. Example:justified = "hello".ljust(10)
center()
: Centers a string in a field of a specified width. Example:centered = "hello".center(10)
isdigit()
: Checks if all characters in a string are digits. Example:result = "123".isdigit()
zfill()
: Pads a numeric string with zeros on the left to a specified width. Example:padded = "42".zfill(5)
swapcase()
: Swaps the case of each character in a string. Example:swapped = "Hello World".swapcase()
expandtabs()
: Expands tab characters in a string to spaces. Example:expanded = "Tab\tExample".expandtabs(4)
isascii()
: Checks if all characters in a string are ASCII. Example:result = "Hello".isascii()
hex()
: Converts an integer to a hexadecimal string. Example:hex_string = hex(255)
oct()
: Converts an integer to an octal string. Example:octal_string = oct(8)
bin()
: Converts an integer to a binary string. Example:binary_string = bin(5)
eval()
: Evaluates a string as a Python expression. Example:result = eval("2 + 3")
exec()
: Executes a dynamically created Python program. Example:exec("print('Hello')")
round()
: Rounds a number to a specified number of decimal places. Example:rounded = round(3.14159, 2)
dir()
: Returns a list of names in the current scope or attributes of an object. Example:names = dir()
globals()
: Returns a dictionary representing the current global symbol table. Example:global_vars = globals()
locals()
: Returns a dictionary representing the current local symbol table. Example:local_vars = locals()
repr()
: Returns a string representation of an object. Example:representation = repr([1, 2, 3])
hash()
: Returns the hash value of an object. Example:hashed = hash("Hello")
callable()
: Checks if an object appears callable. Example:result = callable(print)
id()
: Returns the identity of an object. Example:identity = id([1, 2, 3])
chr()
: Returns a string representing a character with a given Unicode code. Example:character = chr(65)
ord()
: Returns an integer representing the Unicode character. Example:code = ord("A")
compile()
: Compiles a source into a code or AST object. Example:compiled = compile("print('Hello')", '<string>', 'exec')
memoryview()
: Returns a memory view object of an array. Example:view = memoryview(b'Hello')
ascii()
: Returns a string containing a printable representation of an object. Example:representation = ascii('Hello')
bytearray()
: Returns a mutable bytearray object. Example:arr = bytearray([65, 66, 67])
bytes()
: Returns an immutable bytes object. Example:b = bytes([65, 66, 67])
filter()
: Filters elements in a sequence based on a function. Example:filtered = filter(lambda x: x > 0, [-1, 2, -3])
map()
: Applies a function to all elements in a sequence. Example:squared = map(lambda x: x**2, [1, 2, 3])
pow()
: Raises a number to a specified power. Example:result = pow(2, 3)
sum()
: Adds up the elements of a sequence. Example:total = sum([1, 2, 3])
enumerate()
: Returns index and value pairs from a sequence. Example:pairs = list(enumerate(["a", "b", "c"]))
reversed()
: Returns a reversed version of a sequence. Example:reversed_list = list(reversed([1, 2, 3]))
any()
: Returns True if at least one element in a sequence is true. Example:result = any([True, False, False])
all()
: Returns True if all elements in a sequence are true. Example:result = all([True, True, True])
hex()
: Converts an integer to a hexadecimal string. Example:hex_string = hex(255)
oct()
: Converts an integer to an octal string. Example:octal_string = oct(8)
bin()
: Converts an integer to a binary string. Example:binary_string = bin(5)
0 Comments