Python 学习笔记: A byte of python (一) 基础

小白学python,用的是python 简明教程(漠伦译),内容使用Python3,同时使用的系统是Ubuntu16.04
在这里将阅读之后的笔记记录在此 ,有误之处,欢迎大家指正

1. Python中没有单独的char数据类型
2. 格式化方法:format
ex:
print( '{0} was {1} years old when he wrote this book' .format(name, age))
print('Why is {0} playing with that python?' .format(name))

也通过另一种写法
name + 'is' + str(age) + 'years old'
但没有上例方便,可以直接改动文字而不必与变量打交道

format就是将每个参数值替换成格式所在的位置
注意 : print 总是会以一个不可见的“新一行”字符( \n)结尾,因此重复调用 print 将会在相互独立的一行中分别打印。
为防止打印过程中出现这一换行符,可以通过 end 指定其应以空白结尾:
printf ('a', end ='')
printf ('b', end ='')

详细格式说明:
# 对于浮点数 '0.333',保留小数点(.)后三位
print('{0:.3f}'.format(1.0/3))
# 使用下划线填充文本,并保持文字处于中间位置
# 使用 (^) 定义 '___hello___'字符串长度为 11
print('{0:_^11}'.format('hello'))
# 基于关键词输出 'Swaroop wrote A Byte of Python'
print('{name} wrote {book}' .format(name='Swaroop', book='A Byte of Python'))
输出
0.333
___hello___
Swaroop wrote A Byte of Python

3. 转义序列
如果在单引号中生成包含单引号(')的字符串,或者在双引号中生成包含(")的字符串,
则需要转义序列 \
ex : 'what\'s your name ?' 或者也可以 “what 's your name ?”
双引号中使用的是 \\
换行符 \n,制表符\t
在一个字符串中,一个放置在末尾的‘\’ 表示字符串将在下一行继续,但不会添加新的一行

原始字符串
指定一些未经过特殊处理的字符串,比如专义序列,则需要在字符串前增加r 或 R

4. 变量
标识符
标识符的第一个字符 必须是 字母(大小写) 或者 下划线(_),但不可是数字,剩下的可以字母、数字、下划线
标识符区分大小写,myname 与myName 不一样
补: 字符串、2things、my-name、>123 均为无效

5. 对象
Python 将程序中任何内容称为对象(Object) ,例,‘某某对象’

6.逻辑行与物理行
一行物理行对应一行逻辑行,如果需要在一行物理行指定多行逻辑行,则需要使用分号(;)表明结束
然而!,Python不建议使用分号,不应该使用(;),在一行长代码时,可以使用反斜杠( \ )将其拆分多个物理行,通常被称做 显式行链接
某些情况下,逻辑行会以括号开始(方括号或花括号,但不是结束括号),允许不使用反斜杠,这称作 隐式行链接。

7. 缩进
空白区在各行的开头称为 缩进,(使用空格或者制表符)确定逻辑行的缩进级别,而后者又可以确定语句的分组。
放置在一起的语句拥有相同的缩进,每一组这样的语句称为 块。
缩进 建议采用四个空格



  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
this is a book about python. it was written by Swaroop C H.its name is "a byte of python". Table of Contents Preface Who This Book Is For History Lesson Status of the book Official Website License Terms Using the interpreter prompt Choosing an Editor Using a Source File Output How It Works Executable Python programs Getting Help Summary 4. The Basics Literal Constants Numbers Strings Variables Identifier Naming Data Types Objects Output How It Works Logical and Physical Lines Indentation Summary 5. Operators and Expressions Introduction Operators Operator Precedence Order of Evaluation Associativity Expressions Using Expressions Summary 6. Control Flow Introduction The if statement ivUsing the if statement How It Works The while statement Using the while statement The for loop Using the for statement Using the break statement The continue statement Using the continue statement Summary 7. Functions Introduction Defining a Function Function Parameters Using Function Parameters Local Variables Using Local Variables Using the global statement Default Argument Values Using Default Argument Values Keyword Arguments Using Keyword Arguments The return statement Using the literal statement DocStrings Using DocStrings Summary 8. Modules Introduction Using the sys module Byte-compiled .pyc files The from..import statement A module's __name__ Using a module's __name__ Making your own Modules Creating your own Modules from..import The dir() function Using the dir function Summary 9. Data Structures Introduction List Quick introduction to Objects and Classes Using Lists Tuple Using Tuples Tuples and the print statement Dictionary Using Dictionaries Sequences Using Sequences References Objects and References More about Strings String Methods Summary 10. Problem Solving - Writing a Python Script The Problem The Solution First Version Second Version Third Version Fourth Version More Refinements The Software Development Process Summary 11. Object-Oriented Programming Introduction The self Classes Creating a Class object Methods Using Object Methds The __init__ method Using the __init__ method Class and Object Variables Using Class and Object Variables Inheritance Using Inheritance Summary 12. Input/Output Files Using file Pickle Pickling and Unpickling Summary 13. Exceptions Errors Try..Except Handling Exceptions Raising Exceptions How To Raise Exceptions Try..Finally Using Finally Summary 14. The Python Standard Library Introduction The sys module Command Line Arguments More sys The os module Summary 15. More Python Special Methods Single Statement Blocks List Comprehension Using List Comprehensions Receiving Tuples and Lists in Functions Lambda Forms Using Lambda Forms The exec and eval statements The assert statement The repr function Summary 16. What Next? Graphical Software Summary of GUI Tools Explore More Summary A. Free/Libré and Open Source Software (FLOSS) B. About Colophon About the Author C. Revision History Timestamp
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值