python3笔记 3 理解代码及基本类型

更多内容欢迎戳进:我的个人博客

3-1 什么是代码 什么是写代码

代码是现实世界事物在计算机世界中的映射

写代码是将现实世界中的事物用计算机语言来描述

3-2 数字:整形与浮点型

整数:int
浮点数:float
在Python中没有单双精度之分,Python中的float就是其他语言中的double
在Python中没有short,long之分,只用int

利用 type()查看其类型:
>>> type(1)
<class 'int'>
>>> type(-1)
<class 'int'>
>>> type(1.1)
<class 'float'>
>>> type(1.111111111111111111)
<class 'float'>
>>> type(1 + 0.1)
<class 'float'>
>>> type(1 + 1)
<class 'int'>
>>> type(1 + 1.0)
<class 'float'>    //2.0属于float型
>>> type(1 * 1)
<class 'int'>
>>> type(1 * 1.0)
<class 'float'>
>>> type(2 / 2)
<class 'float'>    //牢记:两个int相除为float
>>> type(2 // 2)
<class 'int'>    //双斜杠相除得int

3-3 10、2、8、16进制

>>> 1 // 2
0    //双斜杠理解成整除,只保留整数部分

进制基本概念,不做赘述

3-4 各进制的表示与转换

进制的表示:
>>> 0b10
2    //Python中用0b表示2进制,回车默认转换10进制
>>> 0b11
3    
>>> 0o10
8    //Python中用0o表示8进制
>>> 0x10
16    //Python中用0x表示16进制

进制的转换:
其他进制向二进制转换用 bin()
>>> bin(10)
'0b1010'
>>> bin(0o7)
'0b111'
>>> bin(0xE)
'0b1110'
其他进制向十进制转换用 int()
>>> int(0b111)
7
>>> int(0o77)
63
其他进制向十六进制转换用 hex()
>>> hex(888)
'0x378'
>>> hex(0o777)
'0x1ff'
其他进制向八进制转换用oct()
>>> oct(0b111)
'0o7'
>>> oct(0x777)
'0o3567'

3-5 数字:布尔类型与复数

bool:真/假
complex:复数

>>> type(True)
<class 'bool'>
>>> type(False)
<class 'bool'>
>>> bool(1)
True
>>> bool(2.2)
True
>>> bool(-1)
True
>>> bool(0)
False    //非0为真,0为假
>>> bool('abc')
True
>>> bool('')
False
>>> bool([1,2,3])
True
>>> bool([])
False
>>> bool({1,2})
True
>>> bool({})
False    //空值被认为是False
>>> bool(None)
False

复数表示:
>>> 36j
36j

3-6 字符串:单引号与双引号

str:字符串
单引号、双引号、三引号表示字符串
>>> 'hello'
'hello'
>>> "hello"
'hello'
>>> type('1')
<class 'str'>
>>> "let's go"
"let's go"
>>> 'let"s go'
'let"s go'    //两个单引号中间加上双引号也是没有问题的
>>> 'let\'s go'
"let's go"    //反斜杠 \ 为转义字符

3-7 多行字符串

每行最多79个字符,可用三引号换行,三个单引号与三个双引号皆可
>>> '''hello world
hello world
hello world
'''
'hello world\nhello world\nhello world\n'    // \n为回车
>>> """hello world\n hello world"""
'hello world\n hello world'

print()函数能够看到转义字符转译之后的格式:
>>> print( """hello world\nhello world""")
hello world
hello world

>>> """hello
world"""
'hello\nworld'
>>> 'hello\
world'        //单引号可以加上反斜杠换行
'helloworld'

3-8 转义字符

特殊的字符:无法“看见“的字符或与语言本身语法冲突的字符
\n 换行
\'  单引号
\t  横向制表符

\n 换行
\r  回车
不是同一个概念

>>> print('hello \\n world')
hello \n world
>>> print('c:\\north\\west')
c:\north\west

3-9 原始字符串

字符串前面加上r或者R变为原始字符串,所见即所得
>>> print(r'c:\north\west')
c:\north\west

3-10 字符串运算一

拼接:+
>>> "hello" + "world"
'helloworld'

重复:*
>>> "hello" * 3
'hellohellohello'    //只能和数字相乘

截取任意字符:[int]
>>> "hello world"[0]
'h'    //下标序号从0开始
>>> "hello world"[3]
'l'
>>> "hello world"[-3]
'r'    //正数代表获取字符序号,复数代表末尾数n次得到的字符

3-11 字符串运算二

截取一组字符:[起点:终点的 下一位]
>>> "hello world"[0:5]
'hello'
>>> "hello world"[0:-1]
'hello worl'    //步长
>>> "hello world"[2:-3]
'llo wo'

3-12 字符串运算三

输入数字大于其最大值则定义为其最大值
>>> "hello world"[6:30]
'world'

[int:]代表着截取到最后:
>>> "hello world"[6:]
'world'

[:int]代表着从开始开始截取:
>>> "hello world"[:-5]
'hello '

[负数:]代表从倒数位开始截取到最后:
>>> "hello world"[-4:]
'orld'


更多内容欢迎戳进:我的个人博客

猜你喜欢

转载自blog.csdn.net/qq_36329973/article/details/80903599