Reputation: 21721
This is Common task In PHP and other programming languages.I moved from PHP developer. I want to make sure with this collections. Anyone have who good in python please help me to understand clearly . This is my collections from PHP code.
<?php
$php = array(1,2,3,4,5,6,7,8,9,10);
for ($i = 0; $i < 10 ; $i ++)
echo $php[$i]."<br>";
?>
=>What is in Python?
<?php
for ($i = 0; $i < 10 ; $i ++)
echo $php[$i] = $i +1 ;
?>
=>What is in Python?
<?php
$php = array(1,2,3,4,5,6,7,8,9,10);
foreach ($php as $value)
echo $value."<br>";
?>
=>What is in Python?
<?php
$arr = array(1, 2, 3, 4);
foreach ($arr as &$value) {
$value = $value * 2;
}
// $arr is now array(2, 4, 6, 8)
?>
=>What is in Python?
<?php
$arr = array("mot"=>"one", "hai"=>"two","ba"=> "three");
foreach ($arr as $key => $value) {
echo "Key: $key; Value: $value<br />\n";
}
?>
=>What is in Python?
<?php
$arr = array("one", "two","three");
while (list($key, $value) = each($arr)) {
echo "Key: $key; Value: $value<br />\n";
}
?>
=>What is in Python?
<?php
$arr = array("one", "two","three");
while ($element = each($arr)) {
echo "Key: $element['key']; Value: $element['value']<br />\n";
}
?>
=>What is in Python?
<?php
$products = array( array("ITL","INTEL","HARD"),
array("MIR", "MICROSOFT","SOFT"),
array("Py4C", "pythonkhmer.wordpress.com","TUTORIAL")
);
for ($row = 0; $row < 3; $row++)
{
for ($col = 0; $col <3; $col++)
{
echo "|".$products[$row][$col];
}
echo "<br>";
}
?>
=>What is in Python?
Upvotes: 15
Views: 14553
Reputation: 20466
All of these are quite obvious really. I'm only listing the Pythonic ways to do these things. Update: Examples should now work in both Python 2 and Python 3. In Python 2 you could substitute xrange() for range() and iteritems() for items() for efficiency.
PHP
$php = array(1,2,3,4,5,6,7,8,9,10);
for ($i = 0; $i < 10 ; $i ++)
echo $php[$i]."<br>";
Python (generally you iterate over lists in Python, instead of accessing by index):
lst = [1,2,3,4,5,6,7,8,9,10]
for item in lst:
print(str(item) + "<br>")
for ($i = 0; $i < 10 ; $i ++)
echo $php[$i] = $i +1 ;
Python:
lst = range(1, 11)
for item in lst:
print(item)
Or maybe:
lst = []
for i in range(10):
lst.append(i + 1)
print(lst[-1]) # prints out last element
$php = array(1,2,3,4,5,6,7,8,9,10);
foreach ($php as $value)
echo $value."<br>";
Same as 1st
$arr = array(1, 2, 3, 4);
foreach ($arr as &$value) {
$value = $value * 2;
}
Python:
lst = [1, 2, 3, 4]
lst = [val * 2 for val in lst]
$arr = array("mot"=>"one", "hai"=>"two","ba"=> "three");
foreach ($arr as $key => $value) {
echo "Key: $key; Value: $value<br />\n";
}
Python (note that {...}
creates a dict [dictionary] in Python, not a list/):
dct = {'mot': 'one', 'hai': 'two', 'ba': 'three'}
for key, value in dct.items():
print("Key: %s; Value: %s<br />" % (key, value))
$arr = array("one", "two","three");
while (list($key, $value) = each($arr)) {
echo "Key: $key; Value: $value<br />\n";
}
Python:
lst = ['one', 'two', 'three']
for key, value in enumerate(lst):
print("Key: %d; Value: %s<br />" % (key, value))
$arr = array("one", "two","three");
while ($element = each($arr)) {
echo "Key: $element['key']; Value: $element['value']<br />\n";
}
There is no direct Python equivalent to this.
$products = array( array("ITL","INTEL","HARD"),
array("MIR", "MICROSOFT","SOFT"),
array("Py4C", "pythonkhmer.wordpress.com","TUTORIAL")
);
for ($row = 0; $row < 3; $row++)
{
for ($col = 0; $col <3; $col++)
{
echo "|".$products[$row][$col];
}
echo "<br>";
}
Python:
products = [['ITL', 'INTEL', 'HARD'],
['MIR', 'MICROSOFT', 'SOFT'],
['Py4C', 'pythonkhmer.wordpress.com', 'TUTORIAL']]
for product in products:
for item in product:
print('|' + item)
print('<br>')
Or maybe a more Pythonic version:
for product in products:
print('|%s<br>' % ('|'.join(product)))
Upvotes: 30
Reputation: 1716
This question reminds me of a wise man who had said, "Give a man a fish; you have fed him for today. Teach a man to fish; and you have fed him for a lifetime..."
Upvotes: 0
Reputation: 52371
"What is in Python?", quite a philosophical question, always a great way to start a day. I think what is in Python can best be answered by the Zen of Python (type import this
in an interactive shell):
Sorry, couldnt resist. To answer the question you meant to ask, I direct you to the Python documentation, specifically the section about looping techniques as linked to by lutz.
Unless the syntax in the documentation manage to completely confuse you (though I doubt it) you will see how a loop is defined in Python. And once you have understood that, you will understand, by definition, how they differ (syntactically) from the loops youre used to in PHP.
Still not satisfied? Hmm... I guess you should read the tutorial again. Then, come back and ask specific questions that could yield specific answers. You wont find any silver bullets for a question this broad.
Upvotes: 10