【py code everyday line 1】2023-02-27

本文介绍了在Python中如何使用列表,包括通过索引访问元素,使用title()方法格式化输出,以及通过append()、insert()、del语句、pop()和remove()方法进行添加、删除元素的操作。文章以学习者即将开始Coursera课程为背景,详细讲解了列表这一核心数据结构的基本用法。
摘要由CSDN通过智能技术生成

预计下周要开始Coursera的课程了,现在要快速推动line 1 的学习进度
line 2 进度是page 310(348/554)—Using a Colormap(20230227)

Chapter 3 Introducing lists

Lists are ordered collections, so you can access any element in a list by
telling Python the position, or index, of the item desired

bicycles = ['trec','canno','redline','speci']
print(bicycles[0])

you can format the element ‘trek’ to look more presentable by using the title() method

bicycles = ['trec','canno','redline','speci']
print(bicycles[0].title())
Trec

If you ask for the item at index -1, Python always returns the last item in the list

bicycles = ['trec','canno','redline','speci']
print(bicycles[-1].title())
Speci

This syntax is quite useful, because you’ll often want to access the last items in a list without knowing exactly how long the list is

you can use f-strings to create a message based on a value from a list

bicycles = ['trec','canno','redline','speci']
# print(bicycles[-1].title())
message = f"My first bicycle was a {bicycles[0].title()}."
print(message)
My first bicycle was a Trec.

For example, say we have a list of motorcycles and the first item in the
list is ‘honda’. We can change the value of this first item after the list has
been created:

motorcycle = ['honda','yamaha','suzuki']
print(motorcycle)
motorcycle[0]= 'ducati'
print(motorcycle)
['honda', 'yamaha', 'suzuki']
['ducati', 'yamaha', 'suzuki']

The simplest way to add a new element to a list is to append the item to the list
the new element is added to the end of the list

motorcycle = ['honda','yamaha','suzuki']
print(motorcycle)
motorcycle.append('ducati')
print(motorcycle)
['honda', 'yamaha', 'suzuki']
['honda', 'yamaha', 'suzuki', 'ducati']

The append() method makes it easy to build lists dynamically.
you can start with an empty list and then add items to the list using a series of append() calls

motorcycles = []
motorcycles.append('honda')
motorcycles.append('yamaha')
motorcycles.append('ducati')
print(motorcycles)
['honda', 'yamaha', 'ducati']

You can add a new element at any position in your list by using the insert() method

motorcycles = ['honda', 'yamaha', 'suzuki']

motorcycles.insert(0,'ducati')
print(motorcycles)
在这里插入代码片
['ducati', 'honda', 'yamaha', 'suzuki']

If you know the position of the item you want to remove from a list, you can
use the del statement:

motorcycles = ['honda', 'yamaha', 'suzuki']
print(motorcycles)

del motorcycles[0]
print(motorcycles)
['honda', 'yamaha', 'suzuki']
['yamaha', 'suzuki']

Sometimes you’ll want to use the value of an item after you remove it from a list
The pop() method removes the last item in a list, but it lets you work
with that item after removing it

The term pop comes from thinking of a list as a stack of items and popping one item off the top of the stack

Stack and pile are two words that can be used to describe a group of things.

A stack is a neat and orderly way of arranging things. For example, if you have some books, you could stack them one on top of the other, in a neat and tidy line.

A pile is a more messy and random way of arranging things. For example, if you have some books, you could pile them up in a heap on the floor, with no particular order or pattern.

motorcycles = ['honda', 'yamaha', 'suzuki']
print(motorcycles)

popped_motorcycle = motorcycles.pop()
print(motorcycles)
print(popped_motorcycle)

We start by defining and printing the list motorcycles .
Then we pop a value from the list, and assign that value to the variable popped_motorcycle .
We print the list to show that a value has been removed from the list.
Then we print the popped value to prove that we still have access to the
value that was removed.

['honda', 'yamaha', 'suzuki']
['honda', 'yamaha']
suzuki

You can use pop() to remove an item from any position in a list by including
the index of the item
you want to remove in parentheses

Parentheses are symbols that look like curved brackets (like this: “()”).

They are used to add extra information to a sentence. For example:

“The cat (who was called Fluffy) sat on the mat.”

In this sentence, the information in the parentheses is not essential
to understanding the sentence, but it adds more detail.

motorcycles = ['honda', 'yamaha', 'suzuki']

first_owned = motorcycles.pop(0)
print(f"The first motorcycle I owned was a {first_owned.title()}.")
The first motorcycle I owned was a Honda.

Remember that each time you use pop(), the item you work with is no
longer
stored in the list

If you’re unsure whether to use the del statement or the pop() method,
here’s a simple way to decide: when you want to delete an item from a list
and not use that item in any way, use the del statement; i
f you want to use an
item as you remove it, use the pop() method

An interactive development environment (IDE) is like a playground for computer coders. It’s a special place where you can write and test out computer programs. For example, if you wanted to create a computer game, you could use an IDE to write the code for the game, and then test it out to see if it works.

If you only know the value of the item you want to remove, you
can use the remove() method.

motorcycles = ['honda', 'yamaha', 'suzuki']
print(motorcycles)

motorcycles.remove('honda')
print(motorcycles)
['honda', 'yamaha', 'suzuki']
['yamaha', 'suzuki']

You can also use the remove() method to work with a value that’s being
removed from a list. Let’s remove the value ‘ducati’ and print a reason for
removing it from the list:

motorcycles = ['honda', 'yamaha', 'suzuki']
print(motorcycles)

too_expensive = 'honda'
motorcycles.remove(too_expensive)
print(motorcycles)
print(f"\nA {too_expensive.title()} is too expensive for me.")


['honda', 'yamaha', 'suzuki']
['yamaha', 'suzuki']

A Honda is too expensive for me.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值