python 使用教程(基本数据类型)

python 介绍

  • python是一种十分简洁的动态语言

    以下就是用Python是实现经典的快速排序的算法,十分简洁。

def quicksort(arr):
    if len(arr) <= 1:
        return arr
    pivot = arr[len(arr) / 2]
    left = [x for x in arr if x < pivot]
    middle = [x for x in arr if x == pivot]
    right = [x for x in arr if x > pivot]
    return quicksort(left) + middle + quicksort(right)

print quicksort([3,6,8,10,1,2,1])
#Prints "[1, 1, 2, 3, 6, 8, 10]"
  • python的版本

    目前Python的版本主要是3.4和2.7,这两个版本可能存在兼容性问题,这里使用的是2.7版本,想知道你用的python是什么版本可以在命令行输以下命令python –version

  • 基本的数据类型

    和其他的编程语言一样,python有整型,浮点型,布尔型和字符串型,下面是一些例子。

    数字
    详情可见 [官方文档]

    x = 3
    print type(x) # Prints "<type 'int'>"
    print x       # Prints "3"
    print x + 1   # Addition; prints "4"
    print x - 1   # Subtraction; prints "2"
    print x * 2   # Multiplication; prints "6"
    print x ** 2  # Exponentiation; prints "9"
    x += 1
    print x  # Prints "4"
    x *= 2
    print x  # Prints "8"
    y = 2.5
    print type(y) # Prints "<type 'float'>"
    print y, y + 1, y * 2, y ** 2 # Prints "2.5 3.5 5.0 6.25"

    布尔型

    t = True
    f = False
    print type(t) # Prints "<type 'bool'>"
    print t and f # Logical AND; prints "False"
    print t or f  # Logical OR; prints "True"
    print not t   # Logical NOT; prints "False"
    print t != f  # Logical XOR; prints "True" 

    字符串
    对字符串操作的其它函数可参见 [官方文档]

    hello = 'hello'   # String literals can use single quotes
    world = "world"   # or double quotes; it does not matter.
    print hello       # Prints "hello"
    print len(hello)  # String length; prints "5"
    hw = hello + ' ' + world  # String concatenation
    print hw  # prints "hello world"
    hw12 = '%s %s %d' % (hello, world, 12)  # sprintf style string formatting
    print hw12  # prints "hello world 12"
    s = "hello"
    print s.capitalize()  # Capitalize a string; prints "Hello"
    print s.upper()       # Convert a string to uppercase; prints "HELLO"
    print s.rjust(7)      # Right-justify a string, padding with spaces; prints "  hello"
    print s.center(7)     # Center a string, padding with spaces; prints " hello "
    print s.replace('l', '(ell)')  # Replace all instances of one substring with another;
                               # prints "he(ell)(ell)o"
    print '  world '.strip()  # Strip leading and trailing whitespace; prints "world"
  • python的容器类

    python的容器类主要有列表,字典,集合和元组

    列表

    python 对列表的操作可以说是简洁而优美 [官方文档]

    xs = [3, 1, 2]   # Create a list
    print xs, xs[2]  # Prints "[3, 1, 2] 2"
    print xs[-1]     # Negative indices count from the end of the list; prints "2"
    xs[2] = 'foo'    # Lists can contain elements of different types
    print xs         # Prints "[3, 1, 'foo']"
    xs.append('bar') # Add a new element to the end of the list
    print xs         # Prints "[3, 1, 'foo', 'bar']"
    x = xs.pop()     # Remove and return the last element of the list
    print x, xs      # Prints "bar [3, 1, 'foo']"

    快速的列表操作

    nums = range(5)    # range is a built-in function that creates a list of integers
    print nums         # Prints "[0, 1, 2, 3, 4]"
    print nums[2:4]    # Get a slice from index 2 to 4 (exclusive); prints "[2, 3]"
    print nums[2:]     # Get a slice from index 2 to the end; prints "[2, 3, 4]"
    print nums[:2]     # Get a slice from the start to index 2 (exclusive); prints "[0, 1]"
    print nums[:]      # Get a slice of the whole list; prints ["0, 1, 2, 3, 4]"
    print nums[:-1]    # Slice indices can be negative; prints ["0, 1, 2, 3]"
    nums[2:4] = [8, 9] # Assign a new sublist to a slice
    print nums         # Prints "[0, 1, 8, 9, 4]"

    快速的循环操作

    nums = [0, 1, 2, 3, 4]
    even_squares = [x ** 2 for x in nums if x % 2 == 0]
    print even_squares  # Prints "[0, 4, 16]"

    enumerate获得列表的索引

    animals = ['cat', 'dog', 'monkey']
    for idx, animal in enumerate(animals):
        print '#%d: %s' % (idx + 1, animal)
    
    # Prints "#1: cat", "#2: dog", "#3: monkey", each on its own line
    

    字典

展开阅读全文

没有更多推荐了,返回首页