Learn to summarize the basics of--python 2

Source: Internet
Author: User
Tags delete key readline

#可变类型  Immutable types
Stus = [' aaa ', ' BBB ', ' CCC ', ' ddd ', ' eee ']
Words = ' aha haha '
# take out every element of the list

Print (i)
# remove every character in a string
For j in Words:
Print (j)

# slices, a way to take a list of values
Print (Stus[1:3]) #顾头不顾尾, takes only the element with the subscript 1-2, does not remove the element labeled 3, in list form
Print (Stus[:3]) #开头下标写0和不写一样
Print (stus[-1]) #-1 represents the last element
Print (stus[1:]) #如果后面下标不写, the delegate takes to the end
Print (stus[:]) #开头的下标和结尾的下标都不写, representing the entire list
Print (Nums2[0:11:2])  #第二个冒号后面值代表步长, is a few elements to take once
Print (nums2[::-1]) #步长为负数的话, the value from right to left
# slices also apply to strings


# The Yuan Zu, is immutable
Cities = (' Beijing ', ' Shanghai ') #一旦定义好, no more changes.
cities[0]= ' Tianjin '   # TypeError: ' Tuple ' object does not a support item assignment
Print (Cities.count (' Beijing ')) # Number of output Beijing
Print (Cities.index (' Beijing ')) # output Beijing first occurrence
#字符串定义好后也不能进行修改
S[0]= ' # TypeError: ' str ' object does not a support item assignment

A,b,c,d = 1,2,3,4  # multiple variable definitions
Print (c)
E = f = g = 0 #多个变量相同值定义

A, B =
# # Swap without introducing a third-party variable
A, B = b,a
Print (A, B)
# or
A = 1
b = 2
A = a+b #1 +2=3
b = a #3 -2=1
A = A-B #3 -1=2
Print (A, B)

# Non-empty is true, not 0 is true
# to achieve the same functionality, the less code the better

A = 1
b = 2
Print (A==B)
# 1 of course not equal to 2, so output False
s = [1,2,3,4,5]
Print (1 not in s)
# 1 is clearly inside s, so the output is False
Lift a Chestnut ~
# Age name Sex addr phone qq Email

# k-v
xiaohei= { ' name ': ' Little Black ',
' Age ': 18,
' Sex ': ' Male ',
' Addr ': ' Shanghai ',
' QQ ': ' 123456789 ',
' Email ': ' [email protected] '
}
Print (Xiaohei)
# output: {' email ': ' [email protected] ', ' addr ': ' Shanghai ', ' age ': ' ' name ': ' Little black ', ' sex ': ' Male ', ' qq ': ' 123456789 '}

# Add
xiaohei[' height '] = 180
Xiaohei.setdefault (' house ', ' no Room ')
# output: {' house ': ' No room ', ' email ': ' [email protected] ', ' addr ': ' Shanghai ', ' age ': ' ' name ': ' Little black ', ' sex ': ' Male ', ' height ': ', ' QQ ': ' 123456789 '}
Xiaohei[' age '] = 25
Xiaohei.setdefault (' age ', 30)# SetDefault This way, if the key already exists, then it will not be modified, if key does not exist, the new
Print (Xiaohei)
# Dictionaries are unordered.

# Modify
xiaohei[' height '] = 185
Print (Xiaohei)

# Delete
Xiaohei.pop (' height ')# Specify key deletion, delete key does not exist will be error
del xiaohei[' sex ']# Use Del to delete the key does not exist when the deletion will be an error
Print (Xiaohei)

# Take value
Print (xiaohei[' addr ')# If key does not exist then it will be an error
Print (xiaohei.get (' email '))# If key does not exist return none
Print (xiaohei.get (' email ', '[email protected]‘))# If key does not exist, you can specify a value to return

Xiaohei.clear () # Empty The dictionary
Xiaohei.popitem () # randomly deletes a key

Yaoyuan = {' Chouyan ': ' 1 package ', ' name ': ' Distant '}
Xiaohei.update (Yaoyuan) # Merge the back dictionary into the previous dictionary
Print (Xiaohei)
Print (Xiaohei.values ())
Print (Xiaohei.keys ())
#-----------------------------------------------------------------------#
For k,v in Xiaohei.items (): # Loop out the key in the dictionary with the corresponding values
Print (K,V)
#-----------------------------------------------------------------------#
String Common methods
##### #需要记的!!! ######
# Print (Name.count (' I ')) #查询次数a
# Print (Name.endswith ('. jpg ')) #判断字符串是否以XX结尾
# Print (Name.startswith (' 138 ')) #判断字符串是否以XX开头
# # BOOL Type: True, Falsea
# Print (Name.upper ()) #都变成大写
# Print (Name.lower ()) #都变成小写
# Print (Name.find (' d ')) #找不到元素时, no error, will return-1
# Print (name[1]) #字符串也可以根据下标取值的
# Print (Name.isdigit ()) #判断是否纯数字
# Print (Name.isspace ()) #判断是否全都是空格
# Print (Name.strip ()) #去掉字符串两边的东西, by default remove both spaces and line breaks
# Print (Name.lstrip ()) #至去掉左边的
# Print (Name.rstrip ()) #至去掉右边的
# Print (Name.replace (' abc ', ' hahaha ', 1)) #替换字符串, replace the front with the back, 1 means replace 1 times, do not specify the full replacement
# Print (Names.zfill (5)) #在前面补0
# Print (Names1.split ()) #1, split string, 2, turn the string into a list;3, the default is separated by a space and line break
# Print (Names2.split (', ')) #以逗号进行分割
# stus = [' aaa ', ' BBB ', ' CCC ', ' ddd ', ' eee ']
# Print ('; '). Join (Stus)) #1, the list into a string, 2, a string connection (here is a semicolon connection)

# some minor ones:
# Print (Name.capitalize ())  # Capitalize the first letter of the string
# Print (Name.center, ' * ') #把字符串居中的
# Print (Name.index (' a ')) #找不到元素时, will error
# Print (Name.isidentifier ()) #判断是否为一个合法的变量名
# Print (Name.isalnum ()) #只要有英文/number returns True
# Print (Name.isalpha ()) #判断是否全为英文/Kanji
# Print (Name.istitle ()) #判断首字符是否大写
#-----------------------------------------------------------------------#
Import string

Print (string.ascii_letters) #所有大小写字母
Print (string.ascii_lowercase) #所有的小写字母
Print (string.ascii_uppercase) #所有的大写字母
Print (string.digits) #所有的数字
Print (string.punctuation) #所有的特殊字符
Read and write files
# 1. Open file
# 2. read/write to it
# 3, close the file
#-----------------------------------------------------------------------#
Abc.txt content is:
Ha
Ha ha
Ha ha haha
haha haha
hahaha haha

f = open (' Abc.txt ', encoding= ' utf-8 ')
Print (' ReadLine result: ', F.readline ())   #读取文件一行的数据
# when the top row of ReadLine reads the first line, the pointer has reached the second row, so the following readlines can only read the data after the second row
Print (' readlines result: ', F.readlines ()) # gets all the data in the file, and each row of data is placed in a list
# when the above readlines reads all the data, there is no data behind it, so read reads nothing
Print (' Read result: ', F.read ()) # Get all the contents of the file

So the result is:

#-----------------------------------------------------------------------------------------------#
# read-only mode r Read-write mode r+
# can only read, cannot write, the file does not exist will error # Open does not exist when the error will also
#-----------------------------------------------------------------------------------------------#
# write mode w write read mode w+
# Overwrite the previous file, inside the content, cannot read, the file does not exist, will help you create a
# as long as the R, the file does not exist will definitely error
# as long as the W, the contents of the file will definitely be emptied
#-----------------------------------------------------------------------------------------------#
# Append mode a +
# can read, write, not empty the previous content, the file does not exist will create
#-----------------------------------------------------------------------------------------------#

Lift a chestnut:

# writes all the elements in a list to Abc.txt in turn
f = open (' file read/write ', ' A + ', encoding= ' utf-8 ')
names = [' AAA ', ' BBB ', ' CCC ']  
For name in Names: # Use the For loop to remove the elements in the list in turn
F.write (' \ n ' +name) # and write to Abc.txt in sequence
F.seek (0) # Move the file pointer to the front
Print (F.read ())
F.close ()


Learn to summarize the basics of--python 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.