- Trending Categories
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 34487 Articles for Programming
![Samual Sam](https://www.tutorialspoint.com/assets/profiles/13514/profile/60_83486-1512649303.jpg)
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
![Lakshmi Srinivas](https://www.tutorialspoint.com/assets/profiles/13528/profile/60_91717-1512651120.jpg)
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
![Sravani S](https://www.tutorialspoint.com/assets/profiles/13538/profile/60_67753-1512711355.jpg)
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
![karthikeya Boyini](https://www.tutorialspoint.com/assets/profiles/13518/profile/60_31598-1537784993.jpg)
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
![Samual Sam](https://www.tutorialspoint.com/assets/profiles/13514/profile/60_83486-1512649303.jpg)
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
![V Jyothi](https://www.tutorialspoint.com/assets/profiles/13530/profile/60_77592-1512648869.jpg)
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
![Lakshmi Srinivas](https://www.tutorialspoint.com/assets/profiles/13528/profile/60_91717-1512651120.jpg)
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
![Pythonista](https://www.tutorialspoint.com/assets/profiles/15003/profile/60_128639-1514576467.png)
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.
![Priya Pallavi](https://www.tutorialspoint.com/assets/profiles/13562/profile/60_91320-1512544134.jpg)
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
![karthikeya Boyini](https://www.tutorialspoint.com/assets/profiles/13518/profile/60_31598-1537784993.jpg)
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