0
1

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 5 years have passed since last update.

Python入門 基本的な操作の方法(変数、算術演算子、文字列、リスト型)

Last updated at Posted at 2019-05-11

文字の出力

文字を出力するときはprintを使用します。

print("hello world")
実行結果
hello world

変数

数値

Aという変数に10、Bという変数に5として、変数CにA+Bとして計算することができます。

A = 10
B = 5
C = A + B
print(C)
実行結果
15

この場合、print(C)は15として計算されます。

文字

Aという変数に文字列の「Poke」、Bという変数に文字列の「mon」を入れて、変数CにA+Bとすると文字列を接続することができます。

A = "Poke"
B = "mon"
C = A + B
print(C)
実行結果
Pokemon

この場合、print(C)はPokemonと表示されます。

算術演算子

Pythonで使用できる算術演算子には他のプログラミング言語のように以下のようなものがあります。

演算子 名前 説明
+ 加算 足し算
- 減算 引き算
* 乗算 掛け算
/ 除算 割り算
// 整数除算 割り算を整数で返す
% 余り 割り算のあまり
** べき乗 AのB乗などのべき乗を計算

算術演算子の例その1

price = 240
tax = 8/100
taxincluded = price + price*tax
print(round(taxincluded,0))
実行結果
259.0

算術演算子の例その2

A = 11
B = 3
C = A%B
print(C)
実行結果
2

文字列の操作

シングルクォーテーションとダブルクォーテーション

文字列を記述するときは('文字列')シングルクォーテーションもしくは("文字列")ダブルクォーテーションで囲みます。

print('こんにちは')
実行結果
こんにちは
print("こんにちは")
実行結果
こんにちは

バックスラッシュとraw strings

バックスラッシュ(\)を使用するとクォーテーションをエスケープできます。

print('don\'t')
実行結果
don't

改行(\n)としてみなされたくないときは、クォーテーションの前にrをつけることでエスケープできます。

print('C:\data\number')
実行結果
C:\data
umber
print(r'C:\data\number')
実行結果
C:\data\number

様々なクォーテションの使い方

三連のクォーテーションにすることで文字列を複数列にまたがって書くことができます。またバックスラッシュをつけることで改行を文字列として扱わないようにできます。

print("""\
      こんにちは
      さようなら
      また明日""")
実行結果
      こんにちは
      さようなら
      また明日

文字列は加算演算子(+)で連結、乗算演算子(*)で反復をさせることができます。

print(""+""*8 + "のうち")
実行結果
すもももももももものうち

長い文字列を書きたいときは、文ごとにクォーテーションをつけて書くことによって出力時に連結することができます。

print("吾輩わがはいは猫である。"
      "名前はまだ無い。"
      "どこで生れたかとんと見当けんとうがつかぬ。"
      "何でも薄暗いじめじめした所でニャーニャー泣いていた事だけは記憶している。"
      "吾輩はここで始めて人間というものを見た。")
実行結果
吾輩わがはいは猫である。名前はまだ無い。どこで生れたかとんと見当けんとうがつかぬ。何でも薄暗いじめじめした所でニャーニャー泣いていた事だけは記憶している。吾輩はここで始めて人間というものを見た。

インデックス

文字列はインデックスを指定して文字を取得することができます。0を指定すると最初の1文字目が取得できます。また、マイナスをつけると右から文字を取得できます。

text = "あいうえお"
print(text[0])
実行結果
text = "かきくけこ"
print(text[-1])
実行結果

スライス

スライス(:)は、文字列のうち、一部の文字列を取得することができます。また、text[:i] + text[i:]は常にtextと等しくなります。

text = "あいうえお"
print(text[:2])
実行結果
あい
text = "あいうえお"
print(text[2:])
実行結果
うえお
text = "あいうえお"
print(text[:2] + text[2:])
実行結果
あいうえお

文字列の変更について

Pythonの文字列は、不変 (immutable) なので変更できません。変更する場合は、新しく文字列を作成します。

text = "あいうえお"
text = "" + text[1:]
print(text)
実行結果
かいうえお

文字列の長さ

文字列の長さ (length) を計る場合は、len()を使用します。

text = "あいうえお"
print(len(text))
実行結果
5

リスト型(list)

リスト型は、コンマ区切りの値 (要素) の並びを角括弧で囲んだものとして書き表されます。リスト型は、異なる型を並べることもできますが、通常は、同じ型の値を持ちます。また、リスト型でも、スライス、連結をサポートし、可変 (mutable) 型なので、要素を入れ替えることができます。

リスト型の使い方

number = [1,2,3,4,5]
print(number)
実行結果
[1, 2, 3, 4, 5]
number = [1,2,3,4,5]
print(number[0])
実行結果
1
number = [1,2,3,4,5]
print(number[:2])
実行結果
[1, 2]
number = [1,2,3,4,5]
number = number + [6,7,8,9,10]
print(number)
実行結果
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
number = [1,2,3,4,5]
number[2] = 30
print(number)
実行結果
[1, 2, 30, 4, 5]

append

appendを使うと、リストの末尾に新しい要素を追加することができます。

number = [1,2,3,4,5]
number.append(6)
number.append(7)
print(number)
実行結果
[1, 2, 3, 4, 5, 6, 7]

リスト型におけるスライスとlen

スライスでは、代入やサイズの変更、削除などの操作ができます。また、リストにおいてlenを使用すれば、要素の数を数えることができます。

animal = ["いぬ","ねこ","ねずみ","さる","ぶた"]
print(animal)
実行結果
['いぬ', 'ねこ', 'ねずみ', 'さる', 'ぶた']
animal = ["いぬ","ねこ","ねずみ","さる","ぶた"]
animal[2:4] = ["",""]
print(animal)
実行結果
['いぬ', 'ねこ', '鼠', '猿', 'ぶた']
animal = ["いぬ","ねこ","ねずみ","さる","ぶた"]
animal[2:4] = []
print(animal)
実行結果
['いぬ', 'ねこ', 'ぶた']
animal = ["いぬ","ねこ","ねずみ","さる","ぶた"]
print(len(animal))
実行結果
5
0
1
4

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
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?