Python基础知识(持续更新中)

1.python代码换行

Python 通常是一行写完一条语句,但如果语句很长,我们可以使用反斜杠(\)来实现多行语句 ,例如:
total = item_one + \
item_two + \
item_three
 
 
2.pyhton的数据类型
6种数据类型-Number,String,List,Tuple,Set,Dictionary
不可变数据(3个):Number,String,Tuple
可变数据(3个):List,Dictionary,Set
 
Type()不会认为子类是一种父类类型
isinstance()会认为子类是一种父类类型
 
 
3.zip()函数
lt1=[1,2,3]
lt2=[4,5,6]
lt3=zip(lt1,lt2)
#zip()是可迭代对象,使用时必须将其包含在一个list中,方便一次性显示出所有结果
print(lt3)
print(list(lt3))
print(dict(lt3))
 
输出:
[(1,4),(2,5),(3,6)]
{}
 
看到dict是空的
划重点:
zip()只能执行一次,一次后就是空的了
 
zip()的应用:
杨辉三角:https://leetcode-cn.com/problems/pascals-triangle-ii/
使用错位相加
class Solution(object):
  def get_row(self, rowIndex:int)->List[int]:
    res = [1]
    while len(res) <= rowIndex:
      res = [a+b for a,b in zip([0] + res, res + [0])
    return res
 
4.位置互换
java换位需要一个中间变量,python不需要
a,b = b,a  就换过来了 
 
5.运算符
and 取后面值
or 取前面值
非0即真
print(1 > 2 and 3 or 6) 输出6

posted on 2020-08-12 16:18  测开小白的杂货铺  阅读(118)  评论(0)    收藏  举报