bytes
tutorials
python
popular functions python
Overview:
Python functions are pieces of code that can be reused and passed around. They are utilized to break down an expansive program into littler chunks, making it less demanding to debug and keep up. Functions are declared using the def keyword and can take optional parameters. They can also return values utilizing the return keyword. Functions can be used to perform assignments such as calculations, string manipulations, and file operations. Also, functions can be used to make classes and modules.
What are some popular functions in Python
print("Hello World") # Output: Hello World
Hello world message is printed here
x = 10
print(type(x)) # Output: <class 'int'>
x=10 is taken, and input and its type int is returned
x = input("Enter a number: ") # Output: Enter a number:
The number is inputted here
print(abs(-90)) # Output: 90
An absolute value of -90 is returned, which is 90
pow(2, 3) # Output: 8
The power of 2^3, which is 8, is returned
sorted([2, 3, 1, 4, 5]) # Output: [1, 2, 3, 4, 5]
The list of values is sorted
max(2, 10, -5) # Output: 10
The maximum of the list, which is 10, is returned
round(3.14159) # Output: 3
It rounds off the value and returns 3
id(10) # Output: 140735457659248
The identity of 10 is returned
ord('a') # Output: 97
Ascii code of a is returned
len('Hello World') # Output: 11
The length of the message is displayed
sum([2, 4, 6, 8]) # Output: 20
The sum of the list is returned
help() # Output: help>
For any help or information, this is used
Lambda x: x + 1 # Output: <function <lambda> at 0x0000020F2F35EAE8>
The lambda function takes in a single argument (x) and adds 1 to it.
Map(lambda x: x + 1, [1, 2, 3, 4]) # Output: <map object at 0x0000020F2F35E9E8>
The map function takes in a function (in this case the lambda function) and an iterable (in this case a list of numbers [1,2,3,4]) and applies the function to each element of the iterable and returns a map object.
Filter(lambda x: x > 2, [1, 2, 3, 4]) # Output: <filter object at 0x
The map function takes in a function (in this case, the lambda function) and an iterable (in this case, a list of numbers [1,2,3,4]) and applies the function to each element of the iterable, and returns a map object.
my_list = ["apple", "banana", "cherry"]
for index, item in enumerate(my_list):
print(index, item)
# Output:
# 0 apple
# 1 banana
# 2 cherry
The enumerate function takes an iterable (in this case, a list of strings ["apple", "banana", "cherry"]). It returns an enumerate object, which contains the index and value of each iterable element.
x = 1
y = 2
eval('x + y') # Output: 3
The eval function takes a string as input and evaluates it, returning the result.
list(range(1, 10)) # Output: [1, 2, 3, 4, 5, 6, 7, 8, 9]
The range function takes in two numbers (in this case, 1 and 10) and returns a range object containing the numbers between the two numbers (in this case, 1, 2, 3, 4, 5, 6, 7, 8, 9).
20. Groupby: is used to split the data into groups based on some criteria.
# Grouping data based on 'Team'
import pandas as pd
# Initialize dataframe
df = pd.DataFrame({'Team': ['A','B','C','A','B','C','A','B','C'],
'Score': [1,2,3,4,5,6,7,8,9]})
# Group data based on 'Team'
df_grouped = df.groupby('Team')
# Print the grouped data
print(df_grouped)
This code uses the groupby() function from the pandas library to group the data in the dataframe df based on the 'Team' column. The result of the groupby() function is stored in the df_grouped dataframe. Finally, the grouped data is printed to the console.
Conclusion
We found how these built-in functions can perform different assignments, from fundamental arithmetic to complex database operations. We learned how to create our functions and call them in her code. Along the way, we tested distinctive methods to improve the code, such as looping and nesting. As we continued to investigate and practice, we begun to gain a more profound understanding of Python and its capabilities.
Key takeaways
Quiz
Answer:a. To provide a way to structure programs into logical units
Answer:a. To sort a list of elements
Answer:c. To find the length of a string
Answer:b. To convert a list to a dictionary