Python编程基础知识点

本文详细介绍了Python编程的基础知识,包括字符串操作、列表管理、字典应用等核心内容,并提供了丰富的示例代码帮助理解。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1. 修改字符串的大小写

name = "ada loveLace"
print(name.title())   # 首字母大写,其余字母小写
print(name.lower())   # 字母全都小写
print(name.upper())   # 字母全都大写

结果为:

Ada Lovelace
ada lovelace
ADA LOVELACE

2. 合并字符串用“+”即可

first_word = "Hello! "
second_word = "Dancy."
print(first_word + second_word)

结果为:Hello! Dancy.


3. 使用“\t”来增加空白格

print("\tpython")

结果为:

	python

4. 删除空白格

language = ' python '    # 左右两边都有空白
print(language.strip())   # 删除所有空格
print(language.rstrip())  # 删除右边的空格
print(language.lstrip())  # 删除左边的空格

结果为:

python
 python
python 

5. 列表中插入、删除元素

a. insert插入元素

color = ["red", "yellow", "blue"]
color.insert(0, "green")  # 第一个参数为下标,第二个参数为需要插入的值
print(color)

结果为:['green', 'red', 'yellow', 'blue']

b. del删除任意位置的元素

color = ["red", "yellow", "blue"]
del(color[1]) 
print(color)

结果为:['red', 'blue']

b. pop删除元素

color = ["red", "yellow", "blue"]
print(color.pop())

结果为:blue

删除任意位置的元素:

color = ["red", "yellow", "blue"]
print(color.pop(1))

结果为:yellow

c. remove根据值删除元素

color = ["red", "yellow", "red", "blue"]
color.remove("red")
print(color)

结果为:['yellow', 'red', 'blue']


6. 列表排序

a. 使用sort进行永久排序

color = ["red", "yellow", "blue"]
color.sort()
print(color)

结果为:['blue', 'red', 'yellow']

sort和reverse结合一下:

color = ["red", "yellow", "blue"]
color.sort(reverse=True)
print(color)

结果为:['yellow', 'red', 'blue']

b. 使用sorted临时排序

color = ["red", "yellow", "blue"]
print("Here is the original list:",color)
print("Here is the sorted list:", sorted(color))
print("Here is the original list again:",color)

结果为:

Here is the original list: ['red', 'yellow', 'blue']
Here is the sorted list: ['blue', 'red', 'yellow']
Here is the original list again: ['red', 'yellow', 'blue']

7. reverse反转列表

color = ["red", "yellow", "blue"]
color.reverse()
print(color)

结果为:['blue', 'yellow', 'red']


8. 复制列表

a. 若直接复制,当原始列表或新列表中的数据发生改变时,新的列表或者原始列表也会发生改变。

color = ["green", "red", "blue"]
color2 = color
color.append("yellow")
print(color2)

结果为:['green', 'red', 'blue', 'yellow']

b. 为了让复制后,原始列表与复制后的列表直接无影响,那么复制操作如下:

color = ["green", "red", "blue"]
color2 = color[:]
color.append("yellow")
print(color2)

结果为:['green', 'red', 'blue']


9. 列表中的元素可修改,而元组中的元素无法修改


10. 字典添加键值对

color = {"1": "green", "2" : "red"}
color["3"] = "blue"
print(color)

结果为:{'1': 'green', '2': 'red', '3': 'blue'}


11. 删除字典中的键值对

color = {"1": "green", "2" : "red"}
del color["2"]
print(color)

结果为:{'1': 'green'}


12. 遍历字典中的元素

color = {"1": "green", "2":"red"}
for key, value in color.items():
    print("key:", key, end="")
    print("  value:", value)

结果为:

key: 1  value: green
key: 2  value: red

13. 使用input函数输入数据

message = input("Tell me something, and I will repeat it back to you: ")
print(message)

结果为:

Tell me something, and I will repeat it back to you: 123
123
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值