Python Basics review-1-2 data types-STR, list, tuple, dict

Source: Internet
Author: User
Tags iterable shallow copy

Data type number
引号:    123  数值    ‘123‘ 字符串    整数:ini long   范围:(-2**31 - 2**31)        num = 123    长整型 long  (L)        num = 123L浮点型:float复数型:
Sequence

String, list, tuple all belong to sequence

The two main features of a sequence are index operations and slicing operations

    • Index operations can fetch a specific item from the sequence
    • Slice operations can get a slice from the sequence, which is part of the sequence
Basic operation of the sequence
    • Len (): Find the length of the sequence
      • : Connect two sequences
      • : Repeating sequence elements
    • In: Determines whether the element is in sequence
    • Max (); Returns the maximum value
    • Min (); Returns the minimum value
    • CMP (x, y): compare two sequences for equality
String str
字符串:    str = ‘this is a string‘      单引号    str = "this is a strting"     双引号    str = ‘‘‘this is a string‘‘‘  三引号    str = """ this is a strtng """        单双引号在python下没有区别    三重引号 除了能定义字符串(可包含换行符)  还可以用作注释
Slice

Applies to all sequences
```
In [9]: a = ' abcdef '

In [ten]: A[0:2]
OUT[10]: ' AB '
Remove the label 0 to 2 subscript 2 does not display
In [all]: A[:2]
OUT[11]: ' AB '
Remove the label 0 to 2 subscript 2 does not display
In []: A[:-1]
OUT[12]: ' ABCDE '
Remove the mark 0 to the bottom 1th subscript to the bottom of the 1th one does not show
In []: A[::2]
OUT[13]: ' Ace '
Step is 2, which means to remove the mark 0, 2, 4, 6
In []: A[-3:-1]
OUT[14]: ' De '
From the bottom 3rd to the bottom 1th, the countdown 1th does not show
In []: A[-1:-4:-1]
OUT[15]: ' Fed '
Reverse slice, from the penultimate to the 4th to the bottom, the bottom 4th does not show
in [+]: a[-1:-4:-2]
OUT[16]: ' FD '
Reverse slicing, from the penultimate to the 4th to the bottom, with a step of 2
```

Summarize:
A = "abcdef"

a[A:B:C]A:切片开始的下标,包含B:切片结束的下标,不包含C:正数时表示步长  负数时表示进行反向切片及步长  
Tuple tuble
    • Tuples are immutable like strings
    • Tuples can store a series of values
    • Tuples are usually orientating when a user-defined function can safely take a set of values, that is, the value of the tuple being used does not change

Define tuples;

t = tuble () defines a tuple of multiple elements with the number of cells, the element can be a number, a string, a list, a tuple
t = (1,) when there is only one element, one must be added, the number is not added, the value is assigned to a number
T = () defines an empty tuple

Splitting of tuples:
t = (1, 2, 3)
A, b, C = T assigns the values of tuples 1, 2, and 3 to A, B, and C in turn

Method:

Count () Number of statistics elements in tuples
Index () returns elements in the first occurrence of an element in the subscript

Lists List
    • A list is a data structure that processes a set of ordered items, that is, a sequence of items that can be stored in a list
    • Lists are mutable types of data
    • Create a list

List1 = [] Create an empty list

List2 = List () using the list method

List3 = [' A ', 1, [' A ', 1]]

Method:

    • Append () Append

      L.append (object)--Append object to end

    • Del List[n] Delete an element based on the subscript

    • Remove () Removes the first occurrence of the list in the element

      L.remove (value)--Remove first occurrence of value.

    • Insert () inserts an element before a field index

      L.insert (Index, object)--Insert object before index

    • Sort () to list remember order

      L.sort (Cmp=none, Key=none, Reverse=false)--stable sort in place;

      CMP (x, y)-1, 0, 1

    • Reverse () reversal

      L.reverse ()--Reverse *in place*

    • Pop () deletes and returns the element value of the specified subscript, without specifying the default deletion of the last

      L.pop ([index]), item-Remove and return item at index (default last).

    • Extend () extending the list by appending iteration elements
      Extend list by appending elements from the iterable

Dictionary
    • Dictionary is the only type of mapping in Python (hash table)
    • The Dictionary object is mutable, but the key of the dictionary must be an immutable object, and a dictionary can use different types of key values
    • Create a dictionary

Dict1 = {} using {} to create an empty dictionary

Dict2 = {' name ': ' Li ', ' Age ': 10}

DICT3 = Dict ([' Name ', ' Li '), (' Age ', 10)])

Dict4 = Dict (' A ' =10, ' n ' =100)

  • Method of the Dictionary:

  • Keys () Get all keys

    D.keys (), List of D ' s keys

  • VALUES () gets all values
  • D.values (), List of D ' s values

  • Get () Gets the value corresponding to the key, does not exist when the output can be specified, default is empty

    D.get (K[,d]), D[k] if k in D, else D. D defaults to None.

  • Has_key () to see if key exists

    D.has_key (k)-True if D has a key k, else False

  • Iitems () converted to a list of (key, value)

    D.items (), List of D ' s (key, value) pairs, as 2-tuples

  • Copy () copying

    D.copy (), a shallow copy of D

  • Clear () Empty dictionary

    D.clear (), None. Remove all items from D

  • Pop () deletes the value of the specified key and returns value if the key does not exist to return a given value, otherwise throws a guide

    D.pop (K[,d])-V, remove specified key and return the corresponding value.
    If key is no found, D is returned if given, otherwise keyerror is raised

  • Update () updates a dictionary from a dictionary or two iteration objects

    D.update ([E,]**f), None. Update D from Dict/iterable E and F.

    If E present and has A. Keys () method, Does:for k in e:d[k] = E[k]

    If E present and lacks. Keys () method, Does:for (K, v) in e:d[k] = V

    In either case, this is followed By:for k in f:d[k] = F[k]

  • Fromkeys () uses a sequence to generate a dictionary with a default value of NULL, which specifies

    Dict.fromkeys (S[,v]), New dict with keys from S and values equal to V.
    V defaults to None.

Python Basics review-1-2 data types-STR, list, tuple, dict

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.