LoginSignup
0
1

More than 3 years have passed since last update.

pythonでは演算子 ++, --が使えない(phpとの違い)

Last updated at Posted at 2020-05-16

pythonでは演算子 ++, --が使えない

phpやJacaScriptで使える、「++」と「--」がpythonでは使えない。
使おうとすると構文エラーになる。

エラー

i=0;
while i<5:
   print(i)
   i++

#出力
i++
   ^
SyntaxError: invalid syntax

対処法

i+=1, i-=1を使う

i=0;
while i<5:
     print(i)
     i+=1

#出力
0
1
2
3
4

処理 python php JavaScript
i++ -
i+=1
i=i+1
i-- -
i-=1
i=i-1



for文でi++使えないのかと思ったら、phpとpythonではfor文の書き方が全然違ったので問題なしでした、、

python
for i in range(5):
    print(i)
  • pythonのfor文はlistやrangeなど連続したものを一つずつ取り出す。(phpのforeach)
  • ステップも特に指定しなければ i++と同じ処理になる。
  • phpのfor文は条件が成り立つ間繰り返す。

【python】for文の使い方解説


php
<?php
for ($i=0; $i<5; $i++){
  echo $i;
} 
?>
php(HTMLに埋め込み)
<?php for ($i=0; $i<5; $i++): ?>
  <?php echo $i; ?>
<?php endfor; ?>
} 

phpは各処理の後に「;」が必要。
pythonは改行で処理終了とみなす。

0
1
3

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