三、python的基本类型
一、number
整数 int
浮点数 float
1、type()查看类型
>>> type(1)
<class 'int'>
>>> type(1.1)
<class 'float'>
2、运算
+ 加
* 乘
/ 除,自动转float
// 整除,只保留整数部分
>>> 1+2
3
>>> 2*5
10
>>> 5/2
2.5
>>> 5//2
2
二、10、2、8、16进制
十进制:满十进一
二进制:满二进一
八进制
十六进制
三、各进制的表示与转换
1、表示
二进制:0b
八进制:0o
十六进制:0X
>>> 0b11
3
>>> 0o11
9
>>> 0x1F
31
2、转换
1)转十进制——回车/int()
2)转二进制——bin()
3)转八进制——oct()
3)转十六进制——hex()
>>> 0x1F
31
>>> bin(10)
'0b1010'
>>> int(0b1010)
10
>>> hex(888)
'0x378'
>>> oct(0b111)
'0o7'
三、布尔类型与复数
bool布尔类型:真、假
complex:复数
1、bool类型
>>> True
True
>>> False
False
注意:首字母必须大写
2、bool是number的一种
>>> int(True)
1
>>> int(False)
0
3、规律
None、0、‘’、[]、{}都会被认为是False
非零和非空都会被认为是True
>>> bool(1)
True
>>> bool(2.2)
True
>>> bool(0b111)
True
>>> bool(0b0)
False
>>> bool(0)
False
>>> bool('')
False
>>> bool([])
False
>>> bool({})
False
>>> bool(set())
False
六、单引号、双引号和三引号
1、表示str
单引号等价于双引号
>>> 'hello world'
'hello world'
>>> "hello world"
'hello world'
字符串中间的引号如何表示?
1)成对出现,表示字符串是,引号须要成对出现
内单外双:
内双外单:
或\转义:
>>> "let's go"
"let's go"
>>> 'let"s go'
'let"s go'
>>> 'les\'s go'
"les's go"
2、多行字符串
python建议每行79个字符,那么如何表示多行字符串?
1)三引号表示多行字符串
>>> '''hello world
... hello world
... '''
'hello world\nhello world\n'
>>> """hello world
... hello world
... """
'hello world\nhello world\n'
\n表示换行,那么如果输入\n,是否能表示换行动作?
>>> '''hello\n world\n'''
'hello\n world\n'
直接输入\n不能表示换行,需要使用print()函数
>>> print('''hello\nworld\n''')
hello
world
2)单、双引号表示换行
>>> 'hello\
... world'
'helloworld'
>>> "hello\
... world"
'helloworld'
四、转义字符
特殊的字符,需要使用转义字符
1、无法“看见”的字符,如回车
2、与语言本身语法有冲突的字符,如单引号、双引号
\n——换行
\'——单引号
\t——横向制表符
\r——回车
>>> print('world\rhello')
hello
>>> print('world\thello')
world hello
>>> print('world\nhello')
world
hello
五、原始字符
print("hello \n world")要求\n也被输出显示
>>> print("hello \\n world")
hello \n world
上例中将\ 当作2)类特殊字符,在其前面加上\即可实现需求
实际应用场景,打印某个文件路径
方法1)使用\
方法2)使用r或R,表示不是一个普通字符串,而是一个原始字符串
原始字符串:所见即所得,\不会被当做转义字符
>>> print('c\northwind\northwest')
c
orthwind
orthwest
>>> print('c\\northwind\\northwest')
c\northwind\northwest
>>> print(r'c\northwind\northwest')
c\northwind\northwest
>>> print(R'c\northwind\northwest')
c\northwind\northwest
六、字符串的运算
1、拼接+
2、获取某个字符[n]
3、切片
>>> 'helle'+' '+'world'
'helle world'
>>> 'hello '*3
'hello hello hello '
>>> 'hello world'[4]
'o'
>>> 'hello world'[0:5]
'hello'
>>> 'hello world'[0:-1]
'hello worl'
>>> 'hello world'[0:-3]
'hello wo'
[:n]表示从头开始数n个
[-n:0]表示从末尾开始倒数n个
>>> 'hello world'[-3:]
'rld'
>>> 'hello world'[:3]
'hel'
三、python的基本类型的更多相关文章
- Python学习笔记整理(三)Python中的动态类型简介
Python中只有一个赋值模型 一.缺少类型声明语句的情况 在Python中,类型是在运行过程中自动决定的,而不是通过代码声明.这意味着没有必要事声明变量.只要记住,这个概念实质上对变量,对象和它们之 ...
- Python学习笔记(三)Python基本数字类型及其简单操作(1)
一.数字类型 表示数字或数值的数据类型称为数字类型,Python语言提供3种数字类型:整数.浮点数和复数,分别对应数学中的整数.实数和复数,下面就一起来了解一下他们吧! 1.整数类型 整数类型与数学中 ...
- python基础学习(三)变量和类型
变量的作用:变量就是用来存储数据的. 变量的定义 在python中,变量在使用之前需要进行赋值,变量只有赋值后才能使用,如果变量没有赋值就使用会出现什么情况呢?如下图,使用之前变量未定义,会报错,如下 ...
- python none,null,,,,,类型
内建类型None表示一个空对象,没有方法和属性. None是一个特殊的常量. None和False不同. None不是0. None不是空字符串. None和任何其他的数据类型比较永远返回False. ...
- 别再说Python没有枚举类型了,好好看看
枚举类型可以看作是一种标签或是一系列常量的集合,通常用于表示某些特定的有限集合,例如星期.月份.状态等. Python 的原生类型(Built-in types)里并没有专门的枚举类型,但是我们可以通 ...
- 三. Python基础(3)--语法
三. Python基础(3)--语法 1. 字符串格式化的知识补充 tpl = "我是%s,年龄%d,学习进度100%" %('Arroz',18) print(tpl) # 会提 ...
- python的变量类型(Day6)
Python的变量类型 变量可以指定不同的数据类型,这些变量可以存储整数,小数或字符. 变量赋值 Python 中的变量赋值不需要类型声明 等号(=)用来给变量赋值,等号左边为变量值,等号右边是存储在 ...
- python基础--数值类型和序列类型
Python中数值类型:int(整数),float(浮点数),True/False(布尔值,首字母必须大写) int:1 #任意整数 float:2.3 #小数 python赋值: a = ...
- python的可变类型和不可变类型
Python有六种数据类型:数字类型.字符串类型.列表类型.元组类型.字典类型和集合类型 其中不可变类型包括三种:数字类型.字符串类型和元组类型 剩余三种为可变类型:列表类型.字典类型和集合类型 可变 ...
- Python入门篇-类型注解
Python入门篇-类型注解 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.函数定义的弊端 1>.动态语言很灵活,但是这种特性也是弊端 Python是动态语言,变量随时可 ...
随机推荐
- rails debug
=debug @thesis config下配置 东西需要重启之后才管用
- 使用log4j将不同级别的日志信息输出到不同的文件中
使用log4j.xml xml格式的配置文件可以使用filter. 例如想只把log4j的debug信息输出到debug.log.error信息输出到error.log,info信息输出到info.l ...
- (5)表单Action后台验证
/day31/src/cn/itcast/web/struts2/user/UserAction.java package cn.itcast.web.struts2.user; import com ...
- tkinter之button
Button按钮,直接上代码: from tkinter import * def gs(): global read s=Label(read,text='昨夜西风凋敝树,堵上高楼,望尽天涯路!', ...
- osx快捷键表示图
- JSP 使用<%@include%>报Duplicate local variable path 错误 解决方法
错误提示:Multiple annotations found at this line:- Duplicate local variable path- Duplicate local variab ...
- AtCoder Beginner Contest 104
A - Rated for Me Time Limit: 2 sec / Memory Limit: 1024 MB Score : 100100 points Problem Statement A ...
- ACM学习历程—HDU 1272 小希的迷宫(并查集)
Description 上次Gardon的迷宫城堡小希玩了很久(见Problem B),现在她也想设计一个迷宫让Gardon来走.但是她设计迷宫的思路不一样,首先她认为所有的通道都应该是双向连通的,就 ...
- 【LeetCode】047. Permutations II
题目: Given a collection of numbers that might contain duplicates, return all possible unique permutat ...
- MySQL 5.7新特性
新增特性 Security improvements. MySQL.user表新增plugin列,且若某账户该字段值为空则账户不能使用.从低版本MySQL升级至MySQL5.7时要注意该问题,且建议D ...