<jupyter_start><jupyter_text>数学函数、字符串和对象 本章介绍Python函数来执行常见的数学运算- 函数是完成一个特殊任务的一组语句，可以理解为一个函数相当于一个小功能，但是在开发中，需要注意一个函数的长度最好不要超过一屏- Python中的内置函数是不需要Import导入的<jupyter_code>abs(.10)
max(1,2,3,4,5)
max('abcde')
pow(2,2)    #幂运算
pow(2,2,2)    #幂运算完后取模
num1,num2,num3 = eval(input('输入三个数：'))
choose_method = input('请选择所用的方法如max,min,pow：')
if choose_method == 'max':
    shu1 = max(num1,num2,num3)
    print(shu1)
elif choose_method == 'min':
    shu2 = min(num1,num2,num3)
    print(shu2)
elif choose_method == 'pow':
    shu3 = pow(num1,2) + pow(num2,2) + pow(num3,2)
    print(shu3)<jupyter_output>输入三个数：1,2,3
请选择所用的方法如max,min,pow：max
3<jupyter_text>尝试练习Python内置函数  Python中的math模块提供了许多数学函数<jupyter_code>import math
math.fabs(-1)
math.ceil(2.3)
math.floor(2.3)
import matplotlib.pyplot as plt
list_ = []
for i in range(-50,50):
    res = 1./(1.+ math.exp(-i)) 
    list_.append(res)
plt.plot(list_)
plt.show()
import matplotlib.pyplot as plt
list_ = []
for i in range(-15,15):
    res = math.sin(i)
    list_.append(res)
plt.plot(list_)
plt.show()
import matplotlib.pyplot as plt
image = plt.imread('/Users/ACER/Python/1.jpg')
plt.imshow(image)
import random
random.randint(1,10)      #这个是闭区间如[a,b]
math.sqrt(4)    #开平方<jupyter_output><empty_output><jupyter_text>两个数学常量PI和e，可以通过使用math.pi 和math.e调用<jupyter_code>math.pi
math.e<jupyter_output><empty_output><jupyter_text>EP:- 通过math库，写一个程序，使得用户输入三个顶点(x,y)返回三个角度- 注意：Python计算角度为弧度制，需要将其转换为角度<jupyter_code>x1,y1,x2,y2,x3,y3 = eval(input('>>'))
import math
a=math.sqrt((x2-x3)**2 + (y2-y3)**2)
b=math.sqrt((x3-x1)**2 + (y3-y1)**2)
c=math.sqrt((x2-x1)**2 + (y2-y1)**2)
A=math.acos((a * a - b * b - c * c) / (-2 * b * c))
B=math.acos((b * b - a * a - c * c) / (-2 * a * c))
C=math.acos((c * c - b * b - a * a) / (-2 * b * a))
print(math.degrees(A),math.degrees(B),math.degrees(C))<jupyter_output>>>0,0,1,0,0,1
90.00000000000001 44.999999999999986 44.999999999999986<jupyter_text>字符串和字符- 在Python中，字符串必须是在单引号或者双引号内，在多段换行的字符串中可以使用“”“- 在使用”“”时，给予其变量则变为字符串，否则当多行注释使用<jupyter_code>"""你好，欢迎来到北京"""     #相当于注释
#三个单冒号可以相当于换行
a="""你好，欢迎来到北京.卡的咖啡那是        
肯定JFK是假的是假的能否快速"""
print(a)<jupyter_output>你好，欢迎来到北京.卡的咖啡那是
肯定JFK是假的是假的能否快速<jupyter_text>ASCII码与Unicode码- - -   函数ord、chr- ord 返回ASCII码值- chr 返回字符<jupyter_code>ord('A')           #显示的是十进制数Unicode
chr(97)
chr(ord('A') + 3)
email = '<EMAIL>'
for i in email:
    print(chr(ord(i)),end='')     #输出不换行<jupyter_output><EMAIL><jupyter_text>EP:- 利用ord与chr进行简单邮箱加密<jupyter_code>email = '<EMAIL>'
result = ''
for i in email:
    result = result + chr(ord(i) + 2)
print(result)
import hashlib
text_ = '你是一个好人!'
h = hashlib.md5()
h.update(text_.encode(encoding='utf-8'))
print('加密前：' + text_) 
print('加密后: '+ h.hexdigest())<jupyter_output>加密前：你是一个好人!
加密后: 20b236efb7fb8ae086720083bac26f6e<jupyter_text>转义序列 \- a = "He said,"Johon's program is easy to read"- 转掉它原来的意思- 一般情况下只有当语句与默认方法相撞的时候，就需要转义<jupyter_code>a = "He said,"Johon's program is easy to read"
a = "He said,\"Johon's program is easy to read"   #反斜杠成为转义字符<jupyter_output><empty_output><jupyter_text>高级print- 参数 end： 以什么方式结束打印- 默认换行打印<jupyter_code>print('a','b',sep='')     #消除输出字符之间的空格<jupyter_output>ab<jupyter_text>函数str- 将类型强制转换成字符串类型- 其他一些以后会学到(list,set,tuple...)<jupyter_code>str(True)     #布尔值转换成字符串<jupyter_output><empty_output><jupyter_text>字符串连接操作- 直接使用 “+” - join() 函数<jupyter_code>'a' + 'b'
%time ' '.join(['a','b'])<jupyter_output>Wall time: 0 ns<jupyter_text>EP：- 将 “Welcome” “to” "Python" 拼接- 将int型 100 与 “joker is a bad man” 拼接- 从控制台读取字符串> 输入一个名字返回夸奖此人<jupyter_code>' '.join(['Welcome','to','Python'])    #通过空格进行拼接
' '.join([str(100),'joker is a bad man'])
name = input('>>')
print(' '.join([name,'是个好孩子']))<jupyter_output>>>小明
小明 是个好孩子<jupyter_text>实例研究：最小数量硬币- 开发一个程序，让用户输入总金额，这是一个用美元和美分表示的浮点值，返回一个由美元、两角五分的硬币、一角的硬币、五分硬币、以及美分个数 - Python弱项，对于浮点型的处理并不是很好，但是处理数据的时候使用的是Numpy类型  id与type- id 查看内存地址，在判断语句中将会使用- type 查看元素类型<jupyter_code>type('a')
type(1)
type(True)
a = 256                               #-5到256的ID是相同的
b = 256
print(id(a),id(b))
a = 257
b = 257
print(id(a),id(b))<jupyter_output>1931779916848 1931779915984<jupyter_text>其他格式化语句见书  Homework- 1<jupyter_code>r = eval(input('Enter the length from the center to a vertex:'))
import math
s = 2 * r * (math.sin(math.pi / 5))
Area = 5 * s * s / (4 * (math.tan(math.pi / 5)))
print('The area of the pentagon is ' + str(Area))<jupyter_output>Enter the length from the center to a vertex:5.5
The area of the pentagon is 71.923649044821<jupyter_text>- 2<jupyter_code>x1,y1 = eval(input('Enter point 1 (latitude and longitude) in degrees :'))
x2,y2 = eval(input('Enter point 2 (latitude and longitude) in degrees :'))
import math
radius = 6371.01
a = math.sin(math.radians(x1))
b = math.sin(math.radians(x2))
c = math.cos(math.radians(x1))
d = math.cos(math.radians(x2))
e = math.cos(math.radians(y1-y2))
distance_ = radius * (math.acos((a * b) + (c * d * e)))
print('The distance between the tow points is ' + str(distance_) + ' km')<jupyter_output>Enter point 1 (latitude and longitude) in degrees :39.55,-116.25
Enter point 2 (latitude and longitude) in degrees :41.5,87.37
The distance between the tow points is 10691.79183231593 km<jupyter_text>- 3<jupyter_code>s = eval(input('Enter the side:'))
import math
Area = (5 * s**2) / (4 * (math.tan(math.pi / 5)))
print('The area off the polygon is ' + str(Area))<jupyter_output>Enter the side:5.5
The area off the polygon is 52.044441367816255<jupyter_text>- 4<jupyter_code>n = eval(input('Enter the number of sides:'))
s = eval(input('Enter the side:'))
import math
Area = (n * s**2) / (4 * (math.tan(math.pi / n)))
print('The area off the polygon is ' + str(Area))<jupyter_output>Enter the number of sides:5
Enter the side:6.5
The area off the polygon is 72.69017017488386<jupyter_text>- 5<jupyter_code>num = eval(input('Enter an ASCII code:'))
print('The character is ' + chr(num))<jupyter_output>Enter an ASCII code:69
The character is E<jupyter_text>- 6<jupyter_code>name_ = input('Enter employee is name :')
time_ = eval(input('Enter number of hours worked in a week :'))
pay_rate_ = eval(input('Enter hourly pay rate :'))
federal_tax_ = eval(input('Enter federal tax withholding rate :'))
state_tax_ = eval(input('Enter state tax withholding rate :'))
zong_pay_ = pay_rate_ * time_
Fed_withholding_ = zong_pay_ * 0.2
State_withholding_ = zong_pay_ * 0.09
Total_deduction_ = Fed_withholding_ + State_withholding_
Net_pay_ = zong_pay_ - Total_deduction_
print('Employee Name : ' + name_)
print('Hours Worked : ' + str(time_))
print('Pay Rate : ' + chr(ord('$')) + str(pay_rate_))
print('zong Pay : ' + chr(ord('$')) + str(zong_pay_))
print('Deductions : ')
print('   Federal Withholding(20.0%):' + chr(ord('$')) + str(Fed_withholding_))
print('   State Withholding(9.0%):' + chr(ord('$')) + str(State_withholding_))
print('   Total Deduction:' + chr(ord('$')) + str(Total_deduction_))
print('Net Pay:' + chr(ord('$')) + str(Net_pay_))<jupyter_output>Enter employee is name :Smith
Enter number of hours worked in a week :10
Enter hourly pay rate :9.75
Enter federal tax withholding rate :0.20
Enter state tax withholding rate :0.09
Employee Name : Smith
Hours Worked : 10
Pay Rate : $9.75
zong Pay : $97.5
Deductions : 
   Federal Withholding(20.0%):$19.5
   State Withholding(9.0%):$8.775
   Total Deduction:$28.275
Net Pay:$69.225<jupyter_text>- 7 - 8 进阶：> 加密一串文本，并将解密后的文件写入本地保存<jupyter_code>import hashlib
text_ = '你是一个好人!'
h = hashlib.md5()
h.update(text_.encode(encoding='utf-8'))
print('加密前：' + text_) 
print('加密后: '+ h.hexdigest())<jupyter_output>加密前：你是一个好人!
加密后: 20b236efb7fb8ae086720083bac26f6e
{'fraction_non_alphanumeric': 0.16788031245862572, 'fraction_numerical': 0.05481265722229578, 'mean_word_length': 5.637961335676626, 'pattern_counts': {'":': 0, '<': 68, '<?xml version=': 0, '>': 78, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 3, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '20', 'dir': 'jupyter-structured-clean-dedup', 'id': '374073', 'license': 'Apache-2.0', 'n_tokens_mistral': 3909, 'n_tokens_neox': 3820, 'n_words': 621, 'path': '9.11.ipynb', 'repo_name': 'WangxiaoqiWWW/Python', 'size': '7571'}