Use of Python

Source: Internet
Author: User

1. Common data types

1). digital int float LONG

2). Boolean True False

3). String

4). List

5). Tuples

6). Dictionary

2, Strip () # #去掉字符串前后的空格

3. Split () # #按空格切割字符串

4.Insert() # #向列表中插入元素

Insert a new element in the 2nd position of the name list:

>>> name = [' Jim ', ' Kek ', ' Alex ', ' Wusir ']
>>> Name.insert (2, ' Eric ')
>>> Name
[' Jim ', ' Kek ', ' Eric ', ' Alex ', ' Wusir ']
>>>

5. Count the number of specified elements in a list:

>>> Name
[' Jim ', ' Kek ', ' Alex ', ' Wusir ', ' Jim ']
>>> Name.count (' Jim ')
2
>>>

6. Delete the last element in the list

>>> Name
[' Jim ', ' Kek ', ' Alex ', ' Wusir ', ' Jim ']
>>> name.pop ()
' Jim '
>>> Name
[' Jim ', ' Kek ', ' Alex ', ' Wusir ']
>>>

7. Sorting in the list

>>> Name
[' Jim ', ' Kek ', ' Alex ', ' Wusir ', ' 89 ', ' 28 ']
>>> name.sort ()
>>> Name
[' Up ', ' kek ', ' Alex ', ' Jim ', ' Wusir ', '
>>>

When the list of elements with numbers and letters is combined, sort the numbers in ascending order, and then install the letters in sequence!

8, according to the number of repetitions of the specified element in the list n, loop n the list, delete the specified element:

>>> name = [' Ten ', ' + ', ' cooki ', ' the ' ', ' ' Alex ', ' the ' ' Kek ', ' Jim ', ' Kek ', ' a ', ' ' 10 ', ' 10 ', ' 10 ', ' 10 ']
>>> for i in range (Name.count (')):
... name.remove (' ten ')
...
>>> Name
[' Cooki ', ' the ' ', ' ' ' ' ', ' ' Alex ', ' the ' Kek ', ' Jim ', ' the ' ' KEK ']
>>>

9. Judging the inclusion relationship of elements and lists

>>> Name
[' Cooki ', ' the ' ', ' ' ' ' ', ' ' Alex ', ' the ' Kek ', ' Jim ', ' the ' ' KEK ']
>>> name = [' Alex ', ' Cooki ', ' Jim ', ' Kek ', ' KEK ']
>>> ' Alex ' in name
True
>>>

10. List to tuple

>>> name = [' Alex ', ' Cooki ', ' Jim ', ' KEK ']

>>> tuple_name = tuple (name)
>>> Tuple_name
(' Alex ', ' Cooki ', ' Jim ', ' KEK ')
>>>

11, the tuple is sent to the list

>>> Tuple_name
(' Alex ', ' Cooki ', ' Jim ', ' KEK ')
>>> list_name = List (tuple_name)
>>> List_name
[' Alex ', ' Cooki ', ' Jim ', ' KEK ']
>>>

12, in Python to determine whether the input is a string or a number

>>> str1 = ' Hello '

>>> str1.isdigit () # #判断是否为数字
False
>>>

>>> Str1.isalpha () # #判断是否为字符
True
>>>

>>> str2 = "123"
>>> str2.isdigit ()
True
>>> Str1.isalpha ()
True
>>>

Use of Python

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.