python基本知识
Posted yuyukun
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python基本知识相关的知识,希望对你有一定的参考价值。
1.基本数据类型
1.1int
字符串转换为数字,比如
a = “123”
print(type(a) , a)
b = int(a)
print(type(b),b)
num = “b”
v = int(num, base=16) #16进制
print(v)
age = 3
r=age.bit_length() #求二进制位数
print(r)
results:<class(str),123>, <class(int),123>, 11, 2
1.2.str,字符串转换
#1 首字母大写
test = "aLex"
v=test.capitalize() #将首字母转换成大写,其余小写
print(v) #result:Alex
test = "aLEX"
#2 所有变成小写
v1 = test.casefold() #result:alex 所有字母转换为小写(包括特殊字符)
v2 = test.lower() #result:alex 只能转换字母大小写(区别于casefold())
#3 设置宽度,并将内容居中
v3 = test.center(20,"*") #设置参数,并将内容居中,20代指总长度,“*”代指空白处用*填充
v3 = test.ljust(20,"*") #result:alex****************
v3 = test.rjust(20,"*") #result:***************alex
v3 = test.zfill(20) #result:0000000000000000alex
#4 去字符串中寻找,寻找子序列的出现
test = "aLexalexr"
v4 = test.count(‘e‘, 5) #result:1, 表示从字符串中寻找,字符在子序列中出现的次数,5表示从第5+1个字符开始,注意python首字符位置是0
#5 检测字符以什么为结尾,以什么为开始
v5 = test.endswith(‘ex‘) #result:False 表示检验字符串以什么字符结尾,匹配输出True,否则输出False
v5 = test.startswith(‘ex‘) #result:False 表示检验字符串以什么字符开始,匹配输出True,否则输出False
#6 expandtabs, 断句20空
# 7 从开始往后找,找到第一个之后,获取其未知
v6 = test.find(‘ex‘) #result:2 从开始位置0,寻找到第一个同样字符后,输出位置
test1 = ‘i am {name}, age {a}‘
print(test1)
v7 = test1.format(name=‘alex‘, a=19)
print(v7) #result:i am {name}, age{a} i am alex, age 19 格式化,将大括号中的值替代
v8 = test.format_map({"name":‘alex‘,"a":19}) #以字典方式寻找原字符串中name和a,并将其替换成alex和19,传入的值形式{“name”:‘alex‘, ‘a‘:19}
index 寻找功能与find(找不到显示-1)类似,但找不到报错
v9 = test.isalnum() #判断字符串中是否 只包含字母或数字
2.将字符串转化为数字
inp = "10"
new_inp = int(inp)
3.编码字节数(对于中文)
unicode:2字节
utf-8:3字节
gbk:2字节
4.条件语句
基本if,else...
嵌套if,elif,elif,else...
5.while循环
5.1while条件:
...
print(‘...‘)
5.2while else 当不满足while条件时,执行else内部代码
5.3continue break
continue:终止当前循环,开始下一次循环
break:终止所有循环,调出该循环
例:用户登录(三次机会重试)
count = 0
while count < 3:
user = input(">>>")
pwd = input(">>>")
if user == ‘alex‘ and pwd == ‘123‘
print(‘欢迎登录‘)
break
else:
print(‘用户名或密码错误’)
count = count + 1
6.运算符
6.1结果是值
算数运算 a = 10 * 10
赋值运算 a = a + 1
6.2结果是布尔值
比较运算 a = 1>5 False
逻辑运算 a = 1>6 or 1 == 1 True
成员运算 a ="蚊" in "郑建文"
7.运算python的两种途径:
1.python编译器
2.cmd命令行如:F:pythonpython.exe 1.py(文件名)
8.python常用字符串
8.1粗分类
# join # ‘_‘.join("asdfasdf")
# split
# find
# strip
# upper
# lower
8.2细分类
8.2.1将指定字符串替换为指定字符串
v = test.replace("ex",‘bbb‘,2) #2表示替换2个位置
1 test = "alexalexalex" 2 v = test.replace("ex",‘bbb‘) 3 print(v) 4 v = test.replace("ex",‘bbb‘,2) 5 print(v)
result:albbbalbbbalbbb
albbbalbbbalex
8.2.2字母,数字,下划线 : 标识符
1 # a = "def" 2 # v = a.isidentifier() 3 # print(v)
result:True
8.2.3大小写转换
1 test = "Alex" 2 v = test.swapcase() 3 print(v)
result:aLEX
8.2.4以xxx开头,以xx结尾
1 # test = "backend 1.1.1.1" 2 # v = test.startswith(‘a‘) 3 # print(v) 4 # s = test.endswith(‘a) 5 # print(s)
result:False
False
8.2.5分割,只能根据,true,false:是否保留换行
1 # test = "asdfadfasdf asdfasdf adfasdf" 2 # v = test.splitlines(False) #True保留换行,见结果2 3 # print(v)
result:[‘asdfadfasdf‘, ‘asdfasdf‘, ‘adfasdf‘]
[‘asdfadfasdf ‘, ‘asdfasdf ‘, ‘adfasdf‘]
8.2.6分割为三部分
test = ‘asdfasfdfs‘
1 # v = test.split(‘s‘,2) 2 # print(v) 3 # test.rsplit()
result:[‘a‘, ‘dfa‘, ‘fdfs‘]
8.2.7对应关系替换
1 # test = "aeiou" 2 # test1 = "12345" 3 # v = "asidufkasd;fiuadkf;adfkjalsdjf" 4 # m = str.maketrans("aeiou", "12345") 5 # new_v = v.translate(m) 6 # print(new_v)
result:1s3d5fk1sd;f351dkf;1dfkj1lsdjf
8.2.8移除字符串
1 # 移除指定字符串 2 # 有限最多匹配 3 # test = "xa" 4 # # v = test.lstrip(‘xa‘) 5 # v = test.rstrip(‘9lexxexa‘) 6 # # v = test.strip(‘xa‘) 7 # print(v) 8 9 # test.lstrip() 10 # test.rstrip() 11 # test.strip() 12 # 去除左右空白 13 # v = test.lstrip() 14 # v = test.rstrip() 15 # v = test.strip() 16 # print(v) 17 # print(test)
8.2.9判断和转换大小写
1 # test = "Alex" 2 # v1 = test.islower() 3 # v2 = test.lower() 4 # print(v1, v2) 5 6 # v1 = test.isupper() 7 # v2 = test.upper() 8 # print(v1,v2)
8.2.10将字符串中的每一个元素按照指定分隔符进行拼接(join命令)
1 # test = "你是风儿我是沙" 2 # print(test) 3 # v = "_".join(test) 4 # print(v)
8.2.11判断是否是标题(每个单词首字母大写)
1 # test = "Return True if all cased characters in S are uppercase and there is" 2 # v1 = test.istitle() 3 # print(v1) 4 # v2 = test.title() 5 # print(v2) 6 # v3 = v2.istitle() 7 # print(v3)
8.2.12判断是否全部是空格
v = test.isspace()
8.2.13是否存在不可显示的字符
1 # 制表符 2 # 换行 3 # test = "oiuas dfkj" 4 # v = test.isprintable() 5 # print(v)
8.2.14当前输入是否是数字(3法)
test = "二" # 1,② v1 = test.isdecimal() #十进制 v2 = test.isdigit() #数字即可 v3 = test.isnumeric() #有数字即可,范围最广,包括1,②等
print(v1,v2,v3)
8.2.14是否是字母,汉字
# v = test.isalpha()
8.2.15字符串中是否只包含 字母和数字
# v = test.isalnum()
8.2.16格式化,传入的值 {"name": ‘alex‘, "a": 19}
1 test = ‘i am {name}, age {a}‘ 2 v1 = test.format(name=‘df‘,a=10) 3 v2 = test.format_map({"name": ‘alex‘, "a": 19}) 4 5 test = ‘i am {name}, age {a}‘ 6 v1 = test.format(name=‘yuyukun‘,a=25) 7 v2 = test.format_map({"name":‘yuyukun‘,"a":25}) 8 print(v1,‘ ‘,v2)
8.2.17格式化,将一个字符串中的占位符替换为指定的值
1 # test = ‘i am {name}, age {a}‘ 2 # print(test) 3 # v = test.format(name=‘alex‘,a=19) 4 # print(v) 5 6 # test = ‘i am {0}, age {1}‘ 7 # print(test) 8 # v = test.format(‘alex‘,19) 9 # print(v)
8.2.18index找不到,报错
1 # test = "alexalex" 2 # v = test.index(‘8‘) 3 # print(v)
8.2.18v.find()
1 test = "alexalex" 2 v = test.find(‘lex‘) 3 print(v)
resutl:2
8.2.19expandtabs,断句20(制作表格)
1 # test = "username email password laiying[email protected] 123 laiying[email protected] 123 laiying[email protected] 123" 2 # v = test.expandtabs(20) 3 # print(v)
8.2.20字符串以XXX结尾/开始
1 # v = test.endswith(‘ex‘) 2 # v = test.startswith(‘ex‘)
8.2.21去字符串中寻找,寻找子序列的出现次数
1 # test = "aLexalexr" 2 # v = test.count(‘ex‘) 3 # print(v) 4 5 # test = "aLexalexr" 6 # v = test.count(‘ex‘,5,6) 7 # print(v)
result:2
0
8.2.22设置宽度,并将内容居中
1 # 20 代指总长度 2 # 空白未知填充,一个字符,可有可无 3 # v = test.center(20,"中") 4 # print(v) 5 6 # test = "alex" 7 # v = test.ljust(20,"*") 8 # print(v) 9 10 # test = "alex" 11 # v = test.rjust(20,"*") 12 # print(v) 13 14 # test = "alex" 15 # v = test.zfill(20) 16 # print(v)
8.2.23所有变小写,首字母大写
1 # v = test.capitalize()
2 # v1 = test.casefold()
3 # v2 = test.lower()
9.常用输出工具
# len("asdf")
# for循环
# 索引
# 切片
10.字符串一旦创建不可修改, 一旦修改或者拼接,都会造成重新生成字符串
如:# name = "zhengjianwen"
# age = "18"
# info = name + age
# print(info)
原理如下:
11.python基础练习(一)
11.1制作趣味模板程序 需求:等待用户输入名字、地点、爱好,根据用户的名字和爱好进行任意现实 如:敬爱可亲的 xxx,最喜欢在 xxx 地方干 xxx
test = ‘敬爱可亲的{name},最喜欢在{place}{doing_something}‘ print(test) v = test.format(name=‘yuyukun‘,place=‘法国‘,doing_something=‘游览‘) print(v)
11.2制作随机验证码,不区分大小写。 流程:
- 用户执行程序
- 给用户显示需要输入的验证码
- 用户输入的值
用户输入的值和显示的值相同时现实正确信息; 否则继续生成随机验证码继续等待用户输入
生成随机验证码代码示例:
1 def check_code(): 2 import random 3 checkcode = ‘‘ 4 for i in range(4): 5 current = random.randrange(0,4) 6 if current != i: 7 temp = chr(random.randint(65,90)) 8 else: 9 temp = random.randint(0,9) 10 checkcode += str(temp) 11 return checkcode 12 code = check_code() 13 print(code) 14 15 i = 0 16 v = input("请输入验证码:") 17 test = v.upper() 18 while i < len(code): 19 if test[i] == code[i]: 20 i = i + 1 21 else: 22 i = 0 23 print("验证码错误") 24 v = input("请再次输入验证码:") 25 test = v.upper() 26 continue
11.3开发敏感词语过滤程序, 提示用户输入内容,如果用户输入的内容中包含特殊的字符: 如 "苍老师" "东京热",则将内容替换为 ***
1 uncivilized_vocabulary = input("please input some words:") 2 u_v = uncivilized_vocabulary 3 if u_v == "东京热" or u_v == "苍老师": 4 u_v1 = u_v.replace(‘东京热‘,‘***‘) 5 else: 6 u_v1 = u_v 7 print(u_v1)
11.4
制作表格 循环提示用户输入:用户名、密码、邮箱 (要求用户输入的长度不超过 20 个字符,如果超过则只有前 20 个字符有效) 如果用户输入 q 或 Q 表示不再继续输入,将用户输入的内容以表格形式大隐
1 s = ‘‘ 2 while 1: 3 t = input("go on(g) or quit(q) ") 4 if t != ‘q‘: 5 name = input(‘‘) 6 pwd = input(‘‘) 7 email = input(‘‘) 8 if len(name) > 20: 9 name = name[0:20] 10 if len(pwd) > 20: 11 pwd = pwd[0:20] 12 if len(email) > 20: 13 email = email[0:20] 14 if t == ‘q‘: 15 print(s.expandtabs(20), end=‘‘) 16 break 17 temp = "{0} {1} {2} " 18 v = temp.format(name,pwd,email) 19 s = s + v 20 print(s.expandtabs(20),end=‘‘)
以上是关于python基本知识的主要内容,如果未能解决你的问题,请参考以下文章
Python练习册 第 0013 题: 用 Python 写一个爬图片的程序,爬 这个链接里的日本妹子图片 :-),(http://tieba.baidu.com/p/2166231880)(代码片段