python基础练习1

来源:互联网 发布:斐波那契数列java递归 编辑:程序博客网 时间:2024/06/05 20:43

 

 

参考示例:

#!/usr/bin/python# -*- coding utf8 -*-# 1.Python脚本执行方式# Linux执行方式 chmod u+x a.py# 或者 python ./a.py# windows 安装路径下的Python.exe 如 c:\Python\python.exe d:\code\a.py# 或者在IDE中执行# 2.简单叙述位和字节关系# 一位包含8字节# 3.简单叙述asii unicode utf8 gbk区别# ascii 支持英文字母数字标点# Unicode 扩展ascii位长 支持更多字符# utf-8 压缩Unicode 节约存储空间# gbk  国标 支持英文和汉字# 4.汉字用utf-8和gbk占的位置val = len('中'.encode('utf-8'))print('长度',val)val = len('中'.encode('gbk'))print('长度',val)#utf-8 3位 gbk 2位# 5.Python中注释# Python中注释用# 多行用三个"""# 6.变量注意事项# 不能数字开头 不能内置关键字 只能包含字母数字下划线# 7.有变量n1 = 5 使用int方法得到该变量最少可以用多少个二进制位表示n1 = 5print(n1.bit_length())#3# 8.布尔值分别有什么#True False  字符串空为假 其余为真  数值0为假 其余为真 空列表 空字典等为假# 9.阅读代码 写出执行结果a = 'xp'b = a.capitalize()print(a)print(b)#xp#Xp#10name = ' wuxP'##移除两边空格,输出结果print(name.strip())##判断是否'wu'开头print(name.startswith('wu'))##判断是否P结果print(name.endswith('P'))##替换  u替换为pprint(name.replace('u','p'))##用x分割print(name.split('x'))#分割之后得到什么类型#分割之后得到列表#变大写 变小写print(name.upper())print(name.lower())#输出第二个字符print(name[1])#输出前三个字符print(name[0:3])#输出后2个字符print(name[-2:])#输出e所在索引位置print(name.find('x')) #没找到返回-1#获取子序列不包含最后一个字符print(name[0:-1])#11.题for i in name:    print(i)#可以取 可迭代#12题#ali = ['alex', 'eric', 'rain']target = ''i = 0while i < len(li):    target = target + li[i]    i += 1print(target)#13.python2中range会立即生成所有 推荐使用xrange 每次生成 3只有rang 即xrange的效果#14content = input('Please input : ')coun = content.split('+')sum = int(coun[0]) + int(coun[1])print(sum)#此题也可以让用户分别输入两个数#15content = input('输入: ')i = 0x = 0for item in content:    bo = item.isalpha()    if bo == True:        i = i + 1    else:        x = x + 1print('字母个数: ',i,'数字个数',x)#16 数值型  字符型#17name = input('input name')place = input('input place')do = input('do somthing')content = "亲爱的{name},喜欢在{place},{do},"val = content.format_map({'name': name, 'place': place, 'do': do })print(val)#18def check_code():    import random    check_code = ''    for i in range(4):        current = random.randrange(0, 4)        if current != i:            temp = chr(random.randint(65, 90))        else:            temp = random.randint(0, 9)        check_code += str(temp)    return check_codewhile True:    code = check_code()    print(code)    name = input('请输入验证码: ')    val1 = code.lower()    new_name = name.lower()    if val1 == new_name:        print('正确')        break    else:        print('错误')        continue#19 敏感词过滤valu = input('请输入')wor = ['苍老师', '东京热']for item in wor:    if item in valu:        valu = valu.replace(item,'***')print(valu)#20st = ''while True:    name = input('name: ')    if name == 'q' or name == 'Q':        break    passwd = input('passwd: ')    if passwd == 'q' or passwd == 'Q':        break    mail = input('email: ')    if mail == 'q' or mail == 'Q':        break    st = st + name + '\t' + passwd + '\t' + mail + '\n'print(st)


0 0
原创粉丝点击