1 / 6

Tips

Tips. Iteration. On a list: group = [" Paul","Duncan","Jessica "] for person in group : print( group ) On a dictionary : stock = {'eggs':15, 'milk':3, 'sugar':28} Iterate over keys : for ingredient in stock.iterkeys (): Iterate over values :

zeal
Download Presentation

Tips

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Tips

  2. Iteration • On a list: • group = ["Paul","Duncan","Jessica"]for person in group: print(group) • On a dictionary: • stock = {'eggs':15, 'milk':3, 'sugar':28} • Iterate over keys: • for ingredient in stock.iterkeys(): • Iterate over values: • for count in stock.itervalues(): • Iterate over both: • for ingredient,count in stock.iteritems()

  3. Workingdirectory • The Python sessions workingdirectory is set as the directory from wherePython is called. • This alsoapplieswhen, in Windows, youedit a file in IDLE. • In Windows, the "Start in"-property of a shortcutdetermines the workingdirectory. If it is empty, the directorywhere the shortcut is placed is used. • To change the workingdirectory from withinPython: • import osos.getcwd() # getcurrentworkingdirectoryos.chdir(path) # changeworkingdirectory

  4. Executing a script file • To execute a script file, from a consolewrite: • python scriptfile.py • (This requires the pythondirectory is in the PATH environment variable.) • It is possible to pass arguments to the script: • python script arg1 arg2 • In the script file, the arguments canbefound in the variable sys.argv(remember to import sys).

  5. Executing a script file • To differentiate a script from being run as a module or as a independent script, do as below: • if __name__ == '__main__': # Suite to beexecutedwhen script # is calledindependentantly. • Code outside the if-blocks suite, is alwaysexecuted.

More Related