【Python基础语法记录】

Python基础语法记录

隔好长时间不用Python就什么都不记得了,记在这里方便下次找


一、循环语句

用了for和while。

#1.for
a=['one','two','three','four']
for i in a:
	print(i)
for i in range(len(a)):  #range(0,len(a))
	print(a[i])
#2.while
a,b=5,6
while a<10 and b>0:  # or 
	print(a,b)

二、list相关

1.创建

list1=[1,2,3,4,5]
list2=[i*2 for i in list1]

2.增加

list3=list1+[6,7,8,9,10]   #要在原list基础上添加一些元素用
                                      #但是不想改变原来的list
list4=copy.deepcopy(list1)
list4.append(6) #7,8,9也要这样子加进去

list5=copy.deepcopy(list2)
list5.extend([6,7,8,9]) #如果用append就有[]

#插到第index个位置
list5.insert(index,value)
a=[1,3,4]
a.insert(1,2)  #[1,2,3,4]

3.删除

list3.remove(10)

4.两个list对应位置相乘

list6 = [a * b for a, b in zip(list1,list2)] 

list7 = [list1[i]*list2[i] for i in len(list1)] 
#如果zip用不了就用这个

三、最大最小值

max(list1)
min(list2)
#返回的是数不是index
#如果要最值的数
list1[max(list1)]
list2[min(list2)]

四、结构体

#Python好像没有结构体,用类实现
class MyStructureClass(a):
    class Struct(a):
        def __init__(self, name, age):
            self.name = name
            self.age = age

    def make_struct(self, name, age):
        return self.Struct(name, age)

myclass = MyStructureClass()
test1 = myclass.make_struct('xsk', '22')
test2 = myclass.make_struct('mtt', '23')

print test1.name

#原文地址 https://www.cnblogs.com/nyist-xsk/p/10470527.html

五、str

1.str类型转其他类型(float、int,double)

f='32.5'
floatf=float(f)
intf=int(f) # int是向0方向取整,这里就是32,负数也是向0方向取整

2.list 的str类型转其他类型(float)

f=['13','21,'33','57']
notstr_f = list(map(lambda x: float(x), f))

3.带变量的字符串

f=['13','21,'33','57']
for i in range(len(f)):
	strf='The Number of f[{}] is {}.'.format(i, f[i])
	# strf出来就是The Number of f[0] is 13
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值