Python 3 latest code crumbs

Creating 2D and 3D multidimensional arrays with NumPy

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 array 2D 3D
NumPy 1D array creation with the copy option

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,...

numpy array
Reading CSV with Python's built-in CSV module

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....

csv file open
NumPy float types: a demonstration of precision

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....

numpy float
Your for-loop might be missing an else block

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...

else for-loop
Create an iterable with a constructor

Create an iterable with a constructor

May 24, 2022 by erik

Add a constructor to the EvenNumbers class that accepts a parameter called max. This max comes instead of the hard-coded value of 8. So the iterator must return values until it reaches this max value, but it must include the...

iterators