栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

Python基础语法介绍

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Python基础语法介绍

元组

基本概念、特性

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

  • 定义:使用()将元素括起来,元素之间用“,”括开

  • 特性:不可变,不支持添加,修改,删除等操作

  • 查询:通过下标查询元组指定位置的元素

  • 其他

    • 空元组定义: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 = "")输出结果:Wukong100male13834928470
字典

基本概念、特性

  • 存储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])输出结果:zhangsan3013523464219

内置方法items的用法

#用法(1)for item in user_info_dict.items():    print(type(item))    print(item[0]) #key    print(item[1]) #value输出结果:namezhangsanage30tel13523464219
#用法(二)for key, value in user_info_dict.items():    print(key)    print(value)输出结果:zhangsanage30tel13523464219
集合

基本概念、特性

  • 无序存储不同数据类型不重复元素的序列

  • 定义:{“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, res2a, 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“开始输出结果:6012

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 pythonhi 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:字符串所有字符小写

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/224777.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号