How to check if a file exists in Python without exceptions?

File operations are frequently used in the programs. If we try to open a file whose path doesn’t exist, an error is thrown and the program halts. To avoid this, we can first check if the file exists in the given path and then open the file. In today’s article, let’s discuss different ways to check if a file exists in Python without using the try-except/finally blocks.  We will be using methods from the pathlib module and os modules. Method 1: Using pathlib.Path object Python has an in-built module named pathlib that can be used to check for any file … Read more

How to print the full NumPy array without any truncation in the output?

NumPy module in Python is extensively used while working with arrays. When you are working on large arrays, if you try to print the elements of the array, you see that the output is being truncated. For example, consider the following code snippet: import numpy as np arr=np.arange(1001) print(arr)   Output : [ 0 1 2 … 998 999 1000]   As you can see from the output, the elements after the 3rd element are being truncated and the last few elements are being shown. This happens when the elements in the array exceed 1000 elements. While this might not … Read more

How to read inputs as numbers in Python?

In Python, one can use the input() method to receive the user input. For example, if you want the user to enter two values x, y, and then find the sum of those values, you can do it as follows : x=input(“Enter the value of x: “) y=input(“Enter the value of y: “) print(“x+y is: “,x+y) Output : Enter the value of x: 5 Enter the value of y: 6 x+y is 56 The sum of two numbers 5 and 6 should be 11. However, the output is 56. If you’re wondering why? Then it is because the input() function … Read more

How to create a pandas series from multiple numpy arrays?

Let’s say you have two numpy arrays as shown below: a=np.array([1,2,3,4,5]) b=np.array([6,7,8,9,10]) Problem : You want a create a pandas series from multiple numpy arrays as shown below. 0 0 1 1 2 2 3 3 4 4 5 5 6 6 7 7 8 8 9 9 10   Solution: Join the contents of the array and then add it as a Series 1. First, let’s join the arrays using any of these methods- np.concatenate(), np.join() ,np.hstack, np.append()   2. Once that is done, simply convert this array to pandas Series. import numpy as np import pandas as pd … Read more

Different ways to create a nxn matrix and fill it with a checkerboard pattern

The checkerboard pattern represents a set of 64 black and white squares. Similar to the pattern in the chessboard. In real-time, checkerboard patterns are extensively used to create a no pixel blank area(blank grid). Checkerboard pattern can be represented easily using an array of size 8*8. We can use the numpy module to create an array and fill it with a checkerboard pattern using various functions from numpy. In this article, let’s learn how to create an n*n matrix in a checkerboard pattern. NOTE: If you don’t have numpy, you can install it using the below command: pip install numpy … Read more