python学习第二周(二)key、创建修改txt、数字学习和math各类用法大全

这篇博客是作者自学Python的第二周心得,主要介绍了如何使用key操作,创建和修改txt文件,以及深入探讨了math模块的各种数学函数用法,包括实例,适合后续学习参考。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

自学内容整理,部分内容引用大佬整理内容,有注明来源或者引用网址。尤其是math.类使用非常全。还有使用案例,供以后二次学习。

#目前用的是3.7版本
print('---键的排序:for 循环---')
D1 = {'a',1,'c',3,'b',2}#里面所有值随机顺序
print(D1)

D = {'a':1,'c':3,'b':2}#字典
print(D)
ks = list(D.keys())
print(ks)#按字典顺序打印
# ks.sort()#整理顺序打印
# print(ks)
# for key in ks:
#     print(key,'❤',D[key])#❤也可以用各类符号代替 如=、=>等等
for key in sorted(D):
    print(key,'❤',D[key])

print('---字母大小写打印---')
for c in 'spam':
    print('首字母大写:',c.title())

name = 'hello,Python'
print('第一个字母大写:',name.title())
print('全部大写:',name.upper())
print('全部小写:',name.lower())

print('---计算一列数字的平方---')
squares = []
for x in [1,2,3,4,5]:
    squares.append(x**2)
    print(squares)

D = {'a':1,'c':3,'b':2}#字典
D['e'] = 99
print(D)
value = D.get('x',5)
print(value)

value = D['x'] if 'x' in D else 0 #没加入数值 'x',直接打印为0
print(value)

D['x'] = 98#加入数值 'x',98,打印出数值98
value = D['x'] if 'x' in D else 0
print(value)

print('---元组学习,特性:不可变性---')
T = (1,2,3,4)
print(len(T))#打印元组数值长度
T1=T + (5,6)
print(T1)
print(T[0])#打印第一位数,数字[]<=长度-1
print(T[3])#打印第一位数,数字[]<=长度-1
# print(T[4])#打印第一位数,数字[]<=长度-1,会提示出错。

print('---创建文件,写入修改的内容等---')
file = open('data.txt','w')
file.write('hello!\n')
file.write('world!\n')
file.close()
print('---打开指定文件,读取文件的内容---')
file = open('data.txt')
text = file.read()
print(text)
print(text.split())

print('---并集交集去重等---')
X = set('spam')
Y = {'h','a','m'}
print(X,Y)
print(X&Y)#交集
print(X|Y)#并集
print(X-Y)#不同,去重 去掉Y里面与X重复的内容
print((X|Y)-(X&Y))#去重合并

print('---创建文件,写入修改定义内容---')
p = (X|Y)-(X&Y)
file = open('data.txt','w')
file.write(str(p))
file.close()
print('---打开指定文件,读取文件的内容---')
file = open('data.txt')
text = file.read()
print(text)

print('---定义模块后续调用---')
class Worker:
    def __init__(self,name,pay):
        self.name = name
        self.pay = pay
    def lastName(self):
        return self.name.split()[-1]#取最后一个分割符内容
    def giveRaise(self,percent):
        self.pay *= (1.0 + percent)

bob = Worker('Bob Smith',50000)
sue = Worker('Sue Jones',60000)
print(bob.lastName())
print(sue.lastName())
print(sue.giveRaise(.10))
print(sue.pay)
print(bob,sue)#错误写法
# print(Worker())#不能打印函数

print('---数字学习---')
print('---1、round 浮点数x的四舍五入值---')
#注意:3.5版本及以下,如果距离两边一样远,会保留到偶数的一边。比如round(0.5)和round(-0.5)都会保留到0,而round(1.5)会保留到2。
print ("round(80.23456, 2) : ", round(80.23456, 2))
print ("round(80.23556, 2) : ", round(80.23556, 2))
print ( "round(100.000056, 5) : ", round(100.000056, 5))
print ( "round(100.0000506, 6) : ", round(100.0000506, 6))
print ("round(-100.000056, 2) : ", round(-100.000056, 2))

print('---2、注意数值变化---')
a=3
b=4
print(a+1,b+1)
print(b*3,b/2)
print(a%2,b**2)
print(2+4.0,2.0**b)
print(b/2+a)
print(b/(2.0+a))#没print结果显示为0.80000000000000004
print('a<b is',a<b,";",'a>b is',a>b)#a>b 如果成立True,否则False

print(10/4)
print(10//4)#取整数
print(10/4.0)
print(10//4.0)#取整数
print(10//-4.0)#取整数

print('---3、复数(complex)---')#研究很不够透彻:复数表示为两个浮点数(实部和虚部),并接在虚部增加了j或者J的后缀
print(1j*1J)
print(2+1j*3)
print((2+1j)*3)

print('---3、内置函数---')
import math
print(math.pi,math.e)#Π、pi代表半圆
#理解e是复利增长的极限,有1块钱,复利100%,不管分成多少次,本利和都不会超过e≈2.718
#e=1+1/1!+1/2!+.......+1/n!+R(n)
print(math.pi*2/180)
print(math.sqrt(144),math.sqrt(2))#求平方根
print(pow(2,4),2**4,abs(-42.0),max(1,2,3,4),min(1,2,3,4))#2的4次方,-42.0绝对值

"""
---以下来源于优快云大佬Calling_Wisdom整理--
math.pi--------- 圆周率 
math.ceil(x)------- 对X向上取整
math.floor(x)-------对X向下取整
math.pow(x,y)-------X的Y次方
math.sqrt(x)--------X的平方根
math.fsum(list)--------计算列表内的各元素之和

math.copysign(x,y)      返回与y同号的x值
math.fabs(x)            返回x的绝对值
math.factorial(x)       返回x的阶乘,即x!,x必须为非负整数
math.fmod(x,y)          返回x对y取模的余数(x决定余数符号),与x%y不同(y决定余数符号)

2,乘方/对数函数 
math.exp(x)             返回e**x
math.expm1(x)           返回e**x - 1
math.log(x[,base])      返回x的对数,base默认的是e
math.log1p(x)           返回x+1的对数,base是e
math.log2(x)            返回x关于2的对数
math.log10(x)           返回x关于10的对数
math.pow(x,y)           返回x**y
math.sqrt(x)            返回x的平方根

3,三角函数
math.sin(x)             返回x的正弦,x用弧度制表示
math.cos(x)             返回x的余弦
math.tan(x)             返回x的正切
math.asin(x)            返回x的反正弦,结果用弧度制表示
math.acos(x)            返回x的反余弦
math.atan(x)            返回x的反正切
math.atan2(y,x)         返回atan(y/x)
math.hypot(x,y)         返回sqrt(x*x + y*y)

4,角度,弧度转换函数
math.degrees(x)         弧度 –> 角度
math.radians(x)         角度 -> 弧度

5,双曲线函数
math.acosh(x)           返回x的反双曲余弦
math.asinh(x)           返回x的反双曲正弦
math.atanh(x)           返回x的反双曲正切
math.cosh(x)            返回x的双曲余弦
math.sinh(x)            返回x的双曲正弦
math.tanh(x)            返回x的双曲正切

6,特殊函数
math.erf(x)           # 未知
math.erfc(x)          # 未知
math.gamma(x)         # 未知
math.lgamma(x)        # 未知
"""

"""
下面还有很多案例,从https://www.cnblogs.com/xiaoyh/p/9791670.html大佬中获取。
math库常用函数及举例:
注意:使用math库前,用import导入该库
>>> import math

取大于等于x的最小的整数值,如果x是一个整数,则返回x
>>> math.ceil(4.12)
5

把y的正负号加到x前面,可以使用0
>>> math.copysign(2,-3)
-2.0

求x的余弦,x必须是弧度
>>> math.cos(math.pi/4)
0.7071067811865476

把x从弧度转换成角度
>>> math.degrees(math.pi/4)
45.0

e表示一个常量
>>> math.e
2.718281828459045

exp()返回math.e(其值为2.71828)的x次方
>>> math.exp(2)
7.38905609893065

expm1()返回math.e的x(其值为2.71828)次方的值减1
>>> math.expm1(2)
6.38905609893065

fabs()返回x的绝对值
>>> math.fabs(-0.03)
0.03

factorial()取x的阶乘的值
>>> math.factorial(3)
6

floor()取小于等于x的最大的整数值,如果x是一个整数,则返回自身
>>> math.floor(4.999)
4

fmod()得到x/y的余数,其值是一个浮点数
>>> math.fmod(20,3)
2.0

frexp()返回一个元组(m,e),其计算方式为:x分别除0.5和1,得到一个值的范围,2e的值在这个范围内,e取符合要求的最大整数值,然后x/(2e),得到m的值。如果x等于0,则m和e的值都为0,m的绝对值的范围为(0.5,1)之间,不包括0.5和1
>>> math.frexp(75)
(0.5859375, 7)

对迭代器里的每个元素进行求和操作
>>> math.fsum((1,2,3,4))
10.0

返回x和y的最大公约数
>>> math.gcd(8,6)
2

得到(x2+y2),平方的值
>>> math.hypot(3,4)
5.0

isfinite()如果x不是无穷大的数字,则返回True,否则返回False
>>> math.isfinite(0.1)
True

isinf()如果x是正无穷大或负无穷大,则返回True,否则返回False
>>> math.isinf(234)
False

isnan()如果x不是数字True,否则返回False
>>> math.isnan(23)
False

ldexp()返回x*(2**i)的值
>>> math.ldexp(5,5)
160.0

log(x,a) 如果不指定a,则默认以e为基数,a参数给定时,将 x 以a为底的对数返回。
>>> math.log(math.e)
1.0
>>> math.log(32,2)
5.0
>>>

log10()返回x的以10为底的对数
>>> math.log(10)
2.302585092994046

log2()返回x的基2对数
>>> math.log2(32)
5.0

modf()返回由x的小数部分和整数部分组成的元组
>>> math.modf(math.pi)
(0.14159265358979312, 3.0)

pi:数字常量,圆周率
>>> print(math.pi)
3.141592653589793

pow()返回x的y次方,即x**y
>>> math.pow(3,4)
81.0

radians()把角度x转换成弧度
>>> math.radians(45)
0.7853981633974483

sin()求x(x为弧度)的正弦值
>>> math.sin(math.pi/4)
0.7071067811865476

sqrt()求x的平方根
>>> math.sqrt(100)
10.0

tan()返回x(x为弧度)的正切值
>>> math.tan(math.pi/4)
0.9999999999999999

trunc()返回x的整数部分
>>> math.trunc(6.789)
6
"""



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

老来学python

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值