[慕課網Python入門廖雪峰]
8836 ワード
# -*- coding:utf8 -*-
# 3-1 Python
#Enter a code
print(45678+int('0x12fd2',16))
print('Learn Python in imooc')
print(100 < 99)
print(int('0xff',16)==255)
# 3-2 Python print
#input code
print('hello,python')
print('hello','python')
# 3-3 Python
#print ('hello')
# 3-4 Python
#
x1 = 1
d = 3
n = 100
x100 = x1+(n-1)*d
s=n*x1+n*d*(n-1)/2
print (s)
# 3-5 Python
s = ('Python was started in 1989 by \"Guido\".
Python is free and easy to learn.')
print s
# 3-6 Python raw
print (r'''"To be, or not to be": that is the question....Whether it's nobler in the mind to suffer.''')
# 3-7 Python Unicode
# -*- coding: utf-8 -*-
print ('''
,
。
,
。''')
# 3-8 Python
11.0 / 4 # ==> 2.75
11 / 4 # ==> 2
print (2.5 + 10.0 / 4)
print (2.5 + 10 / 4)
# 3-9 Python
a = 'python'
print ('hello,', a or 'world')
b = ''
print ('hello,', b or 'world')
# a true,
# 4-1 Python list
L = ['Adam',95.5,'Lisa',85,'Bart',59]
print (L)
# 4-2 4-2 Python list
L = [95.5,85,59]
print( L[0])
print( L[1])
print( L[2])
# 4-3 Python list
L = [95.5, 85, 59]
print (L[-1])
print (L[-2])
print (L[-3])
# 4-4 Python
#append() list 。
L = ['Adam', 'Lisa', 'Bart']
L.insert(2, 'Paul')
print (L)
# 4-5 Python list
L = ['Adam', 'Lisa', 'Paul', 'Bart']
L.pop(2)
L.pop(2)
print (L)
# 4-6 Python
L = ['Adam', 'Lisa', 'Bart']
L[-1] = 'Adam'
L[0] = 'Bart'
L[2]=L[-1]
print (L)
# 4-7 Python tuple
t = (0,1,2,3,4,5,6,7,8,9)
print (t)
# 4-8 Python tuple
t = ('Adam',)
print (t)
# 4-9 Python “ ” tuple
# t = ('a', 'b', ['A', 'B'])
t = ('a', 'b', ('A', 'B'))
print t
# 5-1 Python if
score = 75
if score >= 60:
print ('passed')
# 5-2 Python if-else
score = 55
if score >= 60:
print ('passed')
else:
print ('failed')
# 5-3 Python if-elif-else
score = 85
if score >= 90:
print ('excellent')
elif score >= 80:
print ('good')
elif score >= 60:
print ('passed')
else:
print ('failed')
# 5-4 Python for
L = [75, 92, 59, 68]
sum = 0.0
for x in L:
sum += x
print (sum / 4)
# 5-5 Python while
sum = 0
x = 1
while x <=100:
sum += x
x += 2
print (sum)
# 5-6 Python break
sum = 0
x = 1
n = 1
while True:
sum += x
x *= 2
n += 1
if n > 20:
break
print (sum)
# 5-7 Python continue
sum = 0
x = 0
while True:
x = x + 1
if x > 100:
break
if x%2 == 0:
continue
sum += x
print (sum)
# 5-8 Python
for x in ['1','2','3','4','5','6','7','8','9']:
for y in ['1','2','3','4','5','6','7','8','9']:
if x < y:
print (x+y)
# 6-1 Python dict
d = {
'Adam': 95,
'Lisa': 85,
'Bart': 59,
'Paul':75
}
# 6-2 Python dict
d = {
'Adam': 95,
'Lisa': 85,
'Bart': 59
}
print ('Adam: ', d.get('Adam'))
print ('Lisa: ', d.get('Lisa'))
print ('Bart: ', d.get('Bart'))
# 6-3 Python dict
# -*- coding: utf-8 -*-
d = {
95:'Adam',
85:'Lisa',
59:'Bart'
}
# 6-4 Python dict
d = {
95: 'Adam',
85: 'Lisa',
59: 'Bart'
}
d[72] = 'Paul'
# 6-5 Python dict
d = {
'Adam': 95,
'Lisa': 85,
'Bart': 59
}
for key in d:
print (key, ':', d[key])
# 6-6 Python set
#set , list 4 , set 3 。
# set set() list,list set
s = set(['Adam', 'Lisa', 'Bart', 'Paul'])
# 6-7 Python set
# in , true false
s = set([name.lower() for name in ['Adam', 'Lisa', 'Bart', 'Paul']])
print 'adam' in s
print 'bart' in s
# 6-8 Python set
months = set(['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'])
x1 = 'Feb'
x2 = 'Sun'
if x1 in months:
print 'x1: ok'
else:
print 'x1: error'
if x2 in months:
print 'x2: ok'
else:
print 'x2: error'
# 6-9 Python set
s = set([('Adam', 95), ('Lisa', 85), ('Bart', 59)])
for x in s:
print x[0], ':', x[1]
# 6-10 Python set
#set add();set remove()
s = set(['Adam', 'Lisa', 'Paul'])
L = ['Adam', 'Lisa', 'Bart', 'Paul']
for x in L:
if x in s:
s.remove(x)
else:
s.add(x)
print (s)
# 7-2 Python
L = range(1,101) #[1,2,...100]
sum = 0
for i in L :
sum = sum + i*i
print sum
# 7-3 Python
def square_of_sum(L):
return sum([i * i for i in L])
print square_of_sum([1, 2, 3, 4, 5])
print square_of_sum([-5, 0, 5, 15, 25])
# 7-4 Python
import math
def quadratic_equation(a, b, c):
x1 = (-b + math.sqrt(b * b - 4 * a * c))/(2 * a)
x2 = (-b - math.sqrt(b * b - 4 * a * c))/(2 * a)
return x1,x2
print quadratic_equation(2, 3, 0)
print quadratic_equation(1, -6, 5)
# 7-5 Python
def move(n, a, b, c):
if n == 1:
print a, '-->', c
return
move(n-1, a, c, b)
print a, '-->', c
move(n-1, b, a, c)
move(4, 'A', 'B', 'C')
# 7-6 Python
def greet(x = 'world'):
print ('Hello,%s.'%x)
greet()
greet('Bart')
# 7-8 Python
def average(*args):
l = len(args)
if l == 0:return 0.0
else:
return sum(args,0.0)/l
print average()
print average(1, 2)
print average(1, 2, 2, 3, 4)
# 8-1 list
L = range(1, 101)
print L[:10] #
print L[2::3] #3
print L[4:50:5] #50 5
# 8-2
L = range(1, 101)
print L[-10:] #
print L[-46::5] # 10 5
# 8-3
def firstCharUpper(s):
return s.title()
print firstCharUpper('hello')
print firstCharUpper('sunday')
print firstCharUpper('september')
# 9-1
for i in range(1, 101):
if i%7 == 0:
print (i)
# 9-2
#zip() list list:zip([10, 20, 30], ['A', 'B', 'C'])->>>[(10, 'A'), (20, 'B'), (30, 'C')]
L = ['Adam', 'Lisa', 'Bart', 'Paul']
for index, name in enumerate(L):
print index+1, '-', name
# 9-3 dict value
#1. values() dict value list。
#2. itervalues() , dict value, itervalues() values() list 。
d = { 'Adam': 95, 'Lisa': 85, 'Bart': 59, 'Paul': 74 }
print sum(d.values())*1.0/len(d)
d = { 'Adam': 95, 'Lisa': 85, 'Bart': 59, 'Paul': 74 }
sum = 0.0
for score in d.itervalues():
sum = sum + score
print sum/len(d)
# 9-4 dict key value
#items() dict tuple list
# items() iteritems(),iteritems() dict list, tuple
d = { 'Adam': 95, 'Lisa': 85, 'Bart': 59, 'Paul': 74 }
sum = 0.0
for k, v in d.items():
sum = sum + v
print k, ':', v
print 'average', ':', sum/len(d)
# 10-1
print [x*(x+1) for x in range(1,100,2)]
#[1x2, 3x4, 5x6, 7x8, ..., 99x100]
[x * x for x in range(1, 11)]
#[1, 4, 9, 16, 25, 36, 49, 64, 81, 100]
# 10-2
#
d = { 'Adam': 95, 'Lisa': 85, 'Bart': 59 }
def generate_tr(name, score):
if score < 60:
return '%s%s' % (name, score)
return '%s%s' % (name, score)
tds = [generate_tr(name, score) for name, score in d.iteritems()]
print ''
print ''
print '
'.join(tds)
print 'Name Score
'
# 10-3
#isinstance(x, str) x
def toUppers(L):
return [x.upper() for x in L if isinstance(x, str) ]
print toUppers(['Hello', 'world', 101])
# 10-4
# 3 for , 3 。 ,121 , 121。
print [a*100+b*10+c for a in range(1,10) for b in range(0,10) for c in range(0,10) if a == c]