Python Basics: Subset Slice
Python Basics: Subset Slice
Python Basics
Lists
>>>
>>>
>>>
>>>
Subset
Variable Assignment
>>> x=5
>>> x
5
>>> my_list[1]
>>> my_list[-3]
>>>
>>>
>>>
>>>
Slice
>>> x+2
>>> x-2
>>> x*2
>>> x**2
Exponentiation of a variable
>>> x%2
Remainder of a variable
>>> x/float(2)
Division of a variable
10
25
1
Index starts at 0
2.5
my_list[1:3]
my_list[1:]
my_list[:3]
my_list[:]
>>> my_list2[1][0]
>>> my_list2[1][:2]
my_list[list][itemOfList]
str()
Variables to strings
int()
5, 3, 1
Variables to integers
>>> my_list * 2
float()
5.0, 1.0
Variables to floats
bool()
Variables to booleans
my_list.index(a)
my_list.count(a)
my_list.append('!')
my_list.remove('!')
del(my_list[0:1])
my_list.reverse()
my_list.extend('!')
my_list.pop(-1)
my_list.insert(0,'!')
my_list.sort()
'thisStringIsAwesome'
String Operations
>>> my_string * 2
'thisStringIsAwesomethisStringIsAwesome'
Machine learning
Scientific computing
2D plotting
Numpy Arrays
>>> my_array[1]
Index starts at 0
Slice
>>> my_array[0:2]
array([1, 2])
>>> my_2darray[:,0]
my_2darray[rows, columns]
array([1, 4])
>>> my_array * 2
True], dtype=bool)
array([2, 4, 6, 8])
Strings
>>> my_string = 'thisStringIsAwesome'
>>> my_string
Data analysis
Install Python
List Methods
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>>
Import libraries
>>> import numpy
>>> import numpy as np
Selective import
>>> from math import pi
List Operations
True
Libraries
String Operations
Index starts at 0
>>> my_string[3]
>>> my_string[4:9]
String Methods
>>>
>>>
>>>
>>>
>>>
String to uppercase
my_string.upper()
String to lowercase
my_string.lower()
Count String elements
my_string.count('w')
my_string.replace('e', 'i') Replace String elements
my_string.strip()
Strip whitespace from ends
my_array.shape
np.append(other_array)
np.insert(my_array, 1, 5)
np.delete(my_array,[1])
np.mean(my_array)
np.median(my_array)
my_array.corrcoef()
np.std(my_array)
DataCamp