哈哈哈,今天达成在上理的第一个五公里成就,跑完之后雨就面带感动的来了……
接上次的第四章开始啦!
刚刚被这个代码疯狂的蹂躏了……哎~深刻的体会到编程中不同的缩进、不同的逻辑的代价了。
先上个正确的代码压压惊。
>>> squares=[]
>>> for i in range(1,11,2):squares.append(i**2)>>> print(squares)
[1, 9, 25, 49, 81]
错误的代码有很多。。
SyntaxError: multiple statements found while compiling a single statement
>>> squares=[]
>>> for value in range(1,11):square=value**2squares.append(square)print(squares)[1]
[1, 4]
[1, 4, 9]
[1, 4, 9, 16]
[1, 4, 9, 16, 25]
[1, 4, 9, 16, 25, 36]
[1, 4, 9, 16, 25, 36, 49]
[1, 4, 9, 16, 25, 36, 49, 64]
[1, 4, 9, 16, 25, 36, 49, 64, 81]
[1, 4, 9, 16, 25, 36, 49, 64, 81, 100]
#这个是逻辑的错误,差点得到了一个乘法表。。。这里的print放到了循环结构中了。。。>>> squares=[]
>>> for value in range(1,11):squares.append(value**2)print(squares)SyntaxError: unindent does not match any outer indentation level
>>> squares=[]
>>> for value in range(1,11):squares.append(value**2)>>> print(squares)SyntaxError: unexpected indent
>>> numbers=[]
>>> for number in range(1,11,2):numbers.append(number*2)
print(numbers)
SyntaxError: invalid syntax
#上面的都是缩进错误了。。想太多!
下面是一份精简的代码,很棒。
>>> squres=[i**2 for i in range(1,11,2)]
>>> print(squares)
[1, 9, 25, 49, 81]
直接将循环和创建新元素的代码合并到一行,并自动附加新元素。
所谓切片,即是列表中的部分元素的处理。
>>> numbers=[11,111,123,11234,11234]
>>> print(numbers[0:3])
[11, 111, 123]
>>> print(numbers[:4])
[11, 111, 123, 11234]
>>> print(numbers[:])
[11, 111, 123, 11234, 11234]
>>> print(numbers[-3:])
[123, 11234, 11234]
切片可以用于列表的复制。而直接新定义变量名来复制列表,会导致当有新元素添加到原列表时,复制的列表也会同时添加该元素,可能与原来的目的相悖。
所谓元组就是所含元素不可变的列表。列表非常适合用于存储在程序运行期间可能变化的数据集。而当需要创建一系列不可修改的元素时,元组即可满足这种需求。
元组用括号()来表示。列表用【】来表示。
>>> dimensions=(200,40)
>>> dimensions[0]=222
Traceback (most recent call last):File "
TypeError: 'tuple' object does not support item assignment
>>> #元组的不可修改的特性
身为一名有梦想的程序猿,必须要介绍下这个。
PHP8 Python Enhancement Prospal
即Python的格式规范
PEP 8 -- Style Guide for Python Code
目前已知的情报:PEP8建议每级缩进都使用四个空格;输入的代码每行不超过80字符;注释的行长不超过72字符;空行要考虑代码的连贯性与逻辑性。
if you_love_me is True :
print('I love you forever!')
else:
print('Time helps you! I wait!')
IF语句的核心都是一个值为True或者False的表达式,这种表达式成为条件测试。
== means 恒等于
!= means 不等于
and means 并
or means 或
<> <&#61; >&#61; 小于&#xff0c;大于&#xff0c;小于等于&#xff0c;大于等于
常用的条件语句有&#xff1a;
if——else if——elif if——elif——else if——if——if……
elif代码块可以有多个。
>>> age&#61;24
>>> if age<4:price&#61;0
elif age<18:price&#61;5
elif age<65:price&#61;10
else:price&#61;15>>> print(&#39;Your admisssion cost is $&#39;&#43;str(price)&#43;&#39;!&#39;)
Your admisssion cost is $10!
#这里也碰到过错误&#xff0c;需要考虑清楚缩进的位置&#xff01;
if代码块也可以有多个。
>>> good_night&#61;[&#39;see you tommorrow&#39;,&#39;well-dreamed&#39;,&#39;byebye&#39;]
>>> if&#39;see you tommorrow&#39;in good_night&#xff1b;
SyntaxError: invalid character in identifier
>>> if &#39;see you tommorrow&#39; in good_night:print(&#39;he&#39;)
if&#39;well-dreamed&#39; in good_night:SyntaxError: invalid syntax
>>> good_night&#61;[&#39;see you tommorrow&#39;,&#39;well-dreamed&#39;,&#39;byebye&#39;]
>>> if&#39;see you tommorrow&#39;in good_night:print(&#39;he&#39;)he
>>> if&#39;see you tommorrow&#39;in good_night:print(&#39;he&#39;)if &#39;well-dreamed&#39; in good_night:print(&#39;she&#39;)if &#39;byebye&#39; in good_night:print(&#39;he and she&#39;)he
she
he and she
如果只是想执行一个代码块&#xff0c;就使用if—elif—else结构&#xff1b;如果要运行多个代码块&#xff0c;就使用一系列独立的if语句。
if语句与循环语句的首次遇见&#xff1a;
>>> requested_toppings&#61;[&#39;mushrooms&#39;,&#39;green peppers&#39;,&#39;extra cheese&#39;]
>>> for requested_topping in requested_toppings:if requested_topping &#61;&#61; &#39;green peppers&#39;:print(&#39;Sorry,we are out of green peppers right now!&#39;)else:print(&#39;Adding &#39;&#43;requested_topping&#43;&#39;!&#39;)Adding mushrooms!
Sorry,we are out of green peppers right now!
Adding extra cheese!
所谓字典&#xff0c;就是一系列的键值对。用花括号{ }表示。一个键对应一个值。键和值之间用冒号分隔&#xff0c;而键—值对之间用逗号分隔。
Python不关心字典中每个键值对的顺序&#xff0c;只关心键值对之间的关联关系。
>>> aliens&#61;{}
>>> aliens[&#39;color&#39;]&#61;&#39;green&#39;
>>> aliens[&#39;points&#39;]&#61;5
>>> print(aliens)
{&#39;color&#39;: &#39;green&#39;, &#39;points&#39;: 5}
好了好了&#xff0c;迎接周三一天课的洗礼了&#xff01;明天再战&#xff01;
先到这了&#xff0c;纯小白下次再来&#xff01;
喜欢梅子酒&#xff0c;喜欢进步的你&#xff01;
附&#xff1a;输错的代码&#xff0c;提醒自己&#xff01;
Python 3.6.1 (v3.6.1:69c0db5, Mar 21 2017, 17:54:52) [MSC v.1900 32 bit (Intel)] on win32
Type "copyright", "credits" or "license()" for more information.
>>> squares&#61;[]
>>>
>>> for value in range(1,11):squares.append(value**2)
print(squares)
SyntaxError: invalid syntax
>>> squares&#61;[]
>>> for value in range(1,11):squares.append(value**2)print(squares)[1]
[1, 4]
[1, 4, 9]
[1, 4, 9, 16]
[1, 4, 9, 16, 25]
[1, 4, 9, 16, 25, 36]
[1, 4, 9, 16, 25, 36, 49]
[1, 4, 9, 16, 25, 36, 49, 64]
[1, 4, 9, 16, 25, 36, 49, 64, 81]
[1, 4, 9, 16, 25, 36, 49, 64, 81, 100]
>>> squares&#61;[]
>>> for value in range(1:11):SyntaxError: invalid syntax
>>> squares&#61;[]
>>> for value in range(1,11):squares.append(value**2)
print(squares)
SyntaxError: invalid syntax
>>> squares&#61;[]
>>> for value in range(1,11);
SyntaxError: invalid syntax
>>> squares&#61;[]
>>> for value in range(1,11):squares.append(value**2)>>> squares&#61;[]
>>> for value in range(1,11):squares.append(value**2)SyntaxError: multiple statements found while compiling a single statement
>>> squares&#61;[]
>>> for value in range(1,11):square&#61;value**2squares.append(square)print(squares)[1]
[1, 4]
[1, 4, 9]
[1, 4, 9, 16]
[1, 4, 9, 16, 25]
[1, 4, 9, 16, 25, 36]
[1, 4, 9, 16, 25, 36, 49]
[1, 4, 9, 16, 25, 36, 49, 64]
[1, 4, 9, 16, 25, 36, 49, 64, 81]
[1, 4, 9, 16, 25, 36, 49, 64, 81, 100]
>>> squares&#61;[]
>>> for value in range(1,11):squares.append(value**2)print(squares)SyntaxError: unindent does not match any outer indentation level
>>> squares&#61;[]
>>> for value in range(1,11):squares.append(value**2)>>> print(squares)SyntaxError: unexpected indent
>>> numbers&#61;[]
>>> for number in range(1,11,2):numbers.append(number*2)
print(numbers)
SyntaxError: invalid syntax
>>> squares&#61;[]
>>> for i in range(1,11,2):squares.append(i**2)>>> print(squares)
[1, 9, 25, 49, 81]
>>> squres&#61;[i**2 for i in range(1,11,2)]
>>> print(squares)
[1, 9, 25, 49, 81]
>>> numbers&#61;[11,111,123,11234,11234]
>>> print(numbers[0:3])
[11, 111, 123]
>>> print(numbers[:4])
[11, 111, 123, 11234]
>>> print(numbers[:])
[11, 111, 123, 11234, 11234]
>>> print(numbers[-3:])
[123, 11234, 11234]
>>> dimensions&#61;(200,40)
>>> dimensions[0]&#61;222
Traceback (most recent call last):File "
TypeError: &#39;tuple&#39; object does not support item assignment
>>>
>>> print(&#39;Welcome to section 5&#39;)
Welcome to section 5
>>> age&#61;24
>>> if age<10:price&#61;0>>> elif age<15:SyntaxError: invalid syntax
>>> age&#61;24
>>> if age<10:price&#61;0>>> elif age<14:SyntaxError: invalid syntax
>>> age&#61;24
>>> if age<10:price&#61;0elif age<15:SyntaxError: invalid syntax
>>> age&#61;24
>>> if age<4:price&#61;0
elif age<18:price&#61;5
elif age<65:price&#61;10
else:price&#61;15>>> print(&#39;Your admisssion cost is $&#39;&#43;str(price)&#43;&#39;!&#39;)
Your admisssion cost is $10!
>>> good_night&#61;[&#39;see you tommorrow&#39;,&#39;well-dreamed&#39;,&#39;byebye&#39;]
>>> if&#39;see you tommorrow&#39;in good_night&#xff1b;
SyntaxError: invalid character in identifier
>>> if &#39;see you tommorrow&#39; in good_night:print(&#39;he&#39;)
if&#39;well-dreamed&#39; in good_night:SyntaxError: invalid syntax
>>> good_night&#61;[&#39;see you tommorrow&#39;,&#39;well-dreamed&#39;,&#39;byebye&#39;]
>>> if&#39;see you tommorrow&#39;in good_night:print(&#39;he&#39;)he
>>> if&#39;see you tommorrow&#39;in good_night:print(&#39;he&#39;)if &#39;well-dreamed&#39; in good_night:print(&#39;she&#39;)if &#39;byebye&#39; in good_night:print(&#39;he and she&#39;)he
she
he and she
>>> requested_toppings&#61;[&#39;mushrooms&#39;,&#39;green peppers&#39;,&#39;extra cheese&#39;]
>>> for requested topping in requested_toppings:SyntaxError: invalid syntax
>>> requested_toppings&#61;[&#39;mushrooms&#39;,&#39;green peppers&#39;,&#39;extra cheese&#39;]
>>> for requested_topping in requested_toppings:if requested_topping &#61;&#61; &#39;green peppers&#39;:print(&#39;Sorry,we are out of green peppers right now!&#39;)else:print(&#39;Adding &#39;&#43;requested_topping&#43;&#39;!&#39;)Adding mushrooms!
Sorry,we are out of green peppers right now!
Adding extra cheese!
>>> aliens&#61;{}
>>> aliens[&#39;color&#39;]&#61;&#39;green&#39;
>>> aliens[&#39;points&#39;]&#61;5
>>> print(aliens)
{&#39;color&#39;: &#39;green&#39;, &#39;points&#39;: 5}
>>>