Python is a widely used programming language emphasizes on code readability. Its syntax allows programmers to express concepts in fewer lines of code. For those who are familiar with python, the following practices with python fundamentals could help you to improve your Python coding skills.
Level Up Your Skills with Python Function
Function is one of Python's fundamental that must be mastered by every data scientist. It consists of a group of related statements that runs when it is called and performs a specific task. Python function starts with def keyword. It is also avoiding repetition which makes its code reusable. Here is an example of a Python function:
def first():
print("hello world")
*"def" is a head of Python’s function, while "first" is the name of function.
Then, once a function has defined, it can be called from another function or program by just typing the function name such as:
first()
hello world
Furthermore, we can also use arguments as the value that are sent to the function when it is called by placing them inside the function’s brackets:
def second(a,b):
area = a*b
print(area)
second(2,5)
10
*a and b are the arguments for the second function
Lastly, to end the function and returns the result of its call, you can use a Return Statement as follow:
def third(a,b):
area = a*b
return(area)
third(10,20)
200
Writing Code to Process Files and Directories
Learn how to process files and directories is one of the python fundamentals in data science. In Python, there are several built-in modules and functions that could be used for processing files and directories divided into os, os.path, shutil, and pathlib.
Moreover, there are also several common files and directories operations in python that broken down into: open and read files, create a directory, delete files and directories, copy files and directories, and move files and directories.
The first operation is open and read files in python. Reading a file is pretty easy in python, it only requires the open() function. The open() takes 2 arguments consist of file and mode, for example:
In [ ]: with open('data.txt ', 'r ') as f:
data = f.read()
*This code will open data.txt as a read only file, if we want to write a data to a file we can change the mode from 'r' to 'w'
Next, the second operation is creating a directory. To create a new directory in Python, you can use os or pathlib module. In python, there are two directories that could be created with os module that divided into single directory and multiple directories.
Create a single directory:
In [ ]: import os
os.mkdir('example_directory/')
*This code will create a directory named "example_directory". Keep in mind if there is already a directory with a same name, "os.mkdir()" will raise "FileExistsError".
Create multiple directories:
In [ ]: import os
os.makedirs('dir1/dir2/dir3')
*We can make multiple directories with os.makedirs(). As seen in the example, you only need to specify the directory and subdirectory names.
Then, the third operation is delete files and directories by using os.remove(): functions as follow:
In [ ]: import os
data_file = 'C:\\Users\\multimatics\\Desktop\\Test\\data.txt'
os.remove(data_file)
*This code will delete the data.txt inside the specified directory.
After that, the fourth operation is copy files and directories. The operations can be carried out by using the shutil module:
For copying files:
In [ ]: import shutil
src = 'path/to/file.txt'
dst = 'path/to/destination_dir'
shutil.copy2(src, dst)
*As seen above, shutil.copy2() can be used to copy a file to another directory
For copying directories:
In [ ]: import shutil
shutil.copytree('data', 'backup')
*Keep in mind that the destination directory should not exist along with missing parent directories.
Lastly, the fifth operations is move files and directories to another location by using shutil.move():
In [ ]: import shutil
shutil.move('dir_1/', 'backup/')
*As seen above, it moves the directory dir_1 to the directory called backup. If you want to move a file, simply change the source to file rather than a directory.
Conclusion
The best way we learn anything is by practice and exercise questions. This also applies when you are interested to lurk into the python programming world, so that you can become more familiar with Python and start to create your own python program,
Reference:
Ndlovu, V. Working With Files in Python. January 27th, 2021. Retrieved From: https://realpython.com/working-with-files-in-python/
Oliphant, T. E. (2007). Python for scientific computing. Computing in Science & Engineering, 9(3), 10-20.
Payne, J. (2010). Beginning python: Using python 2.6 and python 3.1. Wiley.