Category Archives: Python

Any & All in Python

Any and All are two built ins provided in python used for successive And/Or. Any Returns true if any of the items is True. It returns False if empty or all are false. Any can be thought of as a sequence of OR operations on the provided iterables. It short circuit the execution i.e. stop… Read More »

Partial Functions in Python

Partial functions allow us to fix a certain number of arguments of a function and generate a new function. Example: Output: 3145 In the example we have pre-filled our function with some constant values of a, b and c. And g() just takes a single argument i.e. the variable x. Another Example : Output: 312… Read More »

Global and Local Variables in Python

Global variables are the one that are defined and declared outside a function and we need to use them inside a function. Output: I love Geeksforgeeks If a variable with same name is defined inside the scope of function as well then it will print the value given inside the function only and not the… Read More »

Ternary Operator in Python

Ternary operators also known as conditional expressions are operators that evaluate something based on a condition being true or false. It was added to Python in version 2.5. It simply allows to test a condition in a single line replacing the multiline if-else making the code compact. Syntax : [on_true] if [expression] else [on_false] Example… Read More »

Get() method for dictionaries in Python

In python dictionaries, following is a conventional method to access a value for a key. The problem that arises here is that the 3rd line of the code returns a key error : Traceback (most recent call last): File “.\”, line 3, in print (dic[“C”]) KeyError: ‘C’ The get() method is used to avoid such… Read More »

Transpose a matrix in Single line in Python

Transpose of a matrix is a task we all can perform very easily in python (Using a nested loop). But there are some interesting ways to do the same in a single line. In Python, we can implement a matrix as nested list (list inside a list). Each element is treated as a row of… Read More »

Regular Expressions in Python | Set 2 (Search, Match and Find All)

Regular Expression in Python with Examples | Set 1 The module re provides support for regular expressions in Python. Below are main methods in this module. Searching an occurrence of pattern : This method either returns None (if the pattern doesn’t match), or a re.MatchObject that contains information about the matching part of the… Read More »