由乐动体育LD90.VIP提供的
#!/bin/env python
#-- coding:utf8 --
学 python 的 记录
练习一天 ~
adict = dict([(‘name’,‘bob’),(‘age’,25)])
print(len(adict))
print(adict)
a = hash(100) # 判断给定的数据是不是不可变的 , 不可变数据才能作为 key
print(a)
a = adict.keys() # 取出所有字典的 key
print(a)
a = adict.values() # 取出字典所有的 value
print(a)
a = adict.items() # 取出 key:value 对
print(a)
#get 方法常用很重要
print(adict.get(‘name’)) # 取出字典中 name 对应的 value, 如果没有则返回 None
print(adict.get(‘qq’)) #None
print(adict.get(‘qq’,‘not found’)) # 没有 qq, 返回指定内容 :not found
print(adict.get(‘age’,‘not found’))
a = adict.update({‘phone’: ‘123456’}) # 返回值为 None
print(a)
b = adict[‘phone’] = ‘123456’ # 存在则更新 不存在就添加 并且可赋值
print(b)
# 集合相当于是无值 (value) 的字典 , 所以也用 {} 表示
myset = set(‘hello’)
print(len(myset))
for ch in myset:
print(ch)
aset = set(‘abc’)
bset = set(‘cde’)
aset & bset # 交集
aset.intersection(bset) # 交集
aset | bset # 并集
aset.union(bset) # 并集
aset - bset # 差补
aset.difference(bset) # 差补
aset.add(‘new’)
print(aset)
aset.update([‘aaa’,‘bbb’])
print(aset)
aset.remove(‘bbb’)
cset = set(‘abcde’)
dset = set(‘bcd’)
cset.issuperset(dset) #cset 是 dset 的超集么 ?
dset.issubset(cset) #dset 是 cset 的子集么 ?
#cp /etc/passwd .
#cp /etc/passwd mima
#vim mima -> 修改 , 与 passwd 有些区别
with open(‘passwd’)as fobj:
aset = set(fobj)
with open(‘mima’) as fobj:
bset = set(fobj)
with open(‘diff.txt’,‘w’)as fobj:
fobj.writelines(bset - aset)
import getpass
userdb = {}
def register():
username = input('username: ')
if username in userdb:
print(’%s already exists.’% username)
else:
password = input('password: ')
userdb[username]=password
def login():
username = input('username: ')
password = getpass.getpass('password: ').strip()
if userdb.get(username) != password:
print(‘login failed’)
else:
print(‘login successful’)
def show_menu():
cmds = {‘0’:register,‘1’:login}
prompt = “”"(0)register
(1)login
(2)exit
Please input your choice(0/1/2): “”"
while True:
choice = input(prompt).strip()[0]
if choice not in ‘012’:
print(‘Invalid input,Try again.’)
continue
if choice == ‘2’:
break
if name == ‘main’:
show_menu()
import time
result = 0
start = time.time() # 返回运算前时间戳
for i in range(100000000):
result +=i
end = time.time() # 返回运算后时间戳
print(result)
print(end-start)
print(‘hello world!’)
if 3 > 0:
print(‘ok’)
print(‘yes’)
x = 3; y = 4 # 不推荐 可读性差
print(x+y)
print(‘hello world!’)
print(‘hello’,‘world!’) # 逗号自动添加默认的分隔符 : 空格
print(‘hello’ + ‘world’) #+ 表示字符拼接
print(‘hello’,‘world’,sep=’***’) #sep 指定分隔符
print(’#’ * 50) #* 表示重复 *50 遍
print(‘how are you?’,end=’’) # 取消 print 的默认打空格机构
print(5/2)
print(5//2)
print(5%2)
print(5**3)
print(5>3)
print(3>5)
print(20>10>5)
print(20>10 and 10 > 5)
print(not 20 > 10)
number = input(’ 请输入数字 ; ') #input 用于获取键盘输入
print(number)
print(type(number)) #input 获得的数据是字符型
function(){ //外汇经纪商动态:http://www.fx61.com/news
print(number + 10) # 报错 不能把字符和数字做运算
print(int(number) + 10) #int 可将字符串 10 转换成数字 10
print(number + str(10)) #str 将 10 转换为字符串后实现字符串拼接
username = input('username: ')
print(‘welcome’,username) #print 各项间默认以空格作为分隔符
print(‘welcome’+username) # 助意引号内最后的空格
sentence = ‘tom’s pet is a cat’
sentence2 = “tom’s pet is a act”
sentence3 = ‘tom said:“hello world!”’
sentence4 = ‘tom said"hello world"’
# 三个连续的单引号或双引号 , 可以保存输入格式 , 允许输入多行字符串
words = “”"
hello
world
abcd"""
print(words)
py_str = ‘python’
len(py_str) # 取长度
py_str[0] # 取第一个字符串
‘python’[0]
py_str[-1] # 最后一个字符
#py_str[6] # 错误 , 下表超出范围
py_str[2:4] # 切片 起始下标包含 , 结束下标不包含
py_str[2:] # 从下标为 2 的字符取到结尾
py_str[:2] # 从开头取到下标是 2 之前的字符
py_str[:] # 取全部
# 步长值为 2, 默认是 1
py_str[::2]
py_str[1::2] # 去除 yhn
py_str[::-1] # 步长为负 , 表示自右向左取