Python Foundation II

Source: Internet
Author: User

The content of this section

list, tuple operations
String manipulation
Dictionary operations
Collection operations
File operations
Character encoding and transcoding

1. List, tuple operations

List is one of our most commonly used data types, with lists that make the most convenient storage for your data , modify, and so on

definition list
    
names = [' Alex ', "Tenglan", ' Eric ']

To access the elements in the list by subscript, subscript starting at 0 Count

>>> Names[0]
' Alex '
>>> NAMES[2]
' Eric '
>>> Names[-1]
' Eric '
>>> Names[-2] #还可以倒着取
' Tenglan '

slice: Take multiple elements

>>> names = ["Alex", "Tenglan", "Eric", "Rain", "Tom", "Amy")
>>> Names[1:4] #取下标1至下标4之间的数字, including 1, not including 4
[' Tenglan ', ' Eric ', ' Rain ']
>>> Names[1:-1] #取下标1至-1 value, excluding-1
[' Tenglan ', ' Eric ', ' Rain ', ' Tom ']
>>> Names[0:3]
[' Alex ', ' Tenglan ', ' Eric ']
>>> Names[:3] #如果是从头开始取, 0 can be ignored, the same as the effect of the sentence
[' Alex ', ' Tenglan ', ' Eric ']
>>> names[3:] #如果想取最后一个, must not write-1, only so write
[' Rain ', ' Tom ', ' Amy ']
>>> Names[3:-1] #这样-1 will not be included.
[' Rain ', ' Tom ']
>>> Names[0::2] #后面的2是代表, every other element, take a
[' Alex ', ' Eric ', ' Tom ']
>>> Names[::2] #和上句效果一样
[' Alex ', ' Eric ', ' Tom ']

Additional

>>> names
[' Alex ', ' Tenglan ', ' Eric ', ' Rain ', ' Tom ', ' Amy ']
>>> Names.append ("I'm new Here")
>>> names
[' Alex ', ' Tenglan ', ' Eric ', ' Rain ', ' Tom ', ' Amy ', ' I'm new ']

Insert

>>> names
[' Alex ', ' Tenglan ', ' Eric ', ' Rain ', ' Tom ', ' Amy ', ' I'm new ']
>>> Names.insert (2, "forcibly inserted from Eric Front")
>>> names
[' Alex ', ' Tenglan ', ' forcibly inserted from Eric ', ' Eric ', ' Rain ', ' Tom ', ' Amy ', ' I'm new ']

>>> Names.insert (5, "insert a new pose from behind Eric")
>>> names
[' Alex ', ' Tenglan ', ' force the insertion from Eric ', ' Eric ', ' Rain ', ' insert a new pose from behind Eric ', ' Tom ', ' Amy ', ' I'm new ']

Modify

>>> names
[' Alex ', ' Tenglan ', ' force the insertion from Eric ', ' Eric ', ' Rain ', ' insert a new pose from behind Eric ', ' Tom ', ' Amy ', ' I'm new ']
>>> names[2] = "It's time to substitute."
>>> names
[' Alex ', ' Tenglan ', ' the substitution ', ' Eric ', ' Rain ', ' insert a new pose from behind Eric ', ' Tom ', ' Amy ', ' I'm the new one ']


Delete

>>> del Names[2]
>>> names
[' Alex ', ' Tenglan ', ' Eric ', ' Rain ', ' insert a new pose from behind Eric ', ' Tom ', ' Amy ', ' I'm new ']
>>> del Names[4]
>>> names
[' Alex ', ' Tenglan ', ' Eric ', ' Rain ', ' Tom ', ' Amy ', ' I'm new ']
>>>
>>> names.remove ("Eric") #删除指定元素
>>> names
[' Alex ', ' Tenglan ', ' Rain ', ' Tom ', ' Amy ', ' I'm new ']
>>> Names.pop () #删除列表最后一个值
' I'm the new one. '
>>> names
[' Alex ', ' Tenglan ', ' Rain ', ' Tom ', ' Amy ']

Extended

>>> names
[' Alex ', ' Tenglan ', ' Rain ', ' Tom ', ' Amy ']
>>> B = [+ +]
>>> Names.extend (b)
>>> names
[' Alex ', ' Tenglan ', ' Rain ', ' Tom ', ' Amy ', 1, 2, 3]

Copy

>>> names
[' Alex ', ' Tenglan ', ' Rain ', ' Tom ', ' Amy ', 1, 2, 3]

>>> name_copy = Names.copy ()
>>> name_copy
[' Alex ', ' Tenglan ', ' Rain ', ' Tom ', ' Amy ', 1, 2, 3]

Statistics

>>> names
[' Alex ', ' Tenglan ', ' Amy ', ' Tom ', ' Amy ', 1, 2, 3]
>>> names.count ("Amy")
2

Sort & Flip

>>> names
[' Alex ', ' Tenglan ', ' Amy ', ' Tom ', ' Amy ', 1, 2, 3]
>>> Names.sort () #排序
Traceback (most recent):
File "<stdin>", line 1, in <module>
Typeerror:unorderable types:int () < Str () #3.0 The different data types can not be placed together to sort, wipe
>>> names[-3] = ' 1 '
>>> names[-2] = ' 2 '
>>> names[-1] = ' 3 '
>>> names
[' Alex ', ' Amy ', ' Amy ', ' Tenglan ', ' Tom ', ' 1 ', ' 2 ', ' 3 ']
>>> Names.sort ()
>>> names
[' 1 ', ' 2 ', ' 3 ', ' Alex ', ' Amy ', ' Amy ', ' Tenglan ', ' Tom ']

>>> Names.reverse () #反转
>>> names
[' Tom ', ' Tenglan ', ' Amy ', ' Amy ', ' Alex ', ' 3 ', ' 2 ', ' 1 ']

Get subscript

>>> names
[' Tom ', ' Tenglan ', ' Amy ', ' Amy ', ' Alex ', ' 3 ', ' 2 ', ' 1 ']
>>> Names.index ("Amy")
2 #只返回找到的第一个下标

Meta-group

Tuples are actually similar to the list, but also to save a group of numbers, but once it is created, it can no longer be modified, so it is called a read-only list

Grammar

Names = ("Alex", "Jack", "Eric")

It has only 2 methods, one is count, the other is index, complete.
Program Exercises

Please write down the following procedure.

Program: Shopping Cart Program

Demand:

After you start the program, let the user enter the payroll, and then print the list of items
Allow users to purchase items based on their product number
After the user selects the product, checks whether the balance is enough, enough on the direct debit, enough to remind
You can exit at any time to print the purchased goods and balances when exiting

Example code:

Product_list = [
(' Iphone ', 5800),
(' Mac Pro ', 9800),
(' Bike ', 800),
(' Watch ', 10600),
(' Coffee ', 31),
(' Alex Python ', 120),
]
Shopping_list = []
Salary = input ("Input your Salary:")
If Salary.isdigit (): #判断字符串是不是数字
salary = Int (salary) #把字符串类型转换成整型
While True:
For Index,item in Enumerate (product_list):
#print (Product_list.index (item), item)
Print (Index,item) #打印菜单列表
User_choice = input ("Choose what you want to buy?") >>>: ")
If User_choice.isdigit (): #把字符串类型转换成整型
user_choice = Int (user_choice)
If User_choice < Len (product_list) and User_choice >=0: #判断用户输入的商品号是否在商品菜单列表中
P_item = Product_list[user_choice]
If p_item[1] <= salary: #买的起
Shopping_list.append (P_item)
Salary-= p_item[1]
Print ("Added%s into shopping cart,your current balance is \033[31;1m%s\033[0m"% (p_item,salary))
Else
Print ("\033[41;1m your balance remains [%s], and buy a wool \033[0m"% salary)
Else
Print ("Product code [%s] is not exist!" % User_choice)
elif User_choice = = ' Q ':
Print ("--------shopping list------")
For P in Shopping_list:
Print (P)
Print ("Your Current balance:", salary)
Exit ()
Else
Print ("Invalid option")

Python Foundation II

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.