今日知识点:
- 变量的命名与定义
- 练习debug工具
- print 函数的使用
变量与输出
使用一行代码,同时输出多个变量的值且保证换行的效果——借助print()的参数:
- sep(separator):指定多个对象之间的分隔符,默认为" ";此处使用“\n”换行
- end:指定输出结尾的字符,默认为"\n";可用来实现不换行的场景
- \n 换行符
a,b,c = 1,2,3
#逐行输出-1
print(a)
print(b)
print(c)
#逐行输出-2,使用sep参数
print(a,b,c) #1 2 3
print(a,b,c,sep='\n')
格式化
多个引号——输出的姓名加引号:
- 内外引号不同,内单外双或内双外单
- 使用转义字符,在引号前加“\”
name = 'xiaoming'
city = 'Beijing'
print(f'Name: {name}, City: {city}')
#输出的字符串中带引号
print(f'Name: "{name}", City: {city}')
print(f"Name: \"{name}\", City: {city}")
#name, city分行输出,换行符
print(f"Name: \"{name}\",\nCity: {city}")
变量运算
- 格式化字符串输出,数值型指定小数位数输出。
price = 19.9
discount = 0.8
final_price = price * discount
saved_amount = price - final_price
print(f"最终价格是:{final_price:.2f}")
print(f'节省金额是:{saved_amount:.2f}')
Python基础语法入门
1417

被折叠的 条评论
为什么被折叠?



