Click for All Topics
| Feature | For Loop | While Loop | If Statement | 
| Purpose | Repeats actions a fixed number of times, iterating over a sequence. | Repeats actions based on a condition, as long as the condition is true. | Executes code only if a specific condition is true. | 
| Usage | Useful when you know the number of iterations beforehand. | Useful when you’re not sure about the number of iterations, relying on a condition. | Useful for making decisions and choosing between alternatives. | 
| Structure | Typically involves specifying a range or sequence to iterate over. | Involves setting a condition that controls whether the loop runs or not. | Involves checking a condition and executing code based on whether it’s true or false. | 
| Example | for num in range(5):<br>print(num) | while temperature > 0:<br>cool_down() | if age >= 18:<br>print("You can vote!") | 
| Continuation | Iterates a predefined number of times. | Repeats as long as the condition remains true. | Executes only if the condition is met. | 
| Typical Use Cases | Processing elements in lists, sequences. | Repeated tasks like game loops or user input validation. | Making decisions based on conditions, executing different code paths. | 
Example 1-
				
					my_set = set()
my_set.add(1)
my_set.add(2)
my_set.add(3)
print(my_set)
# Output - 
# {1,2,3} 
				
			
		1 in my_set using the .add() method.print() function.Example 2-
				
					my_set2={10,20,30}
my_set2.add(40)
my_set2.add(20)
print(my_set2)
# Output-
# {40, 10, 20, 30} 
				
			
		Example 3-
				
					my_set3={'this','is','edSlash'}
my_set3.add('Hello')
print(my_set3)
my_set3.add('anurag','dubey')  #will give an error think about it.
# Output - 
# {'Hello','this','is','edSlash'} # may be your output in a different order
# Traceback (most recent call last):
  # File "/home/main.py", line 4, in 
    # my_set3.add('anurag','dubey')  
# TypeError: set.add() takes exactly one argument (2 given)  
				
			
		Example 4-
				
					my_set4={'a','b','c','d'}
my_set4.add(('e','f','g'))
print(my_set4)
# Output -
# {'a', ('e', 'f', 'g'), 'c', 'd', 'b'} 
				
			
		Example 5-
				
					my_set5 = set()
for i in range(1, 6):
    my_set5.add(i)
print(my_set5) 
# Output -
# {1,2,3,4,5} 
				
			
		© 2021 edSlash. All Rights Reserved.