day3复习
>>> for i in range(10):
... if i == 3:
... break
... print(i)
...
0
1
2
>>> for i in range(10):
... if i == 3:
... continue
... print(i)
...
0
1
2
4
>>> while True:
... i = int(input("请输入一个数字:"))
... if i%2 == 0:
... print("偶数")
... else:
... print("奇数")
... if i == 100:
... break
>>> ord("杜")
26460
>>> chr(26460)
'杜'
>>> d = "杜崇崇"
>>> type(d)
<class 'str'>
>>> d.encode("gbk")
b'\xb6\xc5\xb3\xe7\xb3\xe7'
>>> d.encode("utf8")
b'\xe6\x9d\x9c\xe5\xb4\x87\xe5\xb4\x87'
>>> type(d.encode("gbk"))
<class 'bytes'>
>>> type(d.encode("gbk").decode("gbk"))
<class 'str'>
unicode 不能直接写入文件也不能直接在网络传输,必须是bytes
练习1:
>>> a = "测试"
>>> type(a)
<class 'str'>
>>> a.encode("gbk")
b'\xb2\xe2\xca\xd4'
>>> type(a.encode("gbk"))
<class 'bytes'>
>>> type(a.encode("gbk").decode("gbk"))
<class 'str'>
encode 将str(unicode) 转为bytes类型
decode 将bytes类型转为str(unicode)类型
import random
a = [1,2,4,"as","ba"]
random.random() # 随机小数
100+random.random() # 整数+小数
random.randint(1,100) # 随机1,100之间的整数
random.choice() # 随机一个列表里面的值
random.uniform(1,100) # 随机1,100之间的小数点数
random.shuffle(a) # 随机对列表的顺序进行打乱
练习2:随机生成一个小写字母
chr(random.randint(97,123))
chr(97+random.randint(1,25))
chr(ord("A")+random.randint(1,25))
生成随机的10位小写字母:
s = ""
for i in range(10):
... ss = chr(ord("a")+random.randint(1,25))
... s+=ss
生成随机的10位小写字母:
for i in range(10):
... ss = chr(ord("A")+random.randint(1,25))
... s+=ss
生成随机的5个小写字母和5个大写字母:
>>> for i in range(5):
... s+= chr(ord("a")+random.randint(1,25))
...
>>> for i in range(5):
... s+= chr(ord("A")+random.randint(1,25))
生成随机不限定固定大小写个数的10个字母:
lower_num = random.randint(1,9)
uper_num = 10-lower_num
for i in range(lower_num):
s+= chr(ord("a")+random.randint(1,25))
for i in range(uper_num ):
s+= chr(ord("A")+random.randint(1,25))
>>> lower_case = string.ascii_lowercase
>>> lower_case
'abcdefghijklmnopqrstuvwxyz'
>>> low_list = list(lower_case)
>>> low_list
['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z']
>>> low_list_s = low_list[:10]
>>> low_list_s
['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j']
>>> "".join(low_list_s)
'abcdefghij'
>>>
>>> letters = string.ascii_letters
>>> letters
'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'
>>> letters_list = list(letters)
>>> random.shuffle(letters_list)
>>> letters_list
['Q', 'Y', 'V', 'q', 'g', 'p', 'K', 'a', 'T', 'X', 's', 'U', 'H', 'N', 't', 'i', 'b', 'n', 'r', 'h', 'G', 'f', 'd', 'O', 'j', 'A', 'P', 'F', 'R', 'l', 'x', 'z', 'J', 'c', 'E', 'e', 'k', 'o', 'Z', 'D', 'y', 'M', 'I', 'C', 'W', 'w', 'S', 'L', 'v', 'B', 'u', 'm']
>>> "".join(letters_list[] )
KeyboardInterrupt
>>> "".join(letters_list[:10])
'QYVqgpKaTX'
>>> [random.choice(list(string.ascii_letters)) for i in range(1,9)]
['Y', 'g', 'y', 'j', 'G', 'K', 'I', 's']
>>> [random.choice(list(string.ascii_letters)) for i in range(10)]
['M', 'q', 'y', 'x', 'I', 'F', 'I', 'W', 'm', 's']
>>> "".join([random.choice(list(string.ascii_letters)) for i in range(10)])
'RZqoHNduTe'
- Python ord & chr
ord & chr ord: 返回单个 Unicode 字符编码的整数 chr: 给一个 Unicode 编码,返回一个字符 (0 ~ 1,114,111) (ASCII 与 Unicode ...
- Python有用的内置函数divmod,id,sorted,enumerate,input,oct,eval,exec,isinstance,ord,chr,filter,vars,zip
divmod(a, b) 函数接收两个数字类型(非复数)参数,返回一个包含商和余数的元组(a // b, a % b) id() 函数用于获取对象的内存地址. sorted(iterable, key ...
- python 加密算法及其相关模块的学习(hashlib,random,string,math)
加密算法介绍 一,HASH Hash,一般翻译做“散列”,也有直接音译为”哈希”的,就是把任意长度的输入(又叫做预映射,pre-image),通过散列算法,变换成固定长度的输出,该输出就是散列值.这种 ...
- [py模块]random&string取随机字符串
栗子 - 取n位的随机字符串(大小写/数字) def get_random_str(len_str): import string import random letters_nums = strin ...
- 模块 - random/string/os/sys/shutil/zipfile/tarfile
random 模块 方法: >>> random.randint(1,3) #会包含 1 2 3 3 >>> random.randrange(1,3) #会包含 ...
- time/datetime/random/string/os/sys/shutil/zipfile/tarfile - 总结
time 模块: time.time() #时间戳 time.localtime() #当前时间对象元组 time.localtime(123123) #根据时间戳的时间对象 time.mktime( ...
- python加密算法及其相关模块的学习(hashlib,RSA,random,string,math)
加密算法介绍 一,HASH Hash,一般翻译做“散列”,也有直接音译为”哈希”的,就是把任意长度的输入(又叫做预映射,pre-image),通过散列算法,变换成固定长度的输出,该输出就是散列值.这种 ...
- python基础四(json\os\sys\random\string模块、文件、函数)
一.文件的修改 文件修改的两种思路: 1.把文件内容拿出来,做修改后,清空原来文件的内容,然后把修改过的文件内容重新写进去. 步骤: 1.打开文件:f=open('file','a+') #必须用a ...
- 7.27考试总结(NOIP模拟25)[random·string·queue]
死亡的尽头,没有神 T1 random 解题思路 这波是找规律完胜了.. lby dalao根据样例找出了正确的式子:\(\dfrac{n^2-1}{9}\) 然而,我这个菜鸡却推出了这样一个错误的式 ...
随机推荐
- Linux_LEMP
目录 目录 LEMP Nginx mysql PHP php-fpm Script LEMP Nginx是一个高性能的HTTP和反向代理服务器,也是一个 IMAP/POP3/SMTP 服务器,因它的稳 ...
- 前后端分离&接口API设计学习报告
接口API设计学习报告 15331023 陈康怡 什么是API? API即Application Programming Interface.API是一种通道,负责一个程序与另一个程序的沟通.而对于w ...
- JS单引号嵌套的问题,怎么改才能对呢!
JS单引号嵌套的问题,怎么改才能对呢! https://zhidao.baidu.com/question/416584343.html document.getElementById(celbid) ...
- Delphi下利用WinIo模拟鼠标键盘详解 有参考价值
https://blog.csdn.net/fgrass_163/article/details/6365296 Delphi下利用WinIo模拟鼠标键盘详解 2011年04月26日 21:03:00 ...
- Vijos lxhgww的奇思妙想--求K级祖先
给出一棵树求K级祖先.O(N*logN+Q) 更详细的讲解见:https://www.cnblogs.com/cjyyb/p/9479258.html /* 要求k级祖先,我们可以把k拆成" ...
- PHPFPM模式三种运行模式
1.static模式 static模式始终会保持一个固定数量的子进程,这个数量由pm.max_children定义. 2.dynamic模式 子进程的数量是动态变化的,启动时,会生成固定数量的子进 ...
- linux上执行mysql的脚本文件
我们测试过程中,经常需要执行升级脚本或导入生产测试数据,对于轻量的升级脚本可以直接在客户端工具中打开执行,但是对于文件内容比较大的.sql文件,比如几百M,几G的sql文件,直接拖到客户端工具打开执行 ...
- docker配置文件不生效
1.查看docker配置文件位置 systemctl status docker.service 2.修改docker配置文件 vim /lib/systemd/system/docker.servi ...
- getchar返回int类型
#include <stdio.h> /* copy input to output; 2nd version */main(){int c;c = getchar();while(c ! ...
- virtualenvwrapper安装和使用
virtualenvwrapper安装和使用步骤: 1.安装: *nix上安装的命令: pip install virtualenvwrapper windows上安装的命令: pip install ...