| ページ一覧 | ブログ | twitter |  書式 | 書式(表) |

MyMemoWiki

Python if for

提供: MyMemoWiki
ナビゲーションに移動 検索に移動

Pythoni if for

Python | Python Osmosis

if,for

  1. Python 2.6.3 (r263rc1:75186, Oct 2 2009, 20:40:30) [MSC v.1500 32 bit (Intel)] on win32
  2. Type "copyright", "credits" or "license()" for more information.
  3.  
  4. ****************************************************************
  5. Personal firewall software may warn about the connection IDLE
  6. makes to its subprocess using this computer's internal loopback
  7. interface. This connection is not visible on any external
  8. interface and no data is sent to or received from the Internet.
  9. ****************************************************************
  10. IDLE 2.6.3
  11. >>> x = int(raw_input("Please enter an integer: "))
  12. Please enter an integer: 42
  13. >>> type(x)
  14. <type 'int'>
  15. >>> if x < 0:
  16. x = 0
  17. print 'Negative change to zero'
  18. elif x == 0:
  19. print 'Zero'
  20. elif x == 1:
  21. print 'Single'
  22. else:
  23. print 'More'
  24.  
  25. More
  26. >>>
  27. >>> my_list = ['cat', 'window', 'defenestrate']
  28. >>> for x in my_list:
  29. print x, len(x)
  30.  
  31. cat 3
  32. window 6
  33. defenestrate 12
  34. >>>
  35. >>> for x in my_list[:]:
  36. if len(x) > 6:
  37. my_list.insert(0,x)
  38.  
  39. >>> my_list
  40. ['defenestrate', 'cat', 'window', 'defenestrate']