python-data types, meta-ancestors, lists, dictionaries, file operations

Source: Internet
Author: User

Python----data types, meta-ancestors, lists, dictionaries, file operations
1. Boolean Type: Ture False
Non-0 is true non-empty or vacuum (None a= ' empty string)
Conditional judgment returns TRUE or False
2. String: Note: All operations of the string do not change the original string value
Common methods of operation
. Count ()
. Find () #回不存在的元素, return-1
. Join ()
. Encode ()
. Decode () #bytes类型才有的 (bytes: binary type)
. EndsWith ('. txt ') #字符串是否以xx结尾
StartsWith (' A ') #字符串是否以xx开头
. IsDigit () #判断是不是一个整数
. Strip () #以xx去除两边值
. replace (' xx ', ') #替换字符串
. Split (', ') #以xx来分割字符串, returns a list
. format_map# Pass-through dictionary
Forcing type conversions
Int ()
List ()
STR ()
Type () #查看数据类型
Dict () #强制类型转换
3.list List of additions and deletions to search:
Increase: #创建, add value to list
Szz = [' bjz ', ' LW ', ' lhl ', ' jjx ', ' CSS '] #创建一个列表
Szz.append (' ymn ') #从末尾添加值
Szz.insert (0, ' Zyj ') #在指定的位置添加值
Print (SZZ)
Check: Subscript value, slice value
Subscript is the value, the subscript is starting from 0
Slices: Gu Tou regardless of tail
The value in front of the slice colon, omitted and not written, representing the previous values
The value after the slice colon, omitted, is not written, which means that all subsequent values are taken
If you write only a colon, take the entire list and use it to copy a new list.
Step default does not write, is 1, write on the step, write a few elements to take once
When the step is negative, the value starts at the end of the list and is a positive number, starting at the beginning of the list.
Note: The operation of the slice subscript also applies to the string
By deleting:
# del Szz[0] #删除指定下标的值
# Szz.pop (2)
# Szz.pop () #默认从列表的末尾开始删除 can also be removed by passing in the specified subscript
# szz.remove (' JJX ') #删除指定的值, not subscript
Szz.clear () #清空列表
Change:
Szz[0]= ' Walking on the peak '
Print (' Modified list: ', SZZ)
List's built-in methods of operation
# Nums.sort (reverse=true) #排序, default in ascending order, if you want to follow the descending shot, specify Reverse=true
# Print (Szz.index (' ly ')) #获取指定元素的下标
# Print (Szz.count (' Zl ')) #获取指定元素的次数
# szz.reverse () #反转字符串
# szz.extend (SZZ1) #扩展字符串
4. Tuples: An immutable list, it has only two methods
Mysql_config= (
' 127.0.0.1 ',
3306,
' Root ',
' 123456 ')
Mysql_config.count (3306) #查询出现的次数
Mysql_config.index (' root ') #获取指定元素的下标
3, the dictionary additions and deletions to search
How to build a dictionary
4. Read and Write files
Open (encoding= ' utf-8 ') Python2, Python3
File () #python2
R Read Only #
W Write only #
A append write, plus the + number, is read and write, read, add read and write
u# change the line characters in all the files to \ n
b# binary
#三种模式
The. Read () #读取文件的所有内容, which returns a string
. ReadLines () #读取文件的所有内容, a list is returned, and the contents of each row are an element of the list
. ReadLine () #读一行
For I in F:
Print (i)

The. Write () #写一个字符串
. Writelines () #写一个list
. Seek () #移动文件的指针到xx位置
. Tell () #查看文件当前的指针在哪里
. Truncate () #清空文件
. Close () #关闭文件
. Flush () #立即把内存里面的数据写到文件里面
With open (' xx ') as FW:
Fw. ()
If you want to modify the original file, you must create a new file, the source file to write the modified value of the new file
Os.remove (' A.txt ')
Os.rename (' A.txt.bak ', ' a.txt ')
Other:
Iptables-i INPUT 1-p tcp-s 192.168.1.1-j DROP
#这条命令是不让某个ip访问你的服务器
-A input-m state--state new-m tcp-p TCP--dport 80-j ACCEPT
Open 80 port number
Service iptables Restart #重启防火墙让它生效

Time.strftime ('%y-%m-%d%h:%m:%s ')

Import Sys
SYS.ARGV[1]
Import Random
Random: Randomly number
. Ramdom (1,101) Gu Tou regardless of tail--randomly generated 1-100
Count+=1 equivalent to Count=count+1
Count-=1 equivalent to Count=count-1

. Range (0,21) range (0,21)-generates a weight of 0-20

See method Source: Right-click + CTRL
\ n-Wrap
\ t---Tab key 4 spaces
Operator://floor removal except for decimal parts
python2:/is the floor except
Time.strftime ('%y-%m-%d%h:%m:%s ')
\ write separately from the same line of code

Remark: Recommended blog address: http://www.nnzhp.cn/article/16/---file Operations and collections
HTTP://WWW.NNZHP.CN/ARTICLE/14/---data types


python-data types, meta-ancestors, lists, dictionaries, file operations

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.