Python Part 9: For Loops
Last Updated by Code Sport. Filed under classes, courses, pythonPython Part 9: For Loops This post allows you to observe the…
This post allows you to observe the syntax for Python and PHP for-loops using using a dictionary.
""" dict.keys(), dict.values() and dict.items() for key in single_structure_dict: #faster, but equivalent to: for key in single_structure_dict.keys() print(key) http://stackoverflow.com/questions/3294889/iterating-over-dictionaries-using-for-loops-in-python http://stackoverflow.com/questions/17793364/python-iterate-dictionary-by-index """ single_structure_dict = {'Store_1': 'Trader Joe\'s', \ 'Store_2': 'The Fresh Market', 'Store_3': 'Wholefoods'} for key in single_structure_dict: print(key + ' is ' + single_structure_dict[key]) for key, value in single_structure_dict.items(): print(key + ' is ' + value) for key in single_structure_dict: print(single_structure_dict[key]) for i in range(0, 10): print(i) """ NB: May use single or double quotes to define strings. Choose a convention and stick with it """
$single_structure_arr = array ( 'Store_1' => 'Trader Joe\'s', 'Store_2' => 'The Fresh Market', 'Store_3' => 'Wholefoods'); foreach ( $single_structure_arr as $key=>$value ) { echo $key . ' is ' . $value; } foreach ( $single_structure_arr as $value ) { echo $value; } for ($i=0; $i<=9; $i++) { echo $i; } /* * NB: Single and double quotes have differing functionalities in PHP. * Use single quotes to define strings. Use double quotes when * there's a variable in your string that must be evaluated. */
/* * Associative arrays don't exist in JS, so we traditionally use objects to * mimic that functionality and syntax. JS Objects may be accessed via bracket * or dot notation * * https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Working_with_Objects * https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/for...in * * Create via: var single_structure_obj = {} * */ var single_structure_obj = { Store_1 : 'Trader Joe\'s', Store_2 : 'The Fresh Market', Store_3 : 'Wholefoods', }; for (var store in single_structure_obj){ console.log(store + ' is ' + single_structure_obj[store]); //console.log(store, 'is', single_structure_obj[store]); } for (var i=0; i<=9; i++) { console.log(i); } /* * NB: May use single or double quotes to define strings. Choose * a convention and stick with it */