> S trings > L ists Also see NumPy Arrays
Python For Data Science >>>
>>>
my_string
my_string
= 'thisStringIsAwesome'
>>>
>>>
a = 'is'
b = 'nice'
Basics Cheat Sheet
'thisStringIsAwesome' >>> my_list = ['my', 'list', a, b]
>>> my_list2 = [[4,5,6,7], [3,4,5,6]]
String Operations
Selecting List Elements ndex starts at 0
I
Learn Python Basics online at www.DataCamp.com >>> my_string * 2
'thisStringIsAwesomethisStringIsAwesome'
Subset
>>> my_string + 'Innit'
'thisStringIsAwesomeInnit'
>>> my_list[1] #Select item at index 1
>>> 'm' in my_string
>>> my_list[-3] #Select 3rd last item
True
Slice
> Variables and Data Types String Indexing ndex starts at 0
I
>>>
>>>
>>>
my_list[1:3] #Select items at index 1 and 2
my_list[1:] #Select items after index 0
my_list[:3] #Select items before index 3
>>> my_list[:] #Copy my_list
Variable Assignment >>>
>>>
my_string[3]
my_string[4:9]
Subset Lists of Lists
>>> my_list2[1][0] #my_list[list][itemOfList]
>>> x=5
>>> my_list2[1][:2]
>>> x
5
String Methods
>>> my_string.upper() #String to uppercase
L ist Operations
Calculations With Variables >>>
>>>
my_string.lower() #String to lowercase
my_string.count('w') #Count String elements
>>> my_list + my_list
>>> my_string.replace('e', 'i') #Replace String elements
['my', 'list', 'is', 'nice', 'my', 'list', 'is', 'nice']
>>> x+2 #Sum of two variables
>>> my_string.strip() #Strip whitespaces >>> my_list * 2
['my', 'list', 'is', 'nice', 'my', 'list', 'is', 'nice']
>>> x-2 #Subtraction of two variables
>>> my_list2 > 4
True
>>> x*2 #Multiplication of two variables
10
>>> x**2 #Exponentiation of a variable
25
> NumPy Arrays Also see Lists
L ist Methods
>>> x%2 #Remainder of a variable
>>> my_list = [1, 2, 3, 4]
1
>>> my_list.index(a) #Get the index of an item
>>> my_array = np.array(my_list)
>>> x/float(2) #Division of a variable
>>> my_list.count(a) #Count an item
>>> my_2darray = np.array([[1,2,3],[4,5,6]])
2.5 >>> my_list.append('!') #Append an item at a time
>>> my_list.remove('!') #Remove an item
Typ es and Type Conversion Selecting Numpy Array Elements ndex starts at 0
I
>>>
>>>
del(my_list[0:1]) #Remove an item
my_list.reverse() #Reverse the list
>>> my_list.extend('!') #Append an item
Subset >>> my_list.pop(-1) #Remove an item
str()
>>> my_list.insert(0,'!') #Insert an item
'5', '3.45', 'True' #Variables to strings >>> my_array[1] #Select item at index 1
>>> my_list.sort() #Sort the list
2
int()
Slice
> Python IDEs (Integrated Development Environment)
5, 3, 1 #Variables to integers
>>> my_array[0:2] #Select items at index 0 and 1
float() array([1, 2])
5.0, 1.0 #Variables to floats Subset 2D Numpy arrays
bool() >>> my_2darray[:,0] #my_2darray[rows, columns]
array([1, 4])
True, True, True #Variables to booleans
DataCamp Workspace
Free IDE that is included
Create and share
is an in-browser Jupyter IDE with Anaconda documents with live code
Numpy Array Operations
> ibraries
> Asking For Help
>>> my_array > 3
L array([False, False, False, True], dtype=bool)
>>> my_array * 2
array([2, 4, 6, 8])
>>> my_array + np.array([5, 6, 7, 8])
Data analysis Scientific computing D plotting
2 achine learning
M
array([6, 8, 10, 12]) >>> h elp (str)
Import Libraries Numpy Array Functions
>>> import numpy
>>> my_array.shape #Get the dimensions of the array
>>> import numpy as np >>> np.append(other_array) #Append items to an array
>>> np.insert(my_array, 1, 5) #Insert items in an array
>>> np.delete(my_array,[1]) #Delete items in an array
Selective import >>>
>>>
np.mean(my_array) #Mean of the array
np.median(my_array) #Median of the array
>>> my_array.corrcoef() #Correlation coefficient
>>> from math import pi >>> np.std(my_array) #Standard deviation
Learn Data Skills Online at
www.DataCamp.com