#!/usr/bin/env python print('Hello World!')
>>>a = 10 >>>print a >>>print type(a)
>>>a = 1.3
>>>print a,type(a)
>>>s1 = (2, 1.3, 'love', 5.6, 9, 12, False) # s1是一个tuple >>>s2 = [True, 5, 'smile'] # s2是一个list >>>print s1,type(s1) >>>print s2,type(s2)
一个序列作为另一个序列的元素
>>>s3 = [1,[3,4,5]]
空序列
>>>print s1[1] 1.3 >>>print s2[2] smile >>>print s3[1][2] #有点二维数组的味道 5
>>>print s1[:5] # 从开始到下标4 (下标5的元素 不包括在内) >>>print s1[2:] # 从下标2到最后 >>>print s1[0:5:2] # 从下标0到下标4 (下标5不包括在内),每隔2取一个元素 (下标为0,2,4的元素) >>>print s1[2:0:-1] # 从下标2到下标1
>>>print s1[-1] # 序列最后一个元素 >>>print s1[-3] # 序列倒数第三个元素
>>>str = 'abcdef' >>>print str[2:4]
>>>print 1+9 # 加法
>>>print 1.3-4 # 减法
>>>print 3*5 # 乘法
>>>print 4.5/1.5 # 除法
>>>print 3**2 # 乘方
if语句之后的冒号
以四个空格的缩进来表示隶属关系, Python中不能随意缩进
if <条件1>:
statement
elif <条件2>:
statement
elif <条件3>:
statement
else:
statement
print a**2
statement
print i
i = i + 1
if i == 2:
continue
c = a**2 + b**2
这个函数的功能是求两个数的平方和。
首先,def,这个关键字通知python:我在定义一个函数。square_sum是函数名。
c = a**2 + b**2 # 这一句是函数内部进行的运算
return c # 返回c的值,也就是输出的功能。Python的函数允许不返回值,也就是不用return。
return可以返回多个值,以逗号分隔。相当于返回一个tuple(定值表)。
class Bird(object): have_feather = True way_of_reproduction = 'egg'
summer = Bird() print summer.way_of_reproduction
class Bird(object): #object说明Bird类没有父类
have_feather = True way_of_reproduction = 'egg' def move(self, dx, dy): position = [0,0] position[0] = position[0] + dx position[1] = position[1] + dy return position summer = Bird() print 'after move:',summer.move(5,8)
class Chicken(Bird): way_of_move = 'walk' possible_in_KFC = True class Oriole(Bird): way_of_move = 'fly' possible_in_KFC = False summer = Chicken() print summer.have_feather print summer.move(5,8)
新定义的鸡(Chicken)类的,增加了两个属性:移动方式(way_of_move),可能在KFC找到(possible_in_KFC)
class Human(object): def __init__(self, input_gender): self.gender = input_gender def printGender(self): print self.gender li_lei = Human('male') # 这里,'male'作为参数传递给__init__()方法的input_gender变量。 print li_lei.gender li_lei.printGender()
class happyBird(Bird): def __init__(self,more_words): print 'We are happy birds.',more_words summer = happyBird('Happy,Happy!')
>>>nl = [1,2,5,3,5]
>>>print nl.count(5) # 计数,看总共有多少个5 >>>print nl.index(3) # 查询 nl 的第一个3的下标 >>>nl.append(6) # 在 nl 的最后增添一个新元素6 >>>nl.sort() # 对nl的元素排序 >>>print nl.pop() # 从nl中去除最后一个元素,并将该元素返回 >>>nl.remove(2) # 从nl中去除第一个2 >>>nl.insert(0,9) # 在下标为0的位置插入9
运算符,比如+, -, >, <, 以及下标引用[start:end]等等,从根本上都是定义在类内部的方法。
尝试一下
>>>print [1,2,3] - [3,4]
class superList(list): def __sub__(self, b): a = self[:] # 这里,self是supeList的对象。由于superList继承于list,它可以利用和list[:]相同的引用方法来表示整个对象。 b = b[:] while len(b) > 0: element_b = b.pop() if element_b in a: a.remove(element_b) return a print superList([1,2,3]) - superList([3,4])
- 内置函数type(),查询变量类型
- 序列sequence
- 区别tuple,不可更改,list可更改
- tuple用小括号,list用中括号
- tuple: 定值表(元组)
- s1=(2,1.3,'love',False,12)
- list:表
- s1=[Ture,5,'simple']
- s1[1]值为5
- s3=[1,[2,3,4]]
- s3[1][0]值为2
- s1=[Ture,5,'simple']
- 引用方式
- [下限:上限:步长]
- 注意如果有上限,是不含上限本身的。
- s1[:5] # 从开始到下标4 (下标5的元素 不包括在内)
- s1[2:] # 从下标2到最后
- s1[0:5:2] # 从下标0到下标4 (下标5不包括在内),每隔2取一个元素 (下标为0,2,4的元素)
- s1[2:0:-1] # 从下标2到下标1
- 尾部元素引用
- s1[-1] # 序列最后一个元素
- s1[-3] # 序列倒数第三个元素
- [下限:上限:步长]
- 字符串是一个tuple,因此也使用上面引用方式
- 判断返回
- print 5 in [1,3,5],返回TRUE
- 语法
- 使用缩进表示隶属关系
- if <condition>: 注意冒号
- 循环
- for 元素 in 序列: 注意冒号
- range()函数来建立list表
- while
- 函数
- def squeare_sum(a,b):
- 面向对象
- 定义类->创建对象->对象引用类属性
- 类中的方法第一个参数必须是self,无论是否用到。方便自身引用。
- object作为没有父类的 类
- __init__()方法,是初始化的过程;python在创建对象时,自动调用这个方法,这个过程叫做初始化的过程。
- dir():查看对象或者类的所有属性;
- help():查看类或对象的帮助信息;
- list类的方法
- >>>print nl.count(5) # 计数,看总共有多少个5
- >>>print nl.index(3) # 查询 nl 的第一个3的下标
- >>>nl.append(6) # 在 nl 的最后增添一个新元素6
- >>>nl.sort() # 对nl的元素排序
- >>>print nl.pop() # 从nl中去除最后一个元素,并将该元素返回
- >>>nl.remove(2) # 从nl中去除第一个2
- >>>nl.insert(0,9) # 在下标为0的位置插入9
- 如果属性或方法已经在父类中定义,你又在子类中定义了,那么子类的对象会参考子类的定义,而不会载入父类的定义。