6. max()
The max() function returns the highest ranked item in an iterable. Be careful not to confuse this with the most frequently occurring value, though.
Let's print the highest ranked value in the dictionary below using the max() function:
The code above ranks the items in the dictionary alphabetically and prints the last one.
Now use the max() function to see the largest integer in a list:
The max() function returns the highest ranked item in an iterable. Be careful not to confuse this with the most frequently occurring value, though.
Let's print the highest ranked value in the dictionary below using the max() function:
b = {1:"grape", 2:"apple", 3:"applesss", 4:"zebra", 5:"mango"}
print(max(b.values()))
Output: zebraThe code above ranks the items in the dictionary alphabetically and prints the last one.
Now use the max() function to see the largest integer in a list:
a = [1, 65, 7, 9]Output: 65
print(max(a))
❤1
7. min()
The min() function does the opposite of what max() does:
1
apple
The min() function does the opposite of what max() does:
fruits = ["grape", "apple", "applesss", "zebra", "mango"]Output:
b = {1:"grape", 2:"apple", 3:"applesss", 4:"zebra", 5:"mango"}
a = [1, 65, 7, 9]
print(min(a))
print(min(b.values()))
1
apple
8. map()
Like reduce(), the map() function lets you iterate over each item in an iterable. However, instead of producing a single result, map() operates on each item independently.
Ultimately, you can perform mathematical operations on two or more lists using the map() function. You can even use it to manipulate an array containing any data type.
Here's how to find the combined sum of two lists containing integers using the map() function:
Like reduce(), the map() function lets you iterate over each item in an iterable. However, instead of producing a single result, map() operates on each item independently.
Ultimately, you can perform mathematical operations on two or more lists using the map() function. You can even use it to manipulate an array containing any data type.
Here's how to find the combined sum of two lists containing integers using the map() function:
b = [1, 3, 4, 6]Output: 96
a = [1, 65, 7, 9]
# Declare a separate function to handle the addition:
def add(a, b):
return a+b
# Pass the function and the two lists into the built-in map() function:
a = sum(map(add, b, a))
print(a)
👍4
9. getattr()
Python's getattr() returns the attribute of an object. It accepts two parameters: the class and the target attribute name.
Here's an example:
Python's getattr() returns the attribute of an object. It accepts two parameters: the class and the target attribute name.
Here's an example:
class ty:Output:Idowu
def init(self, number, name):
self.number = number
self.name = name
a = ty(5*8, "Idowu")
b = getattr(a, 'name')
print(b)
10. append()
Whether you're delving into web development or machine learning with Python, append() is another Python method you'll often need. It works by writing new data into a list without overwriting its original content.
The example below multiplies each item in a range of integers by three and writes them into an existing list:
Whether you're delving into web development or machine learning with Python, append() is another Python method you'll often need. It works by writing new data into a list without overwriting its original content.
The example below multiplies each item in a range of integers by three and writes them into an existing list:
nums = [1, 2, 3]Output:[2, 4, 3, 6, 9]
appendedlist = [2, 4]
for i in nums:
a = i*3
appendedlist.append(a)
print(appendedlist)
❤1
11. range()
You might already be familiar with range() in Python. It's handy if you want to create a list of integers ranging between specific numbers without explicitly writing them out.
Let's create a list of the odd numbers between one and five using this function:
You might already be familiar with range() in Python. It's handy if you want to create a list of integers ranging between specific numbers without explicitly writing them out.
Let's create a list of the odd numbers between one and five using this function:
a = range(1, 6)Output: [1, 3, 5]
b = []
for i in a:
if i%2!=0:
b.append(i)
print(b)
❤1👍1
12. slice()
Although the slice() function and the traditional slice method give similar outputs, using slice() in your code can make it more readable.
You can slice any mutable iterable using the slice method:
[1, 3, 4, 6]
Pyth
The above code gives a similar output when you use the traditional method below:
Although the slice() function and the traditional slice method give similar outputs, using slice() in your code can make it more readable.
You can slice any mutable iterable using the slice method:
b = [1, 3, 4, 6, 7, 10]Output:
st = "Python tutorial"
sliceportion = slice(0, 4)
print(b[sliceportion])
print(st[sliceportion])
[1, 3, 4, 6]
Pyth
The above code gives a similar output when you use the traditional method below:
print(b[0:4])
print(st[0:4])
13. format()
The format() method lets you manipulate your string output. Here's how it works:
10 is the multiple of 5 and 2, but 14 is for 7 and 2
The format() method lets you manipulate your string output. Here's how it works:
multiple = 5*2Output:
multiple2 = 7*2
a = "{} is the multiple of 5 and 2, but {} is for 7 and 2"
a = a.format(multiple, multiple2)
print(a)
10 is the multiple of 5 and 2, but 14 is for 7 and 2
❤3👍2
14. strip()
Python's strip() removes leading characters from a string. It repeatedly removes the first character from the string, if it matches any of the supplied characters.
If you don't specify a character, strip removes all leading whitespace characters from the string.
The example code below removes the letter P and the space before it from the string:
You can replace (" P") with ("P") to see what happens.
Python's strip() removes leading characters from a string. It repeatedly removes the first character from the string, if it matches any of the supplied characters.
If you don't specify a character, strip removes all leading whitespace characters from the string.
The example code below removes the letter P and the space before it from the string:
st = " Python tutorial"Output: ython tutorial
st = st.strip(" P")
print(st)
You can replace (" P") with ("P") to see what happens.
❤3
15. abs()
Do you want to neutralize negative mathematical outputs? Then try out the abs() function. It can come in handy in computational programming or data science operations.
See the example below for how it works:
Do you want to neutralize negative mathematical outputs? Then try out the abs() function. It can come in handy in computational programming or data science operations.
See the example below for how it works:
neg = 4 - 9Output: 5
pos = abs(neg)
print(pos)
16. upper()
As the name implies, the upper() method converts string characters into their uppercase equivalent:
As the name implies, the upper() method converts string characters into their uppercase equivalent:
y = "Python tutorial"Output: PYTHON TUTORIAL
y = y.upper()
print(y)
👍2❤1
17. lower()
You guessed right! Python's lower() is the opposite of upper(). So it converts string characters to lowercases:
You guessed right! Python's lower() is the opposite of upper(). So it converts string characters to lowercases:
y = "PYTHON TUTORIAL"Output: python tutorial
y = y.lower()
print(y)
👍2
18. sorted()
The sorted() function works by making a list from an iterable and then arranging its values in descending or ascending order:
[9, 4, 3, 1]
[3, 5, 8, 9]
The sorted() function works by making a list from an iterable and then arranging its values in descending or ascending order:
f = {1, 4, 9, 3} # Try it on a set
sort = {"G":8, "A":5, "B":9, "F":3} # Try it on a dictionary
print(sorted(f, reverse=True)) # Descending
print(sorted(sort.values())) # Ascending (default)
Output: [9, 4, 3, 1]
[3, 5, 8, 9]
19. join()
The join() function lets you merge string items in a list.
You only need to specify a delimiter and the target list to use it:
The join() function lets you merge string items in a list.
You only need to specify a delimiter and the target list to use it:
a = ["Python", "tutorial", "on", "MUO"]Output: Python tutorial on MUO
a = " ".join(a)
print(a)
👍4❤1
20. replace()
Python's replace() method lets you replace some parts of a string with another character. It's often handy in data science, especially during data cleaning.
The replace() method accepts two parameters: the replaced character and the one you'll like to replace it with.
Here's how it works:
Cart name
First name
Last name
Python's replace() method lets you replace some parts of a string with another character. It's often handy in data science, especially during data cleaning.
The replace() method accepts two parameters: the replaced character and the one you'll like to replace it with.
Here's how it works:
columns = ["Cart_name", "First_name", "Last_name"]Output:
for i in columns:
i = i.replace("_", " ")
print(i)
Cart name
First name
Last name
👍8❤1
Learning Python for data science can be a rewarding experience. Here are some steps you can follow to get started:
1. Learn the Basics of Python: Start by learning the basics of Python programming language such as syntax, data types, functions, loops, and conditional statements. There are many online resources available for free to learn Python.
2. Understand Data Structures and Libraries: Familiarize yourself with data structures like lists, dictionaries, tuples, and sets. Also, learn about popular Python libraries used in data science such as NumPy, Pandas, Matplotlib, and Scikit-learn.
3. Practice with Projects: Start working on small data science projects to apply your knowledge. You can find datasets online to practice your skills and build your portfolio.
4. Take Online Courses: Enroll in online courses specifically tailored for learning Python for data science. Websites like Coursera, Udemy, and DataCamp offer courses on Python programming for data science.
5. Join Data Science Communities: Join online communities and forums like Stack Overflow, Reddit, or Kaggle to connect with other data science enthusiasts and get help with any questions you may have.
6. Read Books: There are many great books available on Python for data science that can help you deepen your understanding of the subject. Some popular books include "Python for Data Analysis" by Wes McKinney and "Data Science from Scratch" by Joel Grus.
7. Practice Regularly: Practice is key to mastering any skill. Make sure to practice regularly and work on real-world data science problems to improve your skills.
Remember that learning Python for data science is a continuous process, so be patient and persistent in your efforts. Good luck!
1. Learn the Basics of Python: Start by learning the basics of Python programming language such as syntax, data types, functions, loops, and conditional statements. There are many online resources available for free to learn Python.
2. Understand Data Structures and Libraries: Familiarize yourself with data structures like lists, dictionaries, tuples, and sets. Also, learn about popular Python libraries used in data science such as NumPy, Pandas, Matplotlib, and Scikit-learn.
3. Practice with Projects: Start working on small data science projects to apply your knowledge. You can find datasets online to practice your skills and build your portfolio.
4. Take Online Courses: Enroll in online courses specifically tailored for learning Python for data science. Websites like Coursera, Udemy, and DataCamp offer courses on Python programming for data science.
5. Join Data Science Communities: Join online communities and forums like Stack Overflow, Reddit, or Kaggle to connect with other data science enthusiasts and get help with any questions you may have.
6. Read Books: There are many great books available on Python for data science that can help you deepen your understanding of the subject. Some popular books include "Python for Data Analysis" by Wes McKinney and "Data Science from Scratch" by Joel Grus.
7. Practice Regularly: Practice is key to mastering any skill. Make sure to practice regularly and work on real-world data science problems to improve your skills.
Remember that learning Python for data science is a continuous process, so be patient and persistent in your efforts. Good luck!
👍5❤3