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 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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值