Skip to content

Python的学习笔记1

youngperson edited this page Oct 16, 2016 · 1 revision
涉及到中文的时候在代码第一行加上utf-8编码
'''xxxxx''','''可以做多行注释或者是格式化输出
导入模块: import 模块名 as 别名、from 模块名1 import 模块名2(import sys、from sys import argv)
列表就是php中的数组,元组是常量数组(不可修改),字典是php中的关联数组
要把文件夹当包用需要__init__.py文件,是否为主文件if __name__ == '__main__'
yield有进程线程、协程的概念。

# -*- coding:utf-8 -*-
#和用户交互示例代码
for i in range(0,3):
	userName = raw_input("please input userName:")
	passWord = raw_input("please input passWord:")
	if(userName=='root' and passWord=="123456"):
		print "恭喜你,登录成功!"
		break;
	else:
		print "你还有%d次机会" % (2-i)

# -*- coding:utf-8 -*-
#从列表(数组)中找某个值(4)的位置
list = [1,3,4,8,4,53,90,4,22,33,4,87,4,909]
pos = 0
searchVal = 4
for i in range(list.count(searchVal)):
	if pos==0:
		pos = list.index(searchVal)
	else:
		pos = list.index(searchVal,pos+1)
	print "第%d次找到的值%d在列表的第%d个位置" % (i+1,searchVal,pos+1)

# -*- coding:utf-8 -*-
#类似send命令的文件替换脚本
import sys,os

if len(sys.argv) <=4:
	print "usage:./file_replace.py old_txt new_text filename"

old_text,new_text = sys.argv[1],sys.argv[2]
file_name = sys.argv[3]

f = file(file_name,'rb')
new_file = file('%s.bak' % file_name,'wb')
for line in f.xreadlines():
	#print line.replace(old_text,new_text)
	new_file.write(line.replace(old_text,new_text))
f.close()
new_file.close()

if '--bak' not in sys.argv:
	os.rename(file_name,'%s.bak2'%file_name)    #unchange
	os.rename('%s.bak'%file_name,file_name)      #change
else:
	os.rename('%s.bak'%file_name,file_name)      #replace

# -*- coding:utf-8 -*-
#购物车程序
money = raw_input("请输入你拥有的现金:")
goods_list = {'Iphone':5500,'Mac':9900,'Coffee':55,'Python':4800,'Pad':3999}
buy_list = {}
left_money = 0
cost_money = 0
while True:
	print '========商品列表========='
	for goods in  goods_list:
		print goods,(goods_list[goods])
	print '========================'

	goods = raw_input("请输入商品名字进行购买\quit:")

	if len(buy_list)==0:
		left_money = money

	if goods_list.has_key(goods):
		cost_money += goods_list[goods]
		if cost_money<int(money):
			left_money = int(money)-cost_money  

			#计数
			if buy_list.has_key(goods):
				buy_list[goods] += 1 
			else:
				buy_list[goods] = 1

			print '当前剩余金额:%s'%left_money
		else:
			print '当前剩余金额:%s'%left_money
			print '余额不足!'
			print '======已购买商品列表======'
			for goods in  buy_list:
				print '%sx%s'%(goods,buy_list[goods])
			print '========================'
			break;
	elif(goods=='quit'):
		print '当前剩余金额:%s'%left_money
		print '======已购买商品列表======'
		for goods in  buy_list:
			print '%s x %s'%(goods,buy_list[goods])
		print '========================'
		break
	else:    
		print '商品还未上架'
		continue

# -*- coding:utf-8 -*-
#6位验证码生成
import random
code = []
for i in range(6):
	if i == random.randint(1,5):
		code.append(str(random.randint(1,5)))
	else:
		temp = random.randint(65,90)
		code.append(chr(temp))
print ''.join(code)

# -*- coding:utf-8 -*-
#md5加密
import hashlib
str = 'admin'
hash = hashlib.md5()
hash.update(str)
print hash.hexdigest()
print hash.digest()

# -*- coding:utf-8 -*-
#反射的应用
data = raw_input('请输入地址:')
array = data.split('/')

userspance =  __import__('backend.' + array[0])
model = getattr(userspance,array[0])
func = getattr(model,array[1])
func()

Clone this wiki locally