Hi guys. How ae you all? Today in this tutorial we will learn about How to use a for Loop for Multiple Variables in Python. We can use more than two variables in for loop to get the easy and better result. So in this tutorial we will learn about of this by given below methods. So lets learn this guys.
Contents
How to use a for Loop for Multiple Variables in Python
- Use a for Loop for Multiple Variables in Python
to use a for Loop for Multiple Variables in Python just Use for loop. Here we will use for loop and use the multiple variables using the item() function. Here first of all we will make our dictionary and after it we will use for loop with the two variables key and values. And then print it by using f-string. So lets learn this by given below example. I hope you guys like it.
dict1 = {98: "pooja", 92: "Mansi"} for key, value in dict1.items(): print(f"{value} got {key} marks")
Output :pooja got 98 marks Mansi got 92 marks
- How to use a for Loop for Multiple Variables in Python
to use a for Loop for Multiple Variables in Python just Use zip(). You can use zip() function to print the multiple variabls in python. So here we will learn about of this method. Here we are using two different variables fruits and prices And after it we are using for loop with this two variables and then print it with using the f-string And you can see the output. So lets learn this by given below example:
fruits = ["Mango", "Oranges", "Grapes"] prices = [180,100,200] for fruit, price in zip(fruits, prices): print(f"The price of the 1 kg {fruit} is {price}")
Output :The price of the 1 kg Mango is 180 The price of the 1 kg Oranges is 100 The price of the 1 kg Grapes is 200
Method 1: Use for loop
Here we will use for loop and use the multiple variables using the item() function. Here first of all we will make our dictionary and after it we will use for loop with the two variables key and values. And then print it by using f-string. So lets learn this by given below example. I hope you guys like it.
dict1 = {98: "pooja", 92: "Mansi"}
for key, value in dict1.items():
print(f"{value} got {key} marks")
Output :
pooja got 98 marks
Mansi got 92 marks
Method 2: Use zip()
You can use zip() function to print the multiple variabls in python. So here we will learn about of this method. Here we are using two different variables fruits and prices And after it we are using for loop with this two variables and then print it with using the f-string And you can see the output. So lets learn this by given below example:
fruits = ["Mango", "Oranges", "Grapes"]
prices = [180,100,200]
for fruit, price in zip(fruits, prices):
print(f"The price of the 1 kg {fruit} is {price}")
Output :
The price of the 1 kg Mango is 180
The price of the 1 kg Oranges is 100
The price of the 1 kg Grapes is 200
Method 3: Use enumerate()
By using enumerate() you can print multiple variables in python using the for loop. So here we will learn about of this by using this method. Here we will make two different variables. And then use the for loop with this two variables and print it by using the f-string. Here the enumerate provides the index so we can get the more easy output. As you can see in my output. Lets learn this :
fruits = ["Mango", "Oranges", "Grapes"]
prices = [180,100,200]
for i, fruit in enumerate(fruits):
price = prices[i]
print(f"The price of the 1 kg {fruit} is {price}")
Output :
The price of the 1 kg Mango is 180
The price of the 1 kg Oranges is 100
The price of the 1 kg Grapes is 200
Conclusion
Hope all 6 Method Are Useful For You. Comment Below Which Method You Used To Convert String To Datetime In Python. Thank You.