浙大版PTA python程序设计第二章题目知识点解析整理

目录

1. 数据类型和转换

1.1进制转化

1.2字符串(格式化,居中对齐)

1.3虚数类型

2. 运算和表达式

2.1取整运算

2.2四舍五入

2.3逻辑运算

2.4布尔值

2.6布尔值与比较运算

2.7布尔值与条件/循环语句 

3常见错误类型 & 结果类型

3 1进制转换错误

3.2类型错误

3.3表达式结果类型

4 math 模块

5. 字符串拼接

5.1直接拼接

5.2使用加号'+'

5.3使用join()方法 

5.4使用format()方法

5.5使用 f-string(格式化字符串字面量)

5.6 如果需要重复一个字符串,可以使用 * 运算符

6. 复数

7. 字符串结束标志

8. 运算注意事项


 

1. 数据类型和转换

1.1进制转化

a, b = input().split(',')
b = int(b)
c = int(a, b)  # 将 b 进制的 a 转换为十进制
print(c)
# 输入 45, 8 并不会输出 37,因为 'a' 是字符串,应该改成 int(a, b) 而不是 int('a', b)
bin()#注意括号内是int类型###变成二进制
oct()#变成八进制
hex()#变成16进制

hex()[2:]##加切片可以去除0b 0o 0x的前缀,其他类似

``` 例题

a,b = input().split(',')

b=int(b)

c=int('a',b)   #把b进制的a变成十进制

print(c)

输入45,8并会输出37

 False 因为#处第一个参数是绝对引用a字母(变成字符串了)改成int(a,b)即可输出37

 

1.2字符串(格式化,居中对齐)

m=int(input())
print('{:^5}'.format('*'*m))
print('{:^m}'.format('*'*m)) ###ValueError 
#输入5第二个print这里不支持{:^m}在格式化指令中直接使用变量作为字段宽度或其他限制参数的值
print('{:^{width}}'.format('*'*m, width=m))  # (正确写法)输出一个宽度为 m 的居中字符串,其中 m 为输入的值

 

print("这是数字输出格式{:5d}".format(123))  # 输出:这是数字输出格式  123(前面有两个空格,空格表示填充)


```

1.3虚数类型

print(type(1j))  # 输出 <class 'complex'>,J 或 j 是虚数单位

注意j大小写均可  通常小写
```

2. 运算和表达式


2.1取整运算

print(type(3 // 2))  # 输出 <class 'int'>,整除运算符返回整数


```

2.2四舍五入

print(round(17.0 / 3**2, 2))  # 输出 1.89,round 函数第二个参数表示保留小数点后两位


```

2.3逻辑运算

print(0 and 1 or not 2 < True)  # 输出 True,解释如下:
# 2 < True 等同于 False
# not False 等同于 True
# 0 and 1 返回 0(因为 0 为 False)
# 0 or True 返回 True

布尔运算顺序是先比较运算再逻辑运算,逻辑运算中如果没有括号,优先级默认是not>and>or
```

2.4布尔值

下列被视为False,其他所有值都被视为 True

  • 数值 0
  • 空字符串 ""
  • 空列表 []
  • 空元组 ()
  • 空字典 {}
  • 空集合 set()
  • 特殊值 None

 

bool(0)       # False
bool(1)       # True
bool("")      # False
bool("Hello") # True
bool([])      # False
bool([1, 2])  # True
print(int(True))  # 输出 1
print(bool('FALSE'))  # 输出 True,因为 'FALSE' 是非空字符串
print(bool(False))  # 输出 False
print(bool([ ]))  # 输出 False,因为 [] 是空列表
print(bool(None))  # 输出 False


```

逻辑运算符返回值

print(3 and 0 and "hello")  # 输出 0,and 运算符返回第一个 False 的值
print(3 and 0 and 5)  # 输出 0,and 运算符返回第一个 False 的值
print(((2 >= 2) or (2 < 2)) and 2)  # 输出 2
# (2 >= 2) 为 True,(2 < 2) 为 False
# True or False 为 True
# True and 2 返回 2

 

2.6布尔值与比较运算

1 == 1  # True
1 == 2  # False

1 != 1  # False
1 != 2  # True

2 > 1  # True
1 > 2  # False

1 < 2  # True
2 < 1  # False
#>=与<=类似的


2.7布尔值与条件/循环语句 

布尔值在控制程序流程的条件语句中非常重要,例如 if 语句

if True:
    print("This is true.")
else:
    print("This is false.")

 布尔值也可以控制循环语句的执行,例如 while 循环。在下面的程序中,只要条件 count < 5 为真,循环就会继续执行。

count = 0
while count < 5:
    print(count)
    count += 1

 

3常见错误类型 & 结果类型

3 1进制转换错误

try:
    print(bin(12.5))  # 会报错 TypeError,因为 bin() 只接受整数
except TypeError as e:
    print(e)

try:
    print(int("92", 8))  # 会报错 ValueError,因为 '92' 不是有效的八进制数字
except ValueError as e:
    print(e)


```

3.2类型错误

x = 'car'
y = 2
try:
    print(x + y)  # 会报错 TypeError,因为不能将字符串与整数连接
except TypeError as e:
    print(e)


```

3.3表达式结果类型

print(type(1 + 2 * 3.14 > 0))  # 输出 <class 'bool'>,表达式结果为布尔值


```

4 math 模块

import math
print(math.sqrt(4) * math.sqrt(9))  # 输出 6.0


```

5. 字符串拼接

5.1直接拼接

print("hello" 'world')  # 输出 helloworld,字符串直接拼接
print("hello" ,'world')  # 输出 hello world, print()打印函数里面的逗号,相当于一个空格

5.2使用加号'+'

str1 = "hello"
str2 = "world"
result = str1 + " " + str2
print(result)  # 输出 hello world

5.3使用join()方法 

str_list = ["hello", "world"]
result = " ".join(str_list)
print(result)  # 输出 hello world

5.4使用format()方法

str1 = "hello"
str2 = "world"
result = "{} {}".format(str1, str2)
print(result)  # 输出 hello world

5.5使用 f-string(格式化字符串字面量)

str1 = "hello"
str2 = "world"
result = f"{str1} {str2}"
print(result)  # 输出 hello world

5.6 如果需要重复一个字符串,可以使用 * 运算符

str1 = "hello"
result = str1 * 3
print(result)  # 输出 hellohellohello

 


```

 

6. 复数

z = 1 + 2j
print(z.real)  # 输出 1.0
print(z.imag)  # 输出 2.0
print(z.conjugate())  # 输出 (1-2j),返回共轭复数

实部real    虚部imag     共轭复数conjugate

7. 字符串结束标志

在 Python 中,字符串不以 \0 结束,这是在 C 或 C++ 中的做法


```

8. 运算注意事项

print(10 / 2)  # 输出 5.0,浮点运算的结果为浮点数
# 整数运算和浮点运算的结果类型取决于操作数的类型

整数运算 (+, -, *, /, %): 如果两个操作数都是整数(int),那么大多数运算(除了 /)都会返回整数;

浮点(数)运算 (+, -, *, /, %): 如果至少有一个操作数是浮点数(float),那么运算结果将是 float 类型。

未经允许 不得转载

如有错误 敬请指正

 

  • 22
    点赞
  • 20
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值