sunsir 发表于 2017-4-21 08:39:02

python 入门

应用IDLE(python gui)作为编辑器
#1 你好,世界:

>>> print("hello, world")
hello, world
>>>


#2 编写py文件
应用快捷键:
Ctrl+ N 新开一个GUI窗口:
把刚才的代码save到hello.py文件中,按F5 运行一下:
>>> ================================ RESTART ================================
>>>
hello, world
>>>

#3 写个for循环吧:

word=['a','b','c','d','e','f','g']
for x in word:
print(x)

打印结果:

a
b
c
d
e
f
g


#4 写个函数吧:

def sum(a,b):
return a+b
ss=sum
result = ss(2,3)
print(result)


#5 定义一个class吧:

#类和继承
class Base:
def __init__(self):
self.data=[]
def add(self,x):
self.data.append(x)
def addtwice(self,x):
self.add(x)
self.add(x)
#child class extends Base
class Child(Base):
def plus(self,a,b):
return a+b
oChild = Child()
oChild.add("str1")
print(oChild.data)
print(oChild.plus(2,3))

运行结果:

['str1']
5

想要说明的几点:
1.python代码对缩进的要求是很严格的,缩进不对,代码就校验不通过,报语法错误
2.reference website:http://www.cnblogs.com/fortran/archive/2010/08/22/1805690.html
3.有一本电子书:深入python3 可供参考
页: [1]
查看完整版本: python 入门