Python语言学习

Python教程

Python语言学习 - 1.初始变量


>>> 1
1
>>> 100
100
>>> -8080
-8080
>>> 0
0
>>> 1.23
1.23
>>> 3.14
3.14
>>> -9.01
-9.01
>>> 'abc'
'abc'
>>> "xyz"
'xyz'
>>> 'I\'m\"ok\"!'
'I\'m"ok"!'
>>> print 'I\'m ok'
I'm ok
>>> print '|\'m learning \n python'
|'m learning 
python
>>> print '\\\n\\'
\
\
>>> True
True
>>> False
False
>>> 3>2
True
>>> 3>5
False
>>> a=1
>>> t_007='Too7'
>>> Answer = True
>>> a = 123
>>> print a
123
>>> a = 'ABC'
>>> print a
ABC
>>> x = 10
>>> x = x+2
>>> PI = 3.14159265359
>>> 10 /3
3
>>> 10.0/3
3.3333333333333335
>>> 10%3
1
>>> 

Python语言学习 - 2.list结构


>>> classmates = ['Michael','Bob','Tracy']
>>> classmates
['Michael', 'Bob', 'Tracy']
>>> len(classmates)
3
>>> classmates[0]
'Michael'
>>> classmates[3]
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
IndexError: list index out of range
>>> classmates[-1]
'Tracy'
>>> classmates[-4]
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
IndexError: list index out of range
>>> classmates.append('Adam')
>>> classmates
['Michael', 'Bob', 'Tracy', 'Adam']
>>> classmates.insert(1,'Jack')
>>> classmates
['Michael', 'Jack', 'Bob', 'Tracy', 'Adam']
>>> classmates.pop()
'Adam'
>>> classmates
['Michael', 'Jack', 'Bob', 'Tracy']
>>> classmates.pop(1)
'Jack'
>>> classmates
['Michael', 'Bob', 'Tracy']
>>> classmates[1] = 'Sarah'
>>> classmates
['Michael', 'Sarah', 'Tracy']
>>> L = ['Apple',123,True]
>>> S = ['Python','Java',['Asp','php'],'scheme']
>>> len(3)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: object of type 'int' has no len()
>>> len(s)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 's' is not defined
>>> len(S)
4
>>> p = ['asp','php']
>>> s = ['python','java',p,'scheme']
>>> p[1]
'php'
>>> p[2]
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
IndexError: list index out of range
>>> s[2][1]
'php'
>>> L = []
>>> len(L)
0
>>> 


Python语言学习 - 3.索引 4.循环 5.判断


age = 20
if age >= 18:
print 'your age is', age
print 'adult'

age = 3
if age >= 18:
print 'your age is', age
print 'adult'
else:
print 'your age is', age
print 'teenager'


age = 3
if age >= 18:
print 'adult'
elif age >= 6:
print 'teenager'
else:
print 'kid'

age = 20
if age >= 6:
print 'teenager'
elif age >= 18:
print 'adult'
else:
print 'kid'

names = ['Michael', 'Bob', 'Tracy']
for name in names:
print name

sum = 0
for x in [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]:
sum = sum + x
print sum

>>> range(5)
[0, 1, 2, 3, 4]

sum = 0
for x in range(101):
sum = sum + x
print sum

sum = 0
n = 99
while n > 0:
sum = sum + n
n = n - 2
print sum

birth = raw_input('birth: ')
if birth < 2000:
print '00前'
else:
print '00后'


Python语言学习 - 6.字典



>>> d = {'Michael':95,'Bob':75,'Tracy':85}
>>> d['Michael']
95
>>> d.pop('bob')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
KeyError: 'bob'
>>> d.pop('Bob')
75
>>> d
{'Michael': 95, 'Tracy': 85}
>>> 

Python语言学习 - 7.文件处理

>>> f = open('/Users/michael/test.txt', 'r')
>>> f.read()
'Hello, world!'
>>> f.close()

try:
f = open('/path/to/file', 'r')
print f.read()
finally:
if f:
f.close()


with open('/path/to/file', 'r') as f:
print f.read()

>>> f = open('/Users/michael/test.jpg', 'rb')
>>> f.read()
'\xff\xd8\xff\xe1\x00\x18Exif\x00\x00...' # 十六进制表示的字节


>>> f = open('/Users/michael/gbk.txt', 'rb')
>>> u = f.read().decode('gbk')
>>> u
u'\u6d4b\u8bd5'
>>> print u
测试

>>> f = open('/Users/michael/test.txt', 'w')
>>> f.write('Hello, world!')
>>> f.close()



Python语言学习 - 8.函数


def my_abs(x):
if x >= 0:
return x
else:
return -x

def nop():
pass

if age >= 18:
pass

import math

def move(x, y, step, angle=0):
nx = x + step * math.cos(angle)
ny = y - step * math.sin(angle)
return nx, ny

定义函数时需要确定函数名和参数个数

如果有必要可以先对参数的数据类型做检查

函数体内部可以用return随时返回函数结果

函数执行完毕也没有return语句时自动return None

函数可以同时返回多个值但其实就是一个tuple

def power(x):
return x * x

def power(x, n):
s = 1
while n > 0:
n = n - 1
s = s * x
return s

def enroll(name, gender):
print 'name:', name
print 'gender:', gender

def enroll(name, gender, age=6, city='Beijing'):
print 'name:', name
print 'gender:', gender
print 'age:', age
print 'city:', city



Loading Disqus comments...
Table of Contents