1
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

Python基本まとめ(備忘録)

Last updated at Posted at 2019-08-03

Python 基本まとめ(備忘録)

文字列表示

print('Hello, World!')

Hello, World!

print('3 + 4')

3 + 4

連結

print('3' + '4')

34

print(str(3) + '4')

34

文字列の繰り返し

print('3' * 4)

3333

改行

print('ABC\nDEF')

ABC
DEF

.シーケンスを取り除く

setup = 'a duck goes into a bar...'

# '.'シーケンスを取り除く
print(setup.strip('.'))

a duck goes into a bar

先頭の単語のタイトルケース

setup = 'a duck goes into a bar...'

# 先頭の単語のタイトルケース
print(setup.capitalize())

A duck goes into a bar...

すべての単語のタイトルケース

setup = 'a duck goes into a bar...'

# すべての単語をタイトルケース
print(setup.title())

A Duck Goes Into A Bar...

すべての文字を大文字

setup = 'a duck goes into a bar...'

# すべての文字を大文字
print(setup.upper())

A DUCK GOES INTO A BAR...

すべての文字を小文字

setup = 'a duck goes into a bar...'

# すべての文字を小文字
print(setup.lower())

a duck goes into a bar...

大文字小文字を逆

setup = 'a duck goes into a bar...'

# 大文字小文字を逆
print(setup.swapcase())

A DUCK GOES INTO A BAR...

30文字分のスペースの中央に文字列を配置

setup = 'a duck goes into a bar...'

# 30字分のスペースの中央に文字列を配置
print(setup.center(30))

a duck goes into a bar...

左寄せ

setup = 'a duck goes into a bar...'

# 左端
print(setup.ljust(30))

a duck goes into a bar...

右寄せ

setup = 'a duck goes into a bar...'

# 右端
print(setup.rjust(30))
 a duck goes into a bar...

数値

print(3)

3

加算

print(3 + 4)

7

print(3 + int('4'))

7

乗算

print(3 * 4)

12

除算

/だけだと、小数点以下も表示される。//だと、小数点以下は切り捨てられる。
%は、割った余りを求める。

15 / 4
15 // 4
15 % 4

3.75
3
3

べき乗

後ろのオペランドの分だけ繰り返し掛け算する。

2 ** 4

16

変数

x = 10
print(x)

10

比較演算子

比較演算子は、両端のオペランドを比較して、「True」か「False」かを返す。
両端のオペランドが等しいか調べる場合は==を用いる。

4 == 4
4 == 5

True
False

演算子 意味
== 両端のオペランドが等しいときにTrue。
!= 両端のオペランドが等しくないときにTrue。
> 左のオペランドが右のオペランドより大きいときにTrue。
< 左のオペランドが右のオペランドより小さいときにTrue。
>= 左のオペランドが右のオペランド以上のときにTrue。
<= 左のオペランドが右のオペランド以下のときにTrue。

論理演算子

論理演算子は、両端のオペランドがTrueかFalseかを比較する演算子。

3 < 5 and 6 < 8

True

3 < 5 or 6 < 8

True

1
4
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?