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'
 
 
 
 
 

day3 ord,chr,random,string的更多相关文章

  1. Python ord & chr

    ord & chr ord: 返回单个 Unicode 字符编码的整数 chr: 给一个 Unicode 编码,返回一个字符 (0 ~ 1,114,111) (ASCII 与 Unicode ...

  2. 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 ...

  3. python 加密算法及其相关模块的学习(hashlib,random,string,math)

    加密算法介绍 一,HASH Hash,一般翻译做“散列”,也有直接音译为”哈希”的,就是把任意长度的输入(又叫做预映射,pre-image),通过散列算法,变换成固定长度的输出,该输出就是散列值.这种 ...

  4. [py模块]random&string取随机字符串

    栗子 - 取n位的随机字符串(大小写/数字) def get_random_str(len_str): import string import random letters_nums = strin ...

  5. 模块 - random/string/os/sys/shutil/zipfile/tarfile

    random 模块 方法: >>> random.randint(1,3) #会包含 1 2 3 3 >>> random.randrange(1,3) #会包含 ...

  6. time/datetime/random/string/os/sys/shutil/zipfile/tarfile - 总结

    time 模块: time.time() #时间戳 time.localtime() #当前时间对象元组 time.localtime(123123) #根据时间戳的时间对象 time.mktime( ...

  7. python加密算法及其相关模块的学习(hashlib,RSA,random,string,math)

    加密算法介绍 一,HASH Hash,一般翻译做“散列”,也有直接音译为”哈希”的,就是把任意长度的输入(又叫做预映射,pre-image),通过散列算法,变换成固定长度的输出,该输出就是散列值.这种 ...

  8. python基础四(json\os\sys\random\string模块、文件、函数)

    一.文件的修改 文件修改的两种思路: 1.把文件内容拿出来,做修改后,清空原来文件的内容,然后把修改过的文件内容重新写进去. 步骤: 1.打开文件:f=open('file','a+')  #必须用a ...

  9. 7.27考试总结(NOIP模拟25)[random·string·queue]

    死亡的尽头,没有神 T1 random 解题思路 这波是找规律完胜了.. lby dalao根据样例找出了正确的式子:\(\dfrac{n^2-1}{9}\) 然而,我这个菜鸡却推出了这样一个错误的式 ...

随机推荐

  1. 阶段3 1.Mybatis_07.Mybatis的连接池及事务_2 连接池介绍

  2. tuple用法

    1 tuple中的元素可以直接赋给相同个数的变量 tup1 = ('asfa',234) p, q = tup1 print(p) print(q) # asfa # 参考:https://www.r ...

  3. EncodeError: 'latin-1' codec can't encode characters in position 69-70: ordinal not in range(256)

    UnicodeEncodeError: 'latin-1' codec can't encode characters in position 69-70: ordinal not in range( ...

  4. flex 判断对象的类型

    在判断flex对象的类型之前,首先是获取对象类型,获取的方式有: mx.utils.NameUtil.getUnqualifiedClassName(object:Object):String  // ...

  5. linux 安装 sudo

    1.安装sudo# apt-get install sudo2.修改 /etc/sudoers 文件属性为可写# chmod +w /etc/sudoers3.编辑 /etc/sudoers ,添加如 ...

  6. 【ABAP系列】SAP S/4 HANA的SMARTFORMS如何切换到非word编辑器

    公众号:SAP Technical 本文作者:matinal 原文出处:http://www.cnblogs.com/SAPmatinal/ 原文链接:[ABAP系列]SAP S/4 HANA的SMA ...

  7. Linux-定时任务-打包与压缩

    figure:first-child { margin-top: -20px; } #write ol, #write ul { position: relative; } img { max-wid ...

  8. MySql-Mysql技术内幕~SQL编程学习笔记(1)

    1.MySQL的历史,一些相关概念. 2.MySQL数据类型 *通常一个页内可以存放尽可能多的行,那么数据库的性能就越好,选择一个正确的数据类型至关重要. 1>UNSIGNED类型: 将数字类型 ...

  9. adb 配置连接

    一. adb环境安装 1.1. windown 驱动安装 1. 下载驱动(ADB Kits):http://adbshell.com/downloads 2. adb 测试 <1>. 解压 ...

  10. 【SSL2325】最小转弯问题

    题面: \[\Large\text{最小转弯问题}\] \[Time~Limit:1000MS~~Memory~Limit:65536K\] Description 给出一张地图,这张地图被分为 n× ...