摘要:本文主要向大家介绍了Python语言练习(一),通过具体的内容向大家展示,希望对大家学习Python语言有所帮助。
本文主要向大家介绍了Python语言练习(一),通过具体的内容向大家展示,希望对大家学习Python语言有所帮助。
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.判断语句
1 #coding=utf-8
2 #判断
3 score = 90
4
5 if score >= 80:
6 print("很好")
7 elif score >= 60:
8 print ("及格")
9 elif score >= 30:
10 print("不及格")
11 else:
12 print("很差")
13
3.循环语句
1 #coding=utf-8
2 #循环
3 #拼接字字符串
4
5 for i in range(0,100):
6 print("Itme ,{0}{1}{2}".format(i,"hello","python"))
4.面向对象(类)
1 #coding=utf-8
2 #类,构造函数,类的继承
3
4 class hello:
5 def __init__(self,name):
6 self._name=name
7 def sayhello(self):
8 print("hello,{0}".format(self._name))
9
10 class hi(hello):
11 def __init__(self, name):
12 hello.__init__(self, name)
13 def hi(self):
14 print("Hi,{0}".format(self._name))
15
16 h=hello("张三")
17 h.sayhello()
18 h1=hi("李四")
19 h1.hi()
5.导入库
建立文件mylib.py,代码如下:
1 class hello():
2 def sayhello(self):
3 print("hello!")
建立文件loadlib.py,代码如下,两种引用方式。
1 #coding=utf-8
2 #引入外部库的两种方式
3
4 #import mylib
5 #h1=mylib.hello()
6 #h1.sayhello()
7 from mylib import hello
8 h=hello()
9 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 //www.python.org/peps/pep-0263.html for details
原因:不支持中文字符串。
修改:文件开头注释加上 #coding=utf-8
9.错误:
TypeError: this constructor takes no arguments
修改:构造函数,是两个下划线,不是一个!
以上就介绍了Python的相关知识,希望对Python有兴趣的朋友有所帮助。了解更多内容,请关注职坐标编程语言Python频道!
您输入的评论内容中包含违禁敏感词
我知道了
请输入正确的手机号码
请输入正确的验证码
您今天的短信下发次数太多了,明天再试试吧!
我们会在第一时间安排职业规划师联系您!
您也可以联系我们的职业规划师咨询:
版权所有 职坐标-一站式IT培训就业服务领导者 沪ICP备13042190号-4
上海海同信息科技有限公司 Copyright ©2015 www.zhizuobiao.com,All Rights Reserved.
沪公网安备 31011502005948号