Python开发基础知识总结1

温度转换小栗子

TempStr = input("请输入要转化的温度值:")

if TempStr[-1] in ["C", "c"]:
    C = (eval(TempStr[0:-1]) - 38) / 1.8
    print("转化后的温度值是{:.2f}F".format(C))
elif TempStr[-1] in ["F", "f"]:
    F = (eval(TempStr[0:-1]) * 1.8) + 32
    print("转换后的温度值是{:.2f}C".format(F))
else:
    print("输入的格式错误")

知识点

eval 函数用来将一个字符串类型的浮点数或整数进行转换
format 函数 增强了字符串格式化的功能。相对于老版的%格式方法,它有很多优点。
1.在%方法中%s只能替代字符串类型,而在format中不需要理会数据类型;
2.单个参数可以多次输出,参数顺序可以不相同;
3.填充方式十分灵活,对齐方式十分强大;
4.官方推荐用的方式,%方式将会在后面的版本被淘汰。

#数字格式化

print("float {:.2f}".format(3.1415926))
# 保留两位小数
print("float {:+.2f}".format(3.1415926))
# 带符号保留两位小数
print("float -{:.2f}".format(3.1415926))
#带符号保留两位小数
print("float {:.0f}".format(2.71828))
# 不带小数
print("{:0>2d}".format(5))
# 数字补零(填充左边,数字为0)
print("{:0<5d}".format(5))
# 数字补充 (填充右边,数字为零)
print("{:x<4d}".format(10))
# 数字补x (填充右边,数字为x)
print("{:,}".format(10000000))
# 以逗号为分隔符的数字格式
print("{:.2%}".format(0.25))
# 百分比显示
print("{:.2e}".format(10000000000000))
# 指数记法
print("{:10d}".format(13))
# 右对齐(默认宽度为10)
print("{:<10d}".format(10))
# 左对齐 默认宽度为10
print("{:^10d}".format(10))
# 居中对齐(默认宽度为10)
print("{} {{0}}".format("opcai_linux"))
# 输出大括号

数值运算操作符

菜鸟教程

天天向上的力量

# 每周上班六天休息一天
def dadayup(df):
    dayup = 1
    for i in range(365):
        if i % 7 in [0, 6]:
            dayup = dayup * (1 - 0.01)
        else:
            dayup = dayup * (1 + df)
    return dayup

dayfactory = 0.01
while dadayup(dayfactory) < 37.78:
    dayfactory += 0.001

print("工作日的努力的参数是:{:.3f}".format(dayfactory))

循环调用dadayup 函数直到它大于37.78后停止调用
return 返回当前函数的dayup值
i % 7 in [0, 6] 如果余数在0和6中,那么可以判定这天休息

字符串类型格式化

print("ABvdEfGHI".lower()) # 将字母全部转换成大写
print("ABvdEfGHI".upper()) # 将字母全部转换成小写

print("A,B,C".split(","))
print("Python Hello world".count("o"))
print("python".replace("n","n hello world"))

print("python".center(20,"$"))

print("hello python world".strip(" python "))
print("= python =".strip(" =np"))

print(",".join("123456789"))

# format 处理函数

print("{0:=^20}".format("PYTHON")) 

python字符串处理函数

print("ABvdEfGHI".lower())  # 把字符串字母大写转换成小写
print("ABvdEfGHI".upper())  # 把字符串字母小写 转换成大写
print("A,B,C".split(","))   # 通过指定分隔符对字符串进行切片,如果参数 num 有指定值,则分隔 num+1 个子字符串 str.split(str="", num=string.count(str)).  默认为所有的空字符,包括空格、换行(\n)、制表符(\t)等 分割后的字符串将以列表形式返回

print("Python Hello world".count("o")) # Python count() 方法用于统计字符串里某个字符出现的次数。可选参数为在字符串搜索的开始与结束位置[菜鸟教程](https://www.runoob.com/python/att-string-count.html)
print("python".replace("n","n hello world"))  # 把字符串中的 old(旧字符串) 替换成 new(新字符串),如果指定第三个参数max,则替换不超过 max 次。
[菜鸟教程](https://www.runoob.com/python/att-string-replace.html)

print("= python =".strip(" =np")) #
 [菜鸟教程](https://www.runoob.com/python/att-string-strip.html)

print(",".join("123456789")) # 用于将序列中的元素以指定的字符连接生成一个新的字符串 
[菜鸟教程](https://www.runoob.com/python/att-string-join.html)
print("python".center(20,"$")) # 返回一个原字符串居中,并使用空格填充至长度 width 的新字符串。默认填充字符为空格 
[菜鸟教程](https://www.runoob.com/python/att-string-center.html)

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值