print和import的更多信息
print可以打印多个表达式,用逗号分开就行,
从模块导入函数
import somemodule
from somemodule import somefunction
from somemodule import somefunction ,anotherfunction,yetanotherfunction
from somemodule import*
序列解包
x,y,z,= 1,2,3
print x,y,z
结果:
1 2 3
链式赋值
将同一个值赋给多个变量的捷径
x=y=somefunction
增量赋值
x+=1
x*=1
布尔
print bool (42),bool(''),bool(0),bool('I think')
True False False True
注意:false none 0 "" () [] {}会被解释器看作假(false)
if elif else 语句
num = input('enter a number: ')
if num > 0:
print 'the number is positive'
elif num < 0:
print 'the number is negative'
else:
print 'the number is zero'
enter a number: -1
the number is negative
嵌套
name = raw_input('what is your name?: ')
if name.endswith('luo'):
if name.startswith('mr.'):
print 'hello ,mr.luo'
elif name.startswith ('mrs.'):
print 'hello ,mrs.luo'
else:
print 'hello,luo'
else:
print 'hello, stranger'
结果:
what is your name?: mrs.luo
hello ,mrs.luo
使用==运算符判定两个对象是否相等,使用is判定两个对象是否等同(同一个对象)
in:成员资格运算符
布尔运算符
and、or、not就是所谓的布尔运算符
示例:
numbers = input ('enter a number between 1 and 10:')
if numbers <=10 and numbers>=0:
print "is great"
else:
print "wrong"
结果:
enter a number between 1 and 10:2
is great
while 循环
x = 1
while x<=100:
print x
x+=1
部分结果;
94
95
96
97
98
99
100
name = ''
while not name:
name = raw_input("enter your name: ")
print 'hello,%s!'% name
结果:
enter your name: luo
hello,luo!
for循环
示例:
numbers=[0,1,2,3,4]
for number in numbers:
print number
结果:
0
1
2
3
4
for number in range(1,101):
print number
部分结果:
94
95
96
97
98
99
100
循环遍历字典元素,
d = {'x':1,'y':2,'z':3}
for key in d:
print key,'corresponds to',d[key]
结果:
y corresponds to 2
x corresponds to 1
z corresponds to 3
并行迭代
示例:
name = ['x','y','z']
age = [12,13,14]
for i in range(len(name)):
print name[i],'is',age[i],'years old'
结果:
x is 12 years old
y is 13 years old
z is 14 years old
编号迭代
示例:
ndex = 0
strings = ["asd", "123", "aXXX", "cc123XXX123"]
for str in strings:
if 'XXX' in str:
strings[index] = 'luo'
index += 1
print strings
结果:
['asd', '123', 'luo', 'luo']
反转和排序迭代
示例:
print list( reversed('hello world'))
print ''.join(reversed('hello world'))
结果:
['d', 'l', 'r', 'o', 'w', ' ', 'o', 'l', 'l', 'e', 'h']
dlrow olleh
跳出循环
1、break
2、continue
3、while True/break习语
示例:
while True:
word = raw_input('please enter a word: ')
if not word:break
print 'the word was '+ word
结果:
please enter a word: 1
the word was 1
please enter a word: hello
the word was hello
please enter a word:
1、pass
2、del
3、exec和eval
示例:
exec "print 'hello,world' "
结果:
hello,world
示例:
while True:
print eval(raw_input ('enter an arithmetic expression: '))
结果:
enter an arithmetic expression: 4*2+9
17
enter an arithmetic expression: 6/3+10
12
enter an arithmetic expression:
print可以打印多个表达式,用逗号分开就行,
从模块导入函数
import somemodule
from somemodule import somefunction
from somemodule import somefunction ,anotherfunction,yetanotherfunction
from somemodule import*
序列解包
x,y,z,= 1,2,3
print x,y,z
结果:
1 2 3
链式赋值
将同一个值赋给多个变量的捷径
x=y=somefunction
增量赋值
x+=1
x*=1
布尔
print bool (42),bool(''),bool(0),bool('I think')
True False False True
注意:false none 0 "" () [] {}会被解释器看作假(false)
if elif else 语句
num = input('enter a number: ')
if num > 0:
print 'the number is positive'
elif num < 0:
print 'the number is negative'
else:
print 'the number is zero'
enter a number: -1
the number is negative
嵌套
name = raw_input('what is your name?: ')
if name.endswith('luo'):
if name.startswith('mr.'):
print 'hello ,mr.luo'
elif name.startswith ('mrs.'):
print 'hello ,mrs.luo'
else:
print 'hello,luo'
else:
print 'hello, stranger'
结果:
what is your name?: mrs.luo
hello ,mrs.luo
使用==运算符判定两个对象是否相等,使用is判定两个对象是否等同(同一个对象)
in:成员资格运算符
布尔运算符
and、or、not就是所谓的布尔运算符
示例:
numbers = input ('enter a number between 1 and 10:')
if numbers <=10 and numbers>=0:
print "is great"
else:
print "wrong"
结果:
enter a number between 1 and 10:2
is great
while 循环
x = 1
while x<=100:
print x
x+=1
部分结果;
94
95
96
97
98
99
100
name = ''
while not name:
name = raw_input("enter your name: ")
print 'hello,%s!'% name
结果:
enter your name: luo
hello,luo!
for循环
示例:
numbers=[0,1,2,3,4]
for number in numbers:
print number
结果:
0
1
2
3
4
for number in range(1,101):
print number
部分结果:
94
95
96
97
98
99
100
循环遍历字典元素,
d = {'x':1,'y':2,'z':3}
for key in d:
print key,'corresponds to',d[key]
结果:
y corresponds to 2
x corresponds to 1
z corresponds to 3
并行迭代
示例:
name = ['x','y','z']
age = [12,13,14]
for i in range(len(name)):
print name[i],'is',age[i],'years old'
结果:
x is 12 years old
y is 13 years old
z is 14 years old
编号迭代
示例:
ndex = 0
strings = ["asd", "123", "aXXX", "cc123XXX123"]
for str in strings:
if 'XXX' in str:
strings[index] = 'luo'
index += 1
print strings
结果:
['asd', '123', 'luo', 'luo']
反转和排序迭代
示例:
print list( reversed('hello world'))
print ''.join(reversed('hello world'))
结果:
['d', 'l', 'r', 'o', 'w', ' ', 'o', 'l', 'l', 'e', 'h']
dlrow olleh
跳出循环
1、break
2、continue
3、while True/break习语
示例:
while True:
word = raw_input('please enter a word: ')
if not word:break
print 'the word was '+ word
结果:
please enter a word: 1
the word was 1
please enter a word: hello
the word was hello
please enter a word:
1、pass
2、del
3、exec和eval
示例:
exec "print 'hello,world' "
结果:
hello,world
示例:
while True:
print eval(raw_input ('enter an arithmetic expression: '))
结果:
enter an arithmetic expression: 4*2+9
17
enter an arithmetic expression: 6/3+10
12
enter an arithmetic expression: