Python基础教程之第5章 条件, 循环和其它语句

Python 2.7.5 (default, May 15 2013, 22:43:36) [MSC v.1500 32 bit (Intel)] on win32
#Chapter 5 条件, 循环和其它语句
#5.1 print和import的很多其它信息
#对于非常多应用程序来说, 使用logging模块记日志比print语句更合适
#5.1.1 使用逗号输出
#能够看到, 每隔參数之间都自己主动插入了一个空格符
>>> print ‘Age:‘,42
Age: 42
>>> 1,2,3
(1, 2, 3)
#print的參数并不像我们预期那样构成一个元组
>>> print 1,2,3
1 2 3
>>> print (1,2,3)
(1, 2, 3)
>>> name=‘Gumby‘
>>> salution=‘Mr.‘
>>> greeting=‘Hello,‘
>>> print greeting, salution, name
Hello, Mr. Gumby
>>> greeting=‘Hello‘
>>> print greeting, ‘,‘, salution, name
Hello , Mr. Gumby
>>> print greeting + ‘,‘, salution, name
Hello, Mr. Gumby
#假设在结尾处加上逗号,那么接下来的语句会与前一语句在同一行打印(仅仅在脚本中起作用,在交互式Python会话中无效)
print ‘Hello,‘,
print ‘world!‘
#输出Hello, world!
#5.1.2 import语句
#import somemodule
#from somemodule import somefunction
#from somemodule import somefunction, anotherfunction, yetanotherfunction
#from somemodule import *
#为模块提供别名
>>> import math as foobar
>>> foobar.sqrt(4)
2.0
#为函数提供别名
>>> from math import sqrt as foobar
>>> foobar(4)
2.0
#给不同模块中的同名函数提供别名
#from module1 import open as open1
#from module2 import open as open2
#5.2 赋值魔法
#5.2.1 序列解包(sequence unpacking)
>>> x, y, z = 1, 2, 3
>>> print x, y, z
1 2 3
>>> x, y = y, x
>>> print x, y, z
2 1 3
>>> values = 1,2,3
>>> values
(1, 2, 3)
>>> x,y,z=values
>>> x
1
>>> y
2
>>> z
3
>>> scoundrel={‘name‘:‘Robin‘, ‘girlfriend‘:‘Marion‘}
>>> key, value = scoundrel.popitem()
>>> key
‘girlfriend‘
>>> value
‘Marion‘
>>> x,y,z = 1,2

Traceback (most recent call last):
  File "<pyshell#31>", line 1, in <module>
    x,y,z = 1,2
ValueError: need more than 2 values to unpack
>>> x,y,z=1,2,3,4

Traceback (most recent call last):
  File "<pyshell#32>", line 1, in <module>
    x,y,z=1,2,3,4
ValueError: too many values to unpack
#Python 3.0 中有另外一个解包的特性
#a,b,rest*=[1,2,3,4]
#rest的结果将会是[3,4]
#5.2.2 链式赋值
#x=y=somefunction
#等效于
#y=somefunction
#x=y

#5.2.3 增量赋值(augmented assignment)
>>> x=2
>>> x += 1
>>> x *= 2
>>> x
6
>>> fnord = ‘foo‘
>>> fnord += ‘bar‘
>>> fnord *= 2
>>> fnord
‘foobarfoobar‘
#语句块: 缩排的乐趣
#5.4 条件和条件语句
# False None 0 "" ‘‘ () [] {} 会被解释器看做假
>>> True
True
>>> False
False
>>> True == 1
True
#标准的布尔值为False(0)和True(1)
>>> False == 0
True
>>> True + False + 42
43
#bool函数能够用来(和list, str以及tuple相似)转换其它值
>>> bool(‘I think, therefore I am‘)
True
>>> bool(42)
True
>>> bool(‘‘)
False
>>> bool(0)
False
>>> bool([])
False
>>> bool(())
False
>>> bool({})
False
#5.4.2 条件运行和if语句
>>> name = raw_input(‘What is your name?

‘)
What is your name? Gumby
>>> if name.endswith(‘Gumby‘):
...     print ‘Hello, Mr. Gumby‘
...
Hello, Mr. Gumby
#5.4.3 else子句
>>> name = raw_input(‘What is your name? ‘)
What is your name?

Jon
>>> if name.endswith(‘Gumby‘):
...     print ‘Hello, Mr. Gumby‘
... else:
...     print ‘Hello, stranger‘
...
Hello, stranger
#5.4.4 elif子句
>>> num = input(‘Enter a number: ‘)
Enter a number: 0
>>> if num > 0:
...     print ‘The number is positive‘
... elif num < 0:
...     print ‘The number is negative‘
... else:
...     print ‘The number is zero‘
...
The number is zero
#5.4.5 嵌套代码块
>>> name = raw_input(‘What is your name? ‘)
What is your name? Mrs. Gumby
>>> if name.endswith(‘Gumby‘):
...     if name.startswith(‘Mr.‘):
...             print ‘Hello, Mr. Gumby‘
...     elif name.startswith(‘Mrs.‘):
...             print ‘Hello, Mrs. Gumby‘
...     else:
...             print ‘Hello, Gumby‘
... else:
...     print ‘Hello, stranger‘
...
Hello, Mrs. Gumby
#链式比較运算
#比較对象的时候能够使用内建的cmp函数
>>> age=10
>>> 0<age<100
True
>>> age=-1
>>> 0<age<100
False
#相等运算符
>>> "foo" == "foo"
True
>>> "foo" == "bar"
False
>>> "foo" = "foo"
  File "<stdin>", line 1
SyntaxError: can‘t assign to literal
#同一性运算符
#避免将is运算符用于比較相似数值和字符串这类不可变值.
>>> x=y=[1,2,3]
>>> z=[1,2,3]
>>> x == y
True
>>> x == z
True
>>> x is y
True
>>> x is z
False
>>>
>>> x is z
False
>>> x = [1,2,3]
>>> y = [2,4]
>>> x is not y
True
>>> del x[2]
>>> y[1]=1
>>> y.reverse()
>>> x == y
True
>>> x is y
False
#in: 成员资格运算符
>>> name = raw_input(‘What is your name? ‘)
What is your name?

Jonathan
>>> if ‘s‘ in name:
...     print ‘Your name contains the letter "s".‘
... else:
...     print ‘Your name does not contain the letter "s".‘
...
Your name does not contain the letter "s".
>>> "alpha" < "beta"
True
>>> ‘FnOrd‘.lower() == ‘Fnord‘.lower()
True
>>> [1,2] < [2,1]
True
>>> [2,[1,4]]<[2,[1,5]]
True
#布尔运算符

>>> number = input(‘Enter a number between 1 and 10: ‘)
Enter a number between 1 and 10: 8
>>> if number <= 10:
...     if number >=1:
...             print ‘Great!‘
...     else:
...             print ‘Wrong!‘
... else:
...     print ‘Wrong!‘
...
Great!
>>> number = input(‘Enter a number between 1 and 10: ‘)
Enter a number between 1 and 10: 6
>>> if number <= 10 and number >= 1:
...     print ‘Great!‘
... else:
...     print ‘Wrong!‘
...
Great!
>>> number = input(‘Enter a number between 1 and 10: ‘)
Enter a number between 1 and 10: 11
>>> if 1 <= number <= 10:
...     print ‘Great!‘
... else:
...     print ‘Wrong!‘
...
Wrong!
>>> name = raw_input(‘Please enter your name: ‘) or ‘<unknown>‘
Please enter your name:
>>> name
‘<unknown>‘
#短路逻辑和条件表达式
#相似C和Java中的三元运算符
>>> name = ‘Jon‘if True else ‘Jack‘
>>> name
‘Jon‘
>>> name = ‘Jon‘if False else ‘Jack‘
>>> name
‘Jack‘
#5.4.7 断言
>>> age = 10
>>> assert 0 < age < 100
>>> age = -1
>>> assert 0 < age < 100
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AssertionError
#条件后能够加入逗号和字符串,用来解释断言
>>> age = -1
>>> assert 0 < age < 100, ‘The age must be realistic‘
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AssertionError: The age must be realistic
#5.5 循环
#5.5.1 while循环
>>> x=1
>>> while x <= 100:
...     print x
...     x +=1
...
1
2
3
...
100
>>> x
101

>>> name = ‘‘
>>> while not name:
...     name = raw_input(‘Please enter your name: ‘)
...
Please enter your name:
Please enter your name:
Please enter your name: Jon
>>> print ‘Hello, %s!‘ % name
Hello, Jon!
>>> name = ‘‘
>>> while not name or name.isspace():
...     name = raw_input(‘Please enter your name: ‘)
...
Please enter your name:
Please enter your name:
Please enter your name: Chan
>>> print ‘Hello, %s!‘ % name
Hello, Chan!
>>> while not name.strip():
...     name = raw_input(‘Please enter your name: ‘)
...
>>> name = ‘‘
>>> while not name.strip():
...     name = raw_input(‘Please enter your name: ‘)
...
Please enter your name:
Please enter your name:
Please enter your name: Kingston
>>> print ‘Hello, %s!‘ % name
Hello, Kingston!

#5.5.2 for循环
>>> words=[‘this‘, ‘is‘, ‘an‘, ‘ex‘, ‘parrot‘]
>>> for word in words:
...     print word
...
this
is
an
ex
parrot
>>> numbers = [0,1,2,3,4,5,6,7,8,9]
>>> for number in numbers:
...     print number
...
0
1
2
3
4
5
6
7
8
9
>>> range(10)
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
#假设能使用for循环,就尽量不用while循环
#当须要迭代一个巨大的序列时,xrange会比range更高效
>>> for number in range(1,5):
...     print number
...
1
2
3
4
#5.5.3 循环遍历字典元素
>>> 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
>>> for key, value in d.items():
...     print kye, ‘corresponds to‘, value
...
Traceback (most recent call last):
  File "<stdin>", line 2, in <module>
NameError: name ‘kye‘ is not defined
>>> for key, value in d.items():
...     print key, ‘corresponds to‘, value
...
y corresponds to 2
x corresponds to 1
z corresponds to 3
>>>

#5.5.4 一些迭代工具
#并行迭代
>>> names = [‘anne‘, ‘beth‘, ‘george‘, ‘damon‘]
>>> ages = [12, 45, 32, 102]
>>> for in in range(len(names)):

SyntaxError: invalid syntax
>>> for i in range(len(names)):
	print names[i], ‘is‘, ages[i], ‘year old‘

anne is 12 year old
beth is 45 year old
george is 32 year old
damon is 102 year old
>>> for i in range(len(names)):
	print names[i], ‘is‘, ages[i], ‘years old‘

anne is 12 years old
beth is 45 years old
george is 32 years old
damon is 102 years old
>>> zip(names, ages)
[(‘anne‘, 12), (‘beth‘, 45), (‘george‘, 32), (‘damon‘, 102)]
>>> for name, age in zip(names, ages):
	print name, ‘is‘, age, ‘years old‘

anne is 12 years old
beth is 45 years old
george is 32 years old
damon is 102 years old
>>> zip(range(5), xrange(100000000))
[(0, 0), (1, 1), (2, 2), (3, 3), (4, 4)]
#在索引上迭代
>>> strings = [‘I am xxx‘, ‘I like losing my face‘, ‘Say goodbye‘]
>>> for string in strings:
	index = strings.index(string) # Search for the string in the list of strings
	strings[index]=‘[censored]‘

>>> strings
[‘[censored]‘, ‘[censored]‘, ‘[censored]‘]
>>> strings = [‘I am xxx‘, ‘I like losing my face‘, ‘Say goodbye‘]
>>> for string in strings:
	if ‘xxx‘in strings:
		index = strings.index(string) # Search for the string in the list of strings
		strings[index]=‘[censored]‘

>>> strings
[‘I am xxx‘, ‘I like losing my face‘, ‘Say goodbye‘]
>>> strings = [‘I am xxx‘, ‘I like losing my face‘, ‘Say goodbye‘]
>>> for string in strings:
	if ‘xxx‘ in string:
		index = strings.index(string) # Search for the string in the list of strings
		strings[index]=‘[censored]‘

>>>
>>> string
‘Say goodbye‘
>>> strings
[‘[censored]‘, ‘I like losing my face‘, ‘Say goodbye‘]
>>> [‘I am xxx‘, ‘I like losing my face‘, ‘Say goodbye‘]
[‘I am xxx‘, ‘I like losing my face‘, ‘Say goodbye‘]
>>> index = 0
>>> for string in strings:
	if ‘xxx‘ in string:
		strings[index]=‘[censored]‘
index += 1
SyntaxError: invalid syntax
>>> index = 0
>>> for string in strings:
	if ‘xxx‘ in string: strings[index]=‘[censored]‘
	index += 1

>>> strings
[‘[censored]‘, ‘I like losing my face‘, ‘Say goodbye‘]
>>> [‘I am xxx‘, ‘I like losing my face‘, ‘Say goodbye‘, ‘xxx is a sensitive word‘]
[‘I am xxx‘, ‘I like losing my face‘, ‘Say goodbye‘, ‘xxx is a sensitive word‘]
>>> for index, string in enumerate(strings):
	if ‘xxx‘ in string:
		strings[index]=‘[censored]‘

>>> strings
[‘[censored]‘, ‘I like losing my face‘, ‘Say goodbye‘]
>>> strings = [‘I am xxx‘, ‘I like losing my face‘, ‘Say goodbye‘, ‘xxx is a sensitive word‘]
#enumerate函数能够在提供索引的地方迭代索引-值对.
>>> for index, string in enumerate(strings):
	if ‘xxx‘ in string:
		strings[index]=‘[censored]‘

>>> strings
[‘[censored]‘, ‘I like losing my face‘, ‘Say goodbye‘, ‘[censored]‘]

#翻转和排序迭代
>>> sorted([4,3,6,8,3])
[3, 3, 4, 6, 8]
>>> sorted(‘Hello, world!‘)
[‘ ‘, ‘!‘, ‘,‘, ‘H‘, ‘d‘, ‘e‘, ‘l‘, ‘l‘, ‘l‘, ‘o‘, ‘o‘, ‘r‘, ‘w‘]
>>> list(reversed(‘Hello, world‘))
[‘d‘, ‘l‘, ‘r‘, ‘o‘, ‘w‘, ‘ ‘, ‘,‘, ‘o‘, ‘l‘, ‘l‘, ‘e‘, ‘H‘]
>>> ‘‘.join(reversed(‘Hello, world!‘))
‘!dlrow ,olleH‘

#5.5.5 跳出循环
#break
>>> from math import sqrt
>>> for n in range(99, 0, -1);
SyntaxError: invalid syntax
>>> for n in range(99, 0, -1):
	root = sqrt(n)
	if root == int(root):
		print n
		break

81
# continue
# while True/break习惯使用方法
>>> word = ‘dummy‘
>>> while word:
	word = raw_input(‘Please enter a word: ‘)
	# 处理word:
	print ‘The word was ‘ + word

Please enter a word: first
The word was first
Please enter a word: second
The word was second
Please enter a word:
The word was 

>>> word = raw_input(‘Please enter a word: ‘)
Please enter a word: first
>>> while word:
	print ‘The word was ‘ + word
	word = raw_input(‘Please enter a word: ‘)

The word was first
Please enter a word: second
The word was second
Please enter a word: 

>>> while True:
	word = raw_input(‘Please enter a word: ‘)
	if not word: break
	print ‘The word was ‘ + word

Please enter a word: first
The word was first
Please enter a word: second
The word was second
Please enter a word:
#5.5.6 循环中else子句
#原始方案
from math import sqrt
break_out = False
for n in range(99, 81, -1):
	root = sqrt(n)
	if root == int(root):
		break_out = True
		print n
		break

if not break_out:
	print "Didn‘t find it!"

#结果Didn‘t find it!

#改进方案
from math import sqrt
for n in range(99, 81, -1):
	root = sqrt(n)
	if root == int(root):
		print n
		break
else:
	print "Didn‘t find it!"
#结果Didn‘t find it!

#列表推导式(list comprehension)--轻量级循环

>>> [x*x for x in range(10)]
[0, 1, 4, 9, 16, 25, 36, 49, 64, 81]
#能够和if子句联合使用
>>> [x*x for x in range(10) if x % 3 == 0]
[0, 9, 36, 81]
>>> [(x,y) for x in range(3) for y in range(3)]
[(0, 0), (0, 1), (0, 2), (1, 0), (1, 1), (1, 2), (2, 0), (2, 1), (2, 2)]
>>>
>>> result=[]
>>> for x in range(3):
	for y in range(3):
		result.append((x,y))

>>> result
[(0, 0), (0, 1), (0, 2), (1, 0), (1, 1), (1, 2), (2, 0), (2, 1), (2, 2)]

>>> girls=[‘alice‘, ‘bernice‘, ‘clarice‘]
>>> boys=[‘chris‘, ‘arnold‘, ‘bob‘]
>>> [b+‘+‘+g for b in boys for g in girls if b[0] == g[0]]
[‘chris+clarice‘, ‘arnold+alice‘, ‘bob+bernice‘]

#更优方案
>>> girls = [‘alice‘, ‘bernice‘, ‘clarice‘]
>>> boys = [‘chris‘, ‘arnold‘, ‘bob‘]
>>> letterGirls={}
>>> for girl in girls:
	letterGirls.setdefault(girl[0], []).append(girl)

>>> print [b+‘+‘+g for b in boys for g in letterGirls[b[0]]]
[‘chris+clarice‘, ‘arnold+alice‘, ‘bob+bernice‘]
>>> letterGirls
{‘a‘: [‘alice‘], ‘c‘: [‘clarice‘], ‘b‘: [‘bernice‘]}

#5.7 三人行 pass, del 和 exec

#5.7.1 什么都没发生

>>> name = ‘Bill Gates‘
>>> if name == ‘Ralph Auldus Melish‘:
...     print ‘Welcome!‘
... elif name == ‘Enid‘:
...     pass
... elif name == ‘Bill Gates‘:
...     print ‘Access Denied‘
...
Access Denied

#5.7.2 使用del删除
>>> scoundrel = {‘age‘:42, ‘first name‘:‘Robin‘, ‘last name‘:‘of Locksley‘
>>> robin = scoundrel
>>> scoundrel
{‘last name‘: ‘of Locksley‘, ‘first name‘: ‘Robin‘, ‘age‘: 42}
>>> robin
{‘last name‘: ‘of Locksley‘, ‘first name‘: ‘Robin‘, ‘age‘: 42}
>>> scoundrel = None
>>> scoundrel
>>> print scoundrel
None
>>> robin
{‘last name‘: ‘of Locksley‘, ‘first name‘: ‘Robin‘, ‘age‘: 42}
>>> x = 1
>>> y = x
>>> x=1
>>> del x
>>> x
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name ‘x‘ is not defined
>>> x = [‘Hello‘, ‘world‘]
>>> y = x
>>> y[1]=‘Python‘
>>> x
[‘Hello‘, ‘Python‘]
>>> del x
>>> y
[‘Hello‘, ‘Python‘]

#5.7.3 使用exec和eval运行和求值字符串
# exec 在 Python 3.0 中是一个函数而不是语句
>>> exec "print ‘Hello, world!‘"
Hello, world!
>>> from math import sqrt
>>> exec "sqrt=1"
>>> sqrt(4)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: ‘int‘ object is not callable

#命名空间,或称为作用域(scope)
#能够通过in <scope> 来实现, 当中的<scope>就是起到放置代码字符串命名空间作用的字典
>>> from math import sqrt
>>> scope={}
>>> exec ‘sqrt = 1‘ in scope
>>> sqrt(4)
2.0
>>> scope [‘sqrt‘]
1
>>> len(scope)
2
>>> scope.keys()
[‘__builtins__‘, ‘sqrt‘]
# eval--求值字符串
>>> eval(raw_input("Enter an arithmetic express: "))
Enter an arithmetic express: 6 + 18 * 2
42
>>> scope={}
>>> scope[‘x‘]=2
>>> scope[‘y‘]=3
>>> eval(‘x * y‘, scope)
6
>>> scope = {}
>>> exec ‘x=2‘ in scope
>>> eval(‘x*x‘, scope)
4
>>>

#5.8 小结
#打印--print语句能够用来打印由逗号隔开的多个值. 假设语句以逗号结尾,随后的print语句会在同一行内接续打印
#导入--能够用as对模块或函数提供别名
#赋值--通过 序列解包 和 链式赋值 功能, 多个变量能够一次性赋值, 通过 增量赋值 能够原地改变变量
#块--块是通过缩排使语句成组的一种方法. 块能够在条件以及循环语句中使用,也能够在函数和类中使用
#条件--几个条件能够串联使用if/elif/else. 另一个变体叫做条件表达式,形如a if b else c.
#断言--断言简单来说就是肯定某事(布尔表达式)为真, 也可在它后面跟上这么觉得的原因.
#循环--能够使用continue语句跳过块中的其它语句然后继续下一次迭代, 或使用break语句跳出循环
#    还能够选择在循环结尾加上else子句, 当没有运行循环内部的break语句时便会运行else子句中的内容.
#列表推导式--是看起来像循环的表达式.通过它, 能够从旧列表中产生新的列表, 对元素应用函数, 过滤掉不须要的元素,等等.
#pass, del, exec 和 eval 语句. pass语句什么都不做, 能够作为占位符使用. del语句用来删除变量(名称),或数据结构的一部分, 可是不能用来删除值.
#    exec语句用与运行Python程序同样的方式来运行字符串. 内建的eval函数对字符串中的表达式进行求值并返回结果.

#5.8.1 本章的新函数
#chr(n)		返回序数n所代表的字符的字符串(0<=n<=256)
#eval(source[, globals[, locals]])	将字符串作为表达式计算,而且返回值
#enumerate	产生用于迭代的(索引,值)对
#ord(c)		返回单字符字符串的int值
#range([start,] stop[, step])	创建整数的列表
#reversed(seq)	产生seq中值的反向副本, 用于迭代
#sorted(seq[, cmp][, key][, reverse])	返回seq中值排序后的列表副本
#xrange([start,] stop[, step]) 创建xrange对象用于迭代
#zip(seq1, seq2,...)	创建用于并行迭代的新序列
时间: 2024-12-21 05:11:38

Python基础教程之第5章 条件, 循环和其它语句的相关文章

Python基础教程之第2章 列表和元组

D:\>python Python 2.7.5 (default, May 15 2013, 22:43:36) [MSC v.1500 32 bit (Intel)] on win32 Type "help", "copyright", "credits" or "license" for more information. #2.1序列概览 >>> edward=['Edward Gumby', 4

Python基础教程之第3章 使用字符串

Python 2.7.5 (default, May 15 2013, 22:43:36) [MSC v.1500 32 bit (Intel)] on win32 Type "copyright", "credits" or "license()" for more information. #3.1基本字符串操作 >>> website = 'http://www.python.org' >>> websi

Python基础教程之List对象 转

Python基础教程之List对象 时间:2014-01-19    来源:服务器之家    投稿:root 1.PyListObject对象typedef struct {    PyObject_VAR_HEAD    PyObject **ob_item;    Py_ssize_t allocated;} PyListObject; PyObject_VAR_HEAD中的obsize表示该list对象含有的元素个数, 而allocated表示该list对象占用的内存空间. ob_item

Python基础教程(第五章 条件、循环和其他语句)

本文内容全部出自<Python基础教程>第二版,在此分享自己的学习之路. ______欢迎转载:http://www.cnblogs.com/Marlowes/p/5329066.html______ Created on Xu Hoo 读者学到这里估计都有点不耐烦了.好吧,这些数据结构什么的看起来都挺好,但还是没法用它们做什么事,对吧? 下面开始,进度会慢慢加快.前面已经介绍过了几种基本语句(print语句.import语句.赋值语句).在深入介绍条件语句和循环语句之前,我们先来看看这几种基

《python基础教程》第5章 条件、循环和其他语句 读书笔记

第五章:条件.循环和其他语句 1. bool():bool函数能够返回参数的布尔类型True或者False. 2. 相等运算符是两个等号 ==. 3. is 是同一性运算符. 4. 布尔运算符:and,or,not 5. a if b else:如果b为真,则返回a,否则,返回b. 6. 关键字assert,在条件必须为真的情况下,程序才能正常工作,否则出现异常. 7. range函数能够创建一个序列,第一个参数是下限(包含),第二个参数是上限(不包含). 8. xrange函数和range函数

python基础教程第5章——条件循环和其他语句

1.语句块是在条件为真(条件语句)时执行或者执行多次(循环语句)的一组语句.在代码前放置空格来缩进语句即可穿件语句块.块中的每行都应该缩进同样的量.在Phyton中冒号(:)用来标识语句块的开始,块中的每个语句都是缩进的(缩进量相同).当会退到和已经闭合的快一样的缩进量时,就表示当前块已经结束了. 2.布尔值:False None 0 "" () [] {}也就是说标准值False和None.所有类型的数字0(包括浮点型 长整型 和其他类型)空序列(比如空字符串.元组.列表)以及的空字

Python基础教程之import和from...import

一般使用import和from...import...导入模块. 以下述spam.py内的文件代码为例. ''' 遇到问题没人解答?小编创建了一个Python学习交流QQ群:857662006 寻找有志同道合的小伙伴,互帮互助,群里还有不错的视频学习教程和PDF电子书! ''' # spam.py print('from the spam.py') money = 1000 def read1(): print('spam模块:', money) def read2(): print('spam

Python基础(一)简介&amp;入门&amp;条件&amp;循环

一.机器语言(解释型语言和编译型语言)介绍 1.编译型语言:编译型语言即把写好的编程编译成机器语言再执行,在运行之前一次性编译,比如C.C++. 优点:运行速度快,缺点:编译时间长 2.解释型语言:在运行的时候才编译,执行一行编译一行,像Python.ruby.PHP.Java等 优点:编译时间短,什么时候用什么时候编译:缺点:速度慢 二.Python简介 Python是一种面向对象的解释型语言,简单易学,第三方库最多的一门语言 Python的用处:后台开发.自动化运维.爬虫.人工智能.数据分析

Python核心编程基础教程之Python运算符、运算符优先级、表达式简介--20150717

Python核心编程基础教程之Python运算符.运算符优先级.表达式简介 1.Python运算符与表达式: (1)认识Pyhton运算符1:什么是运算符 在Python运算中,有时候我们需要对一个或者多个数字或者一个或者多个字符串进行运算操作,*,+ (2)认识Pyhton运算符2:运算符有哪些以及运算符的使用方法 + :加 - :减 * :乘 / :除 ** :幂 < :小于 > :大于 != :不等于 // :求相除的整数部分 % :求相除的余数部分 & :按位与 | :按位或