Day2 Python learning Array, looping, string

Source: Internet
Author: User

I. Contents of the study

1. Command Sys.path ()

Show Current environment variables

2. Command Os.system ("dir")

Show files in current directory, show only, do not save results

3. Command Text = Os.popen ("dir"). Read ()

Displays the files in the current directory to save the results

4, Command Os.mkdir ("New_dir")

Create a folder called "New_dir"

5, ternary operation

A,b,c = A

D = A If a> b else c

Results D =c

6. Data type Conversion

String goto Binary

' Hello '. Encode ("utf=8")

Binary Turn string

' \XE4\XBD\XA0\XE5\XA5\XBD '. Decode ("Utf-8")

7. Arrays

name = ["AAA", "BBB", "CCC"]

Print (name[0,2]), which is the display, AAA,BBB

Print (Name[-1]), is the display, CCC

Print (Name[-2,-1]), which is the display, BBB,CCC

Print (Name[:3]), which is the display, AAA,BBB,CCC

Name.append ("444"), inserted at the last

Name.insert (1, "555"), inserted into 1

Name.clear (), clear

Name.reverse (), reverse sort

Name.sort (), sorted alphabetically

Name.remove ("AAA"), remove AAA

Name.delete[0], delete 0

Name.extene ([1,2,3,4]), adding an array

name2 = Name.copy (), copy, not reference, such as multi-layer array, copy only memory address (pointer) to multi-layer array, import copy

Additional syntax for array copying name2 = name[:] name2 = list (name)

name2 = Name.deepcopy (), deep copy, or full multi-layer array

Homework:

1. Let the user enter the salary

2. Purchase goods by number

3, select the goods, see whether the balance of purchase, enough to deduct money, not enough re-election

4, press Q Exit, and the purchase of goods and the balance

1Product_list = [2('Iphone', 5800),3('Mac Pro', 9800),4('Bike', 800),5('Watch', 10600),6('Coffee', 31),7('Alex Python', 120),8 ]9Shoping_list = []TenGongzi = input ("Enter Payroll") One ifGongzi.isdigit ():#determine whether a number AGongzi =Int (Gongzi) -      forIndex,iinchEnumerate (product_list):#Output Array Location -             Print(index,i) the      whileTrue: -Shangpings = input ("Enter the item you want to buy") -         ifshangpings.isdigit (): -Shangpings =Int (shangpings) +             ifShangpings < 6 andShangpings >=0: -AA =Product_list[shangpings] +                 ifAA[1] <=Gongzi: AGongzi-= aa[1] at                     Print("the item purchased is%s and the balance is%s"%(Aa[0],gongzi)) - shoping_list.append (AA) -  -  -                 Else: -                     Print("insufficient balance, only:", Gongzi) in             Else: -                 Print("Input Error") to         elifShangpings = ="Q": +             Print("List of items-----purchased------") -             Print(shoping_list) the             Print("Balance:", Gongzi) * exit () $         Else:Panax Notoginseng             Print("need to enter a number")

string Manipulation
1 Name.capitalize () Capitalize first letter2 name.casefold () uppercase all lowercase3Name.center (50,"-") output'---------------------Alex Li----------------------'4Name.count ('Lex') Statistics on the number of lex occurrences5 Name.encode () encodes a string into bytes format6Name.endswith ("Li") to determine whether the string ends with Li7  "Alex\tli". Expandtabs (10) output'Alex Li', convert \ t to a long space8Name.find ('A'Find A, Find returns its index, cannot find return-19 Ten format: One>>> msg ="my name is {}, and the is {}" A>>> Msg.format ("Alex", 22) -     'my name is Alex;' ->>> msg ="My name is {1}, and age is {0}" the>>> Msg.format ("Alex", 22) -     'My name is a, and age is Alex' ->>> msg ="My name is {name}, and age was {age}" ->>> Msg.format (age=22,name="Ale") +     'My name is ale, and an IS' - Format_map +>>> Msg.format_map ({'name':'Alex',' Age': 22}) A     'my name is Alex;' at  -  -Msg.index ('a') returns the index of the string where A is located - '9aA'. Isalnum () True -  - '9'. IsDigit () is an integer in Name.isnumeric - name.isprintable to Name.isspace + Name.istitle - Name.isupper the  "|". Join (['Alex','Jack','Rain']) * 'Alex|jack|rain' $ Panax Notoginseng  - Maketrans the>>> Intab ="Aeiou"  #The is the string has actual characters. +>>> Outtab ="12345" #This is the string has corresponding mapping character A>>> Trantab =Str.maketrans (Intab, Outtab) the>>> +>>> str ="This is a string EXAMPLE....WOW!!!" ->>>str.translate (Trantab) $     'th3s 3s str3ng 2x1mpl2....w4w!!!' $  -Msg.partition (' is'Output'My name',' is','{name}, and age was {age}')  -  the>>>"Alex Li, Chinese name is Lijie". replace ("Li","LI", 1) -      'Alex LI, Chinese name is Lijie'Wuyi  the msg.swapcase Case -sensitive interchange -  Wu  ->>> Msg.zfill (40) About '00000my name is {name}, and age was {age}' $  -  -  ->>> N4.ljust (40,"-") A 'Hello 2orld-----------------------------' +>>> N4.rjust (40,"-") the '-----------------------------Hello 2orld' -  $  the>>> b="ddefdsdff_ haha"  the>>> B.isidentifier ()#detects if a string can be used as a marker, that is, if the variable naming convention is met theTrue

Day2 Python learning Array, looping, string

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.