Python Common data types

Source: Internet
Author: User

One, string (strings)

The string is the most commonly used data type in Python. We can use quotation marks (' or ') to create a string.

var = ' Today is March 2018 ' Print (VAR)  # Output string print (Var[0:8])  # Enter characters from first to seventh

String to go to space:

str = '  aaa1bbb2cccc4dddddd8  ' Print (Str.strip ())  # Strip Remove space on both sides of the string print (Str.lstrip ())  # Lstrip Remove the space to the left of the string print (Str.rstrip ())  # Rstrip Remove the space to the right of the string

When a value is filled in the parentheses of the Strip (), the value is quickly removed from the string (although this value can only be the beginning or end of the string)

Some other common methods:

Print (Str.count (' d ')  # count () counts the number of occurrences of a character print (Str.index (' B '))  # Index () to find the word Poute, the subscript starts at 0, and only shows the subscript at the first occurrence, If you can't find the error print (Str.find (' B '))  # fins () find the word Poute, and the index () usage is similar, the difference is, find if not found will not error, return -1print (' AAA ', ' AAA ')  # Replace (), string replace print (Str.isdigit ())  # IsDigit (), determine whether the string is all digital print (Str.startswith (' AA '))  # StartsWith (), determines whether the character starts with a AA print (Str.endswith (' DD '))  # Endswitch (), determines whether the string ends with DD print (Str.upper ())  # Convert string contents to uppercase print (Str.lower ())  # Converts the contents of a string to lowercase

Second, list (listing)

List is the most commonly used PYTHON6 basic data (number,string,list,tuple,sets,dictionary) type, creating a list of different data items separated by commas using square brackets. As shown below:

List1 = [' aaa ', ' BBB ', ' CCC ']list2 = [1, 3, 5, 7, 9]

List some common methods:

List1 = [' aaa ', ' BBB ', ' CCC ']list1.append (' ddd ')  # append a dddlist1.insert in List1 (3, ' FFF ')  # Insert List1.pop at the specified position (0)  # Delete the specified position element list1.remove (' BBB ') according to the Subscript  # Delete the specified element, if not, will error List1.clear ()  # empty list del list1[-1]  # Delete the element at the specified position list1[0] = ' AAA '   # modifies the element print (List1.index (' BBB ')) in the specified position according to the subscript print ("List1.count (')" in the  list of elements AAA ')  # Number of occurrences of the statistic element in the list list1.reverse ()  # flips the list to print (List1)

List sort:

num = [111, 4, 567,, 0]num.sort ()  # Sort the list in ascending order print (num) num.sort (reverse=true)  # Sort the list in descending order print (num)

Third, tuple (tuple)

A python 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. The following example:

t = (' A ', ' B ', ' C ', ' G ', ' r ', ' a ', ' f ', ' h ') T1 = (1, 4, 6, t2) = ' "", ' GG ', ' DFFG '

The loop value of the tuple, the slice value, and the subscript value are similar to the list, but the elements of the tuple cannot be modified (new, modified, deleted)

Iv. Dictionary (dictionary)

A dictionary is another mutable container model and can store any type of object. Each key value of the dictionary (Key=>value) is split with a colon (:), each pair is separated by a comma (,), and the entire dictionary is enclosed in curly braces ({}) , as shown in the following format:

D = {' name ': ' Tom ', ' sex ': ' Male ', ' age ': ' + ', ' addr ': ' US '}print (type (d))

Some common methods of dictionaries:

Print (Dict.get (' name '))  # based on key value print (Dict.get (' phone ', ' 183 ')  # based on key value, if not the default 183print (dict[' name '])  # based on key value, if not to error dict[' phone ' = ' 18380474562 '  # Add a field to the dictionary and specify the value Dict.setdefault (' phone ', ' 18385014515 ')  # Add a field to the dictionary and specify a value of Dict.setdefault (' name ', ' Lily ')  # If key exists, the value dict.pop (' addr ') will not be modified  # Specify key to delete Dict.popitem ()  # randomly delete a keydel dict[' name '  # specify key to delete Dict.clear ()  # Empty the dictionary print (Dict.keys ())  # Gets the dictionary all the Keyprint (Dict.values ())  # Gets the dictionary all the Valueprint (Dict.items ())  # gets all the Key-values, which is the output of the entire dictionary

Python Common data types

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.