This repository was archived by the owner on Aug 18, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpython3_cheetsheet.tex
More file actions
142 lines (106 loc) · 3.14 KB
/
python3_cheetsheet.tex
File metadata and controls
142 lines (106 loc) · 3.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
\documentclass[uplatex, 11pt]{jsarticle}
\usepackage[dvipdfmx]{graphicx}
\usepackage{float}
\usepackage[dvipdfmx]{hyperref}
\usepackage{pxjahyper}
\usepackage{docmute}
\usepackage{plistings}
\usepackage{color}
\definecolor{pblue}{rgb}{0.13,0.13,1}
\definecolor{pgreen}{rgb}{0,0.5,0}
\definecolor{pred}{rgb}{0.9,0,0}
\definecolor{pgrey}{rgb}{0.46,0.45,0.48}
\lstset{
frame=single,
tabsize=2,
basicstyle=\ttfamily,
keepspaces=true,
showstringspaces=false,
breakatwhitespace=true,
commentstyle=\color{pgreen},
keywordstyle=\color{pblue},
stringstyle=\color{pred},
basicstyle=\ttfamily,
moredelim=[il][\textcolor{pgrey}]{\$\$},
moredelim=[is][\textcolor{pgrey}]{\%\%}{\%\%}
}
\hypersetup{
pdftitle={ACM ICPC Python3 CheetSheet},
pdfauthor={Aya Tokikaze}
}
\renewcommand{\headfont}{\bfseries}
\renewcommand\thefootnote{\arabic{footnote}}
\title{ACM ICPC Python3 CheetSheet}
\author{Aya Tokikaze}
\begin{document}
\maketitle
\tableofcontents
\include{input_and_output}
\section{制御構造}
\subsection{リスト初期化}
リスト内包表記でもできるし\texttt{*}で参照のコピーを作ってもいける \\*
\texttt{*}使ったほうが速いらしいのでリスト内包表記よりも\texttt{*}使ったほうがいい
\subsubsection{1次元配列}
\begin{lstlisting}[language=Python]
# 10この0を持った1次元配列
>>> [0] * 10
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
>>> [0 for x in range(10)]
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
\end{lstlisting}
\subsubsection{2次元配列}
\begin{lstlisting}[language=Python]
# 10この0を10こ持った2次元配列
>>> [[0] * 10 for x in range(10)]
[[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
...
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0]]
# 2次元配列ならこれでも行けそうだけどダメ
>>> a = [[0] * 10] * 10
# 1つ要素を変更すると参照のコピーなので
# すべての要素に変更が加わる
>>> a[0][0] = 1
>>> a
[[1, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[1, 0, 0, 0, 0, 0, 0, 0, 0, 0],
...
[1, 0, 0, 0, 0, 0, 0, 0, 0, 0]]
\end{lstlisting}
\newpage
\subsection{三項演算子}
pythonでの三項演算子は
\begin{lstlisting}[language=Python]
<条件式がTrueのときの値> if <条件式> else <Falseのときの値>
\end{lstlisting}
でできる。
\begin{lstlisting}[language=Python]
>>> a = True
>>> print('wei' if a else 'soiya')
wei
\end{lstlisting}
\subsection{スワップ}
pythonでのスワップはcみたいに値をtmpに突っ込まなくても、以下のようにすればできる。
\begin{lstlisting}[language=Python]
a, b = b, a
\end{lstlisting}
\include{slice}
\section{数値関係}
\subsection{数値の桁数}
\begin{lstlisting}[language=Python]
>>> a = 1234567
>>> int(math.log10(a) + 1)
7
\end{lstlisting}
\section{文字列とか}
\subsection{0で桁あわせ}
0埋めの桁合わせをしたいときは一旦strに変換してから\texttt{zfill}メソッドで指定した桁数分0埋めできる。
\begin{lstlisting}[language=Python]
# 元のすうち
>>> a = 12345
# 合わせたい桁
>>> l = 7
>>> str(a).zfill(l)
0012345
\end{lstlisting}
\end{document}