Python's common built-in sequence structure (list, tuple, dictionary) learning notes

Source: Internet
Author: User
Tags string methods
Lists and tuples
The list is expressed in curly braces [], and the tuple is represented by parentheses ().
Lists can be modified, and strings and tuples cannot be modified.
The Shard of a tuple is also a tuple, a shard of a list or a list.

1. List method:

name=["Zhang3", "Li4", "Wang5"]name.append ("Gou6") #添加项name. Remove ("Gou6") #移除第一个匹配项 or del name[3] to remove Name.insert (3 , "Gou6") #插入项name. Index ("Gou6") #找出第一个匹配项的位置name. Extend (["Gou6", "Xuan7"]) #扩展name. Pop (0) #返回列表的第一项值并从列表中删除之

2. List functions:

>>> a=list ("Hi guys") #把字符串转换为列表 >>> print a[' h ', ' I ', ', ' g ', ' u ', ' y ', ' s ']>>> '. Join (a) 
   #把列表还原成字符串 ' Hi guys ' >>> max (a) #取得列表的最大元素 ' y ' >>> len (a) #取得列表长度7 >>> min (a) #取得最小元素 ' > >> tuple (a) #将列表转换为元组 (' h ', ' I ', ', ' g ', ' u ', ' y ', ' s ') >>> sorted (a) #将列表元素排序 ["', ' g ', ' h ', ' I ', ' s ', ' u ' , ' Y ']

3. List traversal:

A, use the For statement to traverse

For Each_item in Name:   print (Each_item)

B, using the while statement to traverse

I=0while i < Len (name):   print (name[i])   i + = 1

4. Membership 1:

>>> sub= "Hello, you is a bear" >>> "Bear" in Subtrue>>> "Y" in Subtrue>>> raw_input ("W Hat ' s your name? ") In Subwhat ' s your name?beartrue

5. Membership 2:

database=[["Zhang3", "0111"],["Li4", "0112"],["Wang5", "0113"]]username=raw_input ("What ' s your user name?") Id=raw_input ("What ' s your ID?") If [Username,id] in Database:print "Access granted"

6. Find the integer within 10

s = [x for x in range (0, ten) if x% 2 = = 0]

7. Generate 99 Multiplication table

s = [(x, Y, X*y) for x in range (1, ten) for Y in range (1,10) if x>=y]

String

1. Get the string

Name=raw_input ("What ' s your name?")      Print "Hello," + name + ". Welcome to US"

Note: pyhton3.x version canceled raw_input, unified use of input
Output value:

Print name + repr (x) #str用于把值转换为合理的字符串, Repr creates a string, returns the string form of the value #str is a type (like int), repr is a function

2. NewLine characters are denoted by \ n
The original string, with an R in front of the string, as

Print R "c:\nowindows\no" Path= "c:\nowindows\no"; Print repr (path)

3.Unicode string

Print U "Redhat"

Note: pyhton3.x version all strings are Unicode strings
When defining a string, double and single quotation marks are available, except that double quotes can be used inside a string when using single quotes
Boolean value:

>>> bool (' I love you ') true>>> bool (true>>>) bool (1) true>>> bool (' 0 ') true> >> bool (0) false>>> bool (") False

4. String methods

>>> tag= "Baidu indexpage" >>> print tag[8:28] #字符串分片http://www.baidu.com>>> print tag[29:-4 ] #字符串分片baidu indexpage>>> tag.replace ("www.baidu.com", "home.sina.com") #字符串替换 ' Baidu Indexpage ' >> > dirs=["", "usr", "bin", "env"]>>> "/". Join (dirs)  #将列表拼接成字符串 '/usr/bin/env ' >>> print ("C:" + " \ \ ". Join (dirs)) c:\usr\bin\env>>> path="/usr/bin/env ">>> path.split ("/") #将字符串分割成列表 [', ' usr ', ' Bin ', ' env ']

5. Other string methods

>>> s= ' I love you!  ' >>> s.lower () #转换字符串的小写 ' I love you!  ' >>> s.upper () #转换字符串的大写 ' I love you!  ' >>> S.title () #换换字符串为标题 (all words capitalized) ' I love you!  ' >>> s.islower () #判断字符串是否为小写 (can also be judged by uppercase and caption) false>>> S.strip ()  #去除首尾空格, Lstrip Remove the left space, Rstrip Remove right space ' I love you! ' >>> word=s.split () #分割 >>> word[' I ', ' love ', ' you! ' >>> ':: '. Join (word) #合并 ' i::love::you! ' >>> s.count (' o ') #统计出现次数2 >>> s.find (' You ') #查找位置, if not found, return -19>>> s.startswith (' python ') False>>> s.replace (' You ', ' yours ') ' I love yours!  '
  • 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.