Function and Method in Python

Yunya Hsu
1 min readJul 8, 2021

Function

You should be familiar with that already:

#print/ show
print()

#what type it is?
type()

#make that as string
str()

#make it as integer
int()

#make it as boolean value (1=true, 0=false)
bool()

#make it as float
float()

#what’s the feature of that function?
help()

#check the max value in a list
max()

#define how many digitals behind the decimal point you want to keep.
round(1.68343, 2) => 1.68
round(1.68343, 1) => 1.7
round(1.68) => 2

String Method

string.upper()
Make every letter in the string be capital

string.count(“x”)
how many times does x show in the string

List Methods

list.index(“x”)
Get the index of the first element of a list that matches its input and.

list.count()
Get the number of times an element appears in a list.

list.append()
Add an element to the list. Please remember, it will modify the original list.

list.remove()
Remove the FIRST element of a list that matches the input. Please remember, it will modify the original list.

list.reverse()
Reverses the order of the elements in the list it is called on. Please remember, it will modify the original list.

Package then!

Use “import” to summon the package first. Prefix the name of package before the function when utilizing the function.
When import the package, will make all functionality from available to you.

#summon math packate
import math
#access the constant pi (3.141592)
math.pi

If you only need specific function, do following way:

from package import function

--

--