Python基础知识

总结一下Python的语法和开发知识,重点在于Python和其他语法的不同点。

1、操作符:

除法:/,//

python 有两种除法,

3 / 2 = 1.5

3 // 2 = 1

3 / 4 = 0.75

3 // 4 = 0

幂运算操作符:**

2 ** 5 = 32

重点,幂运算符优先级高于单目运算符,例如:

-3 ** 2 = -9

如果是c#的话 Math.pow( -3, 2) = 9;

2、if 判断:

score = 100

if 100 >= score >=90

   print('A')

elif 90 > score >= 80

else:

   print( 'input error' )

3、三元操作符:

small = x if x > y else y

当 x>y 的时候 small = x ,否则 small = y

4、列表操作:

append:列表后追加元素

extend:列表后追加列表

insert:列表中追加元素

remove:列表中按照value移除元素

del:列表中删除对应的元素或直接删除整个列表

pop:列表中按照index移除元素,index不填直接删除最后一个

>>> empty = ['asdf']
>>> empty.append('item1')
>>> empty
['asdf', 'item1']
>>> 
>>> empty.extend(['item2','item3'])
>>> empty
['asdf', 'item1', 'item2', 'item3']
>>> 
>>> empty.insert(2,'itemx')
>>> empty
['asdf', 'item1', 'itemx', 'item2', 'item3']
>>> 
>>> empty.remove('itemx')
>>> empty
['asdf', 'item1', 'item2', 'item3']
>>> 
>>> del empty[1]
>>> empty
['asdf', 'item2', 'item3']
>>> 
>>> empty.pop(1)
'item2'
>>> empty
['asdf', 'item3']

列表的拷贝:

当我们想要从既存列表中拷贝一部分或者全部进行操作的话,需要进行如下操作:

>>> empty
['asdf', 'item1', 'itemx', 'item2']
>>> yy = empty[:]
>>> yy
['asdf', 'item1', 'itemx', 'item2']
>>> empty.pop()
'item2'
>>> yy
['asdf', 'item1', 'itemx', 'item2']

我们能看到当我们pop 列表empty的时候,列表yy的元素没有发生变化。

如果我们直接使用 yy = empty 的话,empty.pop()之后,yy的元素也会发生变化。

5、元组

元组基本和列表类似。区别就是元组不能修改其中的元素值。

>>> tuple1 = (1,2,3,4,5)
>>> tuple1
(1, 2, 3, 4, 5)
>>> type(tuple1)
<class 'tuple'>
>>> tuple1[1]
2
>>> tuple1[1] = 9
Traceback (most recent call last):
  File "<pyshell#29>", line 1, in <module>
    tuple1[1] = 9
TypeError: 'tuple' object does not support item assignment
>>> 

如果要更新元组只能使用如下方法:

>>> tuple2 = tuple1[:3] + (9,8,7) + tuple1[3:]
>>> tuple2
(1, 2, 3, 9, 8, 7, 4, 5)
>>> 

删除元组的方法也是del

>>> del tuple2
>>> tuple2
Traceback (most recent call last):
  File "<pyshell#33>", line 1, in <module>
    tuple2
NameError: name 'tuple2' is not defined
>>> 

6、字符串基本操作函数

6、字符串格式化

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值