python tips

for

for iterating_var in sequence:
statements(s)
for 变量 in 序列(包括列表、元组等一共六种序列)
描述

for letter in 'Python':     # 第一个实例
   print("当前字母: %s" % letter)
 
fruits = ['banana', 'apple',  'mango']
for fruit in fruits:        # 第二个实例
   print ('当前水果: %s'% fruit)
 
print ("Good bye!")

结果

当前字母: P
当前字母: y
当前字母: t
当前字母: h
当前字母: o
当前字母: n
当前水果: banana
当前水果: apple
当前水果: mango
Good bye!

以下地方不推荐出现空格:

		1.紧挨着圆括号,方括号和花括号的
			如:‘spanm( ham[1],{aa:2})’,写成'spanm(ham[1],{aa:2})'
		2.紧贴在逗号,分号或冒号前的
			如:‘if x == 4 :print x , y ; x , y =  y ,x’.写成‘if x == 4print x, y; x, y = y’
		3.紧贴着函数调用的参数列表前
			如:‘dict['key'] = list [index].写成'dict['key'] = list[index]'
		4.紧贴在索引或切片下边开始的开式括号前
			如:‘dict ['key'] = list [index].写成‘dict['key'] = list[index]5.在赋值(或其他)运算符周围的用于和其他并排的一个以上的空格
————————————————
版权声明:本文为CSDN博主「骚年」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/saowan8021/article/details/89478559

split()

str.split(str="", num=string.count(str)).

用来切片,以“ ”里的内容为分隔符,切num次,切成num+1个,num默认为-1,分割所有,返回值为分割后的列表

str = "this is string example....wow!!!"
print (str.split( ))       # 以空格为分隔符
print (str.split('i',1))   # 以 i 为分隔符
print (str.split('w'))     # 以 w 为分隔符

结果

['this', 'is', 'string', 'example....wow!!!']
['th', 's is string example....wow!!!']
['this is string example....', 'o', '!!!']

利用split也可以实现多个输入
比如OJ02:输出第二个整数

a,b,c = (input().split())
a= int(a)
b= int(b)
c= int(c)
print (b)

只需要在输入的时候以空格分开三个整数,就能依次给a,b,c,赋值

六个序列

Python 有 6 个序列的内置类型,但最常见的是列表和元组。
1.列表
列表是最常用的 Python 数据类型,它可以作为一个方括号内的逗号分隔值出现。
列表的数据项不需要具有相同的类型。
创建一个列表,只要把逗号分隔的不同的数据项使用方括号括起来即可。

list1 = ['Google', 'Runoob', 1997, 2000]
list2 = [1, 2, 3, 4, 5 ]
list3 = ["a", "b", "c", "d"]
list4 = ['red', 'green', 'blue', 'yellow', 'white', 'black']

访问列表
list[0] 第一个
list[1] 第二个
list[2] 第三个
list[-1] 最后一个

#!/usr/bin/python3

nums = [10, 20, 30, 40, 50, 60, 70, 80, 90]
print(nums[0:4])
#结果[10, 20, 30, 40]

[0:4]包含左,不包含右
嵌套列表

a = ['a', 'b', 'c']
n = [1, 2, 3]
x = [a, n]
print(x)
print(x[0])
print(x[0][1])

结果

[['a', 'b', 'c'], [1, 2, 3]]
['a', 'b', 'c']
b

os文件/目录方法

os.patch()
主要用于获取文件的属性,比如文件路径os.path.normpath(‘D:\ZYG\Atemp/py1.pfm’)

for response in responses:
    if response.pixels_as_float:
        print("Type %d, size %d" % (response.image_type, len(response.image_data_float)))
        airsim.write_pfm(os.path.normpath('D:\ZYG\Atemp/py1.pfm'), airsim.get_pfm_array(response))
    else:
        print("Type %d, size %d" % (response.image_type, len(response.image_data_uint8)))
        airsim.write_file(os.path.normpath('D:\ZYG\Atemp/py1.png'), response.image_data_uint8)
        

os.name()返回当前操作系统类型,一共有三种posix , nt , java分别是Linux,Windows,Java虚拟机

    result = None
    if os.name == 'nt':
        import msvcrt
        result = msvcrt.getch()

msvcrt.getch()

中断代码执行,等待键盘输入,返回值为输入字符串
Windows系统在pycharm要注意勾选菜单栏Run->Edit Configurations->选中文件勾选下方Emulate terminal in output console
否则会一直执行输入,不会结束msvcrt.getch()

import msvcrt
msvcrt.getch()

结果是
在这里插入图片描述
键盘输入任意字符

D:\ZYG\anaconda3\envs\AirSim\python.exe D:/APPs/AirSim/PythonClient/lxreturn.py

Process finished with exit code 0

运行正常

%s 多个输出

比如OJ09:字符菱形

t = input()
print('  %s' % t)
print(' %s%s%s' % (t, t, t))
print('%s%s%s%s%s' % (t, t, t, t, t))
print(' %s%s%s' % (t, t, t))
print('  %s' % t)

*args, **kwargs

在定义函数时,总会看到两个参数前后分别为*args, **kwargs
比如

def len(*args, **kwargs): # real signature unknown
    """ Return the number of items in a container. """
    pass

其中*args指的是元组类参数,**kwargs指的是接受字典类参数
(*的个数是关键,后边args、kwargs也可以换成别的a、b这种,一个指元组,两个指字典)

def foo(*args, **kwargs):
    print ('args = ', args)
    print ('kwargs = ', kwargs)
    print ('---------------------------------------')
if __name__ == '__main__':
    foo(1,2,3,4)
    foo(a=1,b=2,c=3)
    foo(1,2,3,4, a=1,b=2,c=3)
    foo('a', 1, None, a=1, b='2', c=3)
D:\ZYG\anaconda3\envs\AirSim\python.exe D:/APPs/AirSim/PythonClient/lxreturn.py
args =  (1, 2, 3, 4)                   
kwargs =  {}                           
---------------------------------------
args =  ()                             
kwargs =  {'a': 1, 'b': 2, 'c': 3}     
---------------------------------------
args =  (1, 2, 3, 4)                   
kwargs =  {'a': 1, 'b': 2, 'c': 3}     
---------------------------------------
args =  ('a', 1, None)                 
kwargs =  {'a': 1, 'b': '2', 'c': 3}   
---------------------------------------

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Karsowe

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值