- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Found 10805 Articles for Python
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
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
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
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
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
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
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
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':
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
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
To Continue Learning Please Login
Login with Google