s1 = ' use \' signal quote '
s2 = " use \" double quotes "
s3 = ''' use ' three '' quotes'''
s4 = 'this is first line\
this is first line also'
s5 = '''this is first line
this is second line'''
print s1
print s2
print s3
print s4
print s5
use ' signal quote
use " double quotes
use ' three '' quotes
this is first line this is first line also
this is first line
this is second line
i = 5
print i
i = i+1
print i
s = 'this is a string'
print s
5
6
this is a string
print 'this is a physical line'
print 'this is a logical line'; print 'it has two sentences'
this is a physical line
this is a logical line
it has two sentences
i=3
j=2
print 'and:',i>0 and j>3
print 'or:',i>0 or j>3
print 'not:',not(i>0 and j>3)
print '**:',i**j
print '//:',i//j
and: False
or: True
not: True
**: 9
//: 1
项目 | C | Python |
---|---|---|
数据类型 | 短整型、整型、长整型、浮点型、字符型等 | 整型、长整型、复数、字符串 |
变量 | 需要声明类型 | 不需要声明类型,只需要赋值 |
代码块 | 以{} 来划分代码块 | 用缩进划分 |
代码行 | 每个语句都需要加分号 | 单行语句一般不加分号 |
运算符 | &&,||,!,pow | and , or , not ,** |
注释 | //,/**/ | # |
i = int(raw_input('Enter :'))
if i == 10:
print 'right'
elif i<10:
print 'it\'s to little'
else:
print 'it\'s to big'
python if.py
Enter :11
it's to big
for i in range(1,5,2):
print i,
else:
print ''
for j in ['abc','defg','hijkl','mnopqr']:
print j,
else:
print '\nover'
1 3
abc defg hijkl mnopqr
over
i=23
j=12
running=True
while running:
if j<i:
print j,
j=j+1
elif j>i:
j=j-1
print j,
else:
running=False
else:
print '\n done'
输出结果:
12 13 14 15 16 17 18 19 20 21 22
done
def Max(a,b):
'''This function is returns the max value between two value '''#This is DocString
if a>b:
return a
else:
return b
print Max(2,3) #print the max in 2 and 3
print Max.__doc__ #print the DocString in function
3
This function is returns the max value between two value
def fun(y):
global x #x is a global variable
print 'x is',x
x=20
print 'x is modified to',x
print 'y is',y #y is a local variable
y=111
print 'y is modified to',y
x=5
y=10
fun(y)
print 'x is modified to',x
print 'y is still',y
x is 5
x is modified to 20
y is 10
y is modified to 111
x is modified to 20
y is still 10
def s(name='kerian',age=20,mark=100):
print name,age,mark;
s('toy') #use default param
s('tom',18) #use default param
s(mark=55) #use key param
s(mark=66,name='kindy')#use key param
toy 20 100
tom 18 100
kerian 20 55
kindy 20 66
创建自己的模块与编写Python程序的步骤相同。
创建三个程序,并且model1.py和model2.py被model_demo,py调用
#model1.py
def m1func1():
print 'This function belongs to module1, it\'s function 1'
def m1func2():
print 'This function belogs to module2, it\'s function 2'
m1version = '0.2'
if __name__=='__main__':
print 'module1 is running it\'s main block code'
else:
print 'module1 is called by other programs'
#model2.py
def m2func2():
print 'This function belongs to module2'
m2version = '0.1'
if __name__=='__main__':
print 'module2 is running it\'s main code block'
else:
print 'module2 is called by other programs'
#model_demo.py
from model1 import * #import all function and variable from model1
import model2
if __name__=='__main__':
print 'module_demo is running it\'s main code block'
m1func1()
m1func2()
print m1version
model2.m2func2()
print model2.m2version
else:
print 'module_demo is called by other programs'
module1 is called by other programs
module2 is called by other programs
module_demo is running it's main code block
This function belongs to module1, it's function 1
This function belogs to module2, it's function 2
0.2
This function belongs to module2
0.1
numlist=[5,2,'3',6,'7']
print 'numlist:',numlist
print 'numlist length:',len(numlist)
for item in numlist:
print item,
else:
print ''
numlist.append('1')
print 'add 1 in the end of list:',numlist
numlist.sort()
print 'sorted list:',numlist
del numlist[1]
print 'list number after deleted the second number',numlist
numlist: [5, 2, '3', 6, '7']
numlist length: 5
5 2 3 6 7
add 1 in the end of list: [5, 2, '3', 6, '7', '1']
sorted list: [2, 5, 6, '1', '3', '7']
list number after deleted the second number [2, 6, '1', '3', '7']
mtuple=('a','b','cd',123,456)
for item in mtuple:
print item,
else:
print ''
print mtuple
newtuple=('xy','z',mtuple)
print newtuple
print newtuple[2]
print newtuple[2][2]
name = 'kerain'
age=22
print 'name:%s age:%d' % (name,age)
a b cd 123 456
('a', 'b', 'cd', 123, 456)
('xy', 'z', ('a', 'b', 'cd', 123, 456))
('a', 'b', 'cd', 123, 456)
cd
name:kerain age:22
d={'kerian':'12345','toy':'123456','tom':'54321'}
print 'kerian\'s telephone is:%s '% d['kerian']
print d
d['jack']='9887123'
del d['tom']
print d
for name,tele in d.items():
print 'name:%s tele:%s' % (name,tele),
kerian's telephone is:12345
{'kerian': '12345', 'toy': '123456', 'tom': '54321'}
{'kerian': '12345', 'toy': '123456', 'jack': '9887123'}
name:kerian tele:12345 name:toy tele:123456 name:jack tele:9887123
numlist=['1','2','3','4']
print 'item 0:',numlist[0]
print 'item 2:',numlist[2]
print 'item -1:',numlist[-1]
print 'item -3:',numlist[-3]
print 'item 1 to 3:',numlist[1:3]
print 'item 1 to -1:',numlist[1:-1]
print 'item start to end:',numlist[:]
item 0: 1
item 2: 3
item -1: 4
item -3: 2
item 1 to 3: ['2', '3']
item 1 to -1: ['2', '3']
item start to end: ['1', '2', '3', '4']
numlist =['1','2','3','4']
mylist=numlist
del mylist[0]
print 'mylist:',mylist
print 'numlist:',numlist
mylist=numlist[:]
del mylist[0]
print 'mylist:',mylist
print 'numlist:',numlist
mylist: ['2', '3', '4']
numlist: ['2', '3', '4']
mylist: ['3', '4']
numlist: ['2', '3', '4']