python簡単メモ

1639 ワード

私の環境windows,editplus,python-2.7.6.
1.パラメータ付き出力:
list = {'zhang','wang','li','zhao'}
for s in list:
	print('my first name is {0}'.format(s))


出力結果:
--------Python--------my first name is zhao my first name is zhang my first name is wang my first name is li出力完了(0秒かかる)-正常終了
2.上記の例listの値を漢字に書くと、間違えます.
   SyntaxError: Non-ASCII character '\xe5' in file jichu.py on line 1, but no encoding declared; see http://www.python.org/peps/pep-0263.html for details
pythonはデフォルトで漢字がないので、ここで符号化を変更し、ファイルの先頭に#coding=utf-8の修正結果を以下のように書きます.
 
#coding=utf-8
list = {' ',' ',' ',' '}
for s in list:
	print('my first name is {0}'.format(s))
	

出力結果:
--------Python--------my first name is李my first name is趙my first name is張my first name is王出力完了(0秒かかる)-正常終了
3.pythonのクラス:
 
class Hello:
	def __init__ (self,name):
		self._name = name
	def say(self):
		print('hello {0}'.format(self._name))
	
class Hai:
	def __init__ (self,name):
		hello._init(self,name)
	def sayHai (self):
		print('hai {0}'.format(self.name))

h = Hello("zhang san")
h.say()

出力:
--------Python--------hello zhang san出力完了(0秒かかる)-正常終了
4.pythonファイルのインポート
法1:
import jichu1

h = jichu1.Hello('lisi')
h.say()

法2:
from jichu1 import Hello

h = Hello('lisi')

h.say()

区別がはっきりしている.
ただいくつかの基礎的な知識です.