Python variables and data types

Source: Internet
Author: User
This article describes Python variables and data types in detail. For more information, see Python variables and data types

I. integer
int = 20print intprint 45678 + 0x12fd2
II. floating point number
float = 2.3print float
III. string
A. Use single quotation marks (') to enclose strings with single quotation marks, for example, str = 'This is string' print strb, and double quotation marks (") double quotation marks use strings exactly the same as those in single quotes. for example, str = "this is string"; print strc. use three quotation marks (''') to use three quotation marks, A multi-line string. single and double quotation marks can be freely used in three quotes. for example, str = '''This is stringthis is pythod stringthis is string ''' print str
IV. Boolean value
And: Returns True if all values are True. Or: or operation. if either of them is True, or is True. Not: non-operation. it is a single-object operator that converts True to False and False to True. Bool = Falseprint boolbool = Trueprint bool
5. null values
A null value is a special value in Python, expressed as None. None cannot be understood as 0, because 0 is meaningful, and None is a special null value.
VI. list
#-*-Coding: UTF-8-*-lst = ['A', 'B', 1996,201 7] nums = [1, 3, 5, 7, 8, 13, 20] # print "nums [0]:", nums [0] #1 print "nums []:", nums [] # [5, 7, 8] print "nums [1:]:", nums [1:] # [3, 5, 7, 8, 13, 20] print "nums [: -3]: ", nums [:-3] # [1, 3, 5, 7] print" nums [:]: ", nums [:] # [1, 3, 5, 7, 8, 13, 20] # Update the list nums [0] = "ljq" print nums [0] # delete the list element del nums [0] ''' nums [:]: [3, 5, 7, 8, 13, 20] ''' print "nu MS [:]: ", nums [:] # List script operator print len ([1, 2, 3]) #3 print [1, 2, 3] + [4, 5, 6] # [1, 2, 3, 4, 5, 6] print ['Hi! '] * 4 # [' Hi! ', 'Hi! ', 'Hi! ', 'Hi! '] Print 3 in [1, 2, 3] # Truefor x in [1, 2, 3]: print x, #1 2 3 # List truncation L = ['spam', 'spam', 'Spam! '] Print L [2] #' SPAM! 'Print L [-2] # 'spam' print L [1:] # ['spam', 'Spam! '] # List functions & methods lst. append ('append') # Add a new object lst at the end of the list. insert (2, 'insert') # insert the object to the list lst. remove (1996) # remove the first matching item lst for a value in the list. reverse () # reverse list element, reverse print lst. sort () # sort the original list print lst. pop (1) # remove an element from the list (the last element by default) and return the value of this element print lstprint lst. count ('obj ') # count the number of times an element appears in the list lst. index ('append') # locate the index location of the first matching item of a value from the list, and the index starts from 0 to lst. extend (lst) # append multiple values in another sequence at a time at the End of the list (extend the original list with the new list) print 'end: ', lst
VII. dictionary

A dictionary is the most flexible built-in data structure type in python except the list. A list is a combination of ordered objects, and a dictionary is a collection of unordered objects. The difference between the two is that the elements in the dictionary are accessed by keys, rather than by offset.

#-*-Coding: UTF-8-*-dit = {'name': 'Zara ', 'age': 7, 'class ': 'first'} dict1 = {'ABC': 456} dict2 = {'ABC': 123, 98.6: 37} seq = ('name', 'age ', 'Sex ') # print the value in the access dictionary "dit ['name']:", dit ['name'] print "dit ['age']:", dit ['age'] # Modify the dictionary dit ["age"] = 27 # modify the value of an existing key dit ["school"] = "wutong" # Add a new key/value for print "dict ['age']: ", dit ['age'] print" dict ['school ']: ", dit ['school '] # Delete the dictionary del dit ['name'] # delete the entry d whose key is 'name' It. clear () # clear all dictionary entries del dit # delete dictionary dit = {'name': 'Zara ', 'age': 7, 'class ': 'first'} # Dictionary built-in functions & methods cmp (dict1, dict2) # compares two dictionary elements. Len (dit) # calculate the number of dictionary elements, that is, the total number of keys. Str (dit) # output the printable string representation of the dictionary. Type (dit) # return the input variable type. if the variable is a dictionary, the dictionary type is returned. Dit. copy () # returns a dictionary's shortest copy dit. fromkeys (seq) # Create a new dictionary and use the elements in sequence seq as the dictionary key. val is the initial value dit corresponding to all the keys in the dictionary. get (dit ['name']) # return the value of the specified key. if the value is not in the dictionary, the default value dit is returned. has_key ('class') # if the key returns true in the dictionary dict, otherwise falsedit is returned. items () # returns the recyclable (key, value) tuples in the list dit. keys () # returns a list of all dictionary keys dit. setdefault ('subobject', 'Python') # similar to get (), but if the key does not exist in the dictionary, a key is added and the value is set to defaultdit. update (dict2) # update the dictionary dict2 key/value pair to dict dit. values () # return all the values in the dictionary in the list. clear () # Delete all elements in the dictionary
8. Yuanzu

The Python tuple is similar to the list, except that the elements of the tuples cannot be modified. the tuples use parentheses () and the list uses square brackets []. the creation of tuples is very simple, you only need to add elements in brackets and separate them with commas.

#-*-Coding: UTF-8-*-tup1 = ('Physics ', 'Chemistry', 1997,200 0) tup2 = (1, 2, 3, 4, 5) tup3 = "a", "B", "c", "d" # access tuples print "tup1 [0]:", tup1 [0] # physicsprint "tup1 []:", tup1 [] # ('Chemistry ', 1997) # modify tup4 = tup1 + tup2print tup4 # (12, 34.56, 'ABC', 'XYZ') # delete tuples tup = ('tup3', 'Tup', 1997,200 0) print tupdel tup # tuple Index & cut L = ('spam', 'spam', 'Spam! ') Print L [0] # spamprint L [1] # Spamprint L [2] #' SPAM! 'Print L [-2] # 'spam' print L [1:] # ['spam', 'Spam! '] # Tuples built-in function print cmp (tup3, tup2) # compares two tuples. Len (tup3) # calculate the number of tuples. Max (tup3) # returns the maximum value of elements in the tuples. Min (tup3) # returns the minimum element value in the tuples. L = [1, 2, 3, 4] print Lprint tuple (L) # Convert the list into tuples.
9. define a string
\ N indicates line feed \ t indicates a tab character \ represents \ character itself
10. Unicode string
Python default encoding ASCII code #-*-coding: UTF-8 -*-
11. digital type conversion
Int (x [, base]) converts x to an integer float (x) and converts x to a floating point complex (real [, imag]) to create a plural str (x) convert object x to string repr (x) convert object x to expression string eval (str) to calculate a valid Python expression in the string and return an object tuple (s) convert sequence s to a single tuples list (s) convert sequence s to a list chr (x) convert an integer to a character unichr (x) convert an integer to the Unicode character ord (x) and convert a character to its integer hex (x). convert an integer to a hexadecimal string oct (x) converts an integer to an octal string.

The above is a detailed description of Python variables and data types. For more information, see other related articles in the first PHP community!

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.