0015 _ code implementation of various data types, 0015 Data Types

Source: Internet
Author: User

0015 _ code implementation of various data types, 0015 Data Types

_ Author _ = 'prop 93 '#! /Usr/bin/env python #-*-coding: UTF-8-*-n1 = 123n2 = 456 print (n1 + n2) print (n1. _ add _ (n2 )) n3 = 4 print (n3.bit _ length () # bitwise 4 is 100-> 3
Digital code implementation
_ Author _ = 'prop 93 '#! /Usr/bin/env python #-*-coding: UTF-8-*-# environment: python3.6s = "alex" # print (s [0]) # a # print (s [1]) # l # print (s [2]) # e # print (s [3]) # x # print (s [0: 2]) # The value obtained from al slice [a, B] is 0 <= Result position <value between B [0: 2], that is, 0, 1 # print (s [0: 4]) # while loop # start = 0 # while start <len (s): # print (s [start]) # start + = 1 # for Loop for item in s: if (item = 'l'): continue print (item)
String code implementation
_ Author _ = 'prop 93 '#! /Usr/bin/env python #-*-coding: UTF-8-*-name_list = ['Alex ', 'Eric ', 'rain'] ''' # index print (name_list [0]) #-> alex # Slice print (name_list [0: 2]) #-> ['Alex ', 'Eric '] # lenprint (name_list [2: len (name_list)]) # greater than or equal to 2, less than 3, result: rain-> ['rain'] # forfor I in name_list: print (I) ''' "" # other functions # append element name_list.append ('seven ') name_list.append ('seven') print (name_list) # count the number of occurrences print (name_list.count ('seven ')) #-> 3 # iterable iteratable # extended list temp = ['test', 'hahaha'] name_list.extend (temp) print (name_list) #-> ['Alex ', 'Eric ', 'rain', 'seven', 'test ', 'hahaha'] # index positioning element position print (name_list.index ('Eric ') # insert name_list.insert (1, 'SB') print (name_list) # pop deletes the last value of the list and returns the print (name_list.pop () of the last element. # remove an element name_list.remove ('seven') print (name_list) # reverse invert name_list.reverse () print (name_list) # sort by the ASCII code corresponding to the first character from small to big sort name_list.sort () print (name_list) "# del Delete the specified index location print (name_list) del name_list [1] print (name_list)
List code implementation
_ Author _ = 'prop 93 '#! /Usr/bin/env python #-*-coding: UTF-8-*-#1. tuples and lists are almost the same #2. tuples cannot be modified, and the list can be modified ######## tuples ####### name_tuple = ('Alex ', 'Eric ') # index print (name_tuple [0]) # lenprint (name_tuple [len (name_tuple)-1]) # Slice print (name_tuple []) #0 <= <1 fetch 0th records, alex # forfor I in name_tuple: print (I) # count calculation element appears print (name_tuple.count ('Alex ')) # print (name_tuple.index ('Alex '))
Code Implementation of tuples
_ Author _ = 'prop 93 '#! /Usr/bin/env python #-*-coding: UTF-8-*-# each element of the dictionary is a key-value pair. User_info = {'name': 'Alex ', 'age': 73, 'gender': 'M'} # index # print (user_info ['age']) #-> 73 ### no slice #### for loop default output key # for I in user_info: # print (I) ## a = user_info.keys () # obtain all keys # B = user_info.values () # obtain the values of all keys # c = user_info.items () # obtain all key-value pairs # print (a) # print (B) # print (c) # dict_items ([('name', 'Alex '), ('age', 73), ('gender', 'M')]) # for I in user_info.keys (): # print (I) ### for I in user_info.values (): # print (I) ### for I in user_info.items (): # print (I) for k, v in user_info.items (): # k, v get key, value print (k) print (v) # clear all content # user_info.clear () # print (user_info) # get gets the value based on the key. If the key does not exist, you can specify the default value: val = user_info.get ('age') print (val) # If the index does not exist, an error is returned, if the get key does not exist, you can specify the return value val = user_info.get ('dslfk', "does not exist") print (val) # python3.6 without the has_key method, but it can be implemented: ret = 'age' in user_info.keys () print (ret) # pop gets and removes the specified key entry # popitem gets and removes the last one # update: print (user_info) test = {'a1': 123, 'a2 ': 456} user_info.update (test) print (user_info) # deldel test ['a1'] print (test)
Dictionary code implementation
_ Author _ = 'prop 93 '#! /Usr/bin/env python #-*-coding: UTF-8-*-#1. enumerate # l1 = ['computer ', 'mouse pad', 'U disk', 'yacht '] # for item in l1: # print (item) # indium = input ('Enter the item: ') "# enumerate automatically adds the serial number key to the list (starting from 0 by default) l1 = ['pc ', 'mouse pad ', 'U disk', 'yacht'] print (l1.index ('computer ') for key, item in enumerate (l1): print (key, item) ('Enter the product serial number: ') # convert the string to intprint (l1 [int (indium)]) # If enumerate specifies that the start value does not start from 0, starting from 1, the computer is still displayed when the input is 1. We need to change it to print (l1 [int (indium)]) in print (l1 [int (indium)-1]). "# In python2.7 # range, xrange # range is used to obtain the number within the specified range, range) create a memory space from 0 to 2.7 # xrange is not created yet, only created in the for loop # range in Python3 is equivalent to xrangeprint (range () in )) # range (1, 10) # print (range () # In python2.7->, 9] for I in range (, 2): # The third parameter can be inserted in the range, indicating the step size print (I) for I in range (,-2 ): print (I) "" li = ['Alex ', 'Eric'] # print all index values # method 1 ret = enumerate (li) for I, j in ret: print (I) # Method 2: leee = len (li) for I in range (0, leee): print (I, li [I])
Other content

 

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.