学习python Day3

解压缩

coordinates = (1, 2, 3)#()元件也可用作[]
x, y, z = coordinates

字典

customer = {
“name”: “Cauchy”,
“age”: 25,
“is_verified”: True
}
customer[“name”]#提取相应字符,也可以用get(“name”)
customer.get(“name”, “Jon is worker”)#后面则是为默认default值 输出(当前者不存在时)

需要用{}才能定义字典
customer[“name”] = “Jack”#重新定义name,同时也可以增加没有的变量名,比如birthday

输入手机号转化为英文

customer = {
“1”: “One”,
“2”: “Two”,
“3”: “Three”,
“4”: “Four”,
“5”: “Five”,
“6”: “Six”,
“7”: “Seven”,
“8”: “Eight”,
“9”: “Nine”
}
x = input("> ")#这里把输入的字符串进行拆分
y = “”
for i in x:
y += customer.get(i, “!”) + " " #字符串叠加直接用+
print(y)

迭代可以在字符串中进行

字符拆分

.split(" ")#使用空格分隔字符

函数调用

def x(first_name,last_name ):#可以包含多个参数
print(f"Hi, {first_name}{last_name}")
print(“I Love You”)
print(“I Do Love You”)

#空两行,更加规范
print(“Love”)
x(“John”, “Smith”)#如果本身需要参数,但是调用函数没有输入参数或者足够的参数例如x(“John”),会直接报错,形参和实参,同时可以写作x(last_name=“Smith”,first_name=“John”)也会出现相同结果,只要直接指向了参数,这可以增加语句的可读性,而且指向性必须放在位置参数后面,也就是x(“John”, last_name= “Smith”)
print(“Loved”)

Love
Hi, John Smith
I Love You
I Do Love You
Loved

try…except… 处理错误

try:
a = int(input("> "))
b = 1000 / a
print(b)
except ValueError:
print(“Invalid value”)
except ZeroDivisionError:
print(“a cant be 0”)

#加个空格注释

#注释

已学:
Numbers
Strings
Booleans
——
LIsts
Dictionaries

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值