How to use a for Loop for Multiple Variables in Python

Hi guys. How are you all? I hope you all fine. Today in this tutorial we will learn about How to use a for Loop for Multiple Variables in Python. Here i am giving you some methods which will be useful to use a for Loop for Multiple Variables in Python. Its very easy and also very simple at the same time. So lets learn about of this by given methods.

How to use a for Loop for Multiple Variables in Python

  1. Use a for Loop for Multiple Variables in Python

    to use a for Loop for Multiple Variables in Python just Use .items(). Here i am using a dictionary and using two variables for that. First one is key and the second is value. Here i am also using the dict.items(). This method is very simple and easy and here you can use multiple variables. So lets learn about of this by give below example:
    dict1 = {15: "shreya", 32: "Upasana"} for key, value in dict1.items(): print(f"{value} got {key} marks") Output : shreya got 15 marks Upasana got 32 marks

  2. How to use a for Loop for Multiple Variables in Python

    to use a for Loop for Multiple Variables in Python just Use enumerate. By using enumerate() you can use Multiple Variables in Python. First of all i am making here two different variables. And after it i am using for loop with the enumerate function So by this method we can print two variables together. Lets learn this by given below example. I hope you guys like it. students = ["shreya", "bhumi", "Upasana"] marks = [35,45,42] for i, students in enumerate(students): mark = marks[i] print(f"{students} got {mark} marks") Output : shreya got 35 marks bhumi got 45 marks Upasana got 42 marks

Method 1: Use .items()

Here i am using a dictionary and using two variables for that. First one is key and the second is value. Here i am also using the dict.items(). This method is very simple and easy and here you can use multiple variables. So lets learn about of this by give below example:

dict1 = {15: "shreya", 32: "Upasana"}
for key, value in dict1.items():
    print(f"{value} got {key} marks")

Output :

shreya got 15 marks
Upasana got 32 marks

Method 2: Use enumerate

By using enumerate() you can use Multiple Variables in Python. First of all i am making here two different variables. And after it i am using for loop with the enumerate function So by this method we can print two variables together. Lets learn this by given below example. I hope you guys like it.

students = ["shreya", "bhumi", "Upasana"]
marks = [35,45,42]
for i, students in enumerate(students):
    mark = marks[i]
    print(f"{students} got {mark} marks")

Output :

shreya got 35 marks
bhumi got 45 marks
Upasana got 42 marks

Method 3: Use zip()

zip() function can also be used for to print the multiple variables in python. Here i am using two different variables. And after it i am using the for loop with the zip() function and then print the two variable in one line. You can see this in my output. So lets learn this method by given below example. It is very simple and easy method. I hope you guys like it. So lets learn this:

students = ["shreya", "bhumi", "Upasana"]
marks = [35,45,42]
for students, marks in zip(students, marks):
    print(f"{students} got {marks} marks")

Output :

shreya got 35 marks
bhumi got 45 marks
Upasana got 42 marks

Conclusion

So This is all About This Tutorial. Hope This Tutorial Helped You. Thank You.

Also Read,

Siddharth

I am an Information Technology Engineer. I have Completed my MCA And I have 4 Year Plus Experience, I am a web developer with knowledge of multiple back-end platforms Like PHP, Node.js, Python and frontend JavaScript frameworks Like Angular, React, and Vue.

Leave a Comment