Found 10805 Articles for Python

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

12K+ 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

1K+ 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

492 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

5K+ 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

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

615 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

How to comment each condition in a multi-line if statement in Python?

Samual Sam
Updated on 17-Jun-2020 12:01:22

170 Views

You can do this directly if you are surrounding your multiline if statements conditions in a parenthesis. For example,if (cond1 == 'val1' and    cond2 == 'val2' and # Some comment    cond3 == 'val3' and # Some comment    cond4 == 'val4'):However, this is not possible if you try to do this without a parenthesis. For example, the following code will give an error:if cond1 == 'val1' and \    cond2 == 'val2' and \ # Some comment    cond3 == 'val3' and \ # Some comment    cond4 == 'val4':

What are common programming errors or 'gotchas' in Python?

Nikitha N
Updated on 05-Mar-2020 07:58:40

78 Views

Here are some of the most common python programming mistakes/gotchas that programmers commit:Scope name lookups: Python follows scoping rules in order of LEGB(Local, Enclosing, Global, Built-in). Since python has no strict type binding, programmers can reassociate an outer scope variable to another value that might be used in the outer scope later but is now replaced by some other value.Not differentiating between is and =: The is an operator in python checks if both objects refer to the same memory address. The == operator executes __eq__ function which might check for equality differently for different classes.Modifying a list while iterating ... Read More

What are common Python programming mistakes programmers do?

Lakshmi Srinivas
Updated on 05-Mar-2020 07:57:14

64 Views

Here are some of the most common python programming mistakes/gotchas that programmers commit −Scope name lookups − Python follows scoping rules in order of LEGB(Local, Enclosing, Global, Built-in). Since python has no strict type binding, programmers can reassociate an outer scope variable to another value that might be used in the outer scope later but is now replaced by some other value.Not differentiating between is and == − The is an operator in python checks if both objects refer to the same memory address. The == operator executes __eq__ function which might check for equality differently for different classes.Modifying a ... Read More

Advertisements