ex05.更多的变量和打印

1. 环境介绍

我使用的环境是windows/notepad++/Python 3.7.2

2. 代码

# coding:utf-8
my_name = 'Zed A. Shaw'
my_age = 35 # not a lie
my_height = 74 # inches
my_weight = 180 # lbs
my_eyes = 'Blue'
my_teeth = 'White'
my_hair = 'Brown'

'''
python2
print"Let's talk about %s." % my_name
'''
# python3
print(f"Let's talk about {my_name}.")
print (f"He's {my_height} inches tall.") 
print (f"He's {my_weight} pounds heavy.") 
print ("Actually that's not too heavy.")
print (f"He's got {my_eyes} eyes and {my_hair} hair.")
print (f"His teeth are usually {my_teeth} deppending on the coffee.") 

# this line is tricky,try to get it exactly right

total = my_age + my_height + my_weight
print(f"if I add {my_age},{my_height},and {my_weight} I get {total}.") 

3.输出

ex05输出.png

4.附加练习

练习1:将代码中的my_去掉

# coding:utf-8
# 习题1:将my_去掉
name = 'Zed A. Shaw'
age = 35 # not a lie
height = 74 # inches
weight = 180 # lbs
eyes = 'Blue'
teeth = 'White'
hair = 'Brown'

print(f"Let's talk about {name}.")
print (f"He's {height} inches tall.") 
print (f"He's {weight} pounds heavy.") 
print ("Actually that's not too heavy.")
print (f"He's got {eyes} eyes and {hair} hair.")
print (f"His teeth are usually {teeth} deppending on the coffee.") 

# this line is tricky,try to get it exactly right

total = age + height + weight
print(f"if I add {age},{height},and {weight} I get {total}.") 

5.总结

  1. python2和python3中不同的地方展现出来了,敲完代码后我感觉Python3的逻辑相较于Python2要更好一些。不知道是否是我个人的感受。更加合理了。
  2. 变量嵌入字符串中使用中括号{}括起来
  3. Python3中格式化字符串需要用f进行表示
  4. 格式化字符串的意义就是将变量放到文字中
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • 字符集和编码简介 在编程中常常可以见到各种字符集和编码,包括ASCII,MBCS,Unicode等字符集。确切的说...
    兰山小亭阅读 9,094评论 0 13
  • 3-1 Python中数据类型 计算机顾名思义就是可以做数学计算的机器,因此,计算机程序理所当然地可以处理各种数值...
    六寸光阴丶阅读 498评论 0 1
  • 本篇文章全部copy自廖雪峰老师的Python入门教程,仅供学习用教程链接:Python入门 数据类型 整数 py...
    WangGavin阅读 436评论 1 3
  • 第5章 引用类型(返回首页) 本章内容 使用对象 创建并操作数组 理解基本的JavaScript类型 使用基本类型...
    大学一百阅读 3,689评论 0 4
  • 1/75 1认识Python语言 2/75 序言 培训最终的目标是什么? 衡量一个合格的软件工程师的标准是什么? ...
    清清子衿木子水心阅读 4,324评论 0 1

友情链接更多精彩内容