0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

PythonとTypeScriptの比較

Last updated at Posted at 2025-05-08

業務でTypeScriptを用いることとなったこと・これまでPythonで主に書いていたためその比較を作りました。

型づけ

項目 Python TypeScript 説明
型付け 動的型付け 静的型付け Pythonは実行時にTypeScriptはコンパイル時に型をチェックします。
変数の宣言 型宣言不要 let, const, var と型注釈 TypeScriptでは変数を宣言する際に型を指定(または型推論)します。
定数 明示的な定数はない const TypeScriptでは const で宣言された変数は再代入できません。

コード例
Python:

# 変数の宣言 (型指定なし)
name = "太郎"
age = 30

定数
PI = 3.14159

TypeScript:

// 変数の宣言 (型指定あり)
let name: string = "太郎";
let age: number = 30;

// 定数の宣言
const PI: number = 3.14159;

変数宣言

項目 Python TypeScript 説明
数値型 int, float, complex number TypeScriptでは整数と浮動小数点数を区別しない。
文字列型 str string
真偽値型 bool boolean
リスト/配列 list Array<T> または T[] (例: number[]) TypeScriptの配列は要素の型を指定できます。
タプル tuple [string, number] TypeScriptのタプルは要素の型と数を固定できます。
辞書/オブジェクト dict object, { [key: string]: any }, interface TypeScriptのオブジェクトは構造をより厳密に定義できる。
列挙型 enum (標準ライブラリ) enum 複数の名前付き定数を定義する際に便利。

コード例
Python:


# リスト
my_list = [1, 2, 3]

# 辞書
my_dict = {"name": "太郎", "age": 30}

# タプル
my_tuple = ("apple", 1)

# 列挙型
from enum import Enum
class Color(Enum):
    RED = 1
    GREEN = 2
    BLUE = 3

TypeScript:

// 配列
let myArray: number[] = [1, 2, 3];
let anotherArray: Array&lt;string> = ["apple", "banana"];

// オブジェクト
let myObject: { name: string; age: number } = { name: "太郎", age: 30 };

// タプル
let myTuple: [string, number] = ["apple", 1];

// 列挙型
enum Color {
  Red,
  Green,
  Blue,
}
let myColor: Color = Color.Red;

文字列の埋め込み

項目 Python TypeScript 説明
文字列の埋め込み f-strings (例: f"...{}") テンプレートリテラル (例: `...${}`) Pythonではf文字列で{}内に変数とフォーマットを記述。
TypeScriptではバッククォートで囲み、${} 内に変数を記述。

コード例
Python:

name = "花子"
greeting = f"こんにちは、{name}さん!"
print(greeting)

TypeScript:

let name: string = "花子";
let greeting: string = こんにちは${name}さん;
console.log(greeting);

コード例
Python:

class Person:
def init(self, name, age):
    self.name = name
    self.age = age

def greet(self):
    print(f"私の名前は{self.name}、年齢は{self.age}歳です。")
person = Person("一郎", 25)
person.greet()

TypeScript:

class Person {
  public name: string;
  private age: number;

constructor(name: string, age: number) {
  this.name = name;
  this.age = age;
}

public greet(): void {
  console.log(私の名前は<span class="math-inline">\{this\.name\}年齢は</span>{this.age}歳です。);
}
}   

const person = new Person("一郎", 25);
person.greet();
// person.age = 26; // エラー: privateメンバーにはアクセスできません
0
0
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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?