Python is a popular language, and even if you google it, it's noisy, so you have to put it together yourself. Even if there is one more noise.
You can easily reach Official Documents by googled with python docs What you want to find out
.
A level that allows you to fight with the D rank of the paiza skill check.
stdout
https://docs.python.org/ja/3/library/functions.html#print
#In the case of numerical value
print(123)
#For strings
print("thanks")
--Objects other than strings are also converted to strings --Last line break
stdin
https://docs.python.org/ja/3/library/functions.html#input
s = input()
--The trailing line break is removed
https://docs.python.org/ja/3/tutorial/introduction.html#numbers https://docs.python.org/ja/3/tutorial/datastructures.html#tuples-and-sequences
n = 123 #integer
f = 123.4 #Decimal
s1 = 'String' # string (When specifying with single quotes)
s2 = "String" # string (When specifying with double quotes)
ln = [1, 2, 3] #list
lx = [1, "two"] #Different types can be mixed
d = { "a": 1, "b": 2, "c": 3 } #Dictionary type(Things commonly called Hash, Map, or Dict)
t = (1, "two") #Tuple(Tuple)
#Boolean value
b1 = True
b2 = False
#Null or nil-like things
null = None
--You can use either single-quoted or double-quoted strings.
--Note that if you use {a: 1}
as the dictionary key without quotes instead of {"a ": 1}
, the value of variable a will be used as the key.
--Access to tuple elements can be accessed with t [0]
, just like lists.
https://docs.python.org/ja/3/library/functions.html#int
i = int("123")
https://docs.python.org/ja/3/library/functions.html#func-str
s = str(123)
https://docs.python.org/ja/3/tutorial/controlflow.html#if-statements
name = input()
if name == "Anchan":
print("Noble, seriously painful, this is Forever ...")
elif name == "Mi-chan":
print("Too thin")
else:
print("I really quit ...!")
--If you do not add :
after each line of ʻif ..., ʻelif ...
, ʻelse, an error will occur. --Blocks should be indented with spaces instead of being enclosed in
{} etc. --ʻElif
and ʻelse parts can be omitted --ʻElif
part can be written multiple times
--Note that it is ʻelif, not ʻelse if
or ʻelse if. --Since Python is not a ʻif expression
but a ʻif statement, you cannot directly assign the result of a conditional branch to a variable (unlike a language with a ʻif expression
).
https://docs.python.org/ja/3/tutorial/controlflow.html#for-statements
ns = [1, 2, 3]
for n in ns:
print(n)
--An error will occur if you do not add :
after for ... in ...
--Blocks should be indented with spaces instead of being enclosed in {}
etc.
https://docs.python.org/ja/3/library/stdtypes.html#str.split
source = "Pecoline Coccolo Cal"
names = source.split()
def f(a, b, c):
return a + b + c
--def
followed by function name, argument list, :
--If you do not write return
, it will not be a return value
--Blocks are indented with spaces
--You can write multiple sentences
sqrt
import math
x = math.sqrt(2)
stdin
with line breaks to make a" list of strings "import sys
ts = sys.stdin.read().splitlines()
--It looks like you have to importthe
sys` module.
ns = map(int, ["1", "2", "3"])
--You can convert without problems even if there is a line break at the end of the character string
--Since it is a map
object, if you really want to list it, use the list
function as the return value of map
.
ss = map(str, [1, 2, 3])
--Since it is a map
object, if you really want to list it, use the list
function as the return value of map
.
range
to list
ns = list(range(10))
--It seems that you can convert list-like things with the list
function.
s = sum([1, 2, 3, 4, 5])
Functional
#With 2 arguments
plus = lambda a, b: a + b
#With one argument
plus1 = lambda a: plus(1, a)
#With 0 arguments
hello = lambda: print("hello")
--return
returns the return value without writing
unique
ns = list(set([1, 2, 2, 3]))
--It seems that it will be converted back to list
after making it a set
type and removing duplicates.
filter
#For example, when filtering by an even number
even = lambda n: n % 2 == 0
ns = filter(even, range(10))
concat, flatten
ns = sum([[1,2,3], [10, 20, 30]], [])
--By specifying the initial value in the second argument of the sum
function, it looks like fold
or reduce
using+
.
map
#For example, if you add 1 to each element
plus1 = lambda n: n + 1
ns = map(plus1, [1, 2, 10, 20, 100, 200])
reduce
from functools import reduce
ns = [1, 2, 3, 4, 5]
#Oreore sum
plus = lambda a, b: a + b
my_sum = reduce(plus, ns, 0)
#Oreore product
times = lambda a, b: a * b
my_product = reduce(times, ns, 1)
#Ore ole len
one = lambda _: 1
my_len = reduce(plus, map(one, ns), 0)
--Import reduce
from the functools
module
Recommended Posts