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入門 - 学習記録

Last updated at Posted at 2025-04-14

Python学習記録

2025.04.14

やったこと

  • Pythonのインストール
  • Pythonの基本動作の確認

Pythonの特徴

  • スクリプト言語である -> 書いたプログラムをコンピュータがすぐに理解できる、JS、PHP、Pythonなど
  • 型を書かなくても動く
  • ライブラリが豊富

MacにPythonをインストール

  • pyenv(バージョン管理に使用されるツール)を使用
brew install pyenv 
  • Python3系のインストール
    環境変数を設定(zsh の場合 ~/.zshrc、bash の場合 ~/.bash_profile)
echo 'export PYENV_ROOT="$HOME/.pyenv"' >> ~/.zshrc
echo 'export PATH="$PYENV_ROOT/bin:$PATH"' >> ~/.zshrc
echo 'eval "$(pyenv init -)"' >> ~/.zshrc
source ~/.zshrc  # bash の人は source ~/.bash_profile
設定行 意味
export PYENV_ROOT=... pyenv の場所を教える
export PATH=... pyenv のコマンドが使えるようにする
eval "$(pyenv init -)" pyenv をシェルで使えるように初期化

Python 3の最新版を取得してインストール

# 最新の Python 3系バージョンを取得
latest_python3=$(pyenv install -l | grep -v '[a-zA-Z]' | grep -E '^\s*3\.[0-9]+\.[0-9]+$' | tail -1 | tr -d ' ')
echo "Installing $latest_python3..."

# インストール
pyenv install "$latest_python3"

# グローバルに設定(デフォルトで使われるようにする)
pyenv global $latest_python3

# 再ハッシュ(コマンドのリンク更新)
pyenv rehash

インストールの確認

python --version
python3 --version
pyenv versions

Pythonを動かしてみる

簡単な計算と文章を表示させてみる
今回はcnanoを使用したがvimやその他を使うことも可能

$ python
>>> 3 + 2
5
$ touch code.py
$ nano code.py

エディタに画面が切り替わるはず

print('Hello World')

とnanoでかき、code.pyを保存して実行してみる

$ python code.py
Hello World

となれば成功。

これでpythonを使う準備は完了。

<その他のメモ>

  • スクリプト言語の反対はコンパイラ言語 -> 機械語への翻訳にコンパイラを利用する言語のこと、C言語やJava、Go言語など
  • 環境変数は「今のターミナル環境における設定・情報」を表す変数のこと。
  • bash と zsh はどちらも「シェル(Shell)」と呼ばれるプログラムで、ターミナルでコマンドを解釈して実行してくれるインターフェース
0
0
1

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?