Python Basic Data type rollup

Source: Internet
Author: User

#!/usr/bin/env python
#-*-Encoding:utf-8-*-
# Author:administrator
# MAKE_TIME:2018/6/9
" "
Data type:
Immutable data types: int, str, bool, tuple, Frozenset
variable data types: list, dict, set
Public Methods: Count (), Len (), Del, copy ()
Len (all of them)
count (number, dictionary, collection does not have this method)
index (number, dictionary, collection does not have this method)
variable data types have copy ()

integers, strings, lists, tuples, Booleans can be added (dictionaries, collections are not additive)

integer: int
# Immutable data Types
bit_length () view numbers in memory for a minimum of several
Mathematical Operations: +-*///(DIVISIBLE)% * * (Power)
comparison operation: >,<,>=,<=,==,!=
assignment operation: =,+=,*=,/=,-=,%=,,//=,**=

logical operation: not OR and
from left to right
Priority: () not and OR
1 (non-zero Integer): true 0:false
1 or 2 left non 0 take left, 0 take right, and opposite

identity operation: Is isn't judged two objects are not for the same memory address

member operation: In not in determines whether the character/element is in the target, Str,list,dict,set,tuple

bit operations:
& and Operations 1 1 to 1, 1 0 to 0, 0 0 to 0
| or operations 1 1 to 1, 1 0 to 1, 0 0 to 0 .

Method:
Int.bit_lens () View number of bytes in memory
Boolean value: True False
#不可变数据类型, True 1,false 0 (0,none,false,[], (), {},set (), False)

string: str
# Immutable data type, ordered, can be sliced, iterate loops, interactive default input as String
# denoted by single or double quotation marks: ' element '
# cannot be changed or deleted
slices: Indexes in left-to-right strings starting from 0
[Start:end:step] Gu Tou regardless of the tail
[1:3] The characters in the string index 1 to 2
[1:-1]-1 indicates the last character
Operation Method:
del str Delete string
len (str) Statistics string length, built-in method
Str.count (Element) the number of elements in the statistics string
str.center (width, "padding does not write") specifies the center of the length, the default padding is a space
str.title () uppercase letters separated by non-letter
China country---China country
* Str.capitalize () First letter, other separate words lowercase
China_country-China_country
str.swapcase ()---> String case cross-turn
* * Str.upper ()----> all capitalized
Str.isupper () are all uppercase
* * Str.lower ()-----> all into lowercase
Str.islower () are all lowercase
str.encoding ("encode the string in which encoding set")
* * Str.startswith ("element") determines whether a string begins with, returns a bool value
Str.endswith ("element")
* * * Str.find ("element", Star,end) find the index of the element, if the element does not exist, return-1
* * * Str.index ("element", Star,end) to find the index of the element, if the element does not exist, error
* * * * Str.strip ("element is empty") remove the \n,\t,\s and other whitespace characters at both ends, or you can specify the characters to be deleted from both ends
Str.lstrip ()
Str.rstrip ()
* * * * * Str.split ("Separator nullable, default space, multiple spaces as a") split string, return list
Str.lsplit ()
Str.rsplit ()
* * * str.replace (old,new, replace several times)
* * * STR.FORMAT () string formatting
"{}my name is {}". Format (11, "Xiu Wen")
"{1}my name is {2},next year is {1}". Format (11, "Xiu Wen")
{id}is old boy, my name {name} ". Format (ID =11,name=" Sun ") cannot put a dictionary
percent percent semicolon format
"My name is%s, I am%d"% ("Sun", +) "
"My name is% (name) s and I am% (age) d"% (DIC) "can be put in a dictionary
%s String
%d integers
%f decimal%.2f reserved 2 decimal
more than 3.6 version format can be: F "{name} is already logged in"

Str.isalnum () is a letter or number that returns a bool value
Str.isalpha () are all letters
whether the Str.isdigit () is all digital
Str.isspace () whether there is a white space character
* * * * * Str.join (iterable iterative) string concatenation, the parentheses must be iterative data, and only for one parameter
Note:
"Sun". Join ([11,22]) error, join cannot convert a list with a number to a string, you need to first convert the number to a string
"*". Join ("%s"%id for ID in list)
object in STR determines whether the element is in Str

Listing: List
# variable data types, ordered, can be sliced, iterate loops, elements can be any type of data
# denoted by [element,]
# can be increased and censored
slices:
as with the string, the index tangent
Increase:
List.append (Element) adds an element to the end of the list
List.insert (index, Element) specify the index location to add elements to the list
list.extend (iterable) iteration adds multiple elements to the end of the list
Delete:
List.pop (index) is deleted by index, when no element is added, the last one is deleted by default
List.remove (element/object) deleted based on element
del list Delete list
del List[:3] Delete based on index tiles
list.clear () empty list
Change:
Lis[index] = New_data Index Tile modification
lis = [11,22,33,44,55]
Lis[:2] = [66,77,88] # lis = [66, 77, 88, 33, 44, 55] step is 1 o'clock, do not consider the number of corresponding, cut a piece of space
Lis[:3:2] = [66,77] # error, step is not 1 o'clock, the number of left and right elements must be consistent
#ValueError: Attempt to assign sequence of size 3 to extended slice of size 2
Check:
Lis[index] Search by index
for I in list: Iterate through iteration, I is the list of each element
Method:
del list Delete list
length of the Len (list) statistic list
the number of occurrences of the specified element in the List.count (object) statistic list
List.index (object,start,end) Find the index of an element
List.sort (reverse=false) sort, default to ascending order, when reverse value is set to True, descending
List.reverse () The order of elements in the list is reversed.
list.copy () list shallow copy
the object in list determines whether the objects are in the list

tuples: Tuple
# Immutable data types, ordered, can be sliced, iterate loops, elements can be arbitrary data types
# cannot be changed or deleted
# denoted by parentheses ()
Check:
tuple[2] by sectioning
For i in tuple: Iterate through the loop
Method:
Tuple.count (object)
Tuple.index (object,start,end)
len (tuple)
object in tuple

Dictionary: Dict
# variable data type, key-value pair exists, key immutable, hash (int,str,bool,frozenset), value can be any data type, unordered, non-slicing, iterative loop query
# denotes {key:value} in curly braces
# can be deleted and checked
Increase:
dic = {}
Dic[new_key] = new_data directly incremented if key exists then update value
Dic.setdefault (Key,value) This method once Key,value is created, and value does not specify [], {}, set (), values cannot be changed
new_dic = Dict.fromkeys (iterable,values)
new_dic = Dict.fromkeys ([11,22],[33,44])-->{11: [ [+], []]
the preceding key corresponds to the same value, and when the following value is a mutable data type, such as a list, change any value,
the other will also change, shallow copy, otherwise do not change
Delete:
Dic.pop (key) specifies that key is deleted, the last item is deleted by default, the value corresponding to delete key is deleted, if key does not exist error Keyerror:
Dic.popitem () random Delete, return the deleted key value pair in tuple form
3.6 above dictionary built-in for order, Popitem () default Delete last, but interview to say random delete
del Dic[key] Specifies key deletion, if key does not exist error Keyerror:
del dic
dic.clear ()
Change:
Dic[key] = New_data
dic.update (dic1) DIC updates dic1 to its own dictionary, and key is repeatedly updated to the new value
Check:
Dic[key] Key does not exist, error
Dic.get[key, "Custom return value not present"] key does not exist by default return None, can also customize the return value
Dic.keys () saves all keys in a copy-list format, which can be iterated
dic.values ()
Dic.items ()
for I in DIC I is the key of DiC
Method:
Len (DIC)
set: Set
# mutable data types, unordered, elements unique and non-repeatable, non-hashed, immutable data types, can iterate
# in the form of {}, mainly used to remove weight
# cannot be sliced (unordered), can be added and censored
Increase:
Set.add (Element)
set.update (iterable) Iteration of update to set
Delete:
Set.pop () random deletion
Set.remove (Element) No error
set.clear ()
del set
Change:
due to disorder, can only be deleted after the whole add the way
Set.remove (elements to be changed)
Set.add (new Element)
Check:
For i in set:
Method:
Len (set)
intersection: & the same part
The set: | Fitness
The difference set A-B removes the element in a, minus
s = Frozenset ([11,22,33]) changes the variable data type to immutable

Python Basic Data type rollup

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.