您的位置:首页 > 编程语言 > Python开发

python入门(一)

2016-04-03 18:28 507 查看
python入门基本语法

1.自定义函数

#coding=utf-8
#自定义函数,传入参数,没有传入参数

def sayHello():
print ("hello word")

def maxed(a,b):
if(a>b):
print(a)
else:
print(b)

sayHello()
print(maxed(2,3))


2.判断语句

#coding=utf-8
#判断
score = 90

if score >= 80:
print("很好")
elif score >= 60:
print ("及格")
elif score >= 30:
print("不及格")
else:
print("很差")


3.循环语句

#coding=utf-8
#循环
#拼接字字符串

for i in range(0,100):
print("Itme ,{0}{1}{2}".format(i,"hello","python"))


4.面向对象(类)

#coding=utf-8
#类,构造函数,类的继承

class hello:
def __init__(self,name):
self._name=name
def sayhello(self):
print("hello,{0}".format(self._name))

class hi(hello):
def __init__(self, name):
hello.__init__(self, name)
def hi(self):
print("Hi,{0}".format(self._name))

h=hello("张三")
h.sayhello()
h1=hi("李四")
h1.hi()


5.导入库

  建立文件mylib.py,代码如下:

class hello():
def sayhello(self):
print("hello!")


  建立文件loadlib.py,代码如下,两种引用方式。

#coding=utf-8
#引入外部库的两种方式

#import mylib
#h1=mylib.hello()
#h1.sayhello()
from mylib import hello
h=hello()
h.sayhello()


6.python版本下载

  3.x有更多的新特性,2.x运行速度更快
  学习开发使勇2.7.8最好

7.开发环境

  pycharm集成开发环境,本文使用eclipse,python2.7

8.错误:

File "F:\python\jike_leaon\src\flow.py", line 1
SyntaxError: Non-ASCII character '\xc5' in file F:\python\jike_leaon\src\flow.py on line 1, but no encoding declared;

see http://www.python.org/peps/pep-0263.html for details
原因:不支持中文字符串。
修改:文件开头注释加上 #coding=utf-8

9.错误:

TypeError: this constructor takes no arguments

修改:构造函数,是两个下划线,不是一个!

2016-04-03 18:33:34
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: