ゲームハード開発エンジニアが作業効率化のためPythonを勉強してみた

Python最速学習方法 → サンプルコードから学ぶ!
参考にしたサイト:http://www.python-izm.com/


内容
1: インタプリタ設定
2: ソースコードが書かれるencodeを設定
4-6: 関数定義
10: __main__ (よくある構文)
15-19: 複数行に渡る文字列
24: 数値->文字列変換
28: 文字列->数値変換
36: タプル
44-46: コマンドライン引数の取得
49-52: リスト
53-54: for文
56-64: while文
66-68: ファイル書き込み
70-73: ファイル読み込み
76-80: HTML解析


コード
     
    
    
  1. #!/usr/bin/python
  2. # -*- coding: utf-8 -*-

  3. # function
  4. def test():
  5. print "call test function"


  6. # インタプリタからimportされたときに実行されない構文
  7. if __name__ == "__main__":
  8. test()

  9. test_str = """
  10. hoge
  11. hogege
  12. hogegege
  13. """
  14. print test_str

  15. int_data = 100
  16. str_data = "yen"
  17. print str(int_data) + str_data
  18. print str_data.replace("yen","dollar")

  19. test_str = "100"
  20. print int(test_str)*100

  21. import datetime
  22. today        =    datetime.date.today()
  23. print today


  24. def getToday(today):
  25. value = (today.year, today.month, today.day)
  26. return value

  27. tuple_data = getToday(today)
  28. print tuple_data
  29. print tuple_data[0]


  30. import sys
  31. param = sys.argv
  32. print param


  33. for_data = [] # list
  34. for_data.append("python")
  35. for_data.append("PHP")
  36. for_data.append("Java")
  37. for value in for_data:
  38. print value

  39. counter = 0
  40. while counter < 10:
  41. counter += 1
  42. if counter == 1:
  43. print"one"
  44. elif counter == 8:
  45. print "eight"
  46. else:
  47. print counter

  48. file = open("write.txt", "w")
  49. file.write("Python file")
  50. file.close()

  51. read = open("write.txt","r")
  52. for row in read:
  53. print row
  54. read.close()


  55. import urllib2
  56. url = "http://www.python-izm.com/"
  57. html = urllib2.urlopen(url)
  58. print unicode(html.read(),"utf-8")
  59. html.close()

以上の文法を一気にかけるようにしておく。



このブログの人気の投稿

Callback関数を知らん人がまず理解すべきことのまとめ。

C言語でBluetoothスタックを叩きたい人のBluetooth開発入門その1

C++プログラミング入門(1) // 倉庫番プログラムの実装