天使漫步IT工作室天使漫步IT工作室

【代码片段】python自增(++)和自减(--)的正确用法


Warning: count(): Parameter must be an array or an object that implements Countable in /www/wwwroot/u11u.com/usr/themes/wq/functions.php on line 110

Warning: count(): Parameter must be an array or an object that implements Countable in /www/wwwroot/u11u.com/usr/themes/wq/functions.php on line 116

如果你习惯于例如 C++ , Java , PHP 等其他的语言,也许你会想要尝试使用 ++ 或者 -- 自增自减一个变量。在Python中是没有这样的操作符的。

那么python中应该怎么操作自增或者自减呢?

答案:

spam = 1
spam += 1

#或者

spam = 1
spam = spam + 1

#同理

spam = 1
spam -= 1

#或者

spam = 1
spam = spam - 1

如果你依然使用

spam = 1
spam++

#

spam = 1
spam--

会抛出“SyntaxError: invalid syntax”)的错误。因为不要使用++或者--,python中没有这样的语法。

本站原创,欢迎转载,转载敬请标明出处:天使漫步IT工作室 » 【代码片段】python自增(++)和自减(--)的正确用法
添加新评论


Warning: Use of undefined constant php - assumed 'php' (this will throw an Error in a future version of PHP) in /www/wwwroot/u11u.com/usr/themes/wq/comments.php on line 38