Programming Articles - Page 2185 of 3366

Add the element in a Python list with help of indexing

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

209 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

How to use Predicate and BiPredicate in lambda expression in Java?

raja
Updated on 13-Jul-2020 07:18:22

2K+ Views

A Predicate interface defined in java.util.function package. It represents a boolean-valued function with one argument. It is kind of a functional interface whose functional method is the test(). BiPredicate interface is similar to the Predicate interface with two arguments. It can be used as an assignment target for a lambda expression.Syntax for Predicate@FunctionalInterface public interface PredicateExampleimport java.util.*; import java.util.function.*; import java.util.stream.*; public class EmployeePredicateTest {    public static void main(String[] args) {       Employee e1 = new Employee(1, 23, "M", "Raja");       Employee e2 = new Employee(2, 13, "M", "Jai");       Employee e3 = new Employee(3, 36, "F", "Yamini");       ... Read More

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

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

409 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

609 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

605 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

casefold() string in Python Program

Hafeezul Kareem
Updated on 02-Jan-2020 06:31:14

177 Views

In this tutorial, we are going to discuss the string method str.casefold(). It doesn't take any arguments. The return value of the method is a string that is suitable for the caseless comparisons.What are caseless comparisons? For example, the german lower case letter ß is equivalent to ss. The str.casefold() method returns the ß as ss. It converts all the letters to lower case.Example Live Demo# initialising the string string = "TUTORIALSPOINT" # printing the casefold() version of the string print(string.casefold())OutputIf run the above program, you will get the following result.tutorialspointLet's see the example where caseless comparison works. If you directly compare ... Read More

How to sort a list by name using a Comparator interface in Java?

raja
Updated on 13-Jul-2020 07:00:37

846 Views

Comparator interface can be used to order the objects of user-defined classes. It is capable of comparing two objects of two different classes. We can sort a list of objects where we can't modify the object’s source code to implement Comparable interface. A lambda expression can't be executed on its own and used to implement methods that are declared in a functional interface.In the below example, we need to sort a list by name using the Comparator interface and Stream API with the help of lambda expressions.Exampleimport java.util.*; import java.util.function.*; import java.util.stream.*; public class ListSortByNameTest {    public static void main(String[] args) {       List arrayList = new ArrayList(); ... Read More

Matrix Multiplication and Normalization in C program

Arnab Chakraborty
Updated on 02-Jan-2020 06:18:27

859 Views

Matrix MultiplicationNow procedure of Matrix Multiplication is discussed. The Matrix Multiplication can only be performed, if it satisfies certain condition. Suppose two matrices are P and Q, and their dimensions are P (a x b) and Q (z x y) the resultant matrix can be found if and only if b = x. Then the order of the resultant matrix R will be (m x q).AlgorithmmatrixMultiply(P, Q): Assume dimension of P is (a x b), dimension of Q is (z x y) Begin    if b is not same as z, then exit    otherwise define R matrix as (a ... Read More

Advertisements