Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "Tips. Iteration On a list: o group = ["Paul","Duncan","Jessica"] for person in group: print(group) On a dictionary: o stock = {'eggs':15, 'milk':3, 'sugar':28}"— Presentation transcript:

1 Tips

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

3 Working directory The Python sessions working directory is set as the directory from where Python is called. This also applies when, in Windows, you edit a file in IDLE. In Windows, the "Start in"-property of a shortcut determines the working directory. If it is empty, the directory where the shortcut is placed is used. To change the working directory from within Python: o import os os.getcwd() # get current working directory os.chdir(path) # change working directory

4 Executing a script file To execute a script file, from a console write: o python scriptfile.py (This requires the python directory is in the PATH environment variable.) It is possible to pass arguments to the script: o python script arg1 arg2 In the script file, the arguments can be found 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: o if __name__ == '__main__': # Suite to be executed when script # is called independentantly. Code outside the if-blocks suite, is always executed.

6


Download ppt "Tips. Iteration On a list: o group = ["Paul","Duncan","Jessica"] for person in group: print(group) On a dictionary: o stock = {'eggs':15, 'milk':3, 'sugar':28}"

Similar presentations


Ads by Google