I started to touch it because I wanted to study Python, so [Detailed explanation of Python grammar](https://www.amazon.co.jp/Python%E6%96%87%E6%B3%95%E8%A9%B3 % E8% A7% A3-% E7% 9F% B3% E6% 9C% AC-% E6% 95% A6% E5% A4% AB / dp / 4873116880 / ref = cm_cr_arp_d_pl_foot_top? Ie = UTF8) Make a note of what you thought. I usually write mainly in Java, so I think it will probably be the focus of what is different from Java.
--The name Python comes from "Monty Python's Flying Circus" (comedy show). --In the sample code, people like "foo", "bar", and "baz" have a habit of using "spam", "ham", and "egg". (It seems that it was used in Tale) ――I think that the unfamiliar word comes from this.
_Behavior
>>> 1+1
2
>>> _ #The last evaluated calculation result is "_Is it really?
2
>>> _ + _ # 2+2=
4
>>> _ # 「 _Is set to 4
4
Write multiple processes at once (do not do basic)
print(1+1); print('Hello')
--In python, specify the code block only by indentation. (Also called "suite") --The standard indentation is 4 spaces.
Numeric type
>>> 100 #integer
100
>>> 100.0 #Floating point number
100.0
>>> 0b100 #Binary number
4
>>> 0o100 #8 base
64
>>> 0x100 #Hexadecimal
256
Concatenation of conditional operators
>>> 99
99
>>> 0 < _ < 100 # (0 < _) and (_ < 100)You don't have to write like
True
Dictionary object
#So-called associative array / hash table. key& value
>>> {1:'one', 2:'two'}
{1: 'one', 2: 'two'}
>>> {} #Empty dictionary
{}
>>> d = {1:'one', 2:'two'}
>>> d[1] #Element reference
'one'
>>> d[1] = 'Ichi' #Change the value of an element
>>> d[1]
'Ichi'
>>> d
{1: 'Ichi', 2: 'two'}
>>> del d[1] #Delete element
>>> d[1]
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
KeyError: 1
>>> d
{2: 'two'}
list
#The list can be updated, but it cannot be used as a dictionary key.
L = [1, 2, 3]
L = [] #Empty list
L = [1, 'Two', [3, 4, 5]]
Tuple
#Tuples cannot be updated, but can be used as dictionary keys.
t = (1, 2, 3)
t = 1, 2, 3 # ()Is not required
t = () #Empty tuples()Expressed in
t = (1, 'Two', [3, 4, 5])
Sequence access
#Arrays such as strings, lists, and tuples are called sequences, and any kind of object can access its elements in the same way.
>>> S = 'abcdefg' #Even a string
>>> S[0]
'a'
>>> S[2]
'c'
>>> L = [0, 1, 2, 3, 4, 5] #Even on the list
>>> L[0]
0
>>> L[2]
2
#Take from behind with a negative index value
>>> S[-1]
'g'
>>> S[-3]
'e'
#Specify the range and take
>>> S[1:4]
'bcd'
>>> S[-3:-1]
'ef'
>>> S[:3] #If the start position is omitted, the same applies from the beginning and the end.
'abc'
#Change elements
>>> L = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> L[0] = 'spam'
>>> L
['spam', 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> L[0:4] = ['ham', 'egg'] #It seems that it can be updated by specifying the range, ignoring even the number of elements if it is a list
>>> L
['ham', 'egg', 4, 5, 6, 7, 8, 9]
--In Python's if statement, not only the logical value type True, but all non-zero numbers and non-empty collections are true. --Number 0, empty collection, None object is false.
--The Python for statement iterates through the __ string and each element in the collection. __
python
>>> S = 'spam'
#It seemed that the character string type was looped as it was, which made me feel uncomfortable, but if it is "execution of processing for each element"
#I'm convinced that "sequences can access elements in the same way for any kind of object"
#Strictly speaking, it is an "Iterable object" (described later).
>>> for c in S:
... print(c)
...
s
p
a
m
else clause
while runnning:
if not c:
break
else:
#Execute when the while loop ends without breaking, the same for for
--The Python collection (list / dictionary. Data structure for efficiently manipulating objects such as set types) provides an interface (__ iterator __) for fetching element objects one by one. ing. --The object that implements the interface for getting the iterator is called Iterable object. --Since all lists and character strings are iterable objects, various language functions and libraries can be written in an element type-independent manner. (Example: for statement)
File objects are also iterable
import sys
for line in sys.stdin: #Read line by line from standard input
print(line, end='') #Write the read line to standard output
function
>>> def avg(a, b, c): #Function definition
... return (a+b+c)/3
...
>>> avg(10, 20, 30)
20.0
>>> avg(b=20, c=30, a=10) #Argument can be called by name (keyword argument), avg(10, 20, 30)Same as
20.0
>>> def avg(a, b=20, c=30): #You can also specify the default value of the argument
... return (a+b+c)/3
...
>>> avg(10) # avg(10, 20, 30)Same as
20.0
class
>>> class Spam: #Class definition
... def ham(self, egg, bacon): #Since the first argument always receives the object to which the method belongs, it is customarily set to self.
... print('ham', egg, bacon)
...
>>> obj = Spam()
>>> obj.ham('foo', 'bar') # 'foo'Is the second argument,'bar'Is the third argument
ham foo bar
>>> class Spam2:
... def __init__(self, ham, egg): # __init__()The method is called automatically if it is in the class (like a constructor)
... self.ham = ham
... self.egg = egg
...
>>> obj = Spam2('foo', 'bar')
>>> print(obj.ham, obj.egg)
foo bar
>>> obj.new_attribute = 1 #You can add attributes suddenly. Really?
>>> print(obj.new_attribute)
1
Recommended Posts