Python编程:从⼊门到实践Python编程
第⼀章起步
1.1 搭建编程环境
Hello World程序。
>>>print("Hello world!")
Hello world!
第⼆章变量和简单数据类型
2.1 变量
>>> message = “Hello”
>print(message)
Hello
2.1.1 变量的命名和使⽤
变量名只能包含字母、数字和下划线(不能有空格),且数字不能打头。
不要将函数名作为变量名
慎⽤⼩写l和⼤写O,易被委任为1和0。习惯⽤⼩写字母命名变量
2.2 字符串
2.2.1 表⽰⽅法:单、双引号
2.2.2 修改字符串⼤⼩写 → .title() 改为⼤写 → .upper() 改为⼩写 → .lower()
2.2.3 合并(拼接)字符串 → +
>>> lastname ="Guo"
>firstname ="Dongling"
>fullname = lastname +" "+ firstname
>print("Hello,"+ fullname.title()+" !")
Hello,Guo Dongling !
2.2.4 使⽤制表符或换⾏符来添加空⽩ → \n \t
2.2.5 删除末尾空⽩ → .rstrip() 删除开头空⽩ → .lstrip() 同时删除开头和结尾空⽩ → strip() 2.3 数字
2.3.1 整数
>>>2+3*4
14
>>>(2+3)*4
20
2.3.2 浮点数
网站音乐代码
2.3.3 使⽤函数str()避免类型错误(将数字转换为字符串)
2.4 Python之禅
>>>import this
>Beautiful is better than ugly.
Explicit is better than implicit.
Simple is better than complex.
Complex is better than complicated.
Flat is better than nested.
Sparse is better than dense.
Readability counts.
Special cases aren't special enough to break the rules.
Although practicality beats purity.
Errors should never pass silently.
Unless explicitly silenced.
In the face of ambiguity, refuse the temptation to guess.
There should be one--and preferably only one --obvious way to do it.
Although that way may not be obvious at first unless you're Dutch.
Now is better than never.
Although never is often better than *right* now.
If the implementation is hard to explain, it's a bad idea.
If the implementation is easy to explain, it may be a good idea.
Namespaces are one honking great idea -- let's do more of those!
第三章列表简介
3.1 列表是什么
列表是由⼀系列按特定顺序排列的元素组成的,⽤[ ]来表⽰列表,并⽤逗号来分割其中的元素。
3.1.1 访问列表元素,索引是从0开始
>>> bicycle =['trek','cannodale','redline']
>print(bicycle)
['trek','cannodale','redline']
>print(bicycle[0])
学python编程入门trek
>print(bicycle[-1])
redline
3.2 修改、添加和删除元素
3.2.1 修改列表元素
3.2.2 使⽤⽅法append列表末尾添加元素; 使⽤⽅法list列表中添加元素
3.3.3 从列表中删除元素:①使⽤del语句删除元素(已知元素的索引) ②使⽤⽅法pop删除列表中任何位置的元素(已知元素的索引,删除后可继续使⽤它的值)③使⽤⽅法remove删除元素(已知元素的值,删除后可继续使⽤它的值)
使⽤del和pop()的判断标准:如果你要从列表中删除⼀个元素,且不再以任何⽅式使⽤它,就是要del语句;如果你要在删除元素后继续使⽤它,就是⽤pop()
使⽤remove()只删除第⼀个指定的值。如果要删除所有的,要是⽤循环来判断
>>> bicycle =['trek','cannodale','redline']
>del bicycle[0]
>print(bicycle)
['cannodale','redline']
>>>bicycle =['trek','cannodale','redline']
>first_owned = bicycle.pop(0)
>print(bicycle)
>print(first_owned)
['cannodale','redline']
trek
>>>bicycle =['trek','cannodale','redline']
&ve('cannodate')
>print(bicycle)
['trek','redline']
3.3 组织列表
3.3.1 使⽤⽅法sort对列表进⾏永久性排序(按照字母顺序排列)
3.3.2 使⽤函数sorted对列表进⾏临时排序(按照字母顺序排列)
调⽤sorted(),列表元素的排列顺序没有改变
3.3.3 使⽤⽅法reverse永久性倒序打印列表
3.3.4 使⽤函数len确定列表的长度
⽅法与函数的区别⽅法:list.sort() 函数:sorted(list)
第四章操作列表
4.1 遍历整个列表
>>>magicians =['alice','david','carlina']
>sort_magicians =sorted(magicians)
>for magician in sort_magicians:
print(magician.title()+",that was a great trick!")
print("I can't wait to see your next trick,"+ magician.title()+"\n")activewear是什么意思
print("Thank you, everyone. That was a great magic show!")百度在线翻译
Alice,that was a great trick!
I can't wait to see your next trick,Alice
Carlina,that was a great trick!
I can't wait to see your next trick,Carlina
David,that was a great trick!
I can't wait to see your next trick,David
Thank you, everyone. That was a great magic show!
使⽤for循环式要避免缩进错误!
4.2 创建数字列表
4.2.1 使⽤函数range(差⼀⾏为)
>>>squares =[]
for value in range(1,11):
square = value**2
squares.append(square)
print(squares)
[1,4,9,16,25,36,49,64,81,100]
>>>squares =[]
for value in range(1,11):
squares.append(value**2)
print(squares)
[1,4,9,16,25,36,49,64,81,100]
创建更复杂的列表时,可使⽤上述两种⽅法中的任何⼀种。有时候,使⽤临时变量会让代码更易读;⽽在其他情况下,这样做只会让代码⽆谓地变长。你⾸先应该考虑的是编写清晰易懂且能完成所需功能的代码。等到审核代码时,再考虑采⽤更⾼效的⽅法
4.2.2 对列表执⾏简单的统计计算
函数min、max、sum
4.2.3 列表解析
将for循环和创建新元素的代码合并成⼀⾏,并主动附加新元素。
>>>squares =[value**2for value in range(1,11)]
print(squares)
[1,4,9,16,25,36,49,64,81,100]
4.3 使⽤列表的⼀部分——切⽚
同样存在差⼀⾏为
手机创建网站
>>>my_foods =['noodles','pizza','cake']
friend_foods = my_foods[:]
print(my_foods)
['noodles','pizza','cake']
4.4 元组java面试题在哪里刷题
列表⾮常适合⽤于存储在程序运⾏期间可能变化的数据集。列表是可以修改的,这对处理⽹站的⽤户列表或游戏中的⾓⾊列表⾄关重要。然⽽,有时候你需要创建⼀系列不可修改的元素,元组可以满⾜这种需求。Python将不能修改的值称为不可变的,⽽不可变的列表被称为元组。
元组⽤()来表⽰
dimensions =(200,50)
for dimension in dimensions:
print(dimension)
200
50
元组中的元素虽然不能改变,但是可以直接修改元组变量。
4.5 设置代码格式
4.5.1 缩进 每级缩进使⽤4个空格,避免混⽤制表符和空格
4.5.2 ⾏长 每⾏不超过80个字符,注释的⾏长不超过72个字符
4.5.3 空⾏ 可以使⽤空⾏来将程序的不同部分分开,但不能滥⽤
第五章 if语句
5.1 条件测试
>>>cars =["audi","bmw","subaru","toyota"]
for car in cars:
if car =="bmw":
print(car.upper())
else:
print(car.title())
Audi
BMW
Subaru
Toyota
5.1.1检查是否相等 使⽤两个等号"=="来进⾏判断
⼀个等号表⽰陈述,可解读为“将变量进⾏赋值”;两个等号表⽰发问,可解读为“变量的值是??吗”
如果在判断时想要忽略⼤⼩写,可以使⽤⽅法lower()
5.1.2 检查是否不相等 使⽤“!=”来进⾏判断
5.1.3 ⽐较数字
建议==、>=、<=等⽐较运算符两边各添加⼀个空格,可以让代码阅读起来更容易
5.1.4 使⽤and、or检查多个条件
5.1.5 使⽤in检查特定值是否包含在列表中
5.2 if语句
if-elif-else语句 如果判断条件较多,可以使⽤多个elif代码块,else代码块可以省略
5.3 使⽤if语句处理列表
#确定列表不是空的
>>>request_toppings =[]
>if request_toppings:
for request_topping in request_toppings:
print("addling"+ request_topping)
print("\nfinish making your pizza!")
else:
print("Are you sure you want a plain pizza?")
第六章字典
在Python中,字典是⼀系列键-值对。每个键都与⼀个值相关联,你可以使⽤键对来访问与之相关联的值。与键相关联的值可以是数字、字符串、列表乃⾄字典,事实上,可将任何Python对象⽤作字典中的值。⽤花括号{ }来表⽰,相对应的键-值对之间⽤逗号分隔。
6.1 修改、删除字典中的值
#修改字典中的值
>>>alien ={
'x_position':0,
'y_position':25,
'speed':'medium',
}
>print("Alien's original position is ("+str(alien['x_position'])+","+str(alien['y_position'])+")")
>if alien['speed']=='slow':
x_increament =1
elif alien['speed']=='medium':
x_increament =2
else:
x_increament =3
>alien['x_position']= alien['x_position']+ x_increament
>print("Alien now is on ("+str(alien['x_position'])+","+str(alien['y_position'])+")")
Alien's original position is(0,25)
Alien now is on (2,25)
#删除字典中的值
del alien['speed']
6.2 遍历字典
使⽤⽅法items、keys、values遍历键对值或键
可以使⽤⽅法set()来去除列表中的重复元素

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。