随笔 - 404  文章 - 4  评论 - 0  阅读 - 25万

day02-Python基础

>>> if a > b:
... c = a+b
... else:
... c = a-b
...
>>> c
-1

三元运算:

>>> c = a+b if a>b else a-b
>>> c
-1

1:列表、元组

列表

1
2
3
4
5
6
7
>>> names = ['apple','banana','orange','watermelon']
>>> names[0]
'apple'
>>> names[1]
'banana'
>>> names[-1]
'watermelon'

切片:取多个元素

复制代码
>>> names = ['apple','orange','banana','watermelon','persimmon']
>>> names[1:4]
['orange', 'banana', 'watermelon']
>>> names[1:-1]
['orange', 'banana', 'watermelon']
>>> names[:3]
['apple', 'orange', 'banana']
>>> names[3:]
['watermelon', 'persimmon']
>>> names[3:-1]
['watermelon']
>>> names[0::2]  #2代表,每隔一个元素,就取一个
['apple', 'banana', 'persimmon']
>>> names[::2]
['apple', 'banana', 'persimmon']
>>>
View Code
复制代码
追加:
复制代码
>>> names
['apple', 'orange', 'banana', 'watermelon', 'persimmon']
>>> names.append('litchi')
>>> names
['apple', 'orange', 'banana', 'watermelon', 'persimmon', 'litchi']
View Code
复制代码
 插入:
复制代码
>>> names
['apple', 'orange', 'banana', 'watermelon', 'persimmon', 'litchi']
>>> names.insert(2,"从banana前插入")
>>> names
['apple', 'orange', '从banana前插入', 'banana', 'watermelon', 'persimmon', 'litchi']
>>> names.insert(4,'从banana后添加')
>>> names
['apple', 'orange', '从banana前插入', 'banana', '从banana后添加', 'watermelon', 'persimmon', 'litchi']
View Code
复制代码

 修改:

复制代码
>>> names
['apple', 'orange', '从banana前插入', 'banana', '从banana后添加', 'watermelon', 'persimmon', 'litchi']
>>> names[1]="orange2"
>>> names
['apple', 'orange2', '从banana前插入', 'banana', '从banana后添加', 'watermelon', 'persimmon', 'litchi']
View Code
复制代码

删除:

复制代码
>>> names
['apple', 'orange2', '从banana前插入', 'banana', '从banana后添加', 'watermelon', 'persimmon', 'litchi']
>>> del names[2]
>>> names
['apple', 'orange2', 'banana', '从banana后添加', 'watermelon', 'persimmon', 'litchi']
>>> del names[3]
>>> names
['apple', 'orange2', 'banana', 'watermelon', 'persimmon', 'litchi']
>>> names.remove("orange2") #删除指定元素
>>> names
['apple', 'banana', 'watermelon', 'persimmon', 'litchi']
>>> names.pop()    #删除列表最后一个值
'litchi'
>>> names
['apple', 'banana', 'watermelon', 'persimmon']
View Code
复制代码

扩展:

复制代码
>>> names
['apple', 'banana', 'watermelon', 'persimmon']
>>> b = [1,2,3]
>>> names.extend(b)
>>> names
['apple', 'banana', 'watermelon', 'persimmon', 1, 2, 3]
View Code
复制代码

拷贝:

复制代码
>>> names
['apple', 'banana', 'watermelon', 'persimmon', 1, 2, 3]
>>> names_copy = names.copy()
>>> names_copy
['apple', 'banana', 'watermelon', 'persimmon', 1, 2, 3]
View Code
复制代码

统计:

复制代码
>>> names
['apple', 'banana', 'watermelon', 'persimmon', 1, 2, 3]
>>> names.count("apple")
1
View Code
复制代码

排序&翻转:

复制代码
>>> names
['apple', 'banana', 'persimmon', 'watermelon', 1, 2, 3]
>>> names.sort()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: '<' not supported between instances of 'int' and 'str'
>>> names[-3] = '1'
>>> names[-2] = '2'
>>> names[-1] = '3'
>>> names
['apple', 'banana', 'persimmon', 'watermelon', '1', '2', '3']
>>> names.sort()
>>> names
['1', '2', '3', 'apple', 'banana', 'persimmon', 'watermelon']
>>> names.reverse()
>>> names
['watermelon', 'persimmon', 'banana', 'apple', '3', '2', '1']
View Code
复制代码

获取下标:

复制代码
>>> names
['watermelon', 'persimmon', 'banana', 'apple', '3', '2', '1']
>>> names.index("banana")
2
View Code
复制代码

元组

元组:只有两个方法:count、index

1
names = ("oracle","mysql","db2")
复制代码
>>> names = ("oracle","mysql","db2")
>>> names.index("mysql")
1
>>> names.count("db2")
1
View Code
复制代码

元组

特性:不可修改 

复制代码
>>> name = "Guido" 
>>> name.capitalize()  #首字母大写
'Guido'
>>> name.casefold()  #大写全部转换成小写
'guido'
>>> name.center(50,"-") 
'----------------------Guido-----------------------'
>>> name.count('do') #统计do出现的次数
1
>>> name.encode() #将字符串编码成bytes格式
b'Guido'
>>> name.endswith("ui")  #判断字符串是否以ui结尾
>>> "Gui\tdo".expandtabs(10)  #将\t转换成多长的空格
'Gui       do'
False
>>> name.find('G') 查找A,找到返回其索引,找不到返回-1
0
View Code
复制代码

 2:字符串操作

 特性-不可修改

复制代码

>>> name = "Guido"
>>> name.capitalize() #首字母大写
'Guido'
>>> name.casefold() #大写全部转换成小写
'guido'
>>> name.center(50,"-")
'----------------------Guido-----------------------'
>>> name.count('do') #统计do出现的次数
1
>>> name.encode() #将字符串编码成bytes格式
b'Guido'
>>> name.endswith("ui") #判断字符串是否以ui结尾
>>> "Gui\tdo".expandtabs(10) #将\t转换成多长的空格
'Gui do'
False
>>> name.find('G') 查找A,找到返回其索引,找不到返回-1
0

format :
>>> msg = "my name is {},and age is {}"
>>> msg.format("James",58)
'my name is James,and age is 58'
>>> msg = "my name is {1},and age is {0}"
>>> msg.format("James",58)
'my name is 58,and age is James'
>>> msg = "my name is {name},and age is {age}"
>>> msg.format(age=22,name="james")
'my name is james,and age is 22'
>>>
format_map:
>>> msg.format_map({'name':'james','age':58})
'my name is james,and age is 58'
>>> msg.index('i')
8
>>> '10abc'.isalnum()
True
>>> '10'.isdigit()
True
>>> name = "Scott"
>>> name.isnumeric()
False
>>> name.isprintable()
True
>>> name.isspace()
False
>>> name.istitle()
True
>>> name.isupper()
False
>>> "|".join(['Guido','Python'])
'Guido|Python'
maketrans:
>>> intab = "linus" #This is the string having actual characters.
>>> outtab = "12345" #This is the string having corresponding mapping character
>>> trantab = str.maketrans(intab,outtab)
>>> str = "this is string example....wow!"
>>> str.translate(trantab)
'th25 25 5tr23g examp1e....wow!'


>>> msg
'my name is {name},and age is {age}'
>>> msg.partition('is')
('my name ', 'is', ' {name},and age is {age}')

>>> "beijing is beautiful city!".replace("beijing","Beijing",1)
'Beijing is beautiful city!'
>>> msg.swapcase()
'MY NAME IS {NAME},AND AGE IS {AGE}'
>>> msg.zfill(40)
'000000my name is {name},and age is {age}'
>>> name ="Hello,world!"
>>> name.ljust(40,"-")
'Hello,world!----------------------------'
>>> name.rjust(40,"-")
'----------------------------Hello,world!'

>>> b="guido_james_天才"
>>> b.isidentifier() #检测一段字符串可否被当作标志符,即是否符合变量命名规则
True

复制代码

 

 

 

posted on   HelonTian  阅读(159)  评论(0编辑  收藏  举报
编辑推荐:
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

点击右上角即可分享
微信分享提示