Insert Object in a List at a Given Position in Python

Vikram Chiluka
Updated on 27-Aug-2023 03:34:37

37K+ Views

In Python, a list is an ordered sequence that can hold several object types such as integer, character, or float. In other programming languages, a list is equivalent to an array. In this article, we will show you how to insert an item/object in a list at a given position using python. − Insert an item at a specific position in a list Insert an item at the first position in the list Insert an item at the last/end position in the list Assume we have taken a list containing some elements. We will insert an object in ... Read More

Date Validation in Python

Vikram Chiluka
Updated on 27-Aug-2023 03:29:17

33K+ Views

In this article, we will show you how to do date validation in Python. Now we see 2 methods to accomplish this task− Using datetime.strptime() function Using dateutil.parser.parse() function Method 1: Using datetime.strptime() function Algorithm (Steps) Following are the Algorithm/steps to be followed to perform the desired task − Use the import keyword, to import the datetime (To work with dates and times) module. Enter the date as a string and create a variable to store it. Enter the date format as a string and create another variable to store it. Use the try−except blocks for handling the ... Read More

Make Two Plots Side by Side Using Python

Rishikesh Kumar Rishi
Updated on 27-Aug-2023 03:24:13

32K+ Views

Using subplot(row, col, index) method, we can split a figure in row*col parts, and can plot the figure at the index position. In the following program, we will create two diagrams in a single figure.StepsCreating x, y1, y2 points using numpy.With nrows = 1, ncols = 2, index = 1, add subplot to the current figure, using the subplot() method.Plot the line using x and y1 points, using the plot() method.Set up the title, label for X and Y axes for Figure 1, using plt.title(), plt.xlabel(), and plt.ylabel() methods.With nrows = 1, ncols = 2, index = 2, add subplot ... Read More

Difference Between Method and Function in Python

karthikeya Boyini
Updated on 26-Aug-2023 08:56:37

33K+ Views

FunctionA function is a block of code to carry out a specific task, will contain its own scope and is called by name. All functions may contain zero(no) arguments or more than one arguments. On exit, a function can or can not return one or more values.Basic function syntaxdef functionName( arg1, arg2, ...):    ...    # Function_body    ...Let's create our own (user), a very simple function called sum (user can give any name he wants). Function "sum" is having two arguments called num1 and num2 and will return the sum of the arguments passed to the function(sum). When ... Read More

What is PYTHONPATH Environment Variable in Python

Rajendra Dharmkar
Updated on 26-Aug-2023 08:50:16

43K+ Views

In Python, PYTHONPATH is an environment variable that specifies a list of directories to search for Python modules when importing them. When you import a module in Python, Python looks for the module in the directories specified in sys.path, which is a list of directories that includes the current working directory and directories specified in PYTHONPATH. PYTHONPATH is an environment variable which you can set to add additional directories where python will look for modules and packages. For most installations, you should not set these variables since they are not needed for Python to run. Python knows where to find ... Read More

Add New Column to Existing DataFrame in Pandas

Hafeezul Kareem
Updated on 26-Aug-2023 08:39:59

35K+ Views

In this tutorial, we are going to learn how to add a new column to the existing DataFrame in pandas. We can have different methods to add a new column. Let's all of them.Using ListWe can add a new column using the list. Follow the steps to add a new column.Algorithm1. Create DataFrame using a dictionary. 2. Create a list containing new column data. Make sure that the length of the list matches the length of the data which is already present in the data frame. 3. Add the list to the DataFrame like dictionary element.Let's see one example.Example# importing ... Read More

Read CSV File with Pandas Without Header in Python

AmitDiwan
Updated on 26-Aug-2023 08:31:46

38K+ Views

To read CSV file without header, use the header parameter and set it to “None” in the read_csv() method.Let’s say the following are the contents of our CSV file opened in Microsoft Excel −At first, import the required library −import pandas as pdLoad data from a CSV file into a Pandas DataFrame. This will display the headers as well −dataFrame = pd.read_csv("C:\Users\amit_\Desktop\SalesData.csv")While loading, use the header parameter and set None to load the CSV without header −pd.read_csv("C:\Users\amit_\Desktop\SalesData.csv", header=None)ExampleFollowing is the code −import pandas as pd # Load data from a CSV file into a Pandas DataFrame dataFrame = pd.read_csv("C:\Users\amit_\Desktop\SalesData.csv") ... Read More

Convert PDF Files to Excel Files Using Python

Dev Prakash Sharma
Updated on 26-Aug-2023 08:29:07

35K+ Views

Python has a large set of libraries for handling different types of operations. Through this article, we will see how to convert a pdf file to an Excel file. There are various packages are available in Python to convert pdf to CSV but we will use the tabula-py module. The major part of tabula-py is written in Java that reads the pdf document and converts the Python DataFrame into a JSON object.In order to work with tabula-py, we must have java preinstalled in our system. Now, to convert the pdf file to csv we will follow the steps-First, install the ... Read More

Detect Rectangle and Square in an Image using OpenCV Python

Shahid Akhtar Khan
Updated on 26-Aug-2023 08:26:18

44K+ Views

To detect a rectangle and square in an image, we first detect all the contours in the image. Then Loop over all contours. Find the approximate contour for each of the contours. If the number of vertex points in the approximate contour is 4 then we compute the aspect ratio to make a difference between the rectangle and square. If the aspect ratio is between 0.9 and 1.1 we say it is a square else a rectangle See the below pseudocode. for cnt in contours: approx = cv2.approxPolyDP(cnt) if len(approx) == 4: x, y, w, h = ... Read More

Change Contrast and Brightness of an Image Using OpenCV in Python

Shahid Akhtar Khan
Updated on 26-Aug-2023 08:24:50

49K+ Views

In OpenCV, to change the contrast and brightness of an image we could use cv2.convertScaleAbs(). The syntax we use for this method is as follows − cv2.convertScaleAbs(image, alpha, beta) Where image is the original input image. alpha is the contrast value. To lower the contrast, use 0 < alpha < 1. And for higher contrast use alpha > 1. beta is the brightness value. A good range for brightness value is [-127, 127] We could also apply the cv2.addWeighted() function to change the contrast and brightness of an image. We have discussed it in example 2. Steps ... Read More

Advertisements