1.格式化
format='hello, %s. Your ID is %d.'
values=('thy', 38)
print format % values
---> 'hello, thy. Your ID is 38.'
如第二句如示,要修饰多个元素,可以用Tuple,也可以用Dictionary(但不可以用List,因为List会被认为仅仅是一个单值)
2.模板(此功能不是内置的,因此要导入string库)
import string
s=string.Template('$name is an amzaing girl $$!')
s.substitute(name='Rose')
---> 'Rose is an amzaing girl $!' #注意最后一个$
但以上写法在下面例子中会出问题
s=string.Template('$name2nd is a great empire $$!')
s.substitute(name='Henry')
因此编译器无法区分name是单词的一部分。可改为
s=string.Template('${name}2nd is a great empire $$!')
s.substitute(name='Henry')
---> 'Henry2nd is a great empire $!'
还可以用Dictionary中的Value-Name对来替换。
s = Template('A $thing must never $action.')
d = {} #建一个空的Dictionary
d['thing'] = 'true man'
d['action'] = 'let his girl cry'
s.substitute(d)
---> 'A good man must never let his girl cry.'
3.精度
'%10.2f' % 2 ---> ' 2.00'
'%010.2f' % 2 ---> '0000002.00' #注意在10之前的0不可更换,因为若为其它数字,会与10成为整体;若为字母,会成为转义字符
'%10.2s' % 'thy' ---> ' th'
'%.2s' % 'thy' ---> 'th'
但是'%.*s' % 'thy'会出错,这时一定要接Tuple对象,其中Tuple的第一项用来替换精度中的*,可用于由用户定制精度。
'%.*s' % (5, 'Guidovan')
---> 'Guido'
'%*.3s' % (5, 'Guidovan')
---> ' Gui'
附上一段《Beginning Python From Novice To Professional 2nd》中十分精致的示例代码:
--->
The following is a sample run of the program:
Please enter width: 35
===================================
Item Price
———————————————————
Apples 0.40
Pears 0.50
Cantaloupes 1.92
Dried Apricots (16 oz.) 8.00
Prunes (4 lbs.) 12.00
===================================
(由于网页发布的原因,中间不对齐,事实上Item项是左对齐,Price项是右对齐)
相关推荐
Beginning Python from Novice to Professional 英文原版
虽然题目为python入门,并且全书逻辑性并不强,但是按着Hetland的篇目学习并且深究下来收获会很多的。楼主一狠心,这么好的英文原版书决定一点不要有志于学习python,加入python社区和开发行列的同志们资源费了。
《Beginning Python From Novice to Professional》学习笔记八:Abstraction
Laziness Is a Virtue --- Real programmers are lazy. Not lazy in a bad way, but in the sense that they don’t do unnecessary work. 1.创建函数 def fibs(num): result = [0, 1] for i in range(num-2)
Beginning Python: From Novice to Professional 第二版
Gain a fundamental understanding of Python’s syntax and features with the second edition of Beginning Python, an up–to–date introduction and practical reference. Covering a wide array of Python–related programming topics, including addressing language internals, database integration, network programming, and web services, you’ll be guided by sound development principles. Ten accompanying projects will ensure you can get your hands dirty in no time. Updated to reflect the latest in Python programming paradigms and several of the most crucial features found in the forthcoming Python 3.0 (otherwise known as Python 3000), advanced topics, such as extending Python and packaging/distributing Python applications, are also covered. What you’ll learn * Become a proficient Python programmer by following along with a friendly, practical guide to the language’s key features. * Write code faster by learning how to take advantage of advanced features such as magic methods, exceptions, and abstraction. * Gain insight into modern Python programming paradigms including testing, documentation, packaging, and distribution. * Learn by following along with ten interesting projects, including a P2P file–sharing application, chat client, video game, remote text editor, and more. Complete, downloadable code is provided for each project! Who is this book for? Programmers, novice and otherwise, seeking a comprehensive introduction to the Python programming language. About the Apress Beginning Series The Beginning series from Apress is the right choice to get the information you need to land that crucial entry–level job. These books will teach you a standard and important technology from the ground up because they are explicitly designed to take you from “novice to professional.” You’ll start your journey by seeing what you need to know—but without needless theory and filler. You’ll build your skill set by learning how to put together real–world projects step by step. So whether your goal is your next career challenge or a new learning opportunity, the Beginning series from Apress will take you there—it is your trusted guide through unfamiliar territory!
Beginning Python From Novice to Professional (5) - 条件与循环
条件与循环 条件运行: name = raw_input('What is your name? ') if name.endswith('Gumby'): print 'Hello, Mr.Gumby'What is your name? Gumby Hello, Mr.Gumbyname = raw_input('What is your name? ') if name.e...
Beginning Python: From Novice to Professional, 2nd Edition
《Python基础教程, 第三版》的英文电子版。本书是经典的Python入门教程,层次鲜明、结构严谨、内容翔实,特别是后面几章,作者将前面讲述的内容应用到10个引人入胜的项目中,并以模板的形式介绍了项目的开发过程,手把手教授Python编程实践,让读者从项目中领略Python的真正魅力。
第四章 自定义组件、动画
第三章 自定义组件、动画 (一)自定义View 1.1) onMeasure:对当前View的尺寸进行测量 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) widthMeasureSpec和heightMeasureSpec包含测量模式和尺寸大小 int widthMode = MeasureSpec....
Beginning Python From Novice to Professional (3) - 列表操作
列表操作 list函数: >>> list('hello') ['h', 'e', 'l', 'l', 'o']改变列表: >>> x=[1,1,1] >>> x[1]=2 >>> x [1, 2, 1]删除元素: >>> names = ['wu','li','zhao','qian'] >>> del names[1] >>> names ['wu', 'zhao', 'qian']分
PX4 磁罗盘干扰分析
无人机上的电机电流会干扰无人机上搭载的磁罗盘,PX4官方提供了一些方式,通过分析飞行日志,判断电机是否对磁罗盘有严重影响。
python3入门教程 pdf_Python基础教程第3版(Beginning Python From Novice to Professional 3th) pdf...
Magnus Lie Hetland又更新他的 "Beginning Python From Novice to Professional"了,该书为第三版英文,基于Python3.5进行讲解,紧跟技术潮流。希望对Python爱好者有所帮助。这是Python最新的介绍和实际参考。涵盖广泛的Python相关编程主题,包括语言处理、数据库集成、网络编程和Web服务,遵循良好的开发原则。十个配套项目可以...
生物信息学投稿避坑指南:如何根据影响因子和审稿速度选择最适合的期刊
本文为生物信息学研究者提供全面的期刊投稿策略,重点解析影响因子、审稿速度等关键指标,帮助选择最适合的期刊。涵盖算法开发类和应用型研究的投稿路径,分析不同出版社特点及开放获取的利弊,并提供投稿全流程优化建议,助力高效发表研究成果。
【移动APP测试】Firebase Test Lab (FTL) 实战指南:从零搭建自动化测试流水线
本文详细介绍了Firebase Test Lab (FTL) 在移动APP测试中的实战应用,从零搭建自动化测试流水线。通过真实设备、虚拟设备和iOS设备的全面覆盖,FTL帮助开发者高效进行兼容性测试、性能监控和崩溃分析,显著提升应用质量。文章还提供了设备选择策略、成本控制技巧和CI/CD集成的最佳实践,助力团队优化测试流程。
Linux | Bash Shebang 语法与结构及常见问题排查
……
Android12 车载开发TaskView实现分屏
这里只给出TaskView的代码,相关联的源码,请看源码,如果想要使用TaskView需要自己去编译frameWork的源码,打包一个aar,或者jar出来使用,否则无法直接使用。1.如果分屏的应用如启动两个应用按照权重占比,互相不叠加在一起,点触摸击事件都可响应,例如左边启动QQ了,右边启动的是微信,二者窗口都可操作。TaskView是谷歌整合安卓多任务多窗口下交互重新重构的独立出来的module,本身继承自ServiceView,TaskView的相关概念性的东西参考。//创建的taskview。
Beginning Python From Novice to Professional (1) - 数字和表达式
数字和表达式 加减乘除: >>> 2+2 4 >>> 100-50 50 >>> 3*5 15 >>> 1/2 0 >>> 1.0/2.0 0.5求余、乘方: >>> 1%2 1 >>> 10%3 1 >>> 2**3 8 >>> -3**2 -9 >>> (-3)**2 9十六进制、八进制: >>> 0xff 255 >>> 020 16变量: >>> x=3 >>> x*2 6获取输
Far planner代码系列(2)
graph_decoder系列(2) 承接上文 我们接着来看decoder_node.cpp的代码部分 void GraphDecoder::Init() { /* initialize subscriber and publisher */ //graph_sub_ 订阅节点/robot_vgraph 其类型是visibility_graph_msg/Graph的消息 graph_sub_ = nh.subscribe("/robot_vgrap...
AI问数,智慧赋能:基于大语言模型的数据播报员应用实践探索
AI问数技术正在改变企业与数据交互的方式,它以自然语言为桥梁,让数据不再是少数专家的专利,而是成为所有业务人员的决策伙伴。这不仅是技术的创新,更是管理理念的革新。在数字化转型的关键阶段,企业应积极拥抱AI问数技术,通过降低数据使用门槛、提升数据利用效率、释放数据潜在价值,实现真正的数据驱动决策,为企业的高质量发展注入源源不断的智慧动能。AI问数,正在开启一个数据普惠的新时代。
Beginning Python From Novice to Professional (6) - 函数使用
函数使用 定义函数:
【查找单入口空闲区域】-python实现-附ChatGPT解析
时间限制:1s 空间限制: 256MB 限定语言:不限题目描述:给定一个 m x n 的矩阵,由若干字符X’和 O构成,"X表示该处已被占据,O表示该处空闲,请找到最大的单入口空闲区域。空闲区域是由连通的O组成的区域,位于边界的O可以构成入口,单入口空闲区域即有且只有一个位于边界的o作为入口的由连通的O组成的区域如果两个元素在水平或垂直方向相邻,则称它们是“连通”的。第一行输入为两个数字,第一个数字为行数m,第二个数字列数n,两个数字以空格分隔,1
《Beginning Python From Novice to Professional》学习笔记六:Dictionary
1.创建(注意Dictionary是没有顺序的) phonebook = {Alice: 2341, Beth: 9102, Cecil: 3258} 冒号之前为key,之后为value,key必须具有唯一性。 items = [(name, Gumby), (age, 42)] d = dict(items) ---> {age: 42, name:
pscad IEEE标准系统搭建说明
pscad IEEE标准系统搭建说明,包含典型的IEEE 9节点、14节点、30节点、39节点和118节点系统。内含标准测试系统的原始数据和潮流结果。
大中型超市收银系统(可直接运行)
光步前台收银系统系统功能:商品销售、商品退货、挂单、收银员权限、会员销售、单品取消、整单取消、商品查询、脱机销售、脱机数据上传,基本资料下载,开钱箱、小票打印、收款结算,支持多币种付款方式(现金、储值卡、礼券、微信等),支持多种促销。运行环境:.net 4.0登陆信息用户名:1001 密码:123456内置商品信息,商品编码可输入00001000 - 00010000,测试效果相关功能键F5 结算 F9挂单 F11商品查询 F8退货 Insert单品取消 Del整单取消 F12开钱箱F2 会员 Ctrl+Z退出系统 Home 锁屏 相关源码待公开
Beginning Python From Novice to Professional (7) - 类
类 创建简单类: #!/usr/bin/env python __metaclass__ = type class Person: def setName(self,name): self.name = name def getName(self): return self.name def greet(self): print "Hello,world! I'm %s."
廖雪峰Python教程[完整版]
廖雪峰的python教程整理的,已经排好版了,而且还是完整的,适合小白
《Beginning Python From Novice to Professional》学习笔记十一:__Magic__
n大魔力1.构造函数class FooBar: def __init__(self, value=42): self.somevar = value f = FooBar(This is a constructor argument)f.somevar --->This is a constructor argument在继承机制中调用父类的构造函数
IEM模型,地表粗糙度模拟
利用微波反演土壤水分时候,利用IEM模型可以实现对随机地表的模拟
From the very beginning
这个故事绝对不是从今天开始的[笑]在上一个暑假,像所有的程序员一样的 我的冒险从#include <iostream> using namespace std; int main() { cout<<"Hello world!"; return 0; }http://paste.ubuntu.com/23827621/开始呢错过了一些机会,但从现在开始积累也并不晚呢 嗯,请多多指
数据美化 | Python词云wordcloud
什么是词云 词云是一个可以以优雅,美观的方式对“词”进行展示的一个优秀的Python第三方库。就如封面图一样,可以对你的文本数据按照词语的出现频次进行直观,优美的展示。 安装词云 pi...
打印水果价格表(python)
按照以下格式打印水果价格表: =================== Item Price —————————————————— Apples 0.4 Pears 0.5 Cantaloupes 1.92 ==================== de...
【Python】django报错SyntaxError: from __future__ imports must occur at the beginning of the file解决方法
D:\PythonWorkstation\django\django_station\queryset>python manage.py makemigrations Traceback (most recent call last): File "manage.py", line 15, in <module> 省略部门信息..... File "D:\Pyth...
851



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



