Python Learning record 2

Source: Internet
Author: User
Tags shallow copy

One, two modules (SYS and OS)

1 #!/usr/bin/env python2 #_*_ coding:utf-8 _*_3 #Author:taoke4 ImportSYS5 Print(Sys.path)#Print Environment Variables6 Print(Sys.argv[0])#current file relative path, SYS.ARGV is a list, the first element is the relative path of the program itself, followed by the input parameters for the program to run7 8 ImportOS9 #cmd_res= Os.system ("dir") #执行命令不保存结果TenCmd_res = Os.popen ("dir"). Read ()#saves the result of the command execution and returns the Save Address One Print(" -", Cmd_res) AOs.mkdir ("New_dir")#Create a directory

SYS and OS two modules for ease of use

Import

Now looking for modules in the current directory, looking for modules in environment variables

Path for storing third-party modules C:\Python36-32\Lib\site-packages

Second, the conversion between string and bytes in Python

1 #!/usr/bin/env python2 #_*_ coding:utf-8 _*_3 #Author:taoke4str ="I love Beijing Tian ' an gate"5Str_endode = Str.encode ("Utf-8")6Str_endode_decode = Str_endode.decode ("Utf-8")7 Print(Str,type (str))8 Print(Str_endode,type (str_endode))9 Print(Str_endode_decode,type (Str_endode_decode))
<class'str'>b'\xe6\x88\x91\xe7\x88\xb1\xe5\x8c \x97\xe4\xba\xac\xe5\xa4\xa9\xe5\xae\x89\xe9\x97\xa8' <class'  Bytes'><class'str'>

Three, List

1 #!/usr/bin/env python2 #_*_ coding:utf-8 _*_3 #Author:taoke4names = ["xiaoming","Xiaohong","Xiaohei","Xiaoxiao"]5 6 Print(names)7 Print(names[0],names[2])8 Print(Names[1:3])#Gu Tou regardless of tail, slice9 TenNames.append ("xiaobingbing") One Print(names) ANames.insert (1,"Renma") - Print(names)

Shallow copy and deep copy in list

#!/usr/bin/env python#_*_ coding:utf-8 _*_#Author:taokeImportCopynames= ["xiaoming","Xiaohong",["Jack","Toms"],"Xiaohei","Xiaoxiao"]names2= Names.copy ()#Shallow CopyNames3 = copy.copy (names)#Shallow CopyNames4 = copy.deepcopy (names)#Deep CopyNames[2][0] ="JACK"Print(names)Print(Names2)Print(NAMES3)Print(NAMES4)
Operation Result:
['xiaoming','Xiaohong', ['JACK','Toms'],'Xiaohei','Xiaoxiao']['xiaoming','Xiaohong', ['JACK','Toms'],'Xiaohei','Xiaoxiao']['xiaoming','Xiaohong', ['JACK','Toms'],'Xiaohei','Xiaoxiao']['xiaoming','Xiaohong', ['Jack','Toms'],'Xiaohei','Xiaoxiao']

Iv. Tuple (tuple)

Can not change the list, can only check.

V. String (Method of strings)

  1. Str.rjust: Right-justified
  2. Str.ljust: Left Justified
  3. Str.center: Middle Alignment
  4. Str.zfill: The default way
  5. Str.find: String lookup, no return-1
  6. Str.index: Find string position, no error returned
  7. Str.rfind: Find from right
  8. Str.rindex: Ibid.
  9. Str.count: Count the number of occurrences of a string
  10. Str.replace: String substitution
  11. Str.strip: Remove whitespace at the beginning of the string
  12. Str.lstrip: Remove left space
  13. Str.rstrip: Remove Right space
  14. Str.expandtabs: Replace table in string with equal length space
  15. Str.lower:
  16. Str.upper:
  17. Str.swapcase: Reverses string character capitalization
  18. Str.capitalize: string First character uppercase
  19. Str.title: Capitalize first letter in string
  20. Str.split: string split into List
  21. Str.splitlines: Splitting a string into a list by rows
  22. '-'. Join (strlist): Concatenate list strlist into strings with '-'
  23. Str.startswith: Tests whether a string starts with a specified character
  24. Str.endswith: Tests whether a string is terminated with the specified character
  25. Str.isalum: Determines whether a string is full of characters or numbers and has at least one character
  26. Str.isalpha: Judging whether the string is all letters
  27. Str.isdigit: Judging whether the string is all numbers
  28. Str.isspace: Determine if a string contains spaces
  29. Str.islower: Determine if the string is all lowercase
  30. Str.isupper: Determine if the string is all uppercase
  31. Str.istitle: Determine if the first letter is uppercase
  32. Import string
  33. String.atoi ("123", BASE=10/8/16): Converts a string to a number of type int
  34. String.atol: Converting strings to long shaped numbers
  35. String.atof: Converting a string to a floating-point type

Python Learning record 2

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.