博客笔记_python基础知识梳理

学习代码却是还是很艰难很费劲,加油吧:

coding: UTF-8

number = 23
guess = int(raw_input(‘enter an integer:’))
if guess == number:
print ‘congratulations,you guessed it.’#New block starts here
print ‘(but you do not win any prizes!)’#New block ends here

elif guess < number:
print ‘No,it is a little higher than that’
else:
print ‘No,it is a little lower than that’
print ‘Done’

number = 23
running = True
while running:
guess = int (raw_input(‘Enter an integer:’))
if guess == number:
print ‘Congratulations,you guessed it.’
running = False
elif guess < number:
print ‘No,it is a little higher than that’
else:
print ‘No,it is a little lower than that’
else:
print ‘The while loop is over’
print ‘Done’

def sayHello():
print ‘Hello World!’
sayHello()

def say(message,times = 2):
print message*times
say(4,times = 1)

def maxxmum(x,y):
if x>y:
return x
else:
print y
print maxxmum(3,2)

import sys
import mymodule
print ‘The command line arguments are:’
for i in sys.argv:
print i
print ‘The PYTHONPATH is’,sys.path,’\n’
mymodule.sayhi()
print ‘Version’,mymodule.version
shoplist = [‘apple’,’mango’,’carrot’,’banana’]
print ‘I have’,len(shoplist),’items to purchase’

print ‘These items are:’
for item in shoplist:
print item
print ‘\n I also have to buy rice’
shoplist.append(‘rice’)
print ‘My shopping list is now’,shoplist
print ‘I will sort my list now’
shoplist.sort()
print ‘Sorted shopping list is’,shoplist
print ‘The first item I will buy is’,shoplist[0]
olditem = shoplist[0]
del shoplist[0]
print ‘I bought the’,olditem
print ‘My shopping list is now’,shoplist

zoo = (‘wolf’,’elephant’,’penguin’)
print ‘Number of animals in the zoo is’,len(zoo)
new_zoo = (‘monkey’,’dolphin’,zoo)
print ‘Number of animals in the new zoo is’,len(new_zoo)
print ‘All animals in new zoo are’,new_zoo
print ‘Animals brought from old zoo are’,new_zoo[2]
print ‘Last animal brought from old zoo is’,new_zoo[2][2]

age = 22
name = ‘Swaroop’
print ‘%s is %d years old’%(name,age)
print ‘Why is %s playing with that python?’%name

ab = {‘Swaroop’:’[email protected]’,
‘Larry’:’[email protected]’,
‘Matsumoto’:’[email protected]’,
‘Spammer’:’[email protected]
}
print “Swaroop’s address is %s”%ab[‘Swaroop’]
ab[‘Guido’] = ‘[email protected]
del ab[‘Spammer’]
print ‘\n There are %d contacts in the address-book\n’%len(ab)
for name,address in ab.items():
print ‘Contact %s at %s’%(name,address)
if ‘Guido’ in ab:
print “\nGuido’s address is %s”%ab[‘Guido’]

shoplist = [‘apple’,’mango’,’carrot’,’banana’]
print ‘item 0 is’,shoplist[0]
print ‘item 1 is’,shoplist[1]
print ‘item 1 to 3 is’,shoplist[1:3]
print ‘item 2 to end is’,shoplist[2:]
print ‘item 1 to -1 is’,shoplist[1:-1]
print ‘item start to end is’,shoplist[-2::]
name = ‘swaroop’
print ‘characters 1 to 3 is’,name[-1::-1]

poem = ”’Programming is fun
When the work is done
if you wanna make your work also fun:
use Python!
”’
f = file(‘poem.txt’,’w’)
f.write(poem)
f.close()
f = file(‘poem.txt’)
while True:
line = f.readline()
if len(line) == 0:
break
print line,
f.close()

import sys
try:
s = raw_input(‘Enter something –>’)
except EOFError:
print ‘\n Why did you do an EOF on me?’
sys.exit()
except:
print ‘\nSome error/exception occurred.’
finally:
print ‘Done’

a = [[1,2],[3,4],[5,6],[7,8],[9,0]]
b = [8,7,9,7,9]
def func(a,b):
new_list = zip(a,b)

new_list.sort(key = lambda x : x[1])
a_new = []
b_new = []
for value_tuble in new_list:
    a_new.append(value_tuble[0])
    b_new.append(value_tuble[1])
return a_new,b_new

a,b =func(a,b)
print a
print b

a = [[1,2],[3,4],[5,6],[7,8],[9,0]]
b = [8,7,9,7,9]
new_list = zip(a,b)
print new_list
new_list.sort(key = lambda x : x[1])
print new_list

def decorator(func):
def wrapper(*args,**kwargs):
pre_index = 0
for iter_p in func(*args,**kwargs):
if (iter_p + 1)%5 ==0:
yield list(func(*args,**kwargs))[pre_index:iter_p+1]
pre_index = iter_p + 1
return wrapper
@decorator
def print_4_list():
for i in range(20):
yield i
if name==’main‘:
for x in print_4_list():
print x
assert isinstance(x,list)
def foo():
print (‘i am foo’)
print (‘foo is running’)
foo()

def use_logging(func):
print(“%s is running” % func.name)
func()
def bar():
print(‘i am bar’)
use_logging(bar)

def use_logging(func):
print (“%s is running”%func.name)
return func
@use_logging
def bar():
print (‘i am bar’)
bar()

猜你喜欢

转载自blog.csdn.net/qq_25213395/article/details/82189777
今日推荐