Python基础-字段定义及输出

Python基础-字段定义及输出,第1张

目录

1.超市买苹果

2.个人信息

 3.买苹果增强版

4.买苹果改进版

5.格式化输出

1.超市买苹果
# 1.定义苹果的单价
price = 8.5
# 2.挑选苹果
weight =7.5
# 3.计算付款金额
money = weight * price
# 4.只要买苹果 就返回5块钱
money = money - 5
print(money)
2.个人信息
"""
姓名:小明
年龄:18岁
性别:男生
身高:1.75米
体重:75.0公斤
"""
# 在python中,定义变量时是不需要指定变量的类型的
# 在运行的时候,Python解释器,会根据赋值语句等号右侧的数据,自动推导出变量中保存数据的准备类型
# str表示是一个字符串类型
name = "小明"
age = 18    # int 表示一个整数类型
gender = True  # bool是一个布尔类型,真True为1或假False为0
height = 1.75  # float 表示是一个小数类型,浮点数
weight = 75.0
print(name)
print(2 ** 43)
print(type(2 ** 43))  # type(X) 查看x的变量类型
print(2**64)
print(type(2**64))  # python3.0+中,将int和long型统一为int型
first_name ="张"
last_name="三"
print(first_name+last_name)
print(first_name * 10)
print(last_name * 10)
print("-" * 50)
print(first_name + "10")  # 报错 str int 不能相加
input("请输入银行密码:")  # 控制台输入函数
password = input("请输入账号密码:")  # 字符串变量 = input("提示信息:")
print(password)
# 类型转换
int("123")  # 将x转换为一个整数
print(int("123"))
print(int(123234.5))
type(int("123"))
print(type(int("123")))
float("12.3")  # 将x 转换到一个浮点数
print(float("12.3"))
type(float("12.3"))
print(type(float("12.3")))
 3.买苹果增强版
# 输入苹果的单价
price_str = input("苹果的单价:")
# 输入苹果的重量
weight_str = input("苹果的重量:")
# 输入支付的总金额
# money = price_str * weight_str;  # 注意:字符串 *** 作,要么两个字符串相加  要么一个字符串*整数,不能两个字符串相乘
# 输入苹果的单价
# 1>将价格转换成小数
price = float(price_str)
# 2>将重量转换成小数
weight = float(weight_str)
# 3>用两个小数来计算最终的金额
money = price * weight
print(money)
4.买苹果改进版
# 输入苹果的单价
price = float(input("苹果的单价:"))
# 输入苹果的重量
weight = float(input("苹果的重量:"))
# 计算总金额
money = price * weight
print(money)
5.格式化输出
#  定义字符串变量 name,输出 我的名字叫 小明,请多多关照!
name = "小明"
print("我的名字叫%s,请多多关照!" % name)
student_no = 2021210001
print("我的学号是 %06d" % student_no)
price = 10
print("苹果单价 %d" % price)

# 定义小数price weight money
# 输出 苹果单价 9.00元/斤,购买了5.00斤,需要支付45.00元
price = 8.5
weight = 7.5
money = price * weight
print("苹果单价 %.2f 元/斤,购买了 %.4f 斤,需要支付 %.3f 元" % (price, weight, money))

# 定义一个小数 scale,输出 数据比例是 10.00%
scale = 0.8
print("数据比例是 %.2f%%" % (scale * 100))
print("数据比例是 {}%".format(scale*100))
import keyword

print(keyword.kwlist)

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/langs/905396.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-05-15
下一篇2022-05-15

发表评论

登录后才能评论

评论列表(0条)

    保存