Python-based data types

Source: Internet
Author: User

Python-based data type string (string)
    • Enclose a string in quotation marks, where the quotation marks can be single or double quotes

1. Use the method to modify the case of a string

例:>>> name = "ada lovelace">>> print name.title()Ada Lovelace>>> print(name.upper())ADA LOVELACE

2. Merging (stitching) strings

例:>>> first_name = "ada">>> last_name = "lovelace">>> full_name = first_name + " " + last_name>>> print full_nameada lovelace

3. Use tabs or newline characters to add blanks

例:>>> print("Python")Python>>> print("\tPython")    Python>>> print("\nPython\nhello")Pythonhello>>> print("\nPython\n\thello")Python    hello

4. Remove the end-to-end blanks

例:>>> message = ‘ python ‘>>> message‘ python ‘>>> message.rstrip()‘ python‘>>> message.lstrip()‘python ‘>>> message.strip()‘python‘    
Digital

1. Integer (int)

2. Floating point (float)

3. Long Integer type

4. Boolean type (BOOL)

5. Complex Type (complex)

Listing (list)
    • A list consists of a series of elements arranged in a specific order, using square brackets ([]) to represent the list, and separating the elements with commas.

Access list elements, index starting from 0 instead of 1

例:>>> bicycles = [‘trek‘, ‘cannondale‘, ‘redline‘, ‘specialized‘]>>> print bicycles[0]trek>>> print bicycles[1]cannondale>>> print bicycles[-1]specialized

modifying, adding, and deleting elements

Example:>>> motorcycles = [' Honda ', ' Yamaha ', ' Suzuki ']>>> print motorcycles[' Honda ', ' Yamaha ', ' Suzuki '] modified >>> motorcycles[0] = ' Ducati ' >>> print motorcycles [' Ducati ', ' Yamaha ', ' Suzuki '] add >&G T;> motorcycles.append (' Ducati ') >>> print motorcycles [' Honda ', ' Yamaha ', ' Suzuki ', ' Ducati '] Append Just add elements at the end to insert elements in the list with insert () >>> motorcycles = [' Honda ', ' Yamaha ', ' Suzuki '] >>> motorcycles.ins ERT (0, ' Ducati ') >>> print motorcycles [' Ducati ', ' Honda ', ' Yamaha ', ' Suzuki '] Delete the Del method----use the DEL statement to delete the value from the list    In addition, you will no longer be able to access it >>> del motorcycles[0] >>> print motorcycles [' Yamaha ', ' Suzuki '] Pop () method    1. Delete the last element of the list >>> Motorcycles.pop () ' Suzuki ' >>> print motorcycles [' Honda ', ' Yamaha '] 2. Specify element deletion >>> motorcycles.pop (0) ' Honda ' >>> print motorcycles [' Yamaha ', ' Suzuki '] Remov The E () method----Delete the first specified value

Organization List (sort)

1. Use the method sort () to sort the list in a permanent order

例:>>> cars = [‘bmw‘, ‘audi‘, ‘toyota‘, ‘subaru‘]>>> cars.sort()>>> print cars[‘audi‘, ‘bmw‘, ‘subaru‘, ‘toyota‘]反向排序----sort()传递参数reverse=True>>> cars.sort(reverse=True)>>> print cars[‘toyota‘, ‘subaru‘, ‘bmw‘, ‘audi‘]

2. Use the function sorted () to sort the list 临时

    • When the function sorted () is called, the order of the list elements is not changed

3. Reverse the Print List

例:>>> cars = [‘bmw‘, ‘audi‘, ‘toyota‘, ‘subaru‘]>>> cars.reverse()>>> print cars[‘subaru‘, ‘toyota‘, ‘audi‘, ‘bmw‘]

4. Length of the list

>>> cars = [‘bmw‘, ‘audi‘, ‘toyota‘, ‘subaru‘]>>> len(cars)4

Action list

1. Traverse the entire list

例:>>> cars = [‘bmw‘, ‘audi‘, ‘toyota‘, ‘subaru‘]>>> for car in cars:...     print car...bmwauditoyotasubaru

2. Create a list of values

① using function range ()

例:>>> for i in range(1,5):...     print i...1234

② using range () to create a list of numbers

例: >>> num = list(range(1,5))>>> print num[1, 2, 3, 4]

3. Perform simple statistics on a list of numbers

例:>>> digits = [1, 2, 3, 4, 5, 6, 7, 8, 9, 0]>>> min(digits)0>>> max(digits)9>>> sum(digits)45

Use part of a list

1. Slicing

例:>>> players = [‘charles‘, ‘martina‘, ‘michael‘, ‘florence‘, ‘eli‘]>>> print players[1:3][‘martina‘, ‘michael‘]

2. Traversing slices

例:>>> players = [‘charles‘, ‘martina‘, ‘michael‘, ‘florence‘, ‘eli‘]>>> for player in players[:3]:...     print player.title()...CharlesMartinaMichael
Tuples (TUPE)
    • Immutable lists are referred to as tuples
    • Tuples look like lists, but use parentheses instead of square brackets to identify
    • Tuple operations are the same as lists

modifying tuple elements

element values in tuples are not allowed to be modified, but we can concatenate combinations of tuples

例:>>> t = (12, 13, 14, 15, 21, 22 )>>> t1 = t[0:3]>>> t2 = (19,)>>> t3 = t[4:]>>> t = t1 + t2 + t3>>> print t(12, 13, 14, 19, 21, 22)

Delete a tuple element

例:>>> t = (12, 13, 14, 15, 21, 22 )>>> t1 = t[0:3] + t[4:]>>> t = t1>>> print t(12, 13, 14, 21, 22)
Dictionary (dict)
    • A dictionary is another mutable container model and can store any type of object
    • Each key value of the dictionary (key=>value) pairs with a colon (:) split, each pair is separated by a comma (,), and the entire dictionary is included in curly braces ({})

Accessing dictionaries

例:>>> dict = {‘Name‘: ‘Runoob‘, ‘Age‘: 7, ‘Class‘: ‘First‘}>>> print dict[‘Name‘]Runoob

Modify Dictionary

例:>>> dict = {‘Name‘: ‘Runoob‘, ‘Age‘: 7, ‘Class‘: ‘First‘}>>> print dict{‘Age‘: 7, ‘Name‘: ‘Runoob‘, ‘Class‘: ‘First‘}>>> dict[‘Age‘] = 11>>> print dict{‘Age‘: 11, ‘Name‘: ‘Runoob‘, ‘Class‘: ‘First‘}

Delete an element in a dictionary

例:>>> dict = {‘Name‘: ‘Runoob‘, ‘Age‘: 7, ‘Class‘: ‘First‘}>>> del dict[‘Name‘]>>> print dict{‘Age‘: 7, ‘Class‘: ‘First‘}
Collection (SET)
    • Sequence of unordered non-repeating elements
    • The basic function is to test the membership and remove duplicate elements
    • You can create a collection using curly braces ({}) or set () functions

Note: Creating an empty collection must be set () instead of {}, because {} is used to create an empty dictionary

例:>>> student = {‘Tom‘, ‘Jim‘, ‘Mary‘, ‘Tom‘, ‘Jack‘, ‘Rose‘}>>> print studentset([‘Mary‘, ‘Rose‘, ‘Jim‘, ‘Jack‘, ‘Tom‘])

Python-based data types

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.