1、使用的2.7
2、环境变量+python的根目录
3、cmd就可以运行,执行使用 python xxx.py
4、编写代码可以直接python进入代码编辑
5、exit()退出编辑环境
a.append() #加末尾
a.insert(0, 'no') #加指定位置
print a[0] #访问第一个
print a[-2] #访问倒数第二个
a.pop() #删除最后一个,并且返回这个元素
a.pop(1) #删除索引为1的元素
a[1] = 'new' #赋值
t = (1,2,3) #不可变,没有append、insert、pop方法,也不能赋值
t = (1,) #单个参数需要加一个逗号
age = 20
if age > 18:
print 'xxxx'
print age
else:
print 'xxxx'
#非
if not age > 18:
#嵌套
if xxx:
123
elif xxx:
123
elif xxx:
123
123
else:
123
a = [1,2,3]
for name in a:
print name
N = 10
x = 0
while x < N:
print x
x = x + 1
N = 100
x = 0
sum = 0
while x < N :
print x
x = x + 1
sum = sum + x
if x == 50 :
break
print sum
print x
for x in ['a', 'b', 'c']:
for y in ['1', '2', '3']:
print x + y
d = {"one" : 1, "two" : 2}
print len(d)
#1、不存在会报错
d = {"one" : 1, "two" : 2}
if 'one' in d :
print d['one']
else :
print 'defalut'
#2、
print d.get('three') #展示none
print d.get('one') #正常展示
d["one"] = 4
for name in d:
print name
l = [1,2,3,3,3,3]
print l
s = set(l)
print s
s1 = set([1,2,3,4,5,6,6])
print s1
print 1 in s #打印True 大小写区分
s = set([1,2,3,4,5])
for name in s:
print name
s = set([1,2,3])
s.add(7)
for name in s:
print name
l = [1,2,3,4,5]
print sum(l)
def myMethod(x):
if x > 0:
return x
else:
return;
print myMethod(1)
print myMethod(-2)
def myMethod(x,y):
return x + y, x - y
x, y = myMethod(1, 2)
print x
print y
print myMethod(1, 2)
t = myMethod(1, 2)
print t[0]
def fact(n):
if n == 1:
return 1;
else :
return n * fact(n-1)
print fact(1)
print fact(10)
def fn(i, n = 2):
...... # n如果不传则默认是2,顺序必须是先必须参数再默认参数
def fn(*suibian):
......
def average(*i):
x = 0
for num in i:
x = x + num
print x / len(i)
average(1,2,3)
l = [1,2,3,4,5,6]
print l[0:3]
print l[:3]
print l[:] #从头到尾复制一个新的list
print l[::2] #每隔2个取一个值
print l[-3:] #从倒数第三个开始取
首字母大写案例:
def up(s):
print s[0:1].upper() + s[1:]
up('abckdf')
数7的案例:
def seven(num):
s = 0
while s < num :
if s % 7 == 0 :
print s
s = s + 1
def shou(str):
for letter in str:
print letter
seven(100)
shou('asdgdfgdfg')
for index, name in enumerate(["one", "two"]):
print str(index) + name
d = {"one" : 1, "two" : 2}
print d.values()
for v in d.values(): #还要生成一个list,不过好记忆
print v
for v in d.itervalues(): #省下生成list的过程,速度快
print v
key和value都迭代遍历
d = {"one" : 1, "two" : 2}
for k, v in d.items():
print k,' : ',v
乘法口诀表案例:
L = []
for x in range(1, 11):
L.append(x * x)
print L
print [x * x for x in range(1,11)]
#输出:[1,4,9,16,25,36,49,64,81,100]
print [x * x for x in range(1,11) if x % 2 == 0] #只想要偶数的平方
range(1, 10, 2) 输出 [1,3,5,7,9]
d = {"Adam": 95, 'Lisa' : 85, 'Bart' : 59}
tds = ['%s %s '%(name, score)
for name, score in d.iteritems()]
print ''
print 'Name Score '
print '\n'.join(tds)
print '
'
l = ['1','2','3']
s = 't'
print s.join(l)
#打印: 1t2t3t
print [m + n for m in 'ABC' for n in '123']
#打印:['A1', 'A2', 'A3', 'B1', 'B2', 'B3', 'C1', 'C2', 'C3']