Found 10476 Articles for Python

Append Odd element twice in Python

Pradeep Elance
Updated on 02-Jan-2020 09:46:46

223 Views

In this article we will see how to take a list which contains some odd numbers as its elements and then add those odd elements repeatedly into the same list. Which means if odd number is present twice in a list then after processing the odd number will be present four times in that same list.For this requirement we will have many approaches where we use the for loop and the in condition or we take help of of itertools module. We also check for the odd condition by dividing each element with two.Example Live Demofrom itertools import chain import numpy ... Read More

Append at front and remove from rear in Python

Pradeep Elance
Updated on 02-Jan-2020 09:44:40

236 Views

When using Python for data manipulation we frequently and remove elements from list. There are methods which can do this effectively and python provides those function as part of standard library as well as part of external library. We import the external library and use it for this addition and removal of elements. Below we will see two such approaches.Using + operatorExample Live Demovalues = ['Tue', 'wed', 'Thu', 'Fri', 'Sat', 'Sun'] print("The given list : " ,values) #here the appending value will be added in the front and popping the element from the end. result = ['Mon'] + values[:-1] print("The values ... Read More

Alternate range slicing in list (Python)

Yaswanth Varma
Updated on 14-Jul-2025 14:22:54

857 Views

Slicing is used to extract a portion of a sequence (such as a list, a tuple, string) or other iterable objects. Python provides a flexible way to perform slicing using the following syntax. list[start:stop:step] Alternate Range Slicing The alternate range slicing is used to retrieve the elements from a list by skipping the elements at a fixed interval, using the step parameter. For example, if we want every second element from a list, we set step=2. Let’s observe some examples to understand how alternate range slicing works. Example 1 Consider the following ... Read More

Adding a Chartsheet in an excel sheet using Python XlsxWriter module

Pradeep Elance
Updated on 02-Jan-2020 09:34:40

469 Views

In addition to python’s own libraries, there are many external libraries created by individual authors which do a great job of creating additional features in python. Xlsx library is one such library which not only creates excel files containing data from python programs but also creates charts.Creating Pie ChartIn the below example we will create a pie chart using the xlsxwriter writer. Here we first define a workbook then add a worksheet to it in the next step we define the data and decide on the columns where the data will be stored in an excel file based on those ... Read More

Add the element in a Python list with help of indexing

Pradeep Elance
Updated on 02-Jan-2020 09:32:17

202 Views

A python list is a collection data type that is ordered and changeable. Also, it allows duplicate members. It is the most frequently used collection data type used in Python programs. We will see how we can add an element to a list using the index feature.But before adding the element in an existing link, let's access the elements in a list using the index feature.Accessing List using Indexevery element in the list is associated with an index and that is how the elements remain ordered. We can access the elements by looping through the indexes. The below program prints ... Read More

Add similar value multiple times in a Python list

Pradeep Elance
Updated on 02-Jan-2020 09:29:58

7K+ Views

There are occasions when we need to show the same number or string multiple times in a list. We may also generate these numbers or strings for the purpose of some calculations. Python provides some inbuilt functions which can help us achieve this.Using *This is the most used method. Here we use the * operator which will create a repetition of the characters which are mentioned before the operator.Example Live Demogiven_value ='Hello! ' repeated_value = 5*given_value print(repeated_value)Running the above code gives us the following result:Hello! Hello! Hello! Hello! Hello!Using repeatThe itertools module provides repeat function. This function takes the repeatable string ... Read More

Calculate n + nn + nnn + u + n(m times) in Python Program

Hafeezul Kareem
Updated on 02-Jan-2020 07:07:04

398 Views

In this tutorial, we are going to write code to find the sum of the series n + nn + nnn + ... + n (m times). We can do it very easily in Python. Let's see some examples.Input: n = 1 m = 5 Series: 1 + 11 + 111 + 1111 + 11111 Output: 12345AlgorithmFollow the below steps to solve the problem.1. Initialise the n and m. 2. Initialise total to 0. 3. Make the copy of n to generate next number in the series. 4. Iterate the loop m times.    4.1. Add n to the total. ... Read More

Boolean Indexing in Pandas

Hafeezul Kareem
Updated on 02-Jan-2020 06:58:52

2K+ Views

Boolean indexing helps us to select the data from the DataFrames using a boolean vector. We need a DataFrame with a boolean index to use the boolean indexing. Let's see how to achieve the boolean indexing.Create a dictionary of data.Convert it into a DataFrame object with a boolean index as a vector.Now, access the data using boolean indexing.See the example below to get an idea.Exampleimport pandas as pd # data data = {    'Name': ['Hafeez', 'Srikanth', 'Rakesh'],    'Age': [19, 20, 19] } # creating a DataFrame with boolean index vector data_frame = pd.DataFrame(data, index = [True, False, True]) ... Read More

Calendar Functions in Python - ( calendar(), month(), isleap()?)

Hafeezul Kareem
Updated on 02-Jan-2020 06:53:15

596 Views

We can perform the calendar operations using the calendar module in Python. Here, we are going to learn about the different methods of calendar class instance.calendar.calendar(year)The calendar class instance returns the calendar of the year. Let's see one example.Example Live Demo# importing the calendar module import calendar # initializing year year = 2019 # printing the calendar print(calendar.calendar(year))OutputIf you run the above code, you will get the following results.calendar.firstweekday()The method calendar.firstweekday() returns the first weekday in the week i.e.., MONDAY.Example Live Demo# importing the calendar import calendar # getting firstweekday of the year print(calendar.firstweekday())OutputIf you run the above program, you will get ... Read More

Calendar Functions in Python -(monthrange(), prcal(), weekday()?)

Hafeezul Kareem
Updated on 02-Jan-2020 06:47:01

594 Views

We are going to explore different methods of calendar module in this tutorial. Let's see one by one.calendar.monthrange(year, month)The method calendar.monthrange(year, month) returns starting weekday number and number of days of the given month. It returns two values in a tuple. Let's see one example.Example Live Demo# importing the calendar module import calendar # initializing year and month year = 2019 month = 1 # getting the tuple of weekday and no. of days weekday, no_of_days = calendar.monthrange(year, month) print(f'Weekday number: {weekday}') print(f'No. of days: {no_of_days}')OutputIf you run the above code, you will get the following results.Weekday number: 1 No. of ... Read More

Advertisements