Today I have learned for and if loop in Python
What is an IF statement in a for loop?
- As you can see, an if statement within a for loop is perfect to evaluate a list of numbers in a range (or elements in a list) and put them into different buckets, tag them, or apply functions on them – or just simply print them.
What is the difference between if and for loop in Python?
- For loops complete an iterative action for a defined number of elements, while if statements test a condition and then complete an action. Here’s how to combine them. Control flow structures like if statements and for loops are powerful ways to create logical, clean and well organized code in Python.
- fruit=["apple","grape","banana","kiwi"],veg=["cabbage","tomato"]for x in fruit:for y in veg:print(x,y)
Output:
['apple', 'grape', 'banana', 'kiwi'] cabbage ['apple', 'grape', 'banana', 'kiwi'] tomato
- a="apple"b="orange"if len(b)<len(a) :print("a is greatest")elif len(a)==len(b):print("both are equal")else:print("b is greatest")
Output:
b is greatest
Comments
Post a Comment