Python Learning Tutorial 2

Source: Internet
Author: User

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

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.