Lakshmi Srinivas

Lakshmi Srinivas

233 Articles Published

Articles by Lakshmi Srinivas

Page 17 of 24

What is the associativity of Python's ** operator?

Lakshmi Srinivas
Lakshmi Srinivas
Updated on 17-Jun-2020 353 Views

From the Python docs:Operators in the same box group left to right (except for comparisons), including tests, which all have the same precedence and chain from left to right — see section Comparisons — and exponentiation, which groups from right to left).So the ** operator(exponentiation) is right to left associative. For example,2 ** 3 ** 4 will be evaluated as: (2 ** (3 ** 4))For example,print(2 ** 3 ** 0)This will give the output:2

Read More

How to put comments inside a Python dictionary?

Lakshmi Srinivas
Lakshmi Srinivas
Updated on 17-Jun-2020 718 Views

You can put comments like you normally would anywhere in a python script. But note that you can only put single line comments using #. Multiline comments act like strings and you cannot put just a string in between definition of a dict. For example, the following declaration is perfectly valid:testItems = {    'TestOne': 'Hello',    # 'TestTwo': None, }But the following is not:testItems = {    'TestOne': 'Hello',    """    Some random    multiline comment    """ }

Read More

How to sort a Python dictionary by datatype?

Lakshmi Srinivas
Lakshmi Srinivas
Updated on 17-Jun-2020 236 Views

You can sort a list of dictionaries by values of the dictionary using the sorted function and passing it a lambda that tells which key to use for sorting. For example, A = [{'name':'john', 'age':45},      {'name':'andi', 'age':23},      {'name':'john', 'age':22},      {'name':'paul', 'age':35},      {'name':'john', 'age':21}] new_A = sorted(A, key=lambda x: x['age']) print(new_A)This will give the output:[{'name': 'john', 'age': 21}, {'name': 'john', 'age': 22}, {'name': 'andi', 'age': 23}, {'name': 'paul', 'age': 35}, {'name': 'john', 'age': 45}]You can also sort it in place using the sort function instead of the sorted function. For example, A ...

Read More

How to draw a circular gradient in HTML5?

Lakshmi Srinivas
Lakshmi Srinivas
Updated on 15-Jun-2020 332 Views

This method returns a CanvasGradient object that represents a radial gradient that paints along the cone given by the circles represented by the arguments. The first three arguments define a circle with coordinates (x1, y1) and radius r1 and the second a circle with coordinates (x2, y2) and radius r2.createRadialGradient(x0, y0, r0, x1, y1, r1)Here are the parameter values of the createRadialGradient() method −S.NoParameter & Description1x0x-coordinate- Starting point of the gradient2y0y- coordinate - Starting point of the gradient3r0Radius of the starting circle4x1x-coordinate - Ending point of the gradient5y1y- coordinate - Ending point of the gradient6r1Radius of the ending circleYou can ...

Read More

Java program to find the permutation when the values n and r are given

Lakshmi Srinivas
Lakshmi Srinivas
Updated on 13-Mar-2020 388 Views

Permutation refers a number of ways in which set members can be arranged or ordered in some fashion. The formula of permutation of arranging k elements out of n elements is −nPk = n! / (n - k)!Algorithm1. Define values for n and r. 2. Calculate factorial of n and (n-r). 3. Divide factorial(n) by factorial(n-r). 4. Display result as a permutation.Exampleimport java.util.Scanner; public class Permutation { static int factorial(int n) { int f; for(f = 1; n > 1; n--){ ...

Read More

How to Humanize numbers with Python?

Lakshmi Srinivas
Lakshmi Srinivas
Updated on 05-Mar-2020 387 Views

If you want something that converts integers to words like 99 to ninety-nine, you have to use an external package or build one yourself. The pynum2word module is pretty good at this task. You can install it using −$ pip install pynum2wordThen use it in the following way −>>> import num2word >>> num2word.to_card(16) 'sixteen' >>> num2word.to_card(23) 'twenty-three' >>> num2word.to_card(1223)'one thousand, two hundred and twenty-three'If you want to get results like 1.23 million for 1, 230, 000, you can use the humanize library to do so. You can install it using −$ pip install humanizeThen use it in the following way ...

Read More

How to create a lambda inside a Python loop?

Lakshmi Srinivas
Lakshmi Srinivas
Updated on 05-Mar-2020 1K+ Views

You can create a list of lambdas in a python loop using the following syntax −Syntaxdef square(x): return lambda : x*x listOfLambdas = [square(i) for i in [1,2,3,4,5]] for f in listOfLambdas: print f()OutputThis will give the output −1 4 9 16 25You can also achieve this using a functional programming construct called currying. examplelistOfLambdas = [lambda i=i: i*i for i in range(1, 6)] for f in listOfLambdas: print f()OutputThis will give the output −1 4 9 16 25

Read More

How to overload python ternary operator?

Lakshmi Srinivas
Lakshmi Srinivas
Updated on 05-Mar-2020 436 Views

The ternary operator cannot be overloaded. Though you can wrap it up in a lambda/function and use it. For exampleresult = lambda x: 1 if x < 3 else 10 print(result(2)) print(result(1000))OutputThis will give the output −1 10

Read More

How to view a list of all Python operators via the interpreter?

Lakshmi Srinivas
Lakshmi Srinivas
Updated on 05-Mar-2020 360 Views

The help method in the interpreter is very useful for such operations. It provides a rich set of special inputs that you can give to it to get information about the different aspects of the language. Forgetting operator lists, here are some of the commands you can use:All operators>>> help('SPECIALMETHODS')Basic operators>>> help('BASICMETHODS')Numeric operators>>> help('NUMBERMETHODS')Other than operators you can also get attribute methods, callable methods, etc using −>>> help('MAPPINGMETHODS') >>> help('ATTRIBUTEMETHODS') >>> help('SEQUENCEMETHODS1') >>> help('SEQUENCEMETHODS2') >>> help('CALLABLEMETHODS')

Read More

How to find the average of non-zero values in a Python dictionary?

Lakshmi Srinivas
Lakshmi Srinivas
Updated on 05-Mar-2020 3K+ Views

You can do this by iterating over the dictionary and filtering out zero values first. Then take the sum of the filtered values. Finally, divide by the number of these filtered values. examplemy_dict = {"foo": 100, "bar": 0, "baz": 200} filtered_vals = [v for _, v in my_dict.items() if v != 0] average = sum(filtered_vals) / len(filtered_vals) print(average)OutputThis will give the output −150.0You can also use reduce but for a simple task such as this, it is an overkill. And it is also much less readable than using a list comprehension.

Read More
Showing 161–170 of 233 articles
« Prev 1 15 16 17 18 19 24 Next »
Advertisements