《Python编程:从入门到实践》笔记(一)

《Python编程:从入门到实践》笔记(一),第1张

第二章 变量和简单的数据类型 2.2 变量 变量的命名规则
  • 变量名只能包含字母、数字和下划线。
  • 变量名可以字母或下划线打头,但不能以数字打头,例如,可将变量命名为 message_1,但不能将其命名为1_message 。
  • 变量名不能包含空格,但可使用下划线来分隔其中的单词。例如,变量名 greeting_message 可行,但变量名greeting message 会引发错误。
  • 不要将Python关键字和函数名用作变量名,即不要使用Python保留用于特殊用途的单词,如print (请参见附录A.4)。
  • 变量名应既简短又具有描述性。例如,name比n好,student_name 比s_n好,name_length 比length_of_persons_name 好。
  • 慎用小写字母l和大写字母O,因为它们可能被人错看成数字1和0。
2.3 字符串

title() 以首字母大写的方式显示每个单词,即将每个单词的首字母都改为大写
upper() 将字符串改为全部大写
lower() 将字符串改为全部小写
使用 + 加号可以合并(拼接)字符串
使用制表符 \t 或换行符 \n 来添加空白,可以相互组合
删除字符串中的空白:删除末尾空白 retrip()、删除开头空白 lstrip()、删除两端空白 strip()、但是删除是暂时性的,要永久删除的话,需将删除后的结果存回到变量中。

2.4 数字

整数、浮点数
str() 函数可以将数字转化为字符串形式

2.5 注释
import this 编写注释的指导原则

    The Zen of Python, by Tim Peters
    
    Beautiful is better than ugly.
    Explicit is better than implicit.
    Simple is better than complex.
    Complex is better than complicated.
    Flat is better than nested.
    Sparse is better than dense.
    Readability counts.
    Special cases aren't special enough to break the rules.
    Although practicality beats purity.
    Errors should never pass silently.
    Unless explicitly silenced.
    In the face of ambiguity, refuse the temptation to guess.
    There should be one-- and preferably only one --obvious way to do it.
    Although that way may not be obvious at first unless you're Dutch.
    Now is better than never.
    Although never is often better than *right* now.
    If the implementation is hard to explain, it's a bad idea.
    If the implementation is easy to explain, it may be a good idea.
    Namespaces are one honking great idea -- let's do more of those!

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

原文地址: https://outofmemory.cn/langs/739589.html

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

发表评论

登录后才能评论

评论列表(0条)

保存