1 constant definitions (const needs to be created by formation method)
Class _const (object): class Consterror (TypeError):p the "__setattr__" (Self, Name, value): if self.__ Dict__.has_key (name): raise self. Consterror, "Can ' t rebind const (%s)"%name self.__dict__[name]=value def __delattr__ (self, name): if Name In self.__dict__: raise self. Consterror, "Can ' t unbind const (%s)"%name raise Nameerror, Nameimport sys sys.modules[__name__] = _const ()
2 data types
- Integer (int) Example: 0, 6,-2, 2015,-203
- Long Integer Example: 56990l, -12694l, 938476l
- Float type (float) Example: 7.5325, 9.434, 6.66
- Boolean (BOOL) Example: True, False
- Plural type (complex) Example: 6+4j, -5+12j, 98+9j
3 string
"Single quote" "Double quotation mark" "" "" "three quotation marks (can be nested)
4 escape characters and line breaks
Print (complex (4,5))
coment = ' i\ ' m young '
Print (coment)
Print ("Young\npeople")
5 natural character and string repetition
Natural string literals are understood to preserve the format of the string itself, without being affected by escaping.
The literal meaning of string repetition is to repeat the output of the string.
Instance code:
Comment=r ' Our \nyoung ' print (comment) description= "We \nyoung" print (description) three= "Our young\n" *3print (three)
6 Other important data types
#Coding=utf-8#Listpeople=["Liu","as","Zhang San","John Doe","Harry","Zhao Liu","Sun VII","Week eight","Wu Jiu"]PrintPeople[3]#Meta-groupNames= ("Liu","as","Zhang San","John Doe","Harry","Zhao Liu","Sun VII","Week eight","Wu Jiu")PrintPeople[1]#CollectionXitems=set ("1222234566666789") Xitems.add ("x") Xitems.remove ("8") Xitems.discard ("8")PrintXitemsyitems=set ("1357")#intersectionPrint("intersection: {0}". Format (Xitems&yitems))#Xitems&yitems = xitems.intersection (yitems)#and setPrint("The set: {0}". Format (Xitems|yitems))#Xitems|yitems = xitems.union (yitems)#Difference SetPrint("difference set: {0}". Format (Xitems-yitems))#Xitems-yitems = xitems.difference (yitems)Xitems.pop () xitems.clear ( )Print("Xitems Collection is emptied: {0}". Format (xitems))#Dictionarydictionary={'name':'Toutou'," Age":" -","Sex":"male"}Printdictionary["name"]#add an item to the dictionarydictionary['Hobby']='Cnblogs'Printdictionary["Sex"]Printdictionary["Hobby"]Data Type
Python Learning Tutorial 2