Cheatsheets Method and Function in Python3

regiapriandi | Aug. 20, 2023, 2:48 a.m.

Python is a programming language that is suitable for beginners to learn programming, Python is currently widely used in the fields of Machine Learning, Data Scientist, Software Development, and Backend Engineer. The Python version itself currently consists of two versions, namely Python 2 and Python 3. However, in use, Python3 is more widely used at this time even almost all of them use Python 3 compared to Python 2 because Python 3 is a more recent and efficient version.

In this article, I will share knowledge about methods and functions in python3 which are very important for beginners who want to learn more about the Python programming language. The methods and functions in Python3 are the basis of knowledge in programming to be able to implement programming languages in solving problems with various algorithms.

Previously, we first distinguished which one is a method and which one is a function, for example, when we call a command to find out the length of a word, we can call a function called len(), in that function we put the word we want to see the length in. a parenthesis function is len(word), so that's what the function looks like. it can be concluded that the object or variable that can be used for the function is enclosed in brackets.

Then what is the form of the method?, for example, when we want to call a command to add data to a list, here we can call the .append() method by calling the object first, followed by the method and then entering the data we want to add to the object like for example, object.append(data). it can be concluded that for objects that can be used in methods are stored in a position before the dot in the method.

These methods and functions are divided into several topics in the python programming language such as strings, lists, loops, dictionaries and others. For the first, we will discuss python methods and functions in the String section.

Table Of Contents

Strings

Lists

Dictionaries


Strings

len()

The len() function can be used to find the length of an object, this function can be implemented on strings and lists. For an example, see the following code snippet.

country = "Indonesia"
print(len(country))

output: 9

.format()

This method is used to replace { } in the print function, the method is usually used to display an output string with many variables, for example as follows.

country = "Indonesia"
print("{} is the beautiful country in the world".format(country))

output: Indonesia is the beautiful country in the world

.strip()

The strip method serves to delete the same characters at the beginning and at the end of the string, in this method we can remove unused characters at the end and at the beginning of the string by entering the character that you want to delete in the brackets method, it can be seen in the following code snippet.

name = "....Regi Apriandi+++"
print(name.strip(".+"))

output: Regi Apriandi

.title()

This can be used to return a string in the form of a title where each word begins with an uppercase letter, as in the following example.

words = "Jakarta is the biggest city in the entire world"
print(words.title())

.split()

This split method is very useful for splitting a string into separate words in the form of a list, for example it can be used to input several variables simultaneously in the program which will be separated by whitespace or spaces. For an example, see the following code snippet.

first_name, second_name, city = input().split(" ") 
print("{} {} live in {}".format(first_name, second_name, city))

Input: Regi Apriandi Purwokerto

output: Regi Apriandi live in Purwokerto

.find()

This method returns the index of the first letter in the string, for example as shown below.

name = "Regi Apriandi"
print(name.find("i"))

output: 3

.upper()

Upper, this method converts lowercase letters to uppercase letters in a string, string returns uppercase letters for all characters, for example as follows.

city = "Kuala Lumpur"
print(city.upper())

output: KUALA LUMPUR

.join()

Used to concatenate strings in the form of a list, it is very efficient to use compared to looping to combine strings from a list form into a whole string consisting of several separate strings. For an example, see the following section.

words = ["Kuala Lumpur", "is", "the", "best", "place", "for", "programers"]
join_method = " ".join(words)
print(join_method)

output: Kuala Lumpur is the best place for programers

Lists

len()

The same as the len function in the string section, the len function here is used to find out the index length of a list, can be seen from the following code snippet.

country = ["Indonesia", "Singapore", "Malaysia"]
print(len(country))

output: 3

sorted()

This sorted function returns ordered numbers and alphabets stored in a list variable without changing the original variable (unsorted variables), can be seen in the following example.

stock = [1, 10, 2, 4, 9, 6]
sorted_stock = sorted(stock)
print(sorted_stock)

output: [1, 2, 4, 6, 9, 10]

.append()

Used to add value to the list, it is very important to use it in solving problems, for an example, see the following code snippet.

city = ["Jakarta", "Semarang", "Purwokerto"]
city.append("Surabaya")
print(city)

output: ["Jakarta", "Semarang", "Purwokerto", "Surabaya"]

.remove()

This method used to delete values in the list based on the value itself or not based on the index number, for an example, see the following code.

name = ["Regi", "Apriandi"]
name.remove("Apriandi")
print(name)

output: ["Regi"]

.count()

This method is used to find the number of values in the list, for an example, see the following code snippet.

numbers = [1, 1, 3, 4, 2, 0, 9, 1, 5, 1] 
numbers_count = numbers.count(1)
print(numbers_count)

output: 4

.sort()

Returns ordered numbers and alphabets stored in a list variable with changing the original variable, can be seen in the following example.

numbers = [13, 45, 23, 92, 34]
numbers.sort()
print(numbers)

output[13, 23, 34, 45, 92]

.insert()

Insert method works to add value to list based on specific index, can be seen in the example.

state = ["Kedah", "Penang", "Negeri Sembilan"] 
state.insert(1, "Melaka")
print(state)

output: ['Kedah', 'Melaka', 'Penang', 'Negeri Sembilan']

.pop()

The pop method deletes the values in the list based on the index number, the pop method also returns the value deleted by it. An example can be seen in the following code snippet.

state = ["Kedah", "Penang", "Negeri Sembilan"] 
remove_state = state.pop(1)
print(remove_state)
print(state)

output: Penang, ['Kedah', 'Negeri Sembilan']

Dictionaries

.get()

Get is a method that can return a value based on a key in the dictionary, an example can be seen in the following code snippet.

name = {"name": "Regi Apriandi"}
print(name.get("name"))

output: Regi Apriandi

.keys()

The keys method works to return all the keys in the dictionary, for example as follows.

city = {"city": "Labuan"} 
print(city.keys())

output: dict_keys(['city'])

.values()

The values method works to return all the values in the dictionary, for example as follows.
city = {"city": "Labuan"} 
print(city.values())
output: dict_values(['Labuan'])

.items()

The items method works to return all the keys and values in the dictionary, for example as follows.

city = {"city": "Labuan"} 
print(city.items())

output: dict_items([('city', 'Labuan')])

.update()

The update method works by combining two dictionaries by changing the value of the destination dictionary to the new dictionary when the key is in it, but if there is no target key, this method will add the key and value to the previous dictionary with the new value and key dictionary, for an example can be seen in the following code snippet.

dict1 = {"city": "Sabah", "country": "Malaysia"}
dict2 = {"city": "Kuching", "island": "Borneo"}
dict1.update(dict2)
print(dict1)

output: {'city': 'Kuching', 'country': 'Malaysia', 'island': 'Borneo'}

.pop()

The same as the pop method in the list, the pop method here deletes the value and key based on the key, an example can be seen in the following code.

state = {'city': 'Kuching', 'country': 'Malaysia', 'island': 'Borneo'}
state.pop("island")
print(state)

output: {'city': 'Kuching', 'country': 'Malaysia'}

Hopefully this article can help those of you who are studying, and keep the spirit to learn, reading and learning is the key to unlocking the vastness of the world. Thank you.

About Me

I am Regi Apriandi, Welcome to the blog page, I hope this page can be useful for readers who have visited, Thank you.

RSS

0 comments

Leave a comment