List Methods in Python: in, not in, len, min, max

Pavitra
Updated on 01-Jul-2020 07:19:09

26K+ Views

In this article, we will learn about various types of list methods available to us in Python 3.x. Or earlier. These operators allow us to perform the basic operations on the list content.In & Not in operators“in” operator  − This operator is used to check whether an element is present in the passed list or not. Returns true if the element is present in the list otherwise returns false.“not in” operator  − This operator is used to check whether an element is not present in the passed list or not. Returns true if the element is not present in the ... Read More

Internal Working of the foreach Loop in PHP

AmitDiwan
Updated on 01-Jul-2020 07:18:25

277 Views

The ‘foreach’ loop in PHP helps in accessing key value pairs within an array. The ‘foreach’ loop works with arrays only, with the advantage that a loop counter wouldn’t need to be initialized. In addition to this, no condition needs to be set that would be needed to exit out of the loop. The ‘foreach’ loop implicitly does this too.Example Live DemoOutputJoe Hannah Paul SannaAn array of string values is defined and the foreach loop is used to access every element inside the array by giving a variable name. The ‘echo’ is used to display every element of the array on ... Read More

Iterate Over a Dictionary in Python

Pavitra
Updated on 01-Jul-2020 07:15:00

314 Views

In this article, we will learn about iteration/traversal of a dictionary in Python 3.x. Or earlier.A dictionary is an unordered sequence of key-value pairs. Indices can be of any immutable type and are called keys. This is also specified within curly braces.Method 1 − Using iterables directlyExampledict_inp = {'t':'u', 't':'o', 'r':'i', 'a':'l', 's':'p', 'o':'i', 'n':'t'} # Iterate over the string for value in dict_inp:    print(value, end='')OutputtrasonMethod 2 − Using iterables for values of the dictionaryExampledict_inp = {'t':'u', 't':'o', 'r':'i', 'a':'l', 's':'p', 'o':'i', 'n':'t'} # Iterate over the string for value in dict_inp.values():    print(value, end='')OutputoilpitMethod 3 − ... Read More

Check If a String Contains a Specific Substring

AmitDiwan
Updated on 01-Jul-2020 07:14:54

767 Views

To check if a string contains a specific substring, the ‘strlen’ and ‘strpos’ functions can be used. The ‘strlen’ gives the entire length of the string. The function ‘strpos’ finds the position wherein the substring occurs first in the string.Example Live DemoOutputThe substring is present within the string.Two strings are defined and the position of the second string in the first string is checked with the help of the ‘strpos’ function and compared with the length of the first string. Relevant message is displayed on the console.

Iterate Over a List in Python

Pavitra
Updated on 01-Jul-2020 07:13:07

443 Views

In this article, we will learn about iterating/traversing over a list in Python 3.x. Or earlier.A list is an ordered sequence of elements. It is a non-scalar data structure and mutable in nature. A list can contain distinct data types in contrast to arrays storing elements belonging to the same data types.Method 1 − Using iterable without indexExamplelist_inp = ['t', 'u', 't', 'o', 'r', 'i', 'a', 'l', 's', 'p', 'o', 'i', 'n', 't'] # Iterate over the list for value in list_inp:    print(value, end='')Method 2 − Using general way via indexExamplelist_inp = ['t', 'u', 't', 'o', 'r', 'i', ... Read More

Iterate Over a Set in Python

Pavitra
Updated on 01-Jul-2020 07:11:50

3K+ Views

In this article, we will learn about iterating/traversing over a set in Python 3.x. Or Earlier.It is an unordered collection of objects without any duplicates. This can be done by enclosing all the elements within curly braces. We can also form sets by using type casting via a keyword “set”.Method 1 − Using iterables without indexesExampleset_inp = {'t', 'u', 't', 'o', 'r', 'i', 'a', 'l', 's', 'p', 'o', 'i', 'n', 't'} # Iterate over the set for value in set_inp:    print(value, end='')Method 2 − Using indexed access by converting to list typeExampleset_inp = list({'t', 'u', 't', 'o', 'r', ... Read More

Late Static Bindings in PHP

AmitDiwan
Updated on 01-Jul-2020 07:11:33

1K+ Views

The basic idea behind late static bindings is that the concept of inheritance and the concept of ‘self’ keyword don’t follow the same rules. For example, a method ‘fun’ in parent class called in the child class won’t make ‘self’ refer to the child (as expected).The concept of late static bindings brings in a new keyword ‘static’, which when used, binds the function to the runtime class, or the class where the function was first used. In addition to this, any static function or variable is usually executed during runtime and not during compile time. Hence, if a value needs ... Read More

Meaning and Usage of & Assignment Operator in PHP

AmitDiwan
Updated on 01-Jul-2020 07:10:22

848 Views

Instead of copying the data from one variable to another one, the changes made to an array or object can be made to another using the ‘=&’ operator. This is known as the ‘assignment by reference’ method, which means both the values or objects will point to the same data, and no copies of the data are made. This way, data redundancy is avoided.Example Live DemoOutputThe value is : 89Inside the tags, two values are declared wherein the second value is the assignment by reference of the first value. Next, the value of the first variable is changed and the ... Read More

Iterate Over Characters of a String in Python

Pavitra
Updated on 01-Jul-2020 07:10:14

2K+ Views

In this article, we will learn about iterating/ traversing over characters of a string in Python 3.x. Or earlier.The string is a collection of characters which may contain spaces, alphabets or integers. They can be accessed using indexes or via references . Some commonly implemented methods are shown below.Method 1 − The direct itertor without indexingExamplestring_inp = "tutorialspoint" # Iterate over the string for value in string_inp:    print(value, end='')Method 2 − The most common way using index based accessExamplestring_inp = "tutorialspoint" # Iterate over the string for value in range(0, len(string_inp)):    print(string_inp[value], end='')Method 3 − The ... Read More

MongoDB Aggregate Group Multiple Result

AmitDiwan
Updated on 01-Jul-2020 07:03:26

743 Views

To aggregate multiple result, use $group in MongoDB. Let us create a collection with documents −> db.demo765.insertOne( ... ...    { ...       Name:"John", ...       "Category":"ComputerScience", ...       "SubjectName":"MongoDB", ...       "Marks":75 ...    } ... ); {    "acknowledged" : true,    "insertedId" : ObjectId("5eb054525637cd592b2a4b01") } > > db.demo765.insertOne( ...    { ...       Name:"John", ...       "Category":"ComputerScience", ...       "SubjectName":"MySQL", ...       "Marks":85 ...    } ... ); {    "acknowledged" : true,    "insertedId" : ObjectId("5eb054525637cd592b2a4b02") } > db.demo765.insertOne( ... ... Read More

Advertisements