temp= ' Hello '
# 1, type usage: Display data type
T=type (temp)
# 2, dir usage: View the functions that can be used on the variable
B=dir (temp)
# 3, view Help
Help (Type (temp))
# (with error, cause unknown)
# 4, direct click to see the use of keywords, such as the mouse on the keyword, CTRL + Left button, automatically navigate to the keyword function
Temp.upper ()
# 5, int type, view value in binary representation of desired number of digits
A=5
B=a.bit_length ()
# 6, the function of STR class
# Center (n, ' * ') expands the string to n bits and centers the original character, and the rest is populated with the characters within the single quotation mark (default is empty)
A= ' Hello '
B=a.center (10, ' _ ')
# Turn the initial letter into uppercase
A.capitalize ()
# count the number of times a subsequence appears in a string
A= ' ABCDE ABC Acfkab '
B=a.count (' AB ') # to find the number of occurrences of ' ab ' in string a
B=a.count (' ab ', 0,10) #求 ' ab ' occurrences within the 第0-10个 character
# Determine whether to end with, return ture or False
A= ' Hello '
Print (A.endswith (' e '))
Print (A.endswith (' e ', 0,2)) #获取字符串中位置大于等于0小于2的子序列 to determine if the subsequence ends with E
# the tab is converted to a space, the default tab is converted to 8 spaces (in the code, \ t is the TAB key, if the direct point of the TAB key, the system automatically converted to a space)
A= ' hello\t999 '
Print a
Print A.expandtabs ()
Print A.expandtabs (20) # Converts a tab key to 20 spaces
# Find the position in the string where the character is located
A= ' Hello World '
Print S.find (' R ') # back from the go, return if not found-1
# Formatting of strings
A= ' Hello {0} world {1} '
Print S
A1=s.format (' Hi ', 10)
# {0} {1} is a placeholder
# CONCATENATE strings together join
li=[' abc ', ' EFD '] # [] denotes a list, () represents a tuple
S= ' _ '. Join (LI) # the character in single quotation marks is a symbol of the connection string
Print S
# Select characters in a string
A= ' ABCD '
A[0] # index, pick 1th character
A[0:2] # slices, including 0 not including 2
Len (a) # Gets the length of the string
# Other Features
"' Left align content, right padding ljust ()
Make content right-aligned, left-padded rjust ()
Converts a string to lowercase lower ()
Convert string to uppercase upper ()
Remove the left space Lstrip ()
Remove the right space Rstrip ()
Remove both left and right spaces strip ()
Split string partition () splits a string into tuples or lists, by default from left to right
Rpartition (' a ') from right to left after a, split from a into two parts
Substitution character replace (' A ', ' B ') replaces A with b,replace (' A ', ' B ', 1) replaces only one
From right to left find RFind ()
There is a character in parentheses split the string split (), or only in Nth split split (' A ', 1)
Uppercase to lowercase, lowercase to uppercase swapcace ()
To make a heading title ()
‘‘‘
# 7, List of features provided
li=[' abc ', ' Sde ', ' WEC ']
LI[0] # Index
Li[0:2] # Slice
# Append Backward Append
Li.append (' EFG ')
#元素出现的次数
Li.count (' abc ')
# Extend () Add data in bulk
temp=[123,875, ' adb ']
Li.extend (temp)
# Gets the specified index location
Li.index (' abc ')
# inserting Insert ()
Li.insert (1, ' ad ')
# Remove
Li.pop () # Removes the last element from the original list
Li.pop ()
Li.remove (' abc ') # removes the first bracketed element found from the left
# sort
Li.sort ()
# reverse
Li.reverse ()
# Delete Element
del Li[1] # Delete First element
Del Li[0:2]
# 8, tuple tuples (cannot be modified for tuples)
tu= (' AVC ', ' wed ', ' abc ')
# index
Print Tu[0]
# len
Print Tu[len (TU)-1]
# slices
Print Tu[0:1]
# for
For I in Tu:
Print I
# do not support delete operations on tuples
# count, number of occurrences of the calculated element
# index, gets the index position of the specified element
# 9, Info dictionary, each element of the dictionary, key value pairs
a_info={
' Name ': ' Lijing ',
' Age ': 15,
' Gender ': ' M '
}
# index (the value to the left of the colon equals the index, the value to the right of the colon is the corresponding value)
Print a_info[' name ']
# no slices
# loop, default value output key, that is, the value to the left of the colon
For I in A_info:
Print I
# get all the keys
Print A_info.keys ()
# or use loops to get all the keys
For I in A_info.keys (0):
Print I
# Get all the values
Print a_info.values ()
# ###
For I in A_info.values ():
Print I
# Get all key-value pairs
Print A_info.items ()
####
For k,v in A_info.items ():
Print K
Print V
# Clear Erase All content
A_info.clear ()
# Get value based on key, if key does not exist, you can specify a default value
Val=a_info.get (' age ')
Val=a_info.get (' age111 ', 123) # Specifies the default value, returns 123, or None if empty
# when the index value, if the key does not exist will be an error, the use of Get value will not
# Has_key Check the dictionary for the existence of key, Python3 version does not have this function, another method is implemented as follows
Ret= ' age ' in A_info.keys ()
Print ret
# Update Batch Updates
test={
' A1 ': 123,
' A2 ': 456
}
A_info.update (Test)
# Delete
Del test[' A1 ']
# pop () removes an element at a specified position
# Popitem () only remove the last element
# Enumerate automatically generates a column that starts at 0 and increments by 1, which is equivalent to adding a column index
li=[' computer ', ' mouse ', ' u disk ', ' keyboard '
For Key,item in Enumerate (LI): # enumerate (li,1) indicates a self-increment starting from 1
Print Key,item
Inp=input (' Please enter product: ')
Inp_num=int (INP) # input defaults to string type, int function converts string to integer
Print Li[inp_num]
# input content, output index
Inp=input (' Please enter content: ')
Ret=li.index (INP)
Print ret
# Range, xrange (only Python2.7 available)
# range, used to get the number in the specified range, range (0,10000)
# xrange, Xrange (0,10000), with get the number within the specified range
For I in Xrange (1,100):
Print I
# Py3, range, equivalent to py2.7 xrange
Print Range (1,10)
For I in Range (1,10,2): # 2 per self-increment
Print I
# 10, set
# Create
S={11,22,33}
# add
S.add (44)
# clear
S.clear ()
# Find a collection in s that does not exist in B, and assign it to a new variable
S.difference (b)
# Find a collection in s that doesn't exist in B, update yourself
S.difference_update ()
# Remove the specified element, no error is present
S.discard (44)
# Remove the specified element, no error is present
S.remove (44)
# take the intersection of two sets
B={11,22,99,77}
Ret=s.intersection (b)
# take the intersection of two sets and update your own
S.intersection_update (b)
# Determine if it's a subsequence
Ret=s.issubset (b)
# Determine if it is the parent sequence
Ret=s.issuperset (b)
# Determine if there is a intersection, there is false, there is no true
Ret=s.isdisjoint (b)
# Remove an element to get the return value
Ret=s.pop ()
# s exists in the B element that does not exist, and B does not exist in the element merge
Ret=s.symmetric_difference (b)
# Seek and set
S.union (b)
# batch update (add Element)
S.update ([12,34])
Python basic data type features