您的位置:首页 > 编程语言 > Python开发

2.Python起步(未完待续)

2015-10-16 13:22 761 查看
1、输出 hello world!
>>> print 'hello world'
hello world
>>> myString = 'hello world'
>>> print myString
hello world
>>>


2-1、>>>  这个叫主提示符,意思是解释器等待你输入语句

2-2、... 这个叫次提示符,意思是解释器等待你输入语句的剩余部分

3、解释器 输入字符 _ ,可以获取上一次表达式所输出的值

例如: 

>>> 'hello world'
'hello world'
>>> _
'hello world'
>>>


4、print 也可以使用类似于C中printf()函数的占位符语法,替换 其中要打印的字符或字符串,这里称 % 叫做:字符串格式运算符% 

例如:love替换了%s, 365 替换 %d

>>> print "I %s You,  %d dayS" % ('love', 365)
I love You,  365 dayS
>>>


%s 表示由一个字符串来替换,而%d 表示由一个整数来替换,另外一个很常用的就是%f, 它

表示由一个浮点数来替换

5、 >> 这个符号是重定向输出
例如:重定向至标准错误输出

import sys
print >> sys.stderr, 'Fatal error: invalid input!'


例如:重定向到一个路径下的文件中

>>> import sys
>>> logfile = open('D:/Temp/mylog.txt', 'a')
>>> print >> logfile, 'Fatal error:  fuck that!'
>>> logfile.close()
此时你的D盘,Temp目录下,就有有mylog.txt文件,并且的里面的 里面是Fatal error: fuck that!

6、raw_input()函数,该函数接受一个字符串参数,要求用户从标准输入流(键盘)录入字符,,下面是实例

>>> user = raw_input('Enter your name: ')
Enter your name: wangyuanwai
>>> print user
wangyuanwai
>>>
上面将 wangyuanwai赋值给了user,此时 print user,即可以打印出wangyuanwai

7、下面这是把我们输入的1024 字符串类型,用int()函数转换成int类型,如果不用int函数的话,你对字符串实例对象做加减乘除操作,解释器就会报错了,呵呵

num = raw_input('Now enter a number: ')
Now enter a number: 1024
>>> print 'Doubling your number: %d' % (int(num) * 2)
Doubling your number: 2048


>>> print 'output number : %d' % (num * 2)

Traceback (most recent call last):
File "<pyshell#2>", line 1, in <module>
print 'output number : %d' % (num * 2)
TypeError: %d format: a number is required, not str
>>> print 'output number : %d' % (int(num) * 2)
output number : 2048
>>>


8、help()函数,可以查询你不知道函数的名称, 参数 传入函数名称即可

例如:下面我用 help查询了 raw_input函数

>>> help(raw_input)
Help on built-in function raw_input in module __builtin__:

raw_input(...)
raw_input([prompt]) -> string

Read a string from standard input.  The trailing newline is stripped.
If the user hits EOF (Unix: Ctl-D, Windows: Ctl-Z+Return), raise EOFError.
On Unix, GNU readline is used if enabled.  The prompt string, if given,
is printed without a trailing newline before reading.

>>>


9、注释

Python使用 # 符号作注释,从#开始直到一行结束

10、运算符

+、-、*、/、//、%、**

上面一共是7个运算符,说说不认识的吧, , //这个用作浮点数除法(结果会四舍五入),  ** 这个是乘方运算符,哈哈,Python真有意思啊,有两个除法运算符, %这是驱魔运算符

来个例子:

>>> print -2 * 4 + 3**2
1


运算符优先级: -、+和数学中一样,当然是优先级最低的了, *、/、//、%优先级较高, 单木运算符 + 和 - 优先级更高, **的优先级是最高的

11、比较运算符

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

我就不一一介绍运算符了,<> 这个也是不等于运算符 ,即和!= 一样

12、逻辑运算符 (与、或、非),不多解释啦,呵呵
and        or         not 

说个Python特助支持的语法

>>> 3 < 4 < 5
True
Python支持这样的语法,其实它的意思就是 3 < 4 and 3 < 5

C和Java中必须不支持

13、变量

变量语法基本与C和Java,因为它们都是类C语言,哈哈,从C发展出来的

变量名:开头必须是字母,大小写均可,或者_开头

Python是动态类型语言,不需要提前声明变量的类型。 变量的类型,会在赋值的那时候确定(确实先进了)

例如:

C语言里面

int a;

a = 5;

Python里面

>>> a = 5
变量a都是int类型

>>> counter = 0
>>> miles = 1000.0
>>> name = 'Bob'
>>> counter = counter + 1
>>> kilometers = 1.609 * miles
>>> print '%f miles is the same as %f km' % (miles, kilometers)
1000.000000 miles is the same as 1609.000000 km


14、支持简写语句

例如:

详细:n = n + 5

简写:n += 5

15、Python不支持C语言中的自增和自减,即 a++, a-- 这样的不支持,有点意思哈

16、基本数据类型

int 有符号整型

long 长整型

bool 布尔类型

float 浮点数

complex 复数

17、字符串

例子:

>>> 'hello world'
'hello world'
>>> "hello world"
'hello world'
>>> """ hello world """
' hello world '
>>> ''' hello world '''
' hello world '
这就是字符串的语法规则:即单引号里面、双引号里面、连续三个双引号里面,连续三个单引号里面,都是字符串,呵呵,爽

字符串可以看成是字符数组,索引值 ,从最左边 是 从0开始记数。

     反方向第一位的索引值是-1。

索引值有点意思

加号(+) 可用于字符串连接的时候使用

>>> 'hello' + 'world'
'helloworld'


星号(*) 可用于字符串重复的时候使用,这个厉害了

>>> 'hello' * 3
'hellohellohello'


还有字符串中举足轻重的 [ ] 索引运算符 和 [ : ] 切片运算符号,看哥哥下面的例子

>>> name = 'wangyuanwai'
>>> name[0]
'w'
>>> name[:]
'wangyuanwai'
>>> name[-1]
'i'
>>> name[:3]
'wan'
>>> name[2:5]
'ngy'


这个真是方便,像java里String,实例对象的操作,一堆方法,Python确实封装的贴心

18、列表与元组

列表:用中括号 [ ]包裹着,元素个数与类型都是可变的

元组:用()包裹,元素个数与类型不可变, 可以把元组看成是只读的列表,我试了确实不能再为其赋值了,呵呵,有点意思。

也可以使用索引运算符[ ]与切片运算符 [ : ],这一点和字符串一样 哈哈(只不过字符串的元素都是字符,而List里面可以放任何类型的实例对象)

>>> list = [1, 2, 3, 4]
>>> list[0]
1
>>> list
[1, 2, 3, 4]
>>> tuple = (1, 2, 3, 4)
>>> tuple[0]
1
>>> tuple
(1, 2, 3, 4)

19、字典

其实就是key_value,键值对…………

值可以是任意类型的实例对象,字典是用 {} 包起来的,哈哈

>>> dict = {'name' : 'wangyuanwai', 'age' : 20}
>>> dict
{'age': 20, 'name': 'wangyuanwai'}dict是个字典

20、代码块与缩进

Python使用缩进对齐的方式,代表代码块,这点着实很骚

21、if语法

条件表达式不用括号啦,哈哈

if 条件:

    语句………………

当然也有else的:

if 条件:

语句…………

else:

语句…………

当然还有else if语法啦:

if 条件:

语句………………

elif 条件:

语句…………………

22、while循环语法

while 条件:

23、for 循环

明明就是java里面的foreach语法呀呵呵

for item in [ ],

for item in range()

上面range()必须是个函数,具体先不说了,这本书太墨迹了………………

24、文件和内建函数 open(), file()

打开一个文件

windows下

tempFile = open("D:/hello/fuck", access = 'r')

先不说open函数了,以后慢慢说吧

25、异常与错误

python中有try-except,就是java中的try catch

26、函数

语法例子:

>>> def addMe2Me(x):
'apple +operation to argument'
return (x + x)

>>> addMe2Me(5)
10

上面就是一个函数:

def是关键字,必须写上,还有()旁边的冒号

我们看首行还有个一行字符串,即单引号里面的, 嘿嘿,这个也是注释的啦,以后就慢慢用的多了

函数名称addMe2Me, 接受一个参数, 返回 该 参数相加自己的值

默认参数语法:

>>> def foo(debug = True):
"default 用法"
if(debug):
print 'in debug mode'
print 'done'

>>> foo()
in debug mode
done
>>> foo(false)

Traceback (most recent call last):
File "<pyshell#87>", line 1, in <module>
foo(false)
NameError: name 'false' is not defined
>>> foo(False)定义一个函数 foo
如果为它传递参数,那么 debug的默认值将是True,如果传入False,那么debug的值必须是False啊,这里面我还踩了一个坑,哈哈

那就是

Python中的布尔值,关键字是 True和False, 首字母都要大写的, 哈哈, 有点意思

27、类

类:嘿嘿,类是实例对象的蓝图,模版,也可以理解成像是个容器一样

其它的先不写啦

28、模块

类似于C语言中的头文件,与Java的包,有点意思
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: