栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

python练习(一)

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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:pythonjike_leaonsrcflow.py", line 1
SyntaxError: Non-ASCII character 'xc5' in file F:pythonjike_leaonsrcflow.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

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/226083.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号