This content originally appeared on Level Up Coding - Medium and was authored by Md Shamim
Python Functions Everyone Should Know
strip()
To remove any leading and trailing space from a string we can use strip() function:
Spam = " Hello World "
print(spam)
Hello World
print(spam.strip())
Hello World
enumerate()
enumerate() allows us to iterate through a sequence while keeping track of both the index and the element. The enumerate() function accepts an iterable as an argument, such as a list, string, tuple, or dictionary.
CloudList = ["AWS", "GCP", "Azure"]
print("Without enumerate function")
for cloud in CloudList:
print(cloud)
print("\nUsages of enumerate function")
for index, cloud in enumerate(CloudList):
print(index, "-", cloud)
# Output:
Without enumerate function
AWS
GCP
Azure
Usages of enumerate function
0 - AWS
1 - GCP
2 - Azure
f-strings
While using enumerate function we have generated outputs like this:
Usages of enumerate function
0 - AWS
1 - GCP
2 - Azure
But what if we want to generate an output like the following :
0-AWS
1-GCP
2-Azure
In that case, we can use f-strings for formatting purposes:
name = "David"
profession = "DevOps Engineer"
print(f"Hi, I am {name}, Currently working as a {profession}.")
Output:
Hi, I am David, Currently working as a DevOps Engineer.
We can write a Python expression between {..} characters that can refer to variables or literal values.
Now let’s use f-strings concepts in the previous code to format outputs:
CloudList = ["AWS", "GCP", "Azure"]
print("\nUsages of f-strings")
for index, cloud in enumerate(CloudList):
data = f"{index}-{cloud}"
print(data)
Output:
Usages of f-strings
0-AWS
1-GCP
2-Azure
sort()
Using the sort() function we can sort the elements of a list.
myList = ["aws", "gcp", "azure"]
print("Ascending order : ")
myList.sort()
print(myList)
print("Descending order : ")
myList.sort(reverse=True)
print(myList)
Output:
Ascending order :
['aws', 'azure', 'gcp']
Descending order :
['gcp', 'azure', 'aws']
sorted()
The sorted() function returns a sorted list of the specified iterable object.
myList = ["A", "C", "B"]
# Ascending order
sortedList = sorted(myList)
print("Original list", myList)
print("After Sorting", sortedList)
# reversed
reversedSortedList = sorted(myList, reverse=True)
print("After reversing", reversedSortedList)
Output:
Original list ['A', 'C', 'B']
After Sorting ['A', 'B', 'C']
After reversing ['C', 'B', 'A']
Difference between sort() and sorted() function :
The main difference between the sort() and sorted() functions in Python is that the sort() function returns nothing and modifies the original sequence. In contrast, the sorted () function returns a new sequence type containing a sorted version of the given sequence.
zip()
The zip() function takes iterable (which can be zero or more), aggregates them into a tuple and returns it.
list1 = ["AWS", "GCP", "AZURE"]
list2 = ["EKS", "GKE", "AKS"]
test = zip(list1, list2)
print(test)
print(list(test))
Output:
<zip object at 0x100a2c740> # creates an objeect called zip
[('AWS', 'EKS'), ('GCP', 'GKE'), ('AZURE', 'AKS')] # returns tuple
Let’s see another example of how we can use the zip() function along with a for loop:
list1 = ["AWS", "GCP", "AZURE"]
list2 = ["EKS", "GKE", "AKS"]
for cloud, service in zip(list1, list2):
print(f"{cloud} provides {service}")
Output:
AWS provides EKS
GCP provides GKE
AZURE provides AKS
Another example with three lists:
chart1 = [1, 2, 3]
chart2 = [2, 3, 5]
chart3 = [4, 6, 7]
for x, y, z in zip(chart1,chart2,chart3):
print(x, y, z)p
Output:
1 2 4
2 3 6
3 5 7
any() and all()
any() : is a built-in Python function that returns Truewhen any of the items in an iterable object are Trueand returns Falseotherwise.
all() :is a built-in Python function that returns Truewhen all items in an iterable object are True, and returns Falseotherwise.
list1 = [True, True, True]
print("Output of any(): ", any(list1)) # Output: True
print("Output of all(): ", all(list1)) # Output: True
list2 = [False, True, True]
print("Output of any(): ", any(list2)) # Output: True
print("Output of all(): ", all(list2)) # Output: False
Let’s look into another example for a better understanding :
mydict1 = {"user": "admin", "age": "23", "password": 123}
print(any(mydict1.values())) # Output: True
print(all(mydict1.values())) # Output: True
mydict2 = {"user": "admin", "age": "25", "password": ""}
print(any(mydict2.values())) # Output: True
# Output will be "False", because value of the "password" field is empty.
print(all(mydict2.values())) # Output: False
split()
The split() method breaks up a string at the specified separator and returns a list of strings.
mystring = "This a split function test"
mylist = mystring.split(' ')
print(mylist)
# Output: ['This', 'a', 'split', 'function', 'test']
public_clouds = "AWS, GCP, AZURE"
cloud_list = public_clouds.split(',')
print(cloud_list)
# Output: ['AWS', ' GCP', ' AZURE']
numbers = "1.0 : 2.5"
numberlist1 = numbers.split('.')
print(numberlist1)
# Output: ['1', '0 : 2', '5']
numberlist2 = numbers.split(':')
print(numberlist2)
# Output: ['1.0 ', ' 2.5']
If you found this article helpful, please hit the Follow 👉 and Clap 👏 buttons to help me write more articles like this.
Thank You 🖤
Level Up Coding
Thanks for being a part of our community! Before you go:
- 👏 Clap for the story and follow the author 👉
- 📰 View more content in the Level Up Coding publication
- 💰 Free coding interview course ⇒ View Course
- 🔔 Follow us: Twitter | LinkedIn | Newsletter
🚀👉 Join the Level Up talent collective and find an amazing job
Good To Know Python Functions | Part 1 was originally published in Level Up Coding on Medium, where people are continuing the conversation by highlighting and responding to this story.
This content originally appeared on Level Up Coding - Medium and was authored by Md Shamim

Md Shamim | Sciencx (2023-03-16T13:05:37+00:00) Good To Know Python Functions | Part 1. Retrieved from https://www.scien.cx/2023/03/16/good-to-know-python-functions-part-1/
Please log in to upload a file.
There are no updates yet.
Click the Upload button above to add an update.