Python 3 latest code crumbs
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....
NumPy float types: a demonstration of precision
Oct 11, 2022 by erik
The different NumPy float types allow us to store floats in different precision, dependent on the number of bits we allow the float to use. The larger the number of allowed bits, the more precision our array’s elements will have....
Your for-loop might be missing an else block
Aug 30, 2022 by erik
There's a little-known construct in Python that can be quite useful: using else in combination with loops. That's right, for-loops and while-loops can be followed by an else
block! Let's look at how you can use the else block in...