ITPub博客

首页 > 人工智能 > 自然语言 > 8个关于Python的小技巧

8个关于Python的小技巧

自然语言 作者:小咖啡111 时间:2019-05-14 16:38:47 0 删除 编辑

今天分享给大家8个小技巧,希望在实战的时候有帮助!


01.

条件判断





condition=

True




if  condition:
    x= 1
else :
    x=
2

print  (x)


改成





x

=

1

 

if

 condition 

else

 


2



print  ( x )


02.

大数字分割


如果几个非常大的数字比如相加的时候,一个数字后面很多零点时候,我们很难去点这个0,比如:



num1=

10000000000



num2= 100000000
print  (num1+num2)


上面的几个大数字,很多零,点的眼都花了!怎么办呢,Python里面有一个奇淫技巧,可以加下划线分割:



num1=

10

_000_000_000


num2= 100 _000_000
total=num1+num2
print  ( f' {total:,} ' )
>> 10 , 100 , 000 , 000


03.

文件的关闭





f

=

open

(

'log.txt'

,

'r'

)


file_contents= f . read ()
f . close ()

words=file_contents. split ( ' ' )
word_count= len (words)
print  (word_count)


用with来处理更简便



with 

open

(

'log.txt'

,

'r'

) 

as

 

f

:


file_contents= f . read ()
words=file_contents. split ( ' ' )
word_count= len (words)
print  (word_count)


04.

优雅的用下标




names=[

'Corey'

,

'Chris'

,

'Dave'

,

'Apple'

]


index =
for  name in name s:
    print  ( index ,name)
index += 1


使用enumerate来计算下标



names=[

'Corey'

,

'Chris'

,

'Dave'

,

'Apple'

]


for   index ,name in enumerate(names):
    print  ( index ,name)


05.

遍历多个序列




names=[

'Leo'

,

'Lili'

,

'Sam'

,

'Tom'

]


ages=[ 30 , 20 , 28 , 25 ]
for  index,name  in  enumerate(names):
    age=ages[index]
    print  ( f' {name}  is  {age}  old' )


太麻烦了,用zip更方便




for

 name ,age 

in

 zip(names,ages):


    print  ( f' {name}  is  {age}  old' )


有的同学说如果我有3个序列呢,怎么办,一样可以操作



names=[

'Leo'

,

'Lili'

,

'Sam'

,

'Tom'

]


ages=[ 30 , 20 , 28 , 25 ]
habits=[ 'Movies' , 'Dance' , 'Reading' , 'Singing' ]
for  name ,age,habit  in  zip(names,ages,habits):
    print  ( f' {name}  is  {age}  old and like  {habit} ' )


06.

巧妙的使用单下划线


我们在一个序列的时候,有的时候,只想取头和尾,巧妙的用单下划线




a

,_,

b

=(

1

,

2

,

3

)


print  ( a )
print  ( b )
>>
1
3


但是如果有一个很长的序列的时候,我们想取头和尾怎么办呢,传统的做法是:



nums=(

1

,

2

,

3

,

4

,

5

,

6

,

7

,

8

,

9

)


head=nums[ ]
tail=nums[- 1 ]
print  (head)
print  (tail)


如果能巧妙的利用unpack方法,就会很简便:



nums=(

1

,

2

,

3

,

4

,

5

,

6

,

7

,

8

,

9

)


head,*_,tail=nums
print  (head)
print  (tail)


07.

类的属性封装





class

 

Person


()

:


    pass
person=Person()

如果你有一个字典需要来初始化这个类

person_info={'first':'leo','last':'sam'}

你希望最后能print (person.first),怎么办?有没有什么巧妙的方法处理?


用setattr函数




for

 

k

,v in person_info.

items

():


    setattr(person, k ,v)


还有getattr(),可以方便的获取类的属性




for

 

k

 in person_info.

keys

():


    print  (getattr(person, k ))



08.

输入加密的密码




username=

input

(

'Username: '

)


passwd= input ( 'Passwd:' )
print  ( 'Logging In...' )
>>
Username: user
Passwd:admin
Logging In...


密码是显示出来的,这样肯定不太好,有没有其他优雅的方法的,用Python自带的函数getpass




from

 getpass 

import

 getpass


username=input( 'Username: ' )
passwd=getpass( 'Passwd:' )
print  ( 'Logging In...' )
>>
Username: aa
Passwd:
Logging In...

这里passwd后面会显示一个钥匙的图片


以上就是8个小技巧,如果觉得有用,欢迎留言讨论!


Python学习交流群:519489681  备注:小白


来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/31560357/viewspace-2644348/ ,如需转载,请注明出处,否则将追究法律责任。

请登录后发表评论 登录
全部评论

注册时间:2018-10-31

  • 博文量
    66
  • 访问量
    77424