for循环是我们编程中非常常用的一种循环,以下就是for循环在python中的一些应用实例:
1、单层for循环
#!/usr/bin/env python3
#-*- coding: utf-8 -*-
#user:slim_liu
for i in range(10):#循环10次
      print(i)#打印0-9
      if i == 5:#如果打印到5
          break#退出循环
else:
      print("循环顺利结束")#正常循环结束
2、for循环嵌套for循环
for i in range(1,10,2):#从1-10,步长为2
      for j in range(10):#打印0-9
            if j <6:
                continue
        print(i,j)
1、数字,数字有多种表示方法,8进制oct(10) 16进制hex(10) 二进制bin(10)
  1.1整型:int
     1.2长整型:python3去掉了
     1.3布尔:true 和false  bool(0)
     1.4浮点型:0.4
     1.5复数:4+5i
2、字符串的操作
      1.1pritn(r‘aaa\nasdf‘)  r取消特殊含义
      1.2center(20) 居中显示
      1.3count(‘a‘,4,7) #统计某个元素出现的次数,可以指定起始位置和结束位置,从4开始,7结束count(‘a‘,4,-1)
      1.4endswith(‘d‘) 判断结尾真假
      1.5expandtabs(10) 指定table键长度
      1.6find(‘d‘)查找字符
      1.7format格式化字符串 print(‘{}{}‘.format(‘name‘,‘age‘))
      1.8index#显示索引值
      1.9inalnum#字母或者数字为真
      2.0isalpha#只能是字母为真
      2.1isdecimal#十进制为真
      2.2indigit#整型为真
      2.3isnumeric#数字为真
      2.4isidentifier#判断是否包含系统保留关键字
      2.5lstrip\rstrip #删除左右空格,strip指定关键字可以去除指定关键字
      2.6just ljust\rjust #左右填充,可以指定填充字符,指定总长度
      2.7zfill #左填充 指定总长度 000000000000abc 填充的字符只能是0
      2.8 name=‘abacd‘
          ta=str.maketrans(‘a‘,‘1‘) #指定将a替换成1
          print(name.translate(ta))
3、常用字符串的操作:
   1)strip 移除空格
      2)len 查看长度
      3)index 索引
      4)a[1:4]切片
4、运算符:
      1)算数运算 + - * / % // **
      2)比较运算 == != <  >  >= <=
      3)赋值运算 = += -+ *= /= %= //=
      4)位运算 & |  ^  <<  >>
      5)逻辑运算 and  or not
      6)成员运算 in 、not in 判断一个元素是否存在于字符串中
      7)身份运算 is 、not is
      8)布尔值:非零数字及非空字符串自带的布尔值都为True
count=0
oldage=56
while count <3:
      age=input("age>>:")
      if age.isdigit():
            age=int(age)
            if age == oldage:
                  print("猜对了")
                  break
            elif age < oldage:
                  print("猜小了")
            else:
                  print("猜大了")
     else:
          count += 1
          continue
1)定义一个列表:names = []列表用中括号表示
2)列表常用的操作:
    增:
              1.names.append("liuhailong") #追加
              2.names.insert(2,"zhangsan") #插入
        删:
              1.names.remove("liuhailong")
              2.del names[3]
              3.print(names.pop(3))#删除并打印删除的内容  不加条件,删除最后一个
        改:
              1.names[1]="刘海龙"
              2.names.clear()#清空
              3.names.extend(n2) #将n2和names合并
        查:
              1.names[1]#通过下标
              2.names[0::2]#切片打印,names[-3:]#最后三个元素   
              3.names.index("alex")#取元素下标
              4.names.count("liuhailong")#统计目标个数
              5.names.reverse()#反转列表
              6.names.sort#对列表进行排序***python3中不能对int和str进行综合排序
              7.names.copy()#复制列表
原文:http://www.cnblogs.com/slim-liu/p/6009676.html