Simply Basic Python — part 3
This part of the story will tell you basically about function and its packages. If you want to read a more basic python story, please enjoy the previous story.
print() and type()
Thanks to Python that offers a bunch of built-in functions to make your life easier. In the previous story, we already used a built-in function named print()
to display anything inside the brackets. But, just remember the rules, how to write string, int, and other data types. It’s also applied in that function.
Other built-in functions, type()
used to retrieve the data type of any variables. Simply, add your variable inside the brackets, then use print()
function to display it.
x = 25; y = “common function in python”
|| you can use semicolons to separate any single instruction in python.
print(type(x)); print(type(y))
|| out : <class ‘int’>
; out :<class ‘str’>
You want to combine int
and string
on print()
function? it’s possible to do this. Let’s move to example...
height = 169; name = “Jackson”
|| then, we will print a string “The height of Jackson is 169”. For that challenge, we will convert the data type of height
to string.
height_str = str(height)
print(“The height of” + name + “is” + height_str)
And so on..
len() in list
How about list? we have already learned about that in the previous story. The list also has a built-in function. One of them is len()
, to retrieve the length of your list or the total number of items in the list.
var = [1, 2, 3, 5, 6] ; print(len(var))
Out : 5
In case you have known the function name, but have no idea to use it. Don’t worry, python will help you with help()
function. For example, we want to know how to use min()
function. Just type on your code,
help(min)
or
?min
|| Out: list of all arguments and their explanation, and also the input parameter of min
function.
Function with multiple arguments
With example sorted()
function. Based on help(sorted)
we know that sorted(iterable, /, *, key=None, reverse=False).
You can see that sorted have three arguments, iterable
: any collections of objects, e.g. List, key=None
: If you don’t specify the key, it will be None, reverse=False :
if you don’t specify, it will be False. reverse
argument is used to decide your sort is ascending or descending. If you mark reverse as False it will be the default, ascending, and vice versa.
var = [4, 6, 3, 10, 8]; print(sorted(var, reverse=True))
Out : [10, 8, 6, 4, 3]
Method
The method is a function that belongs to a python object such as string, float, int, list, etc. So, the method can be used in a specific object. Let’s move to the code...
String Method
Here are some methods that belong to string. upper()
, lower()
, count()
.. and you can explore the other by yourself.
Let’s say we have str = “This is string”
then, we apply the method. Simply just add a point behind the variable, then call the method.
str.upper()
|| we will get an output str = “THIS IS STRING”
. Also on lower()
method, it will convert all characters in the string to lower case.
How about count()
??
count()
method used to count a specific character in the string. For example, we will count how many ‘is’
in string str.
str.count(“is”)
|| the output will be 2
.
|| Remember, method belongs to a specific object.
List Method
The powerful bracket, list, have some magical method such as index()
, count()
, append()
, remove()
, reverse()
, ..
listHeight = [“James”, 178, “Ericka”, 170, “Popy”, 170]
listHeight.index(“Popy”)
|| the output will be 4
, the index of “popy”
listHeight.count(170)
|| the output will be 2
, numbers of value 170
listHeight.append(“Rilley”)
|| value “Rilley”
will be added to the last index of the list. listHeight = [“James”, 178, “Ericka”, 170, “Popy”, 170, "Rilley"]
listHeight.remove(“Ericka”)
|| this method is used to remove the first value of the list that matches the input parameter. So, “Ericka”
will be removed from the list. listHeight = [“James”, 178, 170, “Popy”, 170, "Rilley"]
listHeight.reverse() || the order of the list will be reverse. So, the output: listHeight = ["Rilley", 170, “Popy”, 170, 178, “James”]
List still has many other methods, so keep learning by doing :))
Special Seasoning
As a beginner, we should do a little upgrade of our skill by using useful packages from python to make our life easier. Let’s say we need to use pi
in mathematical, or any other constant. pi
is a constant that contains in math
packages. So, we can simply import them to our work by doing this:
import math
→ this line will import all of the math
packages. To use it, you can call the pi
with math.pi
If you don’t need all of that, you can specifically define what do you want.
from math import pi
→ it is only imported pi
from math
packages.
Then, if a package has some sub-packages, you need to specify what subpackage you want to import to your project.
Suppose you want to use the function inv()
, which is in the linalg
subpackage of the scipy
package. Then, you want to use an alias myInv
to call it. So, here we go...
from scipy.linalg import inv as myInv
That’s all about function, methods, and packages.. enjoy...