looping through python data structures
play

Looping through Python data structures Justin Kiggins Product - PowerPoint PPT Presentation

DataCamp Python for MATLAB Users PYTHON FOR MATLAB USERS Looping through Python data structures Justin Kiggins Product Manager DataCamp Python for MATLAB Users How for loops work in Python MATLAB Python for i=1:5 for i in [1, 2, 3, 4,


  1. DataCamp Python for MATLAB Users PYTHON FOR MATLAB USERS Looping through Python data structures Justin Kiggins Product Manager

  2. DataCamp Python for MATLAB Users How for loops work in Python MATLAB Python for i=1:5 for i in [1, 2, 3, 4, 5]: disp(i); print(i) end print('done') disp('done'); 1 1 2 2 3 3 4 4 5 5 done done

  3. DataCamp Python for MATLAB Users Looping through a list of strings groceries = ['bread', 'tea', 'banana'] print(groceries) ['bread', 'tea', 'banana'] for item in groceries: print(item) bread tea banana

  4. DataCamp Python for MATLAB Users Looping through a dictionary abbreviations = {'New York': 'NY', 'California': 'CA', 'Illinois': 'IL', 'Texas': 'TX'} print(abbreviations) {'New York': 'NY', 'California': 'CA', 'Illinois': 'IL', 'Texas': 'TX'} for state, abbv in abbreviations.items(): print(state, abbv) New York NY California CA Illinois IL Texas TX

  5. DataCamp Python for MATLAB Users Looping through a 1D NumPy array import numpy as np arr = np.array([1, 2, 3]) print(arr) [1 2 3] for element in arr: print(element) 1 2 3

  6. DataCamp Python for MATLAB Users Looping through a 2D NumPy array import numpy as np X = np.array([[1, 2, 3], [4, 5, 6]]) print(X) [[1 2 3] [4 5 6]] for row in X: print(row) [1 2 3] [4 5 6]

  7. DataCamp Python for MATLAB Users Looping through pandas DataFrames import pandas as pd d = [{'fruit': 'apple', 'color': 'red'}, {'fruit': 'banana', 'color': 'yellow'}, {'fruit': 'pear', 'color': 'green'}] df = pd.DataFrame(d) print(df) color fruit 0 red apple 1 yellow banana 2 green pear for ii, row in df.iterrows(): print(ii, row['fruit'], row['color']) 0 apple red 1 banana yellow 2 pear green

  8. DataCamp Python for MATLAB Users PYTHON FOR MATLAB USERS Let's practice!

  9. DataCamp Python for MATLAB Users PYTHON FOR MATLAB USERS Comparison operators Justin Kiggins Product Manager

  10. DataCamp Python for MATLAB Users Using comparison operators value = 0.967 threshold = 0.85 meets_criteria = value > threshold print(meets_criteria) True

  11. DataCamp Python for MATLAB Users The comparison operators Comparison Python MATLAB Equal == == Not equal != ~= Less than < < Less than or equal <= <= Greater than > > Greater than or equal >= >=

  12. DataCamp Python for MATLAB Users If value = 0.967 threshold = 0.85 meets_criteria = value > threshold if meets_criteria: print('PASS') PASS

  13. DataCamp Python for MATLAB Users Else value = 0.275 threshold = 0.85 meets_criteria = value > threshold if meets_criteria: print('PASS') else: print('FAIL') FAIL

  14. DataCamp Python for MATLAB Users Else if porridge_temperature = 74.6 if porridge_temperature > 130: print('Too hot! :(') elif porridge_temperature < 110: print('Too cold! :(') else: print('Just right :D') Too cold! :(

  15. DataCamp Python for MATLAB Users PYTHON FOR MATLAB USERS Let's practice

  16. DataCamp Python for MATLAB Users PYTHON FOR MATLAB USERS Filtering data Justin Kiggins Product Manager

  17. DataCamp Python for MATLAB Users Comparison operators and NumPy Arrays data = np.array([0.967, 0.56, 0.171, 0.872]) threshold = 0.85 meets_criteria = data > threshold print(meets_criteria) [True, False, False, True]

  18. DataCamp Python for MATLAB Users Filtering NumPy arrays data = np.array([-1, 0.56, -1, 0.872, 1.26]) is_valid = data >= 0 valid_data = data[is_valid] print(valid_data) [0.56, 0.872, 1.26]

  19. DataCamp Python for MATLAB Users Filtering DataFrames monkeys = df['animal'] == 'monkey' bears = df['animal'] == 'bear' monkey_weight = df[monkeys]['weight'].mean() bear_weight = df[bears]['weight'].mean() print(monkey_weight) 35.0 print(bear_weight) 800.0

  20. DataCamp Python for MATLAB Users PYTHON FOR MATLAB USERS Let's practice

  21. DataCamp Python for MATLAB Users PYTHON FOR MATLAB USERS Well done! Justin Kiggins Product Manager

  22. DataCamp Python for MATLAB Users More courses to explore

  23. DataCamp Python for MATLAB Users PYTHON FOR MATLAB USERS Have fun!

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend