iis服务器助手广告广告
返回顶部
首页 > 资讯 > 后端开发 > Python >Python基础语法介绍(3)
  • 759
分享到

Python基础语法介绍(3)

语法基础Python 2023-01-31 07:01:39 759人浏览 八月长安

Python 官方文档:入门教程 => 点击学习

摘要

基本概念、特性 顺序存储相同/不同类型的元素 定义:使用()将元素括起来,元素之间用“,”括开 特性:不可变,不支持添加,修改,删除等操作 查询:通过下标查询元组指定位置的元素 其他 空元组定义:non_tuple = () 只包含一

基本概念、特性

  • 顺序存储相同/不同类型的元素
  • 定义:使用()将元素括起来,元素之间用“,”括开
  • 特性:不可变,不支持添加,修改,删除等操作
  • 查询:通过下标查询元组指定位置的元素
  • 其他
    • 空元组定义:non_tuple = ()
    • 只包含一个元素的元组:one_tuple = ("one",)

顺序存储相同/不同类型的元素

user_info = ("Wukong",  100, "male", "13834928470")

元组不同于列表,它不支持增,删,改。

#不支持增删改操作,例如删除一个元组元素
del user_info[1]

输出结果:
    del user_info[1]
TypeError: 'tuple' object doesn't support item deletion

通过下标查询元素

#查询下标1的元素
age = user_info[1]
print("Age is %d"%age)

遍历元组

for item in user_info:
    print (item, end = "")

输出结果:
Wukong
100
male
13834928470

基本概念、特性

  • 存储key-value键值对类型的数据
  • 定义:{key:value, key:value, ...};字典里不能出现相同的键名
  • 特性:具有增删改操作
  • 查询:根据key查找value
  • 内置方法:get,keys,values,items,clear
  • 循环遍历字典

内置方法keys的用法

user_info_dict = {"name":"zhangsan", "age":"30", "tel":"13523464219"}
for key in user_info_dict.keys(): #key值组成的列表
    print(user_info_dict[key])

输出结果:
zhangsan
30
13523464219

内置方法items的用法

#用法(1)
for item in user_info_dict.items():
    print(type(item))
    print(item[0]) #key
    print(item[1]) #value

输出结果:
<class 'tuple'>
name
zhangsan
<class 'tuple'>
age
30
<class 'tuple'>
tel
13523464219
#用法(二)
for key, value in user_info_dict.items():
    print(key)
    print(value)

输出结果:
zhangsan
age
30
tel
13523464219

基本概念、特性

  • 无序存储不同数据类型不重复元素的序列
  • 定义:{“element1”,“element2”,element3“}
  • 特性:无序存储,可以对元素列表去重
  • 方法:add,update(序列),remove,pop
  • 集合操作:
    • 交集:intersection
    • 并集:uNIOn
    • 差集:difference
    • 对称差集:symmetric_difference

集合对列表去重

id_list = ["id1", "id2", "id3", "id1", "id2"]
distinct_set = set(id_list) #去重
print(distinct_set)

输出结果:
{'id1', 'id2', 'id3'}

集合对字符去重

string_set = set("hello")
print(string_set) #字符串看成是带下标的列表,字符串会拆开然后列表去重

输出结果:
{'h', 'o', 'e', 'l'} 

Note:set是无序的。所以你再运行一次,列表的元素顺序是变化的。

空集合

none_dict = {} #创建一个空字典
none_set = set() #创建一个空集合
print(none_set)

输出结果:
set()

in 和not in

user_id = "id1"
if user_id in distinct_set:
    print(user_id)
else:
    print("not find")

输出结果:
id1

add:添加元素到集合

name_set = {"zhangsan", "lisi"}
name_set.add("wangwu")
print(name_set)

输出结果:
{'lisi', 'wangwu', 'zhangsan'}

update(序列)

name_set.update(["wukong", "lisi", "bajie"]) #列表中的每个元素去重后添加到set里
print(name_set)

输出结果:
{'wukong', 'bajie', 'lisi', 'zhangsan', 'wangwu'}

函数定义

def FunName (parameter_list)
    function block
    return value

例子一:有参数有返回

def Sum(x, y):
    sum = x + y
    return sum

#函数调用
sum = Sum(1, 2)
print(sum)

输出结果:
3

例子二:有多个返回

def x_y_comp_tuple(x, y):
    res1 = x + y
    res2 = x * y
    return res1, res2

a, b = x_y_comp_tuple(2, 3)
print("a = {}, b = {}".fORMat(a, b))

输出结果:
a = 5, b = 6

例子三:列表作为返回值

 稍后填充

find(str[, start, end])

line = "hello world hello python"
print(line.find("world"))
print(line.find("hello"))
print(line.find("hello", 6)) #查找范围从索引”6“开始

输出结果:
6
0
12

count(str[, start, end])

print(line.count("hello")) #查找文本的某个字段或者某个字符串中某个单词
输出结果:
2

replace(old, new[, count])

new_line = line.replace("hello", "hi", 1) #count不指定就全部替换
print(line)
print(new_line)
输出结果:
hello world hello Python
hi world hello python

split(sep[, maxsplit])

line.split(" ") #以空格作为分隔符,以列表方式返回
输出结果:
['hello', 'world', 'hello', 'python']

#指定分隔的个数
line.split(" ", 1)
输出结果:
['hello', 'world hello python']

startswith(prefix[, start, end])

endswith(suffix[, start, end])

upper:字符串所有字符大写

lower:字符串所有字符小写

--结束END--

本文标题: Python基础语法介绍(3)

本文链接: https://www.lsjlt.com/news/192074.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作