ABOUT ME

이 곳은 data scientist가 되기 위한 저의 과정들을 공유하는 공간입니다. 저랑 비슷한 목표를 갖고 있는 사람이 있다면 여기에 있는 저의 내용이 조금이나마 도움이 되면 좋을 거 같네요 :)

Today
Yesterday
Total
  • Finish the second chapter !
    Python/Py4e from Coursera 2020. 10. 4. 16:36

    today i learnt about tuples. he said a tuple is kind of similar to lists, but there is a couple of differences between them. so i want to say the differences!

     

    1. basically, it exactly looks like lists except for using parentheses. that means list uses square brakets[], whereas tuples use parentheses(). 

     

    2. this difference's quite a bit interesting for me. The difference is that lists are mutable, which means things in a list can be replaced as something else. For example,

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

    print(a)

    >>> [1, 2, 3, 4, 5]

    a[2] = 10

    print(a)

    >>> [1, 2, 10, 4, 5]

    But, in tuple's case, they're immutable. So if i have a statement about a tuple, then all the stored values in the tuple are fixed. if you try to change any value, you will get a traceback(which means error). For example,

    b = (1, 2, 3, 4, 5)

    print(b)

    >>> (1, 2, 3, 4, 5)

    b[2] = 10

    Traceback : ...

    ...

    TypeError : 'tuple' object does not support item assignment.

     

    Here's a photo describing all the explanation above.

    3. There are many methods inside lists like append, count, extend, index, insert. But when you use the methods to tuples, it's all prohibited because tuples are not changed once they have specific values.

    4. Another interesting thing is that tuples are comparable each other. If the first item is equal, Python goes on to the next element, and so on , until it finds elements that differ.

    on the picture, it says that it compares the left-most values and if it knows that's right or not, it tells you the logic operator(True, False). If it doesn't match, goes on to the next element, and compares each other.

    'Python > Py4e from Coursera' 카테고리의 다른 글

    Regular expressions  (0) 2020.10.06
    What i learnt today is ...  (0) 2020.10.05
Designed by Tistory.