職案人

求職・歴史・仏教などについて掲載するつもりだが、自分の思いつきが多いブログだよ。適当に付き合って下さい。

pythonのコメント

2020年08月19日 | Python
---コメントについて---


【開発環境】
OS:Win10(64ビット)
言語:Python3.8.5(64bit)

【Pythonにおけるコメントの記述方法】
・対話モード
Microsoft Windows [Version 10.0.18363.1016]
(c) 2019 Microsoft Corporation. All rights reserved.

C:\Users\shyok>python
Python 3.8.5 (tags/v3.8.5:580fbb0, Jul 20 2020, 15:57:54) [MSC v.1924 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> #コメント
>>> old = 25
>>> #年齢
>>> print(old)
25
>>> コメント
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'コメント' is not defined


・プログラムから
「sampl04.py」ファイルのスクリプト

コマンド
C:\Users\shyok>cd D:\Python38\code
C:\Users\shyok>d:
D:\Python38\code>python sampl04.py
25

D:\Python38\code>
コメント
  • X
  • Facebookでシェアする
  • はてなブックマークに追加する
  • LINEでシェアする

Pythonでの文の区切りと長い文を途中で改行して入力する方法

2020年08月19日 | Python
Pythonでの文の区切りと長い文を途中で改行して入力する方法

【開発環境】
OS:Win10(64ビット)
言語:Python3.8.5(64bit)

【文の区切りとして改行を使用する】
Python では文の区切りに、改行を使用する

スクリプト
C:\Users\shyok>python
Python 3.8.5 (tags/v3.8.5:580fbb0, Jul 20 2020, 15:57:54) [MSC v.1924 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> str = "Hello,python"
>>> print(str)
Hello,python
>>>
「sampl01.py」ファイルの場合

D:\Python38\code>python sampl01.py
Hello,Python

D:\Python38\code>

【文の区切りとしてセミコロンを使用する】
Python ではセミコロン(;)も文の区切り文字として使用する事が出来る。

対話モードでの例文
Microsoft Windows [Version 10.0.18363.1016]
(c) 2019 Microsoft Corporation. All rights reserved.

C:\Users\shyok>python
Python 3.8.5 (tags/v3.8.5:580fbb0, Jul 20 2020, 15:57:54) [MSC v.1924 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> num1 = 10;num2 = 8;num3 = 12
>>> print (num1 + num2 + num3)
30
>>>
【長い文を途中で改行する】
Python では改行が文の区切り文字となっているので、長い文であっても途中で改行してしまうとそこで文が終わりとみなされエラーとなる

対話モードでの例文
>>> num = 10 + 20 + 30 + 40 + 50 + 60 + 70
>>> print (num)
280
>>> num = 10 + 20 + 30 + 40 + 50 + 60 +
File "<stdin>", line 1
num = 10 + 20 + 30 + 40 + 50 + 60 +
^
SyntaxError: invalid syntax
>>>
改行の前に ¥を入れてみる
>>> num = 10+20+30+40+50+¥
... 50+60+70
>>> print(num)
330
>>>
「sampl03.py」ファイルにプログラムを書く
num = 10+20+30+40+¥
50+60+70
print(num)

【実行】
D:\Python38\code>python sampl03.py
280

コメント
  • X
  • Facebookでシェアする
  • はてなブックマークに追加する
  • LINEでシェアする