A 2-dimensional (2D) list is simply a list of lists (lists within a list). Here are some examples of 2D lists in Python.
Continue readingCategory Archives: Python
Convert Bytes to Float Numbers in Python
This article discusses how to use struct and Numpy packages in Python to convert bytes to floats and vice-versa. Before we do that, however, let’s briefly discuss the difference between bytes and floating-point numbers.
Continue readingSplit Input by Space in Python
There are two key functions we need to use here: input(<prompt>) and str.split(<char>).
Continue readingRedirect Stdout to a File in Python
Python uses the following two file objects on the sys library as output streams:
- sys.stdout – used for output of print() function and the prompt in input() function,
- sys.stderr – is an output stream for Python’s prompts (like the help() function), errors, and exceptions.
Check if an Argument in a Function Is Passed in Python
Consider the following Python function:
1 2 3 4 5 |
def func1(val1, val2, val3): return val1 * val2 + val3 # Calling the function. result = func1(3, 4, 6) |
Random Delays in Web Scraping in Python
Adding random delays between queries is a great way to avoid getting blocked while running scripts in Python. This technique can be used to ensure that the script is not making too many requests in a short period and thus, preventing it from being blocked by the server.
Continue readingSuppress Console Output in Python
Python interpreter uses the following three file objects on the sys library for standard input, output, and errors (exceptions):
Continue readingPrint Memory Address of Python Variable
Every time a Python object is stored in memory, it is assigned an integer value that references its address. The address is used to distinguish between different objects present in memory.
Continue readingUsing Serial Read or Readline Functions in Python
This article discusses how to read data from serial ports using serial.read() and serial.readline() functions on the pyserial library. Before we do that, however, let us cover some prerequisites.
Continue readingWriting Multi-line Strings Into Excel Cells in Python
Multi-line strings can come in two forms in Excel files (shown by B and C in the Figure below). In B, the sentence is broken word by word so that each word appears in its line, whereas, in C, text wrapping is implemented.
Continue reading