Detailed description of Python variables and data types

Source: Internet
Author: User
python variables and data types

One, integer

int = 20print Intprint 45678 + 0x12fd2

Second, floating point number

float = 2.3print float

Three, string

A, use single quotation marks (') to enclose the string in single quotation marks, for example: str = ' This is string ' Print strb, use double quotation marks (") in double quotation marks and string usages in single quotes exactly the same, for example: str =" This is a string "; Print STRC, using three quotation marks ("") to represent multiple lines of string, can be used to freely use single and double quotation marks in three quotation marks, for example: str = "This is Stringthis is Pythod stringthis is a string" Print str

Four, Boolean value

And: With operations, only the result of all true,and operations is true. Or: Or operation, as long as one of the true,or operation results is true. Not: Non-arithmetic, which is a monocular operator that turns true into False,false to true. bool = Falseprint Boolbool = trueprint bool

Five, null value

The null value is a special value in Python, denoted by 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, 2017]nums = [1, 3, 5, 7, 8, 13, 20]# the value in the Access list print "nums[0]:", Nums[0] # 1PRI  NT "Nums[2:5]:", Nums[2:5] # [5, 7, 8]print "Nums[1:]:", Nums[1:] # [3, 5, 7, 8,, 20]print "nums[:-3]:", nums[:-3] # [1, 3, 5, 7]print "nums[:]:", nums[:] # [1, 3, 5, 7, 8, 13, 20]# update list nums[0] = "LJQ" Print nums[0]# delete list element del nums[0] "n ums[:]: [3, 5, 7, 8, +] "print" nums[:]: ", nums[:]# list script operator print len ([1, 2, 3]) # 3print [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 intercept L = [' spam ', ' spam ', ' spam! '] Print l[2] # ' spam! ' Print L[-2] # ' Spam ' Print l[1:] # [' Spam ', ' spam! '] # List Function & method Lst.append (' Append ') # Add a new object at the end of the list Lst.insert (2, ' Insert ') # Inserts an object into the list Lst.remove (1996) # Removes the first occurrence of a value in the list LST. Reverse () # Reverse List of elements, reverse print lst.sort () # sort the original list print Lst.pop (1) # removes an element from the list (the last element by default), and returns the value of the element print Lstprint lst.co Unt (' obj ') # statistic an element appears in the listNumber of times Lst.index (' append ') # Find the index position of the first occurrence of a value from the list, index starting at 0 lst.extend (LST) # Append multiple values from another sequence at the end of the list (the original list is expanded with a new list) print ' End: ' Lst

Seven, the dictionary

The Dictionary (dictionary) is the most flexible built-in data structure type in Python, in addition to the list. A list is an ordered combination of 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, not by offsets.

#-*-Coding:utf-8-*-dit = {' name ': ' Zara ', ' age ': 7, ' class ': ' first '}dict1 = {' abc ': 456}DICT2 = {' abc ': 123, 98.6:37} Seq = (' name ', ' age ', ' sex ') # access the value in the dictionary print "dit[' name ']:", dit[' name ']print "dit[' age ':", dit[' age ']# Modify dictionary dit["age"] =  27 # Modify the value of the existing key dit["school"] = "Wutong" # Add a new key/value pair to print "dict[' Age '": ", dit[' age ']print" dict[' School ']: ", dit[' school ']# Delete Dictionary del dit[' name ' # Delete key is ' name ' entry dit.clear () # Empty dictionary all entries del dit # Delete dictionary dit = {' name ': ' Zara ', ' age ': 7, ' class ': ' Firs T '}# dictionary built-in Functions & Methods CMP (Dict1, DICT2) # compare two dictionary elements. Len (DIT) # computes the number of dictionary elements, that is, the total number of keys. The Str (DIT) # Output dictionary is a printable string representation. Type (DIT) # Returns the type of the variable entered and returns the dictionary type if the variable is a dictionary. Dit.copy () # Returns a shallow copy of a dictionary dit.fromkeys (seq) # Creates a new dictionary with the keys of the elements in the sequence seq, Val is the initial value corresponding to the dictionary for all Keys Dit.get (dit[' name ']) # Returns the value of the specified key, If the value does not return the default value in the Dictionary Dit.has_key (' class ') # If the key returns true in the dictionary dict, otherwise returns FALSEDIT.ITEMS () # Returns the iterated (key, value) tuple array Dit.keys () # in the list Returns a dictionary of all keys Dit.setdefault (' Subject ', ' Python ') # and get () similar, but if the key does not already exist in the dictionary, the key will be added and the value will be set to Defaultdit.update (DICT2) # Update the key/value pairs of the dictionary dict2 to dict DIT.VAlues () # Returns all values in the dictionary as a list dit.clear () # Delete all elements in the dictionary 

Eight, Ganso

A python tuple (tuple) is similar to a list, except that the elements of a tuple cannot be modified; tuples use parentheses (), and the list uses square brackets []; tuple creation is simple, just add elements in parentheses and separate them with commas (,).

#-*-Coding:utf-8-*-tup1 = (' Physics ', ' Chemistry ', 1997, $) tup2 = (1, 2, 3, 4, 5) Tup3 = "A", "B", "C", "D" # Access tuple Prin T "Tup1[0]:", Tup1[0]  # physicsprint "Tup1[1:3]:", Tup1[1:3]  # (' Chemistry ', 1997) # Modify Tuple Tup4 = Tup1 + tup2print Tup4  # (34.56, ' abc ', ' XYZ ') # Delete tuple Tup = (' Tup3 ', ' Tup ', 1997, $) Print Tupdel tup# tuple index & intercept L = (' spam ', ' Spam ', ' spam! ') Print L[0] #  Spamprint l[1]  # spamprint l[2]  # ' spam! ' Print l[-2]  # ' Spam ' Print l[1:]  # [' Spam ', ' spam! '] # tuple built-in function print CMP (TUP3, tup2)  # compares two tuple elements. Len (TUP3)  # Calculates the number of tuple elements. Max (TUP3)  # Returns the element's maximum value in a tuple. Min (tup3)  # Returns the element minimum value in the tuple. L = [1, 2, 3, 4]print lprint tuple (L)  # Converts the list to tuples.

Nine, define the string

\ n indicates a newline \ t means a tab \ \ means \ character itself

Ten, Unicode string

Python default encoding ASCII encoding #-*-Coding:utf-8-*-

Xi. Conversion of numeric types

int (x [, Base]) converts x to an integer, float (x) converts x to a floating-point number complex (real [, Imag]) creates a complex number str (x) to convert the object x to a string repr (x) to convert the object x to the expression string eval (St R) is used to calculate a valid Python expression in a string and returns an object tuple (s) converts the sequence s to a tuple list (s) converts the sequence s to a list Chr (x) converts an integer to a character unichr (x) Converts an integer to a Unicode character ord (x) converts a character to its integer value hex (x) converts an integer to a hexadecimal string Oct (x) converts an integer to an octal string
Related Article

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.