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

looping through python data structures
SMART_READER_LITE
LIVE PREVIEW

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,


slide-1
SLIDE 1

DataCamp Python for MATLAB Users

Looping through Python data structures

PYTHON FOR MATLAB USERS

Justin Kiggins

Product Manager

slide-2
SLIDE 2

DataCamp Python for MATLAB Users

How for loops work in Python

MATLAB Python

for i=1:5 disp(i); end disp('done'); 1 2 3 4 5 done for i in [1, 2, 3, 4, 5]: print(i) print('done') 1 2 3 4 5 done

slide-3
SLIDE 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

slide-4
SLIDE 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

slide-5
SLIDE 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

slide-6
SLIDE 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]

slide-7
SLIDE 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

slide-8
SLIDE 8

DataCamp Python for MATLAB Users

Let's practice!

PYTHON FOR MATLAB USERS

slide-9
SLIDE 9

DataCamp Python for MATLAB Users

Comparison operators

PYTHON FOR MATLAB USERS

Justin Kiggins

Product Manager

slide-10
SLIDE 10

DataCamp Python for MATLAB Users

Using comparison operators

value = 0.967 threshold = 0.85 meets_criteria = value > threshold print(meets_criteria) True

slide-11
SLIDE 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

>= >=

slide-12
SLIDE 12

DataCamp Python for MATLAB Users

If

value = 0.967 threshold = 0.85 meets_criteria = value > threshold if meets_criteria: print('PASS') PASS

slide-13
SLIDE 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

slide-14
SLIDE 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! :(

slide-15
SLIDE 15

DataCamp Python for MATLAB Users

Let's practice

PYTHON FOR MATLAB USERS

slide-16
SLIDE 16

DataCamp Python for MATLAB Users

Filtering data

PYTHON FOR MATLAB USERS

Justin Kiggins

Product Manager

slide-17
SLIDE 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]

slide-18
SLIDE 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]

slide-19
SLIDE 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

slide-20
SLIDE 20

DataCamp Python for MATLAB Users

Let's practice

PYTHON FOR MATLAB USERS

slide-21
SLIDE 21

DataCamp Python for MATLAB Users

Well done!

PYTHON FOR MATLAB USERS

Justin Kiggins

Product Manager

slide-22
SLIDE 22

DataCamp Python for MATLAB Users

More courses to explore

slide-23
SLIDE 23

DataCamp Python for MATLAB Users

Have fun!

PYTHON FOR MATLAB USERS