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

python基本语法一

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

python基本语法一

import sys

a = 3
b = 4

##列表篇
print(a + b)
a = [1, 2, 3, 4, 6]
b = [2, 5, 9]
a + b   ## 叠加
print(a + b)
del a[3]   ## 按索引删除
a.pop()   #默认从后往前删除
a.remove(1)   ##按元素删除
a.append(b)
print(a)
c = 1
if c in a:
    print("true")
else:
    print("no")

x = [1, 4, 4, 6,]
d = len(x)   ## 列表长度
m = max(x)    ## 列表中最大值
s = min(x)   # 列表中最小值
print(d, m, s)
str1 = 'abcd'     # 类型转换
list2 = list(str1)
list3 = str(list2)    # 将列表转换为字符串类型
print(list3)
item = [2, 5, 3, 1, 9]
item.sort()
print(item)     # 默认从小到大排序
item.sort(reverse=True)   #从大到小排序
print(item)

#元组, 字典

#元组元素不可修改(), 反向索引从 -1 开始
tup1 = tuple()  # 声明空元组
tup2 = (1, 3, 4, 6, 7)
tup2[:2]   # 切片, 从 0 到 2, 2 不可取
print(tup2[:2]) # 左闭右开
tup3 = (3, 8, 9)
tup4 = tup2 + tup3  #叠加
print(tup4)
del tup1  #不可单独删除元素, 只能直接删除元组

#字典
#类型可以任意

dic1 = {}
dic1 = dict()  #定义空字典

dic2 = {'name': "zhangsan", 'age': 20, 'class': 3}
print(dic2['age'])   # 打印age 的 值
dic2['age'] = 19   #改变键值
print(dic2['age'])
del dic2['name']   #删除键 和 键值
print(dic2)
#dic2.clear()   #直接清空字典
print(dic2)
dic2.keys()    #获取所有键值
dic2.values()  #获取所有键
dic2 = {'nam': 'hahah', 'hahah': 666, 'qita': 'hahahahah'}
dic2.update()  #可将上面的  键值对 添加到 字典中
for key, values in dic2.items():
    print(key, values)       #循环打印 键值对
print(dic2)
#字典不允许同一个键出现两次, 也可 len()

#循环语句
a = 1
while a < 10:
    print(a)
    a += 2
str5 = "hahah"
for i in str5:
    print(i)

for i in range(len(str5)):
    print(str5[i])

item = [[1,2], [3, 6], [4, 8], [4, 7]]
for i in item:
    print(i)
    for j in i:
        print(j)
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/280488.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

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

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