LoginSignup
2
3

More than 5 years have passed since last update.

業務でJava書いてる人が初めてのPython基礎

Last updated at Posted at 2018-10-25

Pythonのチュートリアルで、Javaと違うと感じた点等をまとめました。

環境

  • Windows10 Pro
  • VS Code
  • Anaconda
  • Python 3.6.5
  • JupyterNotebook

始めの行に以下のコメントを挿入し、エンコードを指定する


# -*- coding: UTF-8 -*-

コメントの書き方


# this is a comment
spam = 1 # this is a comment
text = "#this is not a comment"

javaとは違いますが特殊というわけではないですね

division always returns a floating point number らしいです。

8 / 5
# 1.6

17 / 3 #float
# 5.666666666666667

17 // 3 #integer
# 5

累乗

5 ** 2 
# 25
2 ** 7
# 128

対話モードのPython(>>>)では最後の表示された結果が「_」に格納される

2 * 3
# 6
_ / 2
# 3.0

文字列


print("ダブルクォーテーション")
# ダブルクォーテーション

print('シングルクォーテーション')
# シングルクォーテーション

print("\"\\」でエスケープ\"")
# "「\」でエスケープ"

print(r"「\」をエスケープ\n")
# 「\」をエスケープ\n

print("""
複数行書く事が可能
「\\」で改行コードをエスケープする\
ことも可能
""")
# 複数行書く事が可能
# 「\」で改行コードをエスケープすることも可能

文字列結合


print(3 * "P" + 'ython')
# PPPython

print("P" "ython")
# Python

print("長い文字列を記入したいときに"
      "この機能が役立つらしいです。")
# 長い文字列を記入したいときにこの機能が役立つらしいです。

text = "できない。"
print("文字列結合" text)
# Error

text = "できる。"
print("文字列結合" + text)
# 文字列結合できる。

文字列操作


text = "Python"

print(text[0])
# P

print(text[-1]) # 右から数えて1文字目
# n

print(text[0:-1]) # 1文字目から、右から2文字目まで(終了値は含まれない)
# Pytho

print(text[:3]) # 3文字目まで(終了値は含まれない)
# Pyt

print(text[3:]) # 4文字目から最後まで
# hon

print(text[6:]) # 空文字が返却されるだけでExceptionではない
# 

text[4] = "a" # 文字列オブジェクトの変更は不可能
# Error

print(text[:4] + "a" + text[5:]) # 結合で新たな文字列を作成
# Pythan

text = 'supercalifragilisticexpialidocious' # めっちゃげんき!
print(len(text)) # text.len()とか使えないんだね…。
# 34

List


li = [1, 3, 5, 7, 9]

print(li)
# [1, 3, 5, 7, 9]

print(li[:]) # liと同じに見えるが、新しいオブジェクトを生成している
# [1, 3, 5, 7, 9]

print(li[-1])
# 9

print(li[-1:])
# [9]

print(li[:-1])
# [1, 3, 5, 7]

print(li[0] + [2] + li[1:-1] + [11, 13, 17]) # 1と素数
# Error(li[0]はint、他はlist))

print([li[0]] + [2] + li[1:-1] + [11, 13, 17]) # 1と素数
# [1, 2, 3, 5, 7, 11, 13, 17] (1:intを[]でListに直し格納)

print(li[:1] + [2] + li[1:-1] + [11, 13, 17]) # 1と素数
# [1, 2, 3, 5, 7, 11, 13, 17] (li[:1]でli[0]以下をListで取得)

List要素操作


li = [1, 3, 5, 7, 9]

li[0] = 2 # List内要素置換
print(li)
# [2, 3, 5, 7, 9]

li[1:] = [4, 6, 8, 10] # 複数要素置換
print(li)
# [2, 4, 6, 8, 10]

li.append(12)
print(li)
# [2, 4, 6, 8, 10, 12]

li.append([14, 16, 18]) # listを入れると入れ子になってしまう
print(li)
# [2, 4, 6, 8, 10, 12, [14, 16, 18]]

li[-1:] = [] # listをlistで上書きして削除
print(li)
# [2, 4, 6, 8, 10, 12]

li.append(14, 16, 18)
# Error

li[-1:] = [12, 14, 16, 18] # 最大要素以上を指定して挿入
print(li)
# [2, 4, 6, 8, 10, 12, 14, 16, 18]

dictionary (HashMap)


dic = {"HashMap":"dict",
       "List":"list",
       "boolean":"bool",
       "{}":":",
       "if(bool)":"if bool",
       "func()":"def func()"}

for val in dic :
      print(val + " => " + dic[val])
# HashMap => dict
# List => list
# boolean => bool
# {} => :
# if(bool) => if bool
# func() => def func()

for key, val in dic.items() :
      print(key + " => " + val)
# HashMap => dict
# List => list
# boolean => bool
# {} => :
# if(bool) => if bool
# func() => def func()

その他の書き方

Pythonのクラスの書き方、メソッドの書き方、if文の書き方、独自クラスの呼び出し方

#%%
class String(str) :
    def equals(self, arg: str) -> bool:
        print(self)
        if self == arg :
            return True
        else :
            return False
#%%
from sample import String

a = String("strs")
b = String("str")
s = "strs"

print( a )
print( a.equals(s) )
print( a.equals("str") )
2
3
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
2
3