見出し画像

Trying DataScience(1) : Cramming Python with Jupyter


Hello everyone!
Nowadays Python is getting more popular, because, it can be used in so many fields.  Especially, 'Data Science' becomes highly sought out.  Actually, same here! I have a strong interest in Python/Data-science, so I have tried learning them a few times, but  it always lasted only a few days. 

As I joined a new company(CRE-CO) this time, a python class is going on. So I made up my mind and study data science again. 

The problem is that I studied Python a few times, but I don't remember the contents well. So I reviewed Python again as soon as possible so that I could study data science. I have organized some points about python and want to share them with you.

I hope the contents below will help you guys who need to review Python in a quick. All the cords runs on Jupyter notebook, which is a one of the most popular Python IDE.



1. Print

In

num = 15
name = 'Sonsan'

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

Out

My number is: 15, and my name is: Sonsan

Please, attention to '.format', which is followed by single quotation.
 'one', 'two' is printed like above.



2. List 

In

my_list = ['a','b','c','d']
my_list[1:3]

Out

['b','c']

As a data type, list is not that different from Java or other languages.
But, there are some points that can be confused in indexing/sliding.
The Index 1 means 'starting with b', not a.

And most importantly, index number 3 is not included in the slicing target. 
It seems that 'my_list[1:3]' returns ['b', 'c',  'd']
But it is not correct!

As I explained, index 3 is not included. 
So, it returns ['b','c'].


3.  Dictionary
Dictionary is a data type in which keys and values are paired.

In

d = {'k1':{'innerkey':[1,2,3]}}

d['k1']['innerykey'][1]

Out

2

'k1' is a key.
{'innerkey':[1,2,3]} is a value.

In  the dictionary d, through key 'k1', grep the value {'innerkey':[1,2,3]} .
Through the key 'innerkey', grep the value [1,2,3] again.
index number 1 from [1,2,3], the value  2.



4. Tuple 
Tuple is Immutable, while List is mutable.

In

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

Out

TypeError: 'tuple' object does not support item assignment



5. Set
Set is a connection of UNIQUE elements 

{1,2,3,1,2,1,2,3,3,3,3,2,2,2,1,1,2}
{1, 2, 3}



6. Range
A range is a built-in function that generates a sequence of numbers.
It is commonly used in loops like for.

In

for i in range(5):
    print(i)

Out

0
1
2
3
4



7. List Comprehension 

A shorter syntax with an existing list.

In

x = [1,2,3,4]

out = []

for item in x:
    out.append(item**2)
print(out)

Out

[1, 4, 9, 16]

The expression became more simple with same results.

In

[item**2 for item in x]

Out

[1, 4, 9, 16]


8. Lambda expression

Very simple form of function. 
Mostly, it takes the form 'lambda parameter: expression'.
It is often used when you need to write a function as a parameter.
(Look at 9: Map and Filter)

def times2(var):
    return var*2
lambda var: var*2


9. Map and Filter

map(function, list)
Map takes the elements out of the list one by one, apply the function, and put the results in a new list.

In

seq = [1,2,3,4]
list(map(lambda var: var*2,seq))

Out

[2, 4, 6, 8]


filter(function, list)
Filter applies the elements in the list to the function and creates a new list with the values that are true. The important this is that elements are 'true' value!

In

seq = [1,2,3,4]
list(filter(lambda item: item%2 == 0,seq))

Out

[2, 4]


10. Split Methods

It is the most basic built-in function to make a list. 

In

st = 'hello my name is Sonsan'
st.split()

Out

['hello', 'my', 'name', 'is', 'Sonsan']


If you insert certain strings, words are splited by them. 


In

tweet = 'Go programming! #programming '
tweet.split('#')[1]

Out

'programming'


Conclusion
I hope it will be helpful you guys! Of course, it doesn't make sense to fully understand Python with just this cramming points. But if you need to review Python right now in a quick, JUST DO IT NOW.

エンジニアファーストの会社 株式会社CRE-CO
ソンさん


この記事が気に入ったらサポートをしてみませんか?