Found 34487 Articles for Programming

How to make loops run faster using Python?

Samual Sam
Updated on 05-Mar-2020 09:55:17

828 Views

This is a language agnostic question. Loops are there in almost every language and the same principles apply everywhere. You need to realize that compilers do most heavy lifting when it comes to loop optimization, but you as a programmer also need to keep your loops optimized.It is important to realize that everything you put in a loop gets executed for every loop iteration. They key to optimizing loops is to minimize what they do. Even operations that appear to be very fast will take a long time if the repeated many times. Executing an operation that takes 1 microsecond ... Read More

How to create a lambda inside a Python loop?

Lakshmi Srinivas
Updated on 05-Mar-2020 09:54:01

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

How do I loop through a JSON file with multiple keys/sub-keys in Python?

Sravani S
Updated on 05-Mar-2020 08:14:19

13K+ Views

You can parse JSON files using the json module in Python. This module parses the json and puts it in a dict. You can then get the values from this like a normal dict. For example, if you have a json with the following content −{    "id": "file",    "value": "File",    "popup": {       "menuitem": [          {"value": "New", "onclick": "CreateNewDoc()"},          {"value": "Open", "onclick": "OpenDoc()"},          {"value": "Close", "onclick": "CloseDoc()"}       ]    } }ExampleYou can load it in your python program and loop ... Read More

How can I loop over entries in JSON using Python?

karthikeya Boyini
Updated on 05-Mar-2020 08:11:45

1K+ Views

You can parse JSON files using the json module in Python. This module parses the json and puts it in a dict. You can then get the values from this like a normal dict. For example, if you have a json with the following content −{    "id": "file",    "value": "File",    "popup": {       "menuitem": [       {"value": "New", "onclick": "CreateNewDoc()"},       {"value": "Open", "onclick": "OpenDoc()"},       {"value": "Close", "onclick": "CloseDoc()"}       ]    } }ExampleYou can load it in your python program and loop over its keys ... Read More

How to execute Python multi-line statements in the one-line at command-line?

Samual Sam
Updated on 05-Mar-2020 08:08:02

2K+ Views

There are multiple ways in which you can use multiline statements in the command line in python. For example, bash supports multiline statements, which you can use like −Example$ python -c ' > a = True > if a: > print("a is true") > 'OutputThis will give the output −a is trueIf you prefer to have the python statement in a single line, you can use the newline between the commands. example$ python -c $'a = Trueif a: print("a is true");'OutputThis will give the output −a is true

How to loop through multiple lists using Python?

V Jyothi
Updated on 05-Mar-2020 08:06:15

516 Views

The most straightforward way seems to use an external iterator to keep track. Note that this answer considers that you're looping on same sized lists. examplea = [10, 12, 14, 16, 18] b = [10, 8, 6, 4, 2] for i in range(len(a)):    print(a[i] + b[i])OutputThis will give the output −20 20 20 20 20ExampleYou can also use the zip method that stops when the shorter of a or b stops.a = [10, 12, 14, 16, 18] b = [10, 8, 6] for (A, B) in zip(a, b):    print(A + B)OutputThis will give the output −20 20 20

How to write inline if statement for print in Python?

Lakshmi Srinivas
Updated on 17-Jun-2020 12:05:59

6K+ Views

Python provides two ways to write inline if statements. These are:1. if condition: statement2. s1 if condition else s2Note that second type of if cannot be used without an else. Now you can use these inline in a print statement as well. For example,a = True if a: print("Hello")This will give the output:Helloa = False print("True" if a else "False")This will give the output:False

compareTo() definition mistake?

Pythonista
Updated on 30-Jul-2019 22:30:22

116 Views

The example works correctly. The compareTo() method is called by a string object and takes another string object as argument. The return value is integer and is difference in Unicode values of characters of respective strings when they are not equal. The value can be -ve, 0 or +ve.

How to use if...else statement at the command line in Python?

Priya Pallavi
Updated on 17-Jun-2020 12:03:59

1K+ Views

There are multiple ways in which you can use if else construct in the command line in python. For example, bash supports multiline statements, which you can use like:$ python -c ' > a = True > if a: > print("a is true") > 'This will give the output:a is trueIf you prefer to have the python statement in a single line, you can use the newline between the commands. For example,$ python -c $'a = Trueif a: print("a is true");'This will give the output:a is true

Where to put comments in an if...elif..else construct?

karthikeya Boyini
Updated on 17-Jun-2020 12:02:42

682 Views

You can put comments anywhere in an if...elif...else statement, ie before each of these blocks or within each of these blocks. Note that you cannot put multiline comments before elif and else blocks though, as these comments are actually strings which imply a break in the whole construct. For example,# If check if True:    # Another Comment style    print("If block") # Else if statement elif False:    # Another Comment style    print("elif block") # Else else:    # Another Comment style    print("Else block")This will give the output:If block

Advertisements