[学] 《Python 核心编程》学习笔记(二)

2. Python 起步

2.1 print

1 print "Hello World!"

2.2

  _:最后一个表达式的值

  >>:输出重定向

  %:打印的格式化字符串和值进行风格,% 后的值为一个 tuple 对象,用 “()” 包含的

print "%s is a number %d."  %  ("Python", 6)

import sys
print >> sys.stderr, "msg"

file = open("filename", "a")
print >> file, "File MSG."
file.close()

hello = raw_input("Hello: ")

print "Num: %d." % (int(hello))

2.3 

  # :注释符号

2.4 运算符

  +   -  *  /  //  %  **

  <  <=  >  >=  ==  !=  <>

  and  or  not

  **:乘方

2.5 变量和赋值

2.6 数字

  int  long  bool  float  complex

  decimal:

    import decimal

    decimal.Decimal("1.1")

  1.1  -> 1.1000001  (一般是这样计数的)

2.7 字符串

  支持 [],[:] 操作

2.8 列表和元组

  从 0 开始索引,支持切片操作 ':'

  list:  []  可更改值

  tuple:  ()  不可更改值

2.9 字典

  {'key' : 'value'}

2.10 代码块与缩进对齐

  

2.11 if

  

if condition:
    something

2.12 while

while condition:
    something

2.13 for

for iteration:
    something

  range()    : 范围

  enumerate()  :枚举

2.14 

  

Y = [ x**2 for x in range(8) if not x % 2]

2.15 open() file()

fobj = open("Filename", "r")
for eachline in fobj:
    print eachline,      # ','  使 print 不打印换行符
fobj.close()

2.16 异常

try:
    something
except IOError, e:
    deal except

2.17 函数

def function_name():
    something

2.18 类

class ClassName(BaseClassName):
    something

2.19 模块

import module_name

2.20 实用函数

dir([obj])
help([obj])
int()
len()
open()
range()
raw_input()
str()
type()
原文地址:https://www.cnblogs.com/YBhello/p/5544632.html