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))
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)])