1.数字整形

  python3不管数字有多大都是int型,没有long类型 

  1>字符串转换为数字

s1 = ""
print(type(s1),s1)
b = int(s1)#不加base默认转换为十进制
print(type(b),b)
b += 1000

输出:

  <class 'str'> 123
  <class 'int'> 123

s1 = ""
s2 = "a"
print(type(s1),s1)
b = int(s1,base=2)#二进制
c = int(s2,base=16)#十六进制
print(type(b),b)
print(type(c),c)

输出:

  <class 'str'> 0011
  <class 'int'> 3
  <class 'int'> 10

  2>-bit_length()方法

age = 7
# 7 111
# 3 11
# 1 01
# 当前数字的二进制至少用n位表示
r = age.bit_length()
print(r)# 3

2.字符串

  str

#!/usr/bin/env python
# -*- coding:utf-8 -*- test = 'helLo'
v = test.capitalize() # 首字母大写
print(v) # Hello
v1 = test.casefold() # 所有变小写,更牛逼
print(v1) # hello
v2 = test.lower() # 所有变小写
print(v2) # hello # 设置宽度,并将内容居中
# 20 代指总长度
# * 空白未知填充,一个字符,可有可无
v3 = test.center(20,'*')
print(v3) # *******helLo******** # 去字符串中寻找子序列出现的次数
v4 = test.count('l',4)
print(v4) #
# 以什么什么结尾或开始,返回bool
v5 = test.endswith('lo')
v6 = test.startswith('h')
print(v5)
print(v6) # 从开始往后找,找到第一个之后,获取其位置
test1 = 'hellohello'
v7 = test1.find('oh', 4, 6) # 大于等于4,小于6
print(v7) # # 格式化,将字符串中的占位符替换为指定的值
test2 = 'i am {name}, age {a}'
print(test2) # 'i am {name}, age {a}'
v8 = test2.format(name='Alex',a=18)
print(v8) # i am Alex, age 18 test3 = 'i am {0}, age {1}'
print(test3) # 'i am {0}, age {1}'
v9 = test3.format('Alex',18)
print(v9) # i am Alex, age 18 v10 = test2.format_map({"name":'Alex',"a":18})
print(v10) # i am Alex, age 18 v11 = test3.index("am")
print(v11) # 判断字符串是否只包含数字和字母
v12 = test2.isalnum()
print(v12) # False
s = "username\temail\tpassword\nAliex\taliex@q.com\t123456"
s1 = s.expandtabs(20);# 把字符串 string 中的 tab 符号转为空格,tab 符号默认的空格数是 8 。
print(s1)

输出:

  username            email               password
  Aliex               aliex@q.com         123456

s2 = '②'
s4 = s2.isdigit() # true
s5 = s2.isdecimal()#flase
print(s4,s5)

 isprintable

是否存在不可显示的字符,\t = 制表符

s8 = 'fjdhf\tdhfds'
s9 = s8.isprintable()
print(s9)

True

join(seq)

  以指定字符串作为分隔符,将 seq 中所有的元素(的字符串表示)合并为一个新的字符串

test = 'HELLO'
t = '_'
v = t.join(test)
print(v)

输出:H_E_L_L_O

ljust,rjust

test = 'alex'
v = test.ljust(20,'*')
print(v) # alex**************** v2 = test.rjust(20,'#')
print(v2) # ###############alex

strip()

截掉字符串的空格或指定字符(\t,\n也可以)。

test = '  alex   '
v1 = test.lstrip() # 去掉左边的
v2 = test.rstrip() # 去掉右边的
v3 = test.strip() # 去掉左右两边的
print(v1,v2,v3) # alex alex alex

maketrans()

创建字符映射的转换表,对于接受两个参数的最简单的调用方式,第一个参数是字符串,表示需要转换的字符,第二个参数也是字符串表示转换的目标。

test = 'aeiou'
test1 = ''
m = str.maketrans(test,test1)
v = 'ahuwfeofjivuoiewsjnaeiou'
new_v = v.translate(m)
print(new_v)

输出:1h5wf24fj3v5432wsjn12345

test = 'testtysusnhfhjsl'
v1 = test.partition('s')
v2 = test.rpartition('s')
print(v1,v2)

输出:

  ('te', 's', 'ttysusnhfhjsl') ('testtysusnhfhj', 's', 'l')

test = 'testtysusnhfhjsl'
v3 = test.split('s',2)
print(v3)

输出:['te', 'tty', 'usnhfhjsl']

字符串一旦创建,不可修改

一旦修改或者拼接,都会造成重新生成字符串

range

# 帮助创建连续的数字
v = range(100) # 0, 1, 2, 3...
v1 = range(0, 100) # 0, 1, 2, 3...
v2 = range(0, 100, 5) # 0, 5, 10, 15...
for item in v:
print(item)
for item in v1:
print(item)
for item in v2:
print(item)
test = input('>>>')
for item in range(len(test)):
print(item,test[item])

3.列表

  list  # 类

  li = [1, 12, 9, "age", "alex"]

  中括号括起来,“,”分割每个元素,列表中的元素可以是数字,字符串...

  集合内部可以放置任何东西

  # 索引取值

  print(li[3])

  # 切片

  print(li[1:3])

#!/usr/bin/env python
# -*- coding:utf-8 -*- li = ['hello', 'world', 'python', 'linux', ['other', 'thing'], 'haha']
# 列表可以修改,内部是以链表形式实现
li[0] = [11, 22, 33] # 修改
li[2:4] = ['PYTHON', 'LINUX'] # 一次修改多个
del li[1] # 删除
del li[3:5]
for item in li:
print(item)
print(li)

输出:

[11, 22, 33]
PYTHON
LINUX
[[11, 22, 33], 'PYTHON', 'LINUX']

#!/usr/bin/env python
# -*- coding:utf-8 -*- li = ['hello', 'world', 'python', 'linux', ['other', 'thing'], 'haha']
# 列表可以修改,内部是以链表形式实现
li[0] = ['', '', ''] # 修改
li[2:4] = ['PYTHON', 'LINUX'] # 一次修改多个
del li[1] # 删除
del li[3:5]
for item in li:
print(item)
print(li) # 操作
print(li[0][1])
# 字符串转换列表
s = 'shffhfjdfe'
l = list(s)
print(l) # 列表转字符串
# 需要自己写for循环实现:既有数字又有字符串
new_s = ''
for i in li:
new_s += str(i)
print(new_s)
# 直接使用join方法:只有字符串
lii = ['hello', 'abcd', 'hsgdh']
v = ''.join(lii)
print(v) # append 方法
# 在原来值后面追加
lii.append('hhhhh')
print(lii) # ['hello', 'abcd', 'hsgdh', 'hhhhh'] # clear 方法 清空列表
lii.clear()
print(lii) # 拷贝
liii = ['hello', 'abcd', 'hsgdh', 'hello']
v = liii.copy() # 浅拷贝
print(v)
# count 方法 计算元素出现的次数
c = liii.count('hello')
print(c)
# extend() 函数用于在列表末尾一次性追加另一个序列中的多个值(用新列表扩展原来的列表)。
# 参数是可迭代对象
liii.extend(['abcd', 'efgh']) # ['hello', 'abcd', 'hsgdh', 'hello', 'abcd', 'efgh']
liii.append(['abcd', 'efgh']) # ['hello', 'abcd', 'hsgdh', 'hello', 'abcd', 'efgh', ['abcd', 'efgh']]
liii.extend('bbb') # ['hello', 'abcd', 'hsgdh', 'hello', 'abcd', 'efgh', ['abcd', 'efgh'], 'b', 'b', 'b']
print(liii) # insert() 在指定索引位置插入元素
li2 = [11, 22, 33, 44]
li2.insert(0, 99)
print(li2) # pop() 删除某个值(可以指定索引,默认删除最后一个),并获取删除的值
v = li2.pop(1)
print(li2)
print(v) # remove() 删除列表中的指定值
li2.remove(33)
print(li2) # reverse() 将当前列表进行反转
li2.reverse()
print(li2) # sort() 排序
li2.sort() # 从小到大
li2.sort(reverse=True) # 从大到小
print(li2)

4.元祖

  tuple

  Python 的元组与列表类似,不同之处在于元组的元素不能修改。

  元组使用小括号,列表使用方括号。

  元组创建很简单,只需要在括号中添加元素,并使用逗号隔开即可。

#!/usr/bin/env python
# -*- coding:utf-8 -*- # tuple 元组,一级不可修改,增加或删除
# 一般写元组的时候,推荐在最后加入一个“,”
test = ('hello', 'world',)
print(test) # 可以被for 循环,可迭代对象
for i in test:
print(i) # 字符串,列表,元组 可以相互转换
li = list(test)
print(li) v = '_'.join(test)
print(v) # hello_world # 元组的一级元素不可修改,二级等元素可以修改
tu = (111, 'abc', [33, 44])
tu[2][1] = 99 # 二级元素是列表,可以修改
print(tu)

输出:

('hello', 'world')
hello
world
['hello', 'world']
hello_world
(111, 'abc', [33, 99])

5.字典

  dict

# 字典是另一种可变容器模型,且可存储任意类型对象。
#字典的每个键值(key=>value)对用冒号(:)分割,每个对之间用逗号(,)分割,整个字典包括在花括号({})中
info = {"k1": "v1", # 键值对
"k2": "v2"}
print(info) # 字典是无序的
# 值可以取任何数据类型,但键必须是不可变的,如字符串,数字或元组。
dict1 = {1: 'a', 2: 'b'}
print(dict1[1])
#del dict1[1]
print(dict1) for item in dict1.keys(): # 键
print(item)
for item in dict1.values(): # 值
print(item)
for item in dict1.items(): # 键值对
print(item) # 根据序列,创建字典,并指定统一的值
v = dict.fromkeys(['k1', 123, ''])
print(v) # {'999': None, 123: None, 'k1': None}
v = dict.fromkeys(['k1', 123, ''], 1200)
print(v) # {'k1': 1200, 123: 1200, '999': 1200} # get() 根据key获取值,当key不存在时,默认为None或指定
v = info.get('k1', 111)
print(v) # pop() 删除
v = info.pop('k1')
print(v) # popitem() 随机删除
info = {"k1": "v1", # 键值对
"k2": "v2"}
v = info.popitem()
print(info, v) # setdefault() 设置值,若已存在,则不设置,获取当前key对应的值
# 若不存在,设置
v = info.setdefault('k3', 'v3')
print(info, v) # {'k1': 'v1', 'k3': 'v3'} v3 # update
info.update({'k1': 'v111', 'k2': 'v222'})
info.update(k4='v4')
print(info)

输出:

{'k2': 'v2', 'k1': 'v1'}
a
{1: 'a', 2: 'b'}
1
2
a
b
(1, 'a')
(2, 'b')
{'999': None, 123: None, 'k1': None}
{'999': 1200, 123: 1200, 'k1': 1200}
v1
v1
{'k1': 'v1'} ('k2', 'v2')
{'k3': 'v3', 'k1': 'v1'} v3
{'k4': 'v4', 'k3': 'v3', 'k2': 'v222', 'k1': 'v111'}

6.布尔值

  bool

可变不可变类型:

可变:列表,字典

不可变:字符串,数字,元组

访问顺序:

1.顺序访问:字符串,列表,元组

2.映射:字典

3.直接访问:数字

存放元素个数:

容器类型:列表,元组,字典

原子:数字,字符串

7.集合

  集合(set)是一个无序的不重复元素序列。

  1.不同元素组成

  2.无序

  3.集合中元素必须是不可变类型

s = set('hello')
print(s) s = set(['ab', 'ab', 'sb'])
print(s) # add
a = {1, 2, 3}
a.add('') # {'3', 1, 2, 3}
a.add(3)
print(a) # 清空
a.clear()
print(a) # set() # pop随机删除
a = {1, 2, 3, 's'}
a.pop() # {1, 2, 's'}
print(a) #remove 可以指定删除元素,若删除元素不存在则报错
a = {1, 2, 3, 's'}
a.remove(2) # {'s', 1, 3}
#a.remove(5) error
print(a) # discard 可以指定删除元素,若删除元素不存在不报错
a = {1, 2, 3, 's'}
a.discard(2) # {1, 3, 's'}
a.discard('cd')
print(a) # 若无集合,求下面列表的交集
python_1 = ['a', 'b', 'c']
linux_1 = ['a', 'b', 'd']
python_and_linux = []
for i in python_1:
if i in linux_1:
python_and_linux.append(i)
print(python_and_linux) # ['a', 'b'] # 集合关系测试
python_2 = ['a', 'b', 'c']
linux_2 = ['a', 'b', 'd'] python_s = set(python_2)
linux_s = set(linux_2)
# 并集
python_and_linux_s = python_s | linux_s
print(python_and_linux_s) # {'d', 'c', 'a', 'b'}
print(python_s.union(linux_s)) # {'b', 'a', 'c', 'd'}
# 交叉补集
python_and_linux_s = python_s ^ linux_s
print(python_and_linux_s) # {'c', 'd'}
print(python_s.symmetric_difference(linux_s)) # {'c', 'd'}
# 差集
print(python_s.difference(linux_s)) # {'c'}
print(python_s - linux_s) # {'c'} print(linux_s - python_s) # {'d'}
# 交集
python_and_linux_s = python_s & linux_s
print(python_and_linux_s) # {'a', 'b'} python_and_linux_s = python_s.intersection(linux_s)
print(python_and_linux_s) # {'a', 'b'} python_s.difference_update(linux_s)
print(python_s) # {'c'} # isdisjoint() 方法用于判断两个集合是否包含相同的元素,如果没有返回 True,否则返回 False。
x = {"apple", "banana", "cherry"}
y = {"google", "runoob", "facebook"}
z = x.isdisjoint(y)
print(z) # True #issubset() 方法用于判断集合的所有元素是否都包含在指定集合中,如果是则返回 True,否则返回 False。
x = {"a", "b", "c"}
y = {"f", "e", "d", "c", "b", "a"}
z = x.issubset(y)
print(z) # True # update() 方法用于修改当前集合,可以添加新的元素或集合到当前集合中,如果添加的元素在集合中已存在,则该元素只会出现一次,重复的会忽略
x = {"apple", "banana", "cherry"}
y = {"google", "runoob", "apple"}
x.update(y)
print(x) # {'banana', 'cherry', 'google', 'apple', 'runoob'} # 不可变集合
s = frozenset('hello')
print(s)

python(六)——基本数据类型介绍的更多相关文章

  1. python学习第六讲,python中的数据类型,列表,元祖,字典,之列表使用与介绍

    目录 python学习第六讲,python中的数据类型,列表,元祖,字典,之列表使用与介绍. 二丶列表,其它语言称为数组 1.列表的定义,以及语法 2.列表的使用,以及常用方法. 3.列表的常用操作 ...

  2. python学习第九讲,python中的数据类型,字符串的使用与介绍

    目录 python学习第九讲,python中的数据类型,字符串的使用与介绍 一丶字符串 1.字符串的定义 2.字符串的常见操作 3.字符串操作 len count index操作 4.判断空白字符,判 ...

  3. python学习第八讲,python中的数据类型,列表,元祖,字典,之字典使用与介绍

    目录 python学习第八讲,python中的数据类型,列表,元祖,字典,之字典使用与介绍.md 一丶字典 1.字典的定义 2.字典的使用. 3.字典的常用方法. python学习第八讲,python ...

  4. python学习第七讲,python中的数据类型,列表,元祖,字典,之元祖使用与介绍

    目录 python学习第七讲,python中的数据类型,列表,元祖,字典,之元祖使用与介绍 一丶元祖 1.元祖简介 2.元祖变量的定义 3.元祖变量的常用操作. 4.元祖的遍历 5.元祖的应用场景 p ...

  5. Python 3 mysql 数据类型

    Python 3 mysql 数据类型 存储引擎决定了表的类型,而表内存放的数据也要有不同的类型,每种数据类型都有自己的宽度,但宽度是可选的 详细参考: http://www.runoob.com/m ...

  6. python 基础之数据类型

    一.python中的数据类型之列表 1.列表 列表是我们最以后最常用的数据类型之一,通过列表可以对数据实现最方便的存储.修改等操作 二.列表常用操作 >切片>追加>插入>修改& ...

  7. (八)python的简单数据类型和变量

    什么是数据类型? 程序的本质就是驱使计算机去处理各种状态的变化,这些状态分为很多种. 例如英雄联盟游戏,一个人物角色有名字,钱,等级,装备等特性,大家第一时间会想到这么表示 名字:德玛西亚------ ...

  8. Nodejs学习笔记(十六)--- Pomelo介绍&入门

    目录 前言&介绍 安装Pomelo 创建项目并启动 创建项目 项目结构说明 启动 测试连接 聊天服务器 新建gate和chat服务器 配置master.json 配置servers.json ...

  9. Python3基础教程2——Python的标准数据类型

    2018年3月12日 这次介绍一些python里面的标准数据类型 当然还是推荐一个比较系统的教程 http://www.runoob.com/python3/python3-tutorial.html ...

随机推荐

  1. nginx反向代理中proxy_set_header 运维笔记

    Nginx proxy_set_header:即允许重新定义或添加字段传递给代理服务器的请求头.该值可以包含文本.变量和它们的组合.在没有定义proxy_set_header时会继承之前定义的值.默认 ...

  2. php类之clone 克隆

    对象也能被“克隆” 在php5中,对象的传递方式默认为引用传递,如果我们想要在内存中生成两个一样的对象或者创建一个对象的副本,这时可以使用“克隆”. 通过 clone 克隆一个对象 对象的复制是通过关 ...

  3. PHP从入门到精通(一)

    (一)PHP简介和基本知识 PHP(外文名:PHP: Hypertext Preprocessor,中文名:“超文本预处理器”)是一种通用开源脚本语言.语法吸收了C语言.Java和Perl的特点,利于 ...

  4. Adobe Photoshop CC 2015使用及扩展工具

    VAdobe Photoshop CC 2015: 简称"PS",是由Adobe Systems开发和发行的图像处理软件 扩展工具: Cuuterman:切图插件: 一个一个切图, ...

  5. ros-安装

    1.安装了ubuntu for ros. 运行评论下边那条命令: 2.rtabamp 3.准备安装机器人导航仿真系统:https://blog.csdn.net/wangchao7281/articl ...

  6. Github的建立及心得体会

    第一次接触Github,这次注册最大的难处就是全英文,着实看不懂.仅凭着认识的几个常用词去了解个具体内容实在是太困难了.所以第一个体会就是要好好学英语背单词,不想看到满屏的英文就感觉头疼,烦躁.第二个 ...

  7. 使用Java+Kotlin双语言的LeetCode刷题之路(一)

    LeetCode learning records based on Java,Kotlin,Python...Github 地址 序号对应 LeetCode 中题目序号 1 两数之和 给定一个整数数 ...

  8. eclipse jee使用

    eclipse jee 安装 已经安装过elipse for Java,不知道会不会冲突? 查过,原来,你就算安装多个elipse for java都没事,更不用说jee.我选择的是eclipse-i ...

  9. PAT L2-002 链表去重

    https://pintia.cn/problem-sets/994805046380707840/problems/994805072641245184 给定一个带整数键值的链表 L,你需要把其中绝 ...

  10. Delphi处理Http请求自定义Header

    在HTTP请求中,get方法是默认的,但在URL地址长度是有限的,请求方法能传送的数据也是有限的,一般get方法传递的数据不能大于2KB,当get请求方法传递的数据长度不能满足需求时,就需要采用另一种 ...