2021-4-10

Python第二次日志

学习了数据类型,数据对象,变量,赋值,数值运算,

基础知识:
变量:等号(=)是赋值语句,可以将任意数据类型赋值给变量。
标识符:自定义的一些符号和名称 且由字母,数字,下划线_组成的序列,并且以字母或者下划线开头,不能以数字开头,不能是python的保留字。
保留字:即关键字。 import keyword
keyword.kwlist 输出当前版本所有保留字
常量:“不可变的量” 使用大写字母组合的变量名表示常量 eg:PI=3.1415926

运算符:
1.算数运算符: =、-、/、//、%、*

a=10
b=5
print(a+b)
print(a-b)
print(a*b)
print(a/b)
print(a//b)
print(a%b)

输出结果:

15
5
50
2.0
2
0

比较运算符:

a=10
b=20
print(a==b)
print(a!=b)
print(a>b)
print(a<b)
print(a>=b)
print(a<=b)

输出结果:

False
True
False
True
False
True

赋值运算符:

a=10
b=20
b-=a
print(b)
b*=a
print(b)
b/=a
print(b)
b**=a
print(b)
b%=a
print(b)
b//=a
print(b)

输出结果:

10
100
10.0
10000000000.0
0.0
0.0

位运算符:

a=60
b=13
print(a&b)
print(a|b)
print(a^b)
print(-a)
print(a<<2)

输出结果:

12
61
49
-60
240

逻辑运算符:

a=10
b=20
print(a and b)
print(a or b)
print(not(a and b))

输出结果:

20
10
False

成员运算符:

a='Hello,world'
if("H" in a):
    print("H in a ")
else:
    print("H is not in a")
b='Python'
if ("z" in b):
    print("z in b")
else:
    print("z is not in b")

输出结果:

H in a 
z is not in b

身份运算符:

x=5
y=10
print(x is y)
print(x is not y)

输出结果:

False
True

python 输出:print()
输入:input()

列表:(有序且可变) eg: [1,2,3,4,5]
列表的增删改查:

list1=[1,2,3,4,5]
list2=[9,10,11]
list1.append(6)##将元素6添加到list1尾部
print(list1)
list1.append(list2)##将list2中所有元素加到list1尾部
print(list1)
list2.insert(1,15)##在list2 的1位置添加一个数15 其余元素后移
print(list2)
list1.remove(2)##在list1中删除首次出现的2 ,其余元素前移一位
print(list1)
list1.pop(1)##删除并返回list1中下标位1的元素(默认值位-1)
print(list1)
list1.clear()##删除list1 中所有元素但保留对象
print(list1)
print(list2.index(10))##返回list2中第一个出现10的下标,若无则抛出异常
print(list2.count(1))##返回list1中1出现的次数
list2.reverse()##对list1所有元素进行逆转
print(list2)
list2.sort(key=id,reverse=False)##对 list1 中所有元素进行排序 key用来指排序依据,reverse决定升序(False)还是降序(False)
print(list2)
print(list2.copy())##返回列表list2 的浅复制


输出结果:

[1, 2, 3, 4, 5, 6]
[1, 2, 3, 4, 5, 6, [9, 10, 11]]
[9, 15, 10, 11]
[1, 3, 4, 5, 6, [9, 15, 10, 11]]
[1, 4, 5, 6, [9, 15, 10, 11]]
[]
2
0
[11, 10, 15, 9]
[9, 10, 11, 15]
[9, 10, 11, 15]

创建: list1=[1,2,3,4,5]
访问: print(“list1[0]”,list1[0])

list1=[1,2,3,4,5]
print("list1[0]",list1[0])

输出结果:

list1[0] 1

列表更新及修改:

list=[1,2,3,4,5]
print("第三个元素是:",list[3])
list[3]=0
print("更新后的第三个元素是:",list[3])
print("原始列表:",list)
del list[3]
print("删除第三个元素:",list)

输出结果:

第三个元素是: 4
更新后的第三个元素是: 0
原始列表: [1, 2, 3, 0, 5]
删除第三个元素: [1, 2, 3, 5]

元组:(不可变序列)将所有元素放入()中 或 “ ”,“ ”,“ ”;#
元组的创建与访问:

tup1=(50)
type(tup1)#不加逗号为整形
tup1=(50,5)
type(tup1)#加上逗号,类型为元组
print("tup1[0]:",tup1[0])
print("tup1[1]:",tup1[1])

输出结果:

tup1[0]: 50
tup1[1]: 5

删除 用del 删除整个元组
连接组合 用+

元组运算符: + 、 *

字典:(无序可变序列)
每个元素的键和值用冒号: 隔开 元素之间用逗号, 隔开
字典的删除、添加:

dict1={'abc':456};
dict2={'deg':789,98.5:37};
print("dict1['abc']:",dict1['abc'])
print("dict2[98.5]:",dict2[98.5])
dict1['Age']="8"
print(dict1)
del dict2['deg']
print(dict2)
print(dict1.clear())

输出结果:

dict1['abc']: 456
dict2[98.5]: 37
{'abc': 456, 'Age': '8'}
{98.5: 37}
None

集合:(无序且可变)
集合 元素添加,删除:

lei=set(("Google","Python","Taobao"))
lei.add("Facebook")
print(lei)
lei.update({1,3})
print(lei)
lei.update([1,4],[2,6])
print(lei)
lei.remove("Taobao")
print(lei)
lei.clear()
print(lei)

输出结果:

{'Google', 'Facebook', 'Taobao', 'Python'}
{'Google', 1, 'Taobao', 'Python', 3, 'Facebook'}
{1, 2, 'Taobao', 3, 4, 6, 'Google', 'Python', 'Facebook'}
{1, 2, 3, 4, 6, 'Google', 'Python', 'Facebook'}
set()

函数:

def hello():
    print("Hello,world")
hello()
##计算面积函数
def area(width,height):
    print(width*height)
area(5,7)
##函数调用
def printme(str):##打印任何传入的字符串
    print(str)
printme('dawfw')

输出结果

Hello,world
35
dawfw
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值