Python 3 latest code crumbs
Return multiple values from a function
May 16, 2023 by erik
With Python, we can return multiple values at once. All you need to do is list your values after the return statement, separated by commas. What we are actually doing here is returning a Python tuple. We could have written...
Write CSV with Python
Apr 4, 2023 by erik
Writing CSV with Python is easy! We can use the built-in csv
library to write to a file. Simply open a file, create a csv_writer
, and start adding rows to the file with the write_row
method which takes any iterable...
Assigning and printing Python variables
Jan 31, 2023 by erik
Variables allow us to store things in memory for as long as the Python program runs. Like a restaurant reservation under your name, a variable is a named reservation of a small part of your computer's memory. We assign a...
Creating 2D and 3D multidimensional arrays with NumPy
Nov 11, 2022 by erik
We can create ndarrays using the np.array
function. We need to use nested lists to introduce the extra dimensions. As you can see when running this example, the output (when using print
) can become a bit hard to read. This...
NumPy 1D array creation with the copy option
Nov 8, 2022 by erik
When creating NumPy arrays, the copy argument controls whether a copy of the input object is made or not. When copy is True
, any changes in the resulting array will not change the input object. However, if it is False
,...
Reading CSV with Python's built-in CSV module
Nov 6, 2022 by erik
Here's how to read a CSV file using Python. The csv.reader
returns Python lists for each row. We print them here, but you can do anything you want with the parsed data....