Skip to content

How to use foreach loop in Python

  • by
Selenium Documentation and Webdriver in Python

Interesting way to use foreach loop in Python. So, friends, today we will have new topics to discuss in Python programming, isn’t that sound good?

How do I use foreach loop in Python?


// PHP:
foreach ($array as $val) {
print($val);
}

// C#
foreach (String val in array) {
console.writeline(val);
}

// Python
for val in array:
print(val)

In the above line, I described the following

  • 1st line only runs the foreach loop in PHP according to the PHP guide.
  • The second is C sharp there you can also pass the string in making an array.
  • The third one, the idea for Python.

The second way to use foreach in Python


names = ['tom', 'john', 'simon']

namesCapitalized = [capitalize(n) for n in names]

There is no foreach loop statement in Python you can use for loop instead of foreach, so, that’s all.

Tags:

Leave a Reply

Your email address will not be published. Required fields are marked *