Study from Python Hour4: Object-oriented ①

Study from Python Hour4: Object-oriented ①

Learning materials

Past posts

environment

What is object-oriented?

What is a method

>>> a ='hogeo'
>>>
>>> print(a)
hogeo
>>>
>>> print(a.upper())
HOGEO
>>>

>>> my_list = ['abc', 'def', 'ghi']
>>> print(my_list)
['abc', 'def', 'ghi']     
>>>
>>> #Add value to list
>>> my_list.append('jkl')			### my_append after list instance()Specify method
>>> print(my_list)
['abc', 'def', 'ghi', 'jkl']         ###A value has been added to the list
>>>
>>>
>>> my_list.reverse()                ###Method to reverse the order
>>> print(my_list)
['jkl', 'ghi', 'def', 'abc']

Find out what methods are available

>>> dir(1)        ###What methods are there for integers
['__abs__', '__add__', '__and__', '__bool__', '__ceil__', '__class__', '__delattr__', '__dir__', '__divmod__', '__doc__', '__eq__', '__float__', '__floor__', '__floordiv__', '__format__', '__ge__', '__getattribute__', '__getnewargs__', '__gt__', '__hash__', '__index__', '__init__', '__init_subclass__', '__int__', '__invert__', '__le__', '__lshift__', '__lt__', '__mod__', '__mul__', '__ne__', '__neg__', '__new__', '__or__', '__pos__', '__pow__', '__radd__', '__rand__', '__rdivmod__', '__reduce__', '__reduce_ex__', '__repr__', '__rfloordiv__', '__rlshift__', '__rmod__', '__rmul__', '__ror__', '__round__', '__rpow__', '__rrshift__', '__rshift__', '__rsub__', '__rtruediv__', '__rxor__', '__setattr__', '__sizeof__', '__str__', '__sub__', '__subclasshook__', '__truediv__', '__trunc__', '__xor__', 'as_integer_ratio', 'bit_length', 'conjugate', 'denominator', 'from_bytes', 'imag', 'numerator', 'real', 'to_bytes']
>>>          ###Last to_It's easy to understand, such as bytes.
>>>
>>> dir('abc')
['__add__', '__class__', '__contains__', '__delattr__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__getnewargs__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__iter__', '__le__', '__len__', '__lt__', '__mod__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__rmod__', '__rmul__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', 'capitalize', 'casefold', 'center', 'count', 'encode', 'endswith', 'expandtabs', 'find', 'format', 'format_map', 'index', 'isalnum', 'isalpha', 'isascii', 'isdecimal', 'isdigit', 'isidentifier', 'islower', 'isnumeric', 'isprintable', 'isspace', 'istitle', 'isupper', 'join', 'ljust', 'lower', 'lstrip', 'maketrans', 'partition', 'replace', 'rfind', 'rindex', 'rjust', 'rpartition', 'rsplit', 'rstrip', 'split', 'splitlines', 'startswith', 'strip', 'swapcase', 'title', 'translate', 'upper', 'zfill']
>>>

Play with the methods of the list instance

>>> ###List initialization
>>> my_list = ['a', 'b', 'c', 'd']

>>> ###Append to the end of the list with the append method
>>> my_list.append('e')
>>> print(my_list)
['a', 'b', 'c', 'd', 'e']

>>> ###Add to the position specified by insert method
>>> my_list.insert(2,'good')
>>> print(my_list)
['a', 'b', 'good', 'c', 'd', 'e']

>>> ###Remove the element specified by the remove method (the element itself, not the index number))
>>> my_list.remove('good')
>>> print(my_list)
['a', 'b', 'c', 'd', 'e']

>>> ###Empty the list with the clear method
>>> my_list.clear()
>>> print(my_list)
[]

>>> ###Returns how many of the items specified by the argument in the count method are in the list.((Like Excel countif)
>>> my_list = ['a', 'a', 'b', 'b', 'b']
>>> my_list.count('b')
3

Other list operators

>>>
>>> print('c' in my_list)
True
>>>
>>> print('x' in my_list)
False
>>>

>>> ###Remove third element from list
>>> print(my_list)
['a', 'b', 'c', 'e']

>>> ###Show 1st to 4th in the list (4th not included)
>>> my_list = ['a', 'b', 'c', 'd', 'e']
>>> b = my_list[1:4]
>>> print(b)
['b', 'c', 'd']

This summary

For Quotations / Lightning Talk

Recommended Posts

Study from Python Hour4: Object-oriented ②
Study from Python Hour4: Object-oriented ①
Study from Python Hour3: Functions
Study from Python Hour2: Control statements
Study from Python Hour7: How to use classes
Study from Python Reading and writing Hour9 files
Study from the beginning of Python Hour8: Using packages
Python study notes_006
Python study note_004
sql from python
MeCab from Python
Python study note_003
Python study notes _005
Python study notes_001
Python study day 1
Study from Python Hour6: Frequently used data types: tuple type, set type, dictionary type
Use thingsspeak from python
Touch MySQL from Python 3
"Object-oriented" learning with python
Operate Filemaker from Python
Use fluentd from python
Access bitcoind from python
Changes from Python 3.0 to Python 3.5
Changes from Python 2 to Python 3.0
Python from or import
Run python from excel
Install python from source
Execute command from Python
Operate neutron from Python!
Use MySQL from Python
Manipulate riak from python
Force Python from Fortran
Use BigQuery from python.
Execute command from python
[Python] Read From Stdin
Use mecab-ipadic-neologd from python
Flatten using Python yield from
Call CPLEX from Python (DO cplex)
Deep Python learned from DEAP
Post from Python to Slack
Grammar features added from Python3.6
Cheating from PHP to Python
Make MeCab available from Python3
Information obtained from tweet_id (Python)
OCR from PDF in Python
Study Python with Google Colaboratory
Run illustrator script from python
Use MySQL from Anaconda (python)
Anaconda updated from 4.2.0 to 4.3.0 (python3.5 updated to python3.6)
Query Athena from Lambda Python
Access Oracle DB from Python
Start / stop GCE from python
Stop Omxplayer from Python code
python pandas study recent summary
Switch from python2.7 to python3.6 (centos7)
[WIP] Fluent Python Study Note
Install pyenv from Homebrew, install Python from pyenv
Python naming convention (from PEP8)
With skype, notify with skype from python!
Use e-Stat API from Python
Register redmine issue from Python