본문 바로가기

Language

(21)
[Error] 5 Common Python Mistakes and How to Fix Them Mistake1 : Indentation and Spaces The Indentation Error can occur when the spaces or tabs are not placed properly. If an indentation error occurs, the transition options of the tabs and spaces on the editor must be changed. Then tabs will automatically be cnaged to 4 spaces. nums = [11, 30, 44, 54] for num in nums: # Using tabs square = num ** 2 # using spaces(4) print(square) # Indentation Eror..
[time/timeit] Measure the time efficiency of the code 1. What is Algorithm Analysis? An algorithm is a generic, step-by-step list of instructions for solving a problem. It is a method for solving any instance of the problem such that given a particular input, the algorithm produces the desired result. Algorithm analysis is concerned with comparing algorithms based on upon the amount of computing resources that each algorithm uses. We can measure th..
[requests] Interacting with Web Pages 1. What is HTTP and requests module? HTTP stands for Hyper Text Transfer Protocol. WWW is about communication between web clients and servers. Communication between client computer and web servers is done by sending HTTP requests and receiving HTTP Response. The requests module allows us to send HTTP requests using Python. The HTTP requests returns a Response object with all the response data(co..
[Error] pipenv - ModuleNotFoundError Question : On ubuntu, i'm trying to install python package inside Pipenv environment. But, it is giving this error : ModuleNotFoundError : No module name 'distutils.cmd' Answer : The issue is resolved by installing distutils manually with 'sudo apt install python3-distutils'. It is working fine below 3.9.9. Source from : https://github.com/pypa/pipenv/issues/4882
[unittest] Unit Testing Our Code 1. What is Unit Testing? Unit testing is a method for testing software that looks at the smallest testable pieces of code, called units, which are tested for correct operation. By doing unit testing, we can verify that each part of the code, including helper functions may not be exposed to the user, works correctly and as intended. # calc.py def add(x, y): """Add Function""" return x + y def sub..
[Syntax] Special arguments *args and **kwargs 1. What is *args and **kwargs arguments? *args allows us to pass a variable number of non-keyword arguments to a Python function. In the function, we should use an atserisk(*) before the parameter name to pass a variable number of arguments.Thus, we're sure that these passed arguments make a tuple inside the function with the same name as the parameter excluding *. **kwargs allows us to pass a v..
[Error] Why does substring slicing with index out of range work? Question : Why doesn't example[999:9999] result in error? Since example[999] does, what is the motivation behind it? Answer : [999:9999] isn't an index, it's a slice, and has different semantics. From the python intro : "Degenerate slice indices are handled gracefully : an index that is too large is replaced by the string size, an upper bound smaller than the lower bound returns an empty string"..
[os] User Underlying Operating System 1. What is OS Module? The OS module provides a portable way of using operating system dependent functionality. 2. Importing OS module import os # check all attributes and methods print(dir(os)) 3. Useful methods of OS module # Get current working directory print(os.getcwd(), '\n') # Change working direcotry os.chdir('...') # List directory : List all files and directory in current working direct..
[sys] The way to import module not found 1. Using sys.path The Python sys.path is a list of directories where the Python interpreter searches for modules. When a module is claimed in a project file, it will search through each one of the directories in the list. So, we can easily load Python files in any directory by modifying element of sys.path. 1.1 Check a list of sys.path import sys print(sys.path) 1.2 Append directories to sys.pat..
[OOP] Implement Logic Gates using Class Inheritance 1. What is Class Inheritance? Inheritance is the ability for one class to be related to another class. Python child classes can inherit characteristic data and behavior from a parent class. Above structure is called Inheritacne hierarchy. This implies that lists inherit important characteristics from sequence, namely the ordering of the underlying data and the operations such as concatenation, r..
[OOP] Creating Fraction Class using OOP 1. Purpose of creating Fraction Class Our purpose on this plot is to create a Python class called "Fraction". The operations for the Fraction class is as follows : Add, Subtract, Multiply, and Divide. And also, this class can show fractions using the standard "slash" form. 2. Defining Class Fraction needs two pieces of data, the numerator and the denominator. In OOP, all classes should provide c..
[Syntax] Meaning of dot Notation We should also notice the familiar "dot" notation for asking an object to invoke a method. List.append(item) can be read as "ask the object List to perform its append method and send it the value item." Even simple data objects such as integers can invoke methods in this way. (54).__add__(21) In this fragment we are asking the integer object 54 to execute its add method (called __add__ in Python..
[collections] Make frequency table automatically 1. What is the collections library? The Python library collections implements specialized container datatypes providing alternatives to Python's general purpose built in containers, dict, list, set, and tuple. The Counter method provide dict subclass for counting hashable objects. When we pass a string or list to the Counter constructor as a factor, an object is returned that tells us how many t..
[chardet] Encoding and Representing Text 1. What is Encoding? Encoding is a processing or processing method that converts the form or form of encoding information, and in the case of character encoding, it is a method of encoding a set of characters. Since the computer does not accept number other than 0 and 1, encoding is required to express chracters. ASCII encoding has 128 characters codes. In the case of ASCII encdoing, in addition..
[csv] Read files 1. What does csv library do? The Python's library called csv allows data from various format to be imported in our program. 2. How to use it? 2.1 Open files one by one First, open the file we want using open buillt-in function. And then, make reader object using reader method from csv library. We should close the file we open after we make reader obejct. The final data we will use will be stored..
[folium] Visualization Map on Python 1. What is Folium? Folium makes it easy to visualize data that's been manipulated in Python on an interactive leaflet map. It enables both the binding of data to a map for choropleth visualizations as well as passing rich vector/raster/HTML visualizations as markers on the map. 2. How to use Folium Step 1 : Import library folium import folium Step 2 : Set the center of map's frame To start map w..
[heapq] Implementing Binary Heap in Python 1. What is Binary Heap? A Binary Heap is a heap, a tree which obeys the property that the root of any tree is greater than or equals to all its children. The primary use of such a data structure is to implement a priority queue. 2. Implementing Binary Max Heap class PriorityQueue: def __init__(self): self.queue = [] def _heapify(self, n, i): largest = i l = 2 * i + 1 r = 2 * i + 2 if l < n and s..
[queue] Implementing Priority Queue in Python 1. What is Priority Queue? A Priority Queue is a special type of queue in which each element is associated with a priority value. And, elements are served on the basis of their priority. Generally, the value of the element itself is considered for assigning the priority. For example, the element with the highest value is considered the highest priority element. 2. Implementing Priority Queue cla..
[Syntax] Exception 1. Exception Statement 1.1 try - except statement If there is any error while executing codes under try statement, then code under except statement will execute. There are three types of try - except statement. try - except : Codes under except statement will execute whatever error it is. try - except [Raised Error] : Codes under statement except will execute when error is in written case. try -..
[beautifultable] Print List in Table Format 1. What is BeautifulTable? This package provides BeautifulTable Class for easily printing tabular data in a visually appealing format to a terminal. Features included but not limited to : Full customization of the look and the feel of the table Build the table as you wish, by adding rows, or by columns or even mixing both these approaches. Full support for colors using ANSI sequences or any libr..