python杂学

博客主要提及了列表、字典、集合的操作以及while相关内容,这些均为信息技术编程领域常见的操作和概念,对编程学习和实践有一定意义。

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

# 交换变量
a = 2
b = 4
a,b = b,a
print(a,b)
4 2
# 转义
str1 = r"我在\n华为"
str2 = "我在\n华为"

print(str1)

print()

print(str2)
我在\n华为

我在
华为
# 除法
print(10 / 3)
print(10 / 3.0)
print(10 // 3)
3.3333333333333335
3.3333333333333335
3
# 二维列表
list1 = []
for i in range(2):
    list1.append([])
    for j in range(3,10,2):
        list1[i].append(j)
print(list1)
print(list1[1][1])
[[3, 5, 7, 9], [3, 5, 7, 9]]
5
# 连接字符串:
str1 = "we are a warm"
str2 = "family"
print(str1 + " " + str2)

print()

str3 = [str1,str2]
print(' '.join(str3))
we are a warm family

we are a warm family
help(str.split)
Help on method_descriptor:

split(self, /, sep=None, maxsplit=-1)
    Return a list of the words in the string, using sep as the delimiter string.
    
    sep
      The delimiter according which to split the string.
      None (the default value) means split according to any whitespace,
      and discard empty strings from the result.
    maxsplit
      Maximum number of splits to do.
      -1 (the default value) means no limit.
# 拆分字符串到一个列表
str1.split(sep = " ")
['we', 'are', 'a', 'warm']
string1 = "believe,lie,lie"
string1.count("lie")
3
string1.find("lie")
2
names = ["磊","亚","涛","虎","光","敏","海","丹"]

操作列表

for name in names:
    print(name)
磊
亚
涛
虎
光
敏
海
丹
help(print)
Help on built-in function print in module builtins:

print(...)
    print(value, ..., sep=' ', end='\n', file=sys.stdout, flush=False)
    
    Prints the values to a stream, or to sys.stdout by default.
    Optional keyword arguments:
    file:  a file-like object (stream); defaults to the current sys.stdout.
    sep:   string inserted between values, default a space.
    end:   string appended after the last value, default a newline.
    flush: whether to forcibly flush the stream.
for name in names:
    print(name, end = " ")
磊 亚 涛 虎 光 敏 海 丹 
names.index("丹")
7
for index,name in enumerate(names):
    print(index,name)
0 磊
1 亚
2 涛
3 虎
4 光
5 敏
6 海
7 丹
for i in range(5):
    print(i,end = ' ')
print()
for j in range(1,4):
    print(j,end = ' ')
print()
for m in range(1,10,2):
    print(m,end = ' ')
0 1 2 3 4 
1 2 3 
1 3 5 7 9 
list2 = [x for x in range(9)]
list2
[0, 1, 2, 3, 4, 5, 6, 7, 8]
max(list2)
8
min(list2)
0
list2.count(8)
1
from collections import Counter
dict(Counter(list2))
{0: 1, 1: 1, 2: 1, 3: 1, 4: 1, 5: 1, 6: 1, 7: 1, 8: 1}
list2[1:3]  # 左闭右开
[1, 2]
list2[:]
[0, 1, 2, 3, 4, 5, 6, 7, 8]
list2[::2]
[0, 2, 4, 6, 8]
list2[-2:]
[7, 8]
list3 = [range(2,8)]
list4 = [range(9,10)]
if list3 == list4:
    print("yes")
else:
    print("no")
if 3 not in list3 and 7 not in list3:
    print("not in")
else:
    print("in")
# if
# elif
# else
no
not in

字典

AMP = {"虎":"男","磊":"男","丹":"女","亚":"女"}
AMP["虎"]
'男'
AMP["敏"] = "女"
AMP
{'丹': '女', '亚': '女', '敏': '女', '磊': '男', '虎': '男'}
AMP["亚"] = "男"
AMP
{'丹': '女', '亚': '男', '敏': '女', '磊': '男', '虎': '男'}
AMP_name = ["虎","磊","丹","亚"]
AMP_sex = ["男","男","女","女"]
dict1 = dict(zip(AMP_name,AMP_sex))
dict1
{'丹': '女', '亚': '女', '磊': '男', '虎': '男'}
dict1.get("丹",-1)
'女'
dict1.get("飞","AMP无此人")
'AMP无此人'
for key,value in AMP.items():
    print(key,value)
虎 男
磊 男
丹 女
亚 男
敏 女
for key in AMP.keys():   # 默认
    print(key)
虎
磊
丹
亚
敏
for value in AMP.values():
    print(value)
男
男
女
男
女

集合

import warnings
warnings.filterwarnings("ignore")
list5 = [1,2,4,6,8,2,6]
print(set(list5))
{1, 2, 4, 6, 8}
set1 = {1,4,7,9}
type(set1)
set
set2 = set()    # 无序
set2.add(5)
set2
{5}
set3 = {"c","c++","python","R"}
set4 = {"java","python"}
print(set3 | set4) # 并集
print(set3 & set4) # 交集
print(set3 - set4) # 差集,前者有后者没有
{'R', 'c++', 'java', 'python', 'c'}
{'python'}
{'R', 'c++', 'c'}

while

str2 = input("请输入:")
str2
请输入:6





'6'
str2 = int(input("请输入:"))
str2
请输入:6





6
i = 6
while i > 0:
    if i > 3:
        i -= 1
        continue
    print("end")
    i -= 1
end
end
end
list6 = []
text = input("请输入性别:")
while text != "q":
    list6.append(text)
    text = input("请输入性别:")
list6
请输入性别:女
请输入性别:男
请输入性别:q





['女', '男']
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值