Found 10784 Articles for Python

assert keyword in Python

Naveen Singh
Updated on 02-Jan-2020 09:51:34

166 Views

Every programming language has feature to handle the exception that is raised during program execution. In python the keyword assert is used to catch an error and prompt a user defined error message rather than a system generated error message. This makes it easy for the programmer to locate and fix the error when it occurs.With AssertIn the below example we use the assert key word to catch the division by zero error. The message is written as per the wish of the programmer.Example Live Demox = 4 y = 0 assert y != 0, "if you divide by 0 it ... Read More

Append Odd element twice in Python

Naveen Singh
Updated on 02-Jan-2020 09:46:46

121 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

Naveen Singh
Updated on 02-Jan-2020 09:44:40

156 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)

Naveen Singh
Updated on 02-Jan-2020 09:40:02

640 Views

Slicing is a very common technique for analyzing data from a given list in Python. But for our analysis sometimes we need to create slices of a list for a specific range of values. For example we need to print 4 elements by skipping every 4 elements from the list. In this article we will see this concept of range slicing in Python.Using range() and len()We create a for loop to go through the entire length of the list but choose only the elements that satisfy the divisibility test. In the divisibility test we check for the value of the ... Read More

after method in Python Tkinter

Naveen Singh
Updated on 02-Jan-2020 09:38:10

2K+ Views

Tkinter is a python library to make GUIs. It has many built in methods to create and manipulate GUI windows and other widgets to show the data and GUI events. In this article we will see how the after method is used in a Tkinter GUI.Syntax.after(delay, FuncName=FuncName) This method calls the function FuncName after the given delay in milisecondDisplaying WidgetHere we make a frame to display a list of words randomly. We use the random library along with the after method to call a function displaying a given list of text in a random manner.Exampleimport random from tkinter import * ... Read More

Adding a Chartsheet in an excel sheet using Python XlsxWriter module

Naveen Singh
Updated on 02-Jan-2020 09:34:40

352 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

Naveen Singh
Updated on 02-Jan-2020 09:32:17

129 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

Naveen Singh
Updated on 02-Jan-2020 09:29:58

5K+ 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

Naveen Singh
Updated on 02-Jan-2020 07:07:04

297 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

Naveen Singh
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

Advertisements