Python笔记(更新中)

math函数

import math
a = math.sin(1)
print(a)

求根公式

a = 1
b = 9
c = 20
x1 = (-b + (b ** 2 - 4 * a * c) ** (1/2)) / (2 * a)
x2 = (-b - math.sqrt(b ** 2 - 4 * a * c)) / (2 * a)
print(x1)#-4.0
print(x2)#-5.0

字符串连接

callme = "Yo,what\'s up,"
print(callme + "Janne")
print(callme + "Danny")

对字符串求长度

s = "hello world"
print(len(s))

通过索引获取单个字符

print(s[0])

布尔类型

b1 = True
b2 = False

空类型

n = None

type函数

print(type(s))
print(type(b1))
print(type(n))
print(type(10))
print(type(10.5))

input函数

user_age = input("请输入您的年龄:")#默认接收到的数据是str类型
print(user_age)

强制类型转换

user_age_after = int(user_age)+100
print(user_age_after)
print("您100年之后" + str(user_age_after) + "岁" )
print("您100年之后" + str(int(user_age)+100) + "岁" )

条件判断语句

emotion = int(input("你的心情指数:"))
if emotion >= 60:
    print("嘻嘻嘻")
else:
    print("呜呜呜")

嵌套语句

if emotion <= 60:
    if emotion == 0:
        print("回家吧孩子,回家吧")
    else:
        print("那真是太糟糕了")
else:
    print("这很好了")

多个条件判断

if 0 <= emotion < 30:
    print("孩子你还好吗")
elif 30 <= emotion < 60:
    print("这很坏了")
elif 60<= emotion < 100:
    print("啦啦啦")
elif emotion >=100:
    print("好心情指数爆表!!!!!!!!")
else:
    print("输入错误!!!")

与或非

print("and-&&")
print("or-||")
print("not--")

列表

shop_list = ["商品1","商品0","商品3"]
print(shop_list)
shop_list.append("商品4")#append方法/在列表里增加
print(shop_list)
shop_list.remove("商品3")#remove方法/在列表删除
print(shop_list)
print(shop_list[0])
print(max(shop_list))
print(min(shop_list))
print(sorted(shop_list))#排序
print(len(shop_list))#元素个数

字典

contacts = {#键:值(键是不可变的,可以把元组作为键,列表不行)
    ("小明",20):"18700000000",
    ("小红",19):"15000000000"
}
contacts[("小兰",18)] = "19000000000"#增加
a = ("小明",20) in contacts   #键in字典 会返回一个布尔值
print(contacts)
del contacts[("小明",20)] #删除
print(contacts)
print(contacts[("小红",19)])
posted @   小火柴(^_^)  阅读(2)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?
点击右上角即可分享
微信分享提示