zoukankan      html  css  js  c++  java
  • 0622 python 基础05

    使用双重for循环,打印 0~100
    # -*- coding: utf-8 -*-
    # D:python est.py
    def printOneToHundred():
        for i in range(10):
            for j in range(1,11):
                print i*10+j,
            print ' '

    printOneToHundred()

    执行结果:
    C:Users***>python d:python est.py
    1 2 3 4 5 6 7 8 9 10

    11 12 13 14 15 16 17 18 19 20

    21 22 23 24 25 26 27 28 29 30

    31 32 33 34 35 36 37 38 39 40

    41 42 43 44 45 46 47 48 49 50

    51 52 53 54 55 56 57 58 59 60

    61 62 63 64 65 66 67 68 69 70

    71 72 73 74 75 76 77 78 79 80

    81 82 83 84 85 86 87 88 89 90

    91 92 93 94 95 96 97 98 99 100

    作业:用双重for循环处理 奇数+2,偶数+3

    全局变量
    本质:变量的作用域

    局部变量1:
    # -*- coding: utf-8 -*-
    # D:python est.py
    def printX():
        x=5
        print x
    printX()

    执行结果:
    C:Users***>python d:python est.py
    5

    局部变量2:
    # -*- coding: utf-8 -*-
    # D:python est.py
    def printX():
        x=5
        print x
    x=10
    printX()

    执行结果:
    C:Users***>python d:python est.py
    5

    全局变量:
    # -*- coding: utf-8 -*-
    # D:python est.py
    def printX():
        global x
        x=x+1
        print x
    x=10
    printX()

    执行结果:
    C:Users***>python d:python est.py
    11

    数据结构
    >>> range(10)
    [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

    列表像一个容器
    >>> list=[] # 声明一个列表
    >>> type(list) # 查看变量类型
    <type 'list'>
    >>> help(list) # help() 查看变量的方法
    Help on list object:

    class list(object)
    |  list() -> new empty list
    |  list(iterable) -> new list initialized from iterable's items
    |
    |  Methods defined here:
    |
    |  __add__(...)
    |      x.__add__(y) <==> x+y
    |
    |  __contains__(...)
    |      x.__contains__(y) <==> y in x
    |
    |  __delitem__(...)
    |      x.__delitem__(y) <==> del x[y]
    |
    |  __delslice__(...)
    |      x.__delslice__(i, j) <==> del x[i:j]
    |
    |      Use of negative indices is not supported.
    |
    |  __eq__(...)
    |      x.__eq__(y) <==> x==y
    |
    |  __ge__(...)
    |      x.__ge__(y) <==> x>=y
    |
    |  __getattribute__(...)
    |      x.__getattribute__('name') <==> x.name
    |
    |  __getitem__(...)
    |      x.__getitem__(y) <==> x[y]
    |
    |  __getslice__(...)
    |      x.__getslice__(i, j) <==> x[i:j]
    |
    |      Use of negative indices is not supported.
    |
    |  __gt__(...)
    |      x.__gt__(y) <==> x>y
    |
    |  __iadd__(...)
    |      x.__iadd__(y) <==> x+=y
    |
    |  __imul__(...)
    |      x.__imul__(y) <==> x*=y
    |
    |  __init__(...)
    |      x.__init__(...) initializes x; see help(type(x)) for signature
    |
    |  __iter__(...)
    |      x.__iter__() <==> iter(x)
    |
    |  __le__(...)
    |      x.__le__(y) <==> x<=y
    |
    |  __len__(...)
    |      x.__len__() <==> len(x)
    |
    |  __lt__(...)
    |      x.__lt__(y) <==> x<y
    |
    |  __mul__(...)
    |      x.__mul__(n) <==> x*n
    |
    |  __ne__(...)
    |      x.__ne__(y) <==> x!=y
    |
    |  __repr__(...)
    |      x.__repr__() <==> repr(x)
    |
    |  __reversed__(...)
    |      L.__reversed__() -- return a reverse iterator over the list
    |
    |  __rmul__(...)
    |      x.__rmul__(n) <==> n*x
    |
    |  __setitem__(...)
    |      x.__setitem__(i, y) <==> x[i]=y
    |
    |  __setslice__(...)
    |      x.__setslice__(i, j, y) <==> x[i:j]=y
    |
    |      Use  of negative indices is not supported.
    |
    |  __sizeof__(...)
    |      L.__sizeof__() -- size of L in memory, in bytes
    |
    |  append(...)
    |      L.append(object) -- append object to end
    |
    |  count(...)
    |      L.count(value) -> integer -- return number of occurrences of value
    |
    |  extend(...)
    |      L.extend(iterable) -- extend list by appending elements from the iterable
    |
    |  index(...)
    |      L.index(value, [start, [stop]]) -> integer -- return first index of value.
    |      Raises ValueError if the value is not present.
    |
    |  insert(...)
    |      L.insert(index, object) -- insert object before index
    |
    |  pop(...)
    |      L.pop([index]) -> item -- remove and return item at index (default last)
    .
    |      Raises IndexError if list is empty or index is out of range.
    |
    |  remove(...)
    |      L.remove(value) -- remove first occurrence of value.
    |      Raises ValueError if the value is not present.
    |
    |  reverse(...)
    |      L.reverse() -- reverse *IN PLACE*
    |
    |  sort(...)
    |      L.sort(cmp=None, key=None, reverse=False) -- stable sort *IN PLACE*;
    |      cmp(x, y) -> -1, 0, 1
    |
    |  ----------------------------------------------------------------------
    |  Data and other attributes defined here:
    |
    |  __hash__ = None
    |
    |  __new__ = <built-in method __new__ of type object>
    |      T.__new__(S, ...) -> a new object with type S, a subtype of T

    列表 list 可以改变(增删改元素)
    >>> list=[] # 声明一个空的列表
    >>> list
    []
    >>> list=[1,'ss','哈哈'] # 声明一个非空列表
    >>> list
    [1, 'ss', 'xb9xfexb9xfe']
    >>> list=[1,2,3,4,5] # 声明一个非空列表
    >>> type(list) # 查看 变量 类型
    <type 'list'>
    >>> list.append(6) # 增
    >>> del list[0] # 删
    >>> list
    [2, 3, 4, 5, 6]
    >>> len(list) # 查看列表的长度
    5

    >>> list=[0,1,2,3,4,5,6,7,8,9]
    >>> print list[0] # 打印list的第一个元素
    0
    >>> print list[2]
    2
    >>> print list[8]
    8
    >>> del list[4]
    >>> list
    [0, 1, 2, 3, 5, 6, 7, 8, 9]
    >>> list[8]=100 # 改
    >>> list
    [0, 1, 2, 3, 5, 6, 7, 8, 100]

    >>> lista=[1,2,3]
    >>> listb=[lista,'a','b']
    >>> listb
    [[1, 2, 3], 'a', 'b']

    遍历 list 的两种方式
    >>> for i in lista:
    ...   print i,
    ...
    1 2 3
    >>> for i in range(len(lista)):
    ...   print lista[i],
    ...
    1 2 3

    list 的嵌套遍历
    >>> for i in listb:
    ...   print i
    ...
    [1, 2, 3]
    a
    b

    >>> import types
    >>> for i in listb:
    ...   if type(i) is types.ListType:
    ...     for j in i:
    ...       print j,
    ...   else:
    ...     print i,
    ...
    1 2 3 a b

    字符串也可以使用for循环遍历
    >>> for s in "Hello World!":
    ...   print s
    ...
    H
    e
    l
    l
    o

    W
    o
    r
    l
    d
    !

    元组 tuple 不能改变(增删改元素)
    声明后不做任何改变,可以看作一个常量
    >>> tuple=()
    >>> tuple
    ()
    >>> type(tuple)
    <type 'tuple'>
    >>> tuple=(1,2,3)
    >>> for i in tuple:
    ...   print i
    ...
    1
    2
    3
    >>> tuple[0]=2
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    TypeError: 'tuple' object does not support item assignment

    格式化输出
    >>> a=1
    >>> b=2
    >>> print "a: %d b: %d" %(a,b)
    a: 1
    b: 2

    作业:
    1、10个元素的list中,奇数坐标元素+1、偶数坐标元素+2,并存回原来的位置
    2、逆序输出一个字符串
    3、一个字符串中,分别输出奇数坐标字符和偶数坐标字符

    >>> str="hello"
    >>> for s in range(len(str)):
    ...   print str[s]
    ...
    h
    e
    l
    l
    o

  • 相关阅读:
    ssh实现免密码登录和文件传输
    linux后台执行程序相关命令
    orchestrator
    curl下载安装与使用
    goland使用
    mysql集群
    consul理解
    my.cnf
    数据库的表设计
    项目常见面试问题
  • 原文地址:https://www.cnblogs.com/blueskylcc/p/5545682.html
Copyright © 2011-2022 走看看