Python之路【第一篇】:Python简介和入门

 

python解释器 :

Cpython 官方标准

Ipython(等于在cpython上加一套主题),交互式shell,支持自动补全,自动缩进,内置许多功能和函数

Jpython  用java重写了python语言和python解释器,Jpython有python库和所有的java类

 Ironpython  .net 版本

PYPY 用python写的解释器,速度快(预编译一些固定的代码,如库)

数据类型与变量

IndentationError:缩进错误

不可变数据类型:一旦被创建就不能更新,即右值所在的内存不能重新赋值,会重新开一片内存
变量名:字母、数字(不能在变量首位)、下划线
缩进统一

注释:

' '、 " "、''' '''(多行注释)(单引号和双引号没有区别)

编码

python开发出来的时候,Unicode编码还没有出来默认是ASCII编码,所以要手动指定

指定解释器和中文支持

要支持中文,加一行:#coding=utf-8或#_*_ coding:utf-8 _*_

常用模块:

os,sys

>>> os.system('pwd')
/tmp



 

 

 交互式和格式化

#_*_ coding:utf-8 _*_
name=raw_input('please input name:')
age=int(raw_input('age:'))
print '''
-------------------
personal info of %s"
        name:%s
        age:%d
-------------------
''' % (name,name,age)
View Code

流程控制:

if语句匹配到一条就不再往下匹配

#_*_ coding:utf-8 _*_
name = raw_input('please input name:')
age = int(raw_input('age:'))
if age >40:
  msg = 'too old'
elif age > 20:
  msg = 'nice,young man'
print '''
-------------------
personal info of %s"
        name:%s
        age:%d
        msg:%s
-------------------
''' % (name,name,age,msg)
View Code

#_*_ coding:utf-8 _*_
name = raw_input('please input name:')
age = int(raw_input('age:'))
if age >20:
  msg = 'nice,young man'
elif age > 40:
  msg = 'old boy'
print '''
-------------------
personal info of %s"
        name:%s
        age:%d
        msg:%s
-------------------
''' % (name,name,age,msg)
View Code

 

 

for等流程控制语句里的变量不是局部变量而是全局变量,函数里的变量才是局部变量

用内置函数自学:

type :查看数据类型

>>> type(1)
<type 'int'>
>>> type("python")
<type 'str'>

dir:查看模块由哪些方法

>>> dir(time)
['__doc__', '__file__', '__name__', '__package__', 'accept2dyear', 'altzone', 'asctime', 'clock', 'ctime', 'daylight', 'gmtime', 'localtime', 'mktime', 'sleep', 'strftime', 'strptime', 'struct_time', 'time', 'timezone', 'tzname', 'tzset']

help:查看模块有什么方法及其具体的用法

>>> import time

>>> help(time)

/Fun  回车,发现有如下方法

     Functions:
    
    time() -- return current time in seconds since the Epoch as a float
    clock() -- return CPU time since process start as a float
    sleep() -- delay for a number of seconds given as a float
    gmtime() -- convert seconds since Epoch to UTC tuple
    localtime() -- convert seconds since Epoch to local time tuple
    asctime() -- convert time tuple to string
    ctime() -- convert time in seconds to string
    mktime() -- convert local time tuple to seconds since Epoch
    strftime() -- convert time tuple to string according to format specification
    strptime() -- parse string to time tuple according to format specification
    tzset() -- change the local timezone
View Code

具体查看某一种方法的用法

>>> help(time.sleep)

 

posted @ 2016-01-12 16:57  沐风先生  阅读(282)  评论(0编辑  收藏  举报