IT数码 购物 网址 头条 软件 日历 阅读 图书馆
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
图片批量下载器
↓批量下载图片,美女图库↓
图片自动播放器
↓图片自动播放器↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁
 
   -> Python知识库 -> 《Python Crash Course》导读(Chapter 4) -> 正文阅读

[Python知识库]《Python Crash Course》导读(Chapter 4)

Code:[ZachxPKU/Python Crash Course]

Chapter 4 Working with lists

4.1 Looping through an entire list


magicians = ['alice', 'david', 'carolina']
for magician in magicians:
    print(magician)

4.1.1 A closer look at looping

4.1.2 Doing more work within a for loop


magicians = ['alice', 'david', 'carolina']
for magician in magicians:
    print(f"{magician.title()}, that was a great trick!")

magicians = ['alice', 'david', 'carolina']
for magician in magicians:
    print(f"{magician.title()}, that was a great trick!")
    print(f"I can't wait to see your next trick, {magician.title()}.\n")

4.1.3 Doing something after a for loop


magicians = ['alice', 'david', 'carolina']
for magician in magicians:
    print(f"{magician.title()}, that was a great trick!")
    print(f"I can't wait to see your next trick, {magician.title()}.\n")

print("Thank you, everyone. That was a great magic show!")

4.2 Avoiding Indentation Errors

4.2.1 Forgetting to indent


magicians = ['alice', 'david', 'carolina']
for magician in magicians:
print(magician)

4.2.2 Forgetting to indent additional lines


magicians = ['alice', 'david', 'carolina']
for magician in magicians:
    print(f"{magician.title()}, that was a great trick!")
print(f"I can't wait to see your next trick, {magician.title()}.\n")

4.2.3 Indenting unnecessarily


message = "Hello Python world!"
    print(message)

4.2.4 Indenting unnecessarily after the loop


magicians = ['alice', 'david', 'carolina']
for magician in magicians:
    print(f"{magician.title()}, that was a great trick!")
    print(f"I can't wait to see your next trick, {magician.title()}.\n")

    print("Thank you, everyone. That was a great magic show!")

4.2.5 Forgetting the colon


magicians = ['alice', 'david', 'carolina']
for magician in magicians
    print(magician)

TRY IT YOURSELF

  • pizzas.py
  • animals.py

4.3 Making Numerical Lists

4.3.1 Using the range() function


for value in range(1, 5):
    print(value)

for value in range(1, 6):
    print(value)

for value in range(6):
    print(value)

4.3.2 Using range() to make a list of numbers


numbers = list(range(1,6))
print(numbers)

even_numbers = list(range(2, 11, 2))
print(even_numbers)

squares = []
for value in range(1, 11):
    square = value ** 2
    squares.append(square)
    
print(squares)

squares = []
for value in range(1, 11):
    squares.append(value ** 2)
    
print(squares)

4.3.3 Simple statistics with a list of numbers


digits = list(range(0, 10))
min(digits)

max(digits)

sum(digits)

4.3.4 List comprehensions


squares = [value ** 2 for value in range(1, 11)]
print(squares)

TRY IT YOURSELF

  • counting_to_twenty.py
  • one_million.py
  • summing_a_million.py
  • odd_numbers.py
  • threes.py
  • cube.py
  • cube_comprehension.py

4.4 Working with part of a list

4.4.1 Slicing a list


players = ['charles', 'martina', 'michael', 'florence', 'eli']
print(players[0:3])

players = ['charles', 'martina', 'michael', 'florence', 'eli']
print(players[1:4])

players = ['charles', 'martina', 'michael', 'florence', 'eli']
print(players[:4])

players = ['charles', 'martina', 'michael', 'florence', 'eli']
print(players[3:])

players = ['charles', 'martina', 'michael', 'florence', 'eli']
print(players[-3:])

You can include a third value in the brackets indicating a slice. If a third value is included, this tells Python how many items to skip between items in the specified range


players = ['charles', 'martina', 'michael', 'florence', 'eli']
print(players[::2])

4.4.3 Looping through a slice


players = ['charles', 'martina', 'michael', 'florence', 'eli']

print("Here are the first three players on my team:")
for player in players[:3]:
    print(player.title())

4.4.3 Copy a list


my_foods = ['pizza', 'falafel', 'carrot cake']
friend_foods = my_foods[:]

print("My favorite foods are:")
print(my_foods)

print("\nMy friend's favorite foods are:")
print(friend_foods)

my_foods = ['pizza', 'falafel', 'carrot cake']
friend_foods = my_foods[:]

my_foods.append('cannoli')
friend_foods.append('ice cream')

print("My favorite foods are:")
print(my_foods)

print("\nMy friend's favorite foods are:")
print(friend_foods)

my_foods = ['pizza', 'falafel', 'carrot cake']
friend_foods = my_foods

my_foods.append('cannoli')
friend_foods.append('ice cream')

print("My favorite foods are:")
print(my_foods)

print("\nMy friend's favorite foods are:")
print(friend_foods)

If we had simply set friend_foods equal to my_foods, we would not produce two separate lists.

TRY IT YOURSELF

  • slices.py
  • my_pizzas_your_pizzas.py
  • more_loops.py

4.5 Tuples

4.5.1 Defining a tuple


dimensions = (200, 50)
print(dimensions[0])
print(dimensions[1])

dimensions = (200, 50)
dimensions[0] = 250

my_t = (3,)
print(my_t)

my_t = (3)
print(my_t)

Tuples are technically defined by the presence of a comma; the parentheses make them look neater and more readable. If you want to define a tuple with one element, you need to include a trailing comma.

4.5.2 Looping Through all in a tuple


dimensions = (200, 50)
for dimension in dimensions:
    print(dimension)

4.5.3 Writing over a tuple


dimensions = (200, 50)
print("Original dimensions:")
for dimension in dimensions:
    print(dimension)
    
dimensions = (400, 100)
print("\nModified dimensions:")
for dimension in dimensions:
    print(dimension)

TRY IT YOURSELF

  • buffet.py

4.6 Styling Your Code

4.6.1 The style guide

4.6.2 Indentation

4.6.3 Line Length

4.6.4 Blank Lines

4.6.5 Other style guideline

TRY IT YOURSELF

PEP 8

4.7 Summary

  Python知识库 最新文章
Python中String模块
【Python】 14-CVS文件操作
python的panda库读写文件
使用Nordic的nrf52840实现蓝牙DFU过程
【Python学习记录】numpy数组用法整理
Python学习笔记
python字符串和列表
python如何从txt文件中解析出有效的数据
Python编程从入门到实践自学/3.1-3.2
python变量
上一篇文章      下一篇文章      查看所有文章
加:2022-09-24 20:53:29  更:2022-09-24 20:54:44 
 
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁

360图书馆 购物 三丰科技 阅读网 日历 万年历 2024年12日历 -2024/12/26 13:30:51-

图片自动播放器
↓图片自动播放器↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
  网站联系: qq:121756557 email:121756557@qq.com  IT数码
数据统计