2009/04/18

python学习2

本篇介绍一些定义

常量

例 

5、1.23、9.25e-3,  'This is a string'、"It's a string!"

这样的固定的字符串。因为不能改变它的值。


在Python中有4种类型的数——整数、长整数、浮点数和复数。 

●     2  整数的例子。 

●     长整数不过是大一些的整数。 

●     3.23和52.3E-4是浮点数的例子。E标记表示10的幂。在这里,52.3E-4表示52.3 * 10-4。 

●     (-5+4j)和(2.3-4.6j)是复数的例子。 


字符串 

用""或者''定义。例如:'中国'  就是一个字符串


包含单引符的情况,比如what's your name?的时候。这样写"What's your name?",即用双引号括下单引号。要在 双引号字符串中使用双引号本身的时候,可以借助于转义符\"

例:"what's your name?\"my name is linjie\""


或者三引号'''。例如:'''what's your name ?"my name is linjie."'''

另外,你可 以用转义符 \\来指示反斜杠本身。 


"This is the first sentence.\ 

This is the second sentence." 

等价于"This is the first sentence. This is the second sentence." 


(还有一些暂不说,以后遇到再说。原教程先说这些却用不到等于白说)


下面来几个最直白的例子


例2

#!/usr/bin/python 

i = 5 

print i 

i = i + 1 

print i 

s = '''This is a multi-line string. 

This is the second line.''' 

print s 


(如果你现在还是迷惑这段代码写在哪里,那么跟我当初一样。这样写在新打开的bash里面:
cd projs
vim var.py
i
#!/usr/bin/python
i=5
print i
i=i+1
print i
s='''This is a multi-line string.
This is the second line.'''
print s

这个时候按键盘上的Esc键,接着输入

:w
:!python var.py


ok!计算机开始跑啦~~

运行结果:

This is a multi-line string. 

This is the second line. 


现在我插一个例子说明一个小符号的用法:


例:

s='This is a string.\

This continues the string.'

print s


运行后输出:


This is a string.This continues the string.


就是说\符号其实就是换行的


ok,说了很多,到python解释器里面转一会。


在bash里面输入  python


然后输入


>>>2+3

5

>>>3*5

15

>>>


其实就是运算的东东~~加减乘除的玩意儿~~


5+5 得到 10

'a' +'b' 得到 'ab'

2*3=6

'la'*3+'lalala'

3**4=81(即3*3*3*3)

4/3=1(因为是整数)

4.0/3.0=1.3333333333333(数不清多少个3)

4//3.0=1.0(取整除返回商)

-25.5%2.25=1.5(取余)


其他还有好多暂时用不上说了也是忘的定义。遇到再说,一说就记住了~`哈哈)


例:


#!/usr/bin/python 

# Filename: expression.py 

length = 5 

breadth = 2 

area = length * breadth 

print 'Area is', area 

print 'Perimeter is', 2 * (length + breadth) 


运行结果:

Area is 10 

Perimeter is 14




if、for和while语句 


if语句 


例 使用if语句 

#!/usr/bin/python 

# Filename: if.py 

number = 23 

guess = int(raw_input('Enter an integer : ')) 

if guess == number: 

    print 'Congratulations, you guessed it.' # New block starts here 

    print "(but you do not win any prizes!)" # New block ends here 

elif guess <>

    print 'No, it is a little higher than that' # Another block 

    # You can do whatever you want in a block ... 

else: 

    print 'No, it is a little lower than that' 

    # you must have guess > number to reach here 

print 'Done' 

# This last statement is always executed, after the if statement is exe- 

cuted 


输出:

Enter an integer : 50 

No, it is a little lower than that 

Done 


while语句

#!/usr/bin/python 

# Filename: while.py 

number = 23 

running = True 

while running: 

    guess = int(raw_input('Enter an integer : ')) 

    if guess == number: 

        print 'Congratulations, you guessed it.' 

        running = False # this causes the while loop to stop 

    elif guess <>

        print 'No, it is a little higher than that' 

    else: 

        print 'No, it is a little lower than that' 

else: 

    print 'The while loop is over.' 

    # Do anything else you want to do here 

print 'Done' 


输出:

Enter an integer : 50 

No, it is a little lower than that. 

Enter an integer : 22 

No, it is a little higher than that. 

Enter an integer : 23 

Congratulations, you guessed it. 

The while loop is over. 

Done 


两个例子都是猜,if就不如while好。只要你试了!!

补一句,学这玩意我头很疼~~