2021-06-07

#2-1
1 数值和字符的相互转化

>>> chr(ord('A')+32)
'a'

2 不同进制数之间的相互转化

>>> int('0x22b',16)
555
>>> int('0x22b')
Traceback (most recent call last):
  File "<pyshell#5>", line 1, in <module>
    int('0x22b')
ValueError: invalid literal for int() with base 10: '0x22b'

3 不同类型数值之间的相互转化

>>> float("3")
3.0
>>> float('3')
3.0

4 数值和字符串的相互转化

>>> str(1)
'1'
>>> eval('123a6')
Traceback (most recent call last):
  File "<pyshell#10>", line 1, in <module>
    eval('123a6')
  File "<string>", line 1
    123a6
        ^
SyntaxError: unexpected EOF while parsing
>>> eval('12.23.45')
Traceback (most recent call last):
  File "<pyshell#11>", line 1, in <module>
    eval('12.23.45')
  File "<string>", line 1
    12.23.45
           ^
SyntaxError: unexpected EOF while parsing
>>> eval('12.34')
12.34

5 判断数据类型

>>> type([3])
<class 'list'>

6 input函数的使用方法
x=input(“请输入一个整数:”)
请输入一个整数:3

>>> x=eval(input("请输入一个整数:"))
请输入一个整数:66.66
>>> 
>>> x=eval(input("请输入一个整数:"))
请输入一个整数:55.55
>>> x=int(input("请输入一个整数:"))
请输入一个整数:55.66
Traceback (most recent call last):
  File "<pyshell#53>", line 1, in <module>
    x=int(input("请输入一个整数:"))
ValueError: invalid literal for int() with base 10: '55.66'
>>> x=int(input("请输入一个整数:"))
请输入一个整数:55

7 print语句

```python
>>> for i in range(10):
	print(i,end=",")
0,1,2,3,4,5,6,7,8,9,



print("",panduan(x))
 print("{}".format(x))#print语句{}和format搭配,
	


 8 引用其它库中的函数


```python
from math import sqrt
>>> sqrt(25)
5.0
>>> from random import random,randint
>>> random(1,10)
Traceback (most recent call last):
  File "<pyshell#29>", line 1, in <module>
    random(1,10)
TypeError: random() takes no arguments (2 given)
>>> random(1,2)
Traceback (most recent call last):
  File "<pyshell#30>", line 1, in <module>
    random(1,2)
TypeError: random() takes no arguments (2 given)
>>> random()
0.7853859557244051
>>> randint(1,10)
3
>>> import random
>>> L=[1,2,3,4,5,6]
>>> random.shuffle(L)
>>> print(L)
[4, 5, 6, 1, 3, 2]
>>> from random import shuffle
>>> L=[1,2,3,4,5]
>>> print(L)
[1, 2, 3, 4, 5]
>>> 

9 求次方函数

>>> pow(3,2)
9
>>> pow(3,2,3)
0

10 保留小数位数函数

>>> round(3.1415926,2)
3.14

11 求绝对值或复数的模的函数

>>> abs(3+4j)
5.0
>>> abs(~5)#按位取反
6
>>> abs(-5)
5

2-1的课后习题
1 输入一个三位数的自然数,计算并输出其百位 十位和个位上的数字
2 已知三角形的两条边长及其夹角,求其第三条边的边长

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值