首页 技术 正文
技术 2022年11月21日
0 收藏 568 点赞 3,510 浏览 1465 个字

1. 数字

类型

  • int, float, bool, complex
  • type() 查看变量类型
  • isinstance(a, int) 查看变量类型

运算符

  • % 取余
  • // 返回商的整数部分
  • ** 幂
  • & 按位与
  • | 按位或
  • ^ 按位异或
  • ~ 按位非
  • and 逻辑与
  • or 逻辑或
  • not 逻辑非
  • in、not in 成员运算符
  • is、is not 判断两个对象是否引用自一个对象
  • id() 用于获取对象内存地址

2. 字符串

a = 'hello'
b = 'seniusen'
a + b # 字符串拼接 'helloseniusen'
a * 2 # 重复输出字符串 'hellohello'# 字符串格式化输出
print(repr(3).rjust(2), repr(16).rjust(3)) # 靠右对齐,ljust()、center() 靠左、居中对齐
print('12'.zfill(5)) # '000123',在数字的左边填充 0print('My name is %s, my lucky number is %d.' %('seniusen', 3))
print('My name is {}, my lucky number is {}.'.format('seniusen', 3))
# My name is seniusen, my lucky number is 3.print('站点列表 {0}, {1}, 和 {other}。'.format('Google', 'Runoob', other='Taobao'))
# 站点列表 Google, Runoob, 和 Taobao。print('常量 PI 的值近似为:%5.3f。' % 3.1415926)
print('常量 PI 的值近似为:{0:5.3f}。'.format(3.1415926))
# 在 ':' 后传入一个整数, 可以保证该域至少有这么多的宽度, .3 表示浮点数保留 3 位小数print('常量 PI 的值近似为: {!r}。'.format(3.1415926)) # 相当于 repr()
print('常量 PI 的值近似为: {!s}。'.format(3.1415926)) # 相当于 str()

3. 元组

a = () # 新建一个空元组
a = (2, ) # 新建一个只有一个元素的元组
a = (2) # 此时 a 为 int 类型
(1, 2, 3) + (4, 5, 6) # (1, 2, 3, 4, 5, 6)
(1, 2, 3) * 2 # (1, 2, 3, 1, 2, 3)

4. 列表

5. 字典

a = {} # 新建一个空字典>>> a = {'name':'seniusen', 'age':21}
>>> a.keys() # 字典的键
dict_keys(['name', 'age'])
>>> a.values() # 字典的值
dict_values(['seniusen', 21])
>>> a.items() # 字典的项
dict_items([('name', 'seniusen'), ('age', 21)])
>>> list(a.keys())
['name', 'age']

6. 集合

a = set() # 新建一个空集合>>> b = set('defgh')
>>> b
{'e', 'h', 'd', 'f', 'g'}
>>> a = set('abcde')
>>> a
{'a', 'b', 'd', 'c', 'e'}
>>> a - b # 只在 a 中不在 b 中的元素
{'a', 'b', 'c'}>>> a & b # 既在 a 中又在 b 中的元素,交集
{'e', 'd'}
>>> a | b # 在 a 和 b 中的所有的元素,并集
{'b', 'g', 'h', 'f', 'c', 'd', 'a', 'e'}
>>> a ^ b # 只在 a 中或只在 b 中的元素
{'b', 'g', 'h', 'f', 'c', 'a'}

7. 数据类型之间的转换

参考资料 菜鸟教程

获取更多精彩,请关注「seniusen」!

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,082
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,556
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,406
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,179
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,815
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,898