Python基础入门笔记
1.数据类型:Numbers(数字) 支持int long float complex
String(字符串)
List(列表)----[] 使用最频繁
Tuple(元组)----() 不能二次赋值,相当于只读列表
Dictionary(字典)----{} 由索引(key)和它对应的值value,类似于数组
这些规则以字符串为例,List 和 Tuple 类似:
①从左到右索引默认0开始的,最大范围是字符串长度少1
②从右到左索引默认-1开始的,最大范围是字符串开头
③print str[2:5] # 输出字符串中第三个至第五个之间的字符串
④加号(+)是列表连接运算符,星号(*)是重复操作
2.运算符:
** 幂
// 取整除 商的整数部分
== 等于 - 比较对象是否相等
+= 加等于
and 与
or 或
not 非
in 如果在指定的序列中找到值返回True,否则返回False。
not in
is is是判断两个标识符是不是引用自一个对象
is not
3.输出:
a = “Hello”
b=2
print a
print "This s a information :%s %d" %(a,b)
4.条件语句:
① num=5
if num==2:
print 'Y'
elif num==3:
print 'N'
elif num==4:
print '4'
else:
print 'nosense'
② num=5
if num==5:print 'Y'
5.循环语句:
①while:
num=0
while(num<9):
print 'The num is :',num
num+=1
else:
print 'over'
②for:
for letter in 'python':
print "The current letter is",letter
if letter=='t':break;
print 'Over'
③continue 语句跳出本次循环,而break跳出整个循环。
6.函数:
def printinfo(name,age):
print "This is a printed message!"
print name
print age
printinfo("xie",20)
7.面向对象:
①构造函数:__init__
def __init__(self,name,salary):
self.name=name
self.salary=salary
Employee.empCount+=1
②类方法要带上的一个变量:照惯例它的名称是self。
③继承:
class Child(Parent): # define child class