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

printOneToHundred()

执行结果:
C:\Users\***>python d:\python\test.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\test.py
def printX():
    x=5
    print x
printX()

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

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

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

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

执行结果:
C:\Users\***>python d:\python\test.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', '\xb9\xfe\xb9\xfe']
>>> 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\nb: %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

0622 python 基础05的更多相关文章

  1. python基础05 if选择

    摘要:if语句是用来检查一个条件,如果条件为真(true),我们运行一个语句块(称为IF块),否则(else)运行另一个语句块(else块).else语句是可选的 程序1(将文件保存为if.py): ...

  2. python基础05 缩进与选择

    作者:Vamei 出处:http://www.cnblogs.com/vamei 欢迎转载,也请保留这段声明.谢谢! 缩进 Python最具特色的是用缩进来标明成块的代码.我下面以if选择结构来举例. ...

  3. Python基础05 缩进和选择

    作者:Vamei 出处:http://www.cnblogs.com/vamei 欢迎转载,也请保留这段声明.谢谢! 缩进 Python最具特色的是用缩进来标明成块的代码.我下面以if选择结构来举例. ...

  4. python基础教程

    转自:http://www.cnblogs.com/vamei/archive/2012/09/13/2682778.html Python快速教程 作者:Vamei 出处:http://www.cn ...

  5. Python 基础练习

    今天接触了python,了解了一下 python 的基础语法,于是想着手训练一下,在本习题集中,参考代码为提供的参考答案,前面的代码为自己思考的代码,最后每道题给出练习的时间. Python 基础练习 ...

  6. Python基础教程【读书笔记】 - 2016/7/31

    希望通过博客园持续的更新,分享和记录Python基础知识到高级应用的点点滴滴! 第十波:第10章  充电时刻 Python语言的核心非常强大,同时还提供了更多值得一试的工具.Python的标准安装包括 ...

  7. Day1 - Python基础1 介绍、基本语法、流程控制

    Python之路,Day1 - Python基础1   本节内容 Python介绍 发展史 Python 2 or 3? 安装 Hello World程序 变量 用户输入 模块初识 .pyc是个什么鬼 ...

  8. Python 基础 二

    Python 基础 二 今天对昨天学习的Python基础知识进行总结,学而不思则惘,思而不学则殆! 一.先对昨天学习的三大循环的使用情况进行总结: 1.while循环的本质就是让计算机在满足某一条件的 ...

  9. python基础篇实战

    1. 判断下面的结果 # 1. 判断下面的结果 # 1 > 1 or 3 < 4 or 4 > 5 and 2 > 1 and 9 > 8 or 7 < 6 pri ...

随机推荐

  1. JAVA 堆设置

    JAVA 堆设置 第四节 堆已经讲得差不多啦,这章我们以一个例子来说说如何设置以及当发生堆溢出的时候怎么排查问题.先看一小段代码:         代码中使用了一个无限循环来为list添加对象,如果采 ...

  2. linux安装桌面软件

    CentOS 作为服务器的操作系统是很常见的,但是因为需要稳定而没有很时髦的更新,所以很少做为桌面环境.在服务器上通常不需要安装桌面环境,最小化地安装 CentOS(也就是 minimal CentO ...

  3. 小鱼提问3 static方法中可以访问某个类的私有变量吗(不通过反射的其他非正常手段)?什么情况下可以?

    class Student { private string _name; public int Age = 0; public static void Test() { Student stu = ...

  4. php7 install memcached extension

    #download source code package from git $ git clone https://github.com/php-memcached-dev/php-memcache ...

  5. [[转]CSS浮动原理

    CSS Float是网页设计最强大的灵活性功能之一.本文介绍CSS Float的基本原理和行为特征,并介绍各种浏器Float特性的Bugs. 内容 基本的浮动原理 浮动是如何进行的 浮动从何处开始 水 ...

  6. 用CSS3绘制图形

    参考资料:http://blog.csdn.net/fense_520/article/details/37892507 本文非转载,为个人原创,转载请先联系博主,谢谢~ 准备: <!DOCTY ...

  7. php 异步处理的gearman

    1. php 是进程处理,单线程到的,没有异步机制,在一些处理花费时间较多的情况导致用户体验较差.可以使用gearman 进行异步处理. 2. gearman 是一个异步处理的socket架构. 需要 ...

  8. 软件测试学习日志————round 1 some questions of two small programs

    Below are four faulty programs. Each includes a test case that results in failure. Answer the follow ...

  9. libwebsocket manual

    Name: libwebsocket_cancel_service - Cancel servicing of pending websocket activity Synopsis: void li ...

  10. 让vs2010的html编辑器验证html5语法

    或者在Tools -> option -> Text Editor -> Html -> Validation