Why should we use Python ?


In last several years Python became a first-class tool for scientific analytical tasks, including the analysis and visualization of large data volumes.

The effectiveness of Python for data science is obvious from the language usage itself, but also due to large and active ecosystem of third-party packages, which are really really helpful for data manipulation, common scientific computing tasks, high-quality visualizations, interactive execution and sharing of code, machine learning and many more use cases.

Python DS cheat sheet.


This cheat sheet provides a quick tour of the essential features of the Python language for data scientists willing to use basic to middle level Python techniques.



Numbers manipulations.



    1 + 1

OUT: 2



    1 * 3

OUT: 3



    1 / 2

OUT: 0.5



    2 ** 4

OUT: 16



    6 % 2

OUT: 0



    5 % 2

OUT: 1



    (1 + 3) * (5 + 5)

OUT: 40


Variable Assignment.



# Can not start with number or special characters
name_of_var = 2

x = 2
y = 3

z = x + y
z

OUT: 5


Data types - Strings.



'single quotes'

"double quotes"

" wrap lot's of other quotes"

Printing.



x = 'hello'

print(x)

OUT: hello



num = 12
name = 'Sam'

print('My number is: {one}, and my name is: {two}'.format(one=num,two=name))

OUT: My number is: 12, and my name is: Sam



print('My number is: {}, and my name is: {}'.format(num,name))

OUT: My number is: 12, and my name is: Sam



print(f'My number is: {num}, and my name is: {name}')

OUT: My number is: 12, and my name is: Sam


Data types - Lists.


Lists are used to store multiple items in a single variable.


[1,2,3]

['hi',1,[1,2]]

my_list = ['a','b','c']

my_list.append('d')

my_list

OUT: ['a', 'b', 'c', 'd']



#indexing
my_list[0]

OUT: 'a'



#slicing
my_list[1:]

OUT: ['b', 'c', 'd']



#changing
my_list[0] = 'NEW'

OUT: ['NEW', 'b', 'c', 'd']



#nesting
nest = [1,2,3,[4,5,['target']]]
nest[3][2][0]

OUT: 'target'


Data types - Dictionaries.


Dictionaries are used to store data values in key:value pairs.


d = {'key1':'item1','key2':'item2'}
d['key1']

OUT: 'item1'


Data types - Booleans.


Booleans represent one of two values: True or False..


True

OUT: True



False

OUT: False


Data types - Tuples.


Tuples - collections which are ordered and unchangeable.


t = (1,2,3)
t[0]

OUT: 1


Data types - Sets.


Sets - collections which are unordered, unchangeable*, and unindexed.


{1,2,3}

OUT: {1,2,3}


Comparison Operators.



1 > 2

OUT: False



1 >= 1

OUT: True



1 == 1

OUT: True



'hi' == 'bye'

OUT: False


Logic Operators.



(1 == 2) and (2 == 3)

OUT: False



(1 == 2) or (2 == 3) or (4 == 4)

OUT: True


if,elif, else Statements.



if 1 == 1:
    print('Yep!')

OUT: Yep!



if 1 == 2:
    print('first')
else:
    print('last')

OUT: last



if 1 == 2:
    print('first')
elif 3 == 3:
    print('middle')
else:
    print('Last')

OUT: middle


for Loops.



seq = [1,2,3,4,5]

for item in seq:
    print(item)

OUT: 1
2
3
4
5


while Loops.



i = 1
while i < 5:
    print('i is: {}'.format(i))
    i = i+1

OUT: i is: 1
i is: 2
i is: 3
i is: 4


range().



range(5)

list(range(5))

OUT: [0, 1, 2, 3, 4]


List comprehension.



x = [1,2,3,4]

[item**2 for item in x]

OUT: [1, 4, 9, 16]


Functions.



def my_func(param1='default'):
    """
    Docstring goes here.
    """
    print(param1)

my_func('new param')

OUT: new param


Lambda expressions - a small anonymous function.



x = lambda a, b : a * b
print(x(5, 6))

OUT: 30


Map and filter.



seq = [1,2,3,4,5]

list(map(lambda var: var*2,seq))

OUT: [2, 4, 6, 8, 10]



list(filter(lambda item: item%2 == 0,seq))

OUT: [2, 4]


Useful methods.



st = 'Hello my name is Sam'

st.lower()

OUT: 'hello my name is sam'



st.upper()

OUT: 'HELLO MY NAME IS SAM'



st.split()

OUT: ['hello', 'my', 'name', 'is', 'Sam']



d.keys()

OUT: dict_keys(['key2', 'key1'])



d.items()

OUT: dict_items([('key2', 'item2'), ('key1', 'item1')])



lst = [1,2,3]

lst.pop()

OUT: 3



lst

OUT: [1, 2]



'x' in ['x','y','z']

OUT: True




See also related topics: