变量以及类型


变量:存储程序运行中的数据,变量有3个要素:变量名、变量类型、变量值。python属于弱类型语言,不需要声明变量类型。

  1. [root@localhost python]# ipython3
  2. In []: a= //变量名=变量值;在堆内存中的一个区域存了一个值为1,内存分为堆内存和栈内存,栈内存的是引用。指向堆内存中的值。
  3.  
  4. In []: b=
  5.  
  6. In []: c=a+b
  7.  
  8. In []: c
  9. Out[]:
  10.  
  11. In []: a
  12. Out[]:
  13.  
  14. In []: type(a)
  15. Out[]: int
  16.  
  17. In []: type(c)
  18. Out[]: int
  19.  
  20. In []: str="hello"
  21.  
  22. In []: str
  23. Out[]: 'hello'
  24.  
  25. In []: x,y=,
  26.  
  27. In []: x
  28. Out[]:
  29.  
  30. In []: y
  31. Out[]:
  32.  
  33. In []:
  34.  
  35. In []: type(str)
  36. Out[]: str

变量类型


标识符:自定义的一些符号和名称,标识符是自己定义的,如变量名、函数名等。


规则:由字母、下划线和数字组成,且不能以数字开头;不能包含一些有特殊意义的符号,如#.&!()等;同时区分大小写。

大小写规则:

驼峰命名法:如小驼峰:userName / userLoginFlag  大驼峰:UserName;类的名字一般首字母大写。

下划线规则:user_name user_login_flag

保留字/关键字:在计算机中有特殊含义的单词。

  1. In [1]: import keyword
  2.  
  3. In [2]: keyword.kwlist
  4. Out[2]:
  5. ['False',
  6. 'None',
  7. 'True',
  8. 'and',
  9. 'as',
  10. 'assert',
  11. 'break',
  12. 'class',
  13. 'continue',
  14. 'def',
  15. 'del',
  16. 'elif',
  17. 'else',
  18. 'except',
  19. 'finally',
  20. 'for',
  21. 'from',
  22. 'global',
  23. 'if',
  24. 'import',
  25. 'in',
  26. 'is',
  27. 'lambda',
  28. 'nonlocal',
  29. 'not',
  30. 'or',
  31. 'pass',
  32. 'raise',
  33. 'return',
  34. 'try',
  35. 'while',
  36. 'with',
  37. 'yield']
  38.  
  39. In [3]:

输出和输出格式

  1. In []: print('hello','world','zyj','hello') //输出多个变量
  2. hello world zyj hello
  3.  
  4. In []: print(+) //输出表达式
  5.  
  6. In []: exit
  7. [root@localhost python]#vim .py
  8. a=
  9. print("my age is:%d"%a ) //变量替换
  10. [root@localhost python]# python3 .py
  11. my age is:
  12. [root@localhost python]#vim .py
  13. a=
  14. b=
  15. print("my age is:%d"%a+b)
  16. [root@localhost python]# python3 .py
  17. Traceback (most recent call last):
  18. File "3.py", line , in <module>
  19. print("my age is:%d"%a+b) //错误行
  20. TypeError: must be str, not int //错误类型
  21. [root@localhost python]#vim .py
  22. a=
  23. b=
  24. print("my age is:%d"%(a+b)) //表达式需要用括号括起来
  25. [root@localhost python]# python3 .py
  26. my age is:
  27. [root@localhost python]#vim .py
  28. a=
  29. b=
  30. my_name="zyj"
  31. print("my age is:%d,my name is:%s" %(a+b,my_name)) //如果需要多个变量替换也要加小括号,并且每个变量用逗号隔开,%d代表整数类型替换,%s代表所有的类型变量替换,可以只记住%s
  32. [root@localhost python]# python3 .py
  33. my age is:,my name is:zyj
  34.  
  35. [root@localhost python]# vim .py
  36. a=
  37. b=
  38. my_name="zyj"
  39. print("my age is:%d,my name is:%s" %(a+b,my_name))
  40. money=
  41. print("I have money:%04d" %money)//不够4位前面用0填充
  42. w=3.1415926
  43. print("w is:%.2f"%w) //输出的数字包含两位小数,如果没有用0填充
  44. print("it's:%d%%"%a) //%表示占位符,%%第一个%表示转义符
  45. [root@localhost python]# python3 .py
  46. my age is:,my name is:zyj
  47. I have money:
  48. w is:3.14
  49. it's:35%

输入和运算符:注意python2和python3中的一些区别。

输入:用户从电脑输入一些字符,可以让用户输入。

  1. [root@localhost python]# vim 02输入.py
  2. #encoding=UTF-
  3. #raw_input()只在python2中有,将输入内容作为字符串。 区别1
  4. a=raw_input("请输入你的名字:") //
  5. [root@localhost python]# python 02输入.py
  6. 请输入你的名字:zyj
  7. [root@localhost python]#
  1. [root@localhost python]# vim 02输入.py
  2. #input函数在python2和python3中都有
  3. #在python2中input函数输入的内容作为表达式,而不是字符串,在python3作为字符串。在python3中取消支持表达式,可以防止某些恶意脚本中包含大量的表达式,提升安全性。区别2
  4. a=input("请输入姓名:")
  5. print(a)
  6. [root@localhost python]# python3 02输入.py
  7. 请输入姓名:ZYJ
  8. ZYJ
  9. [root@localhost python]# python 02输入.py
  10. 请输入姓名:+
  1. [root@localhost python]# vim 02输入.py
  2. name=input("请输入姓名:")
  3. print(name)
  4. age=input("请输入年龄:")
  5. print("你输入的姓名:%s,你输入的年龄:%s" %(name,age))
  6. [root@localhost python]# python3 02输入.py
  7. 请输入姓名:zyj
  8. zyj
  9. 请输入年龄:
  10.  
  11. [root@localhost python]# vim 02输入.py
  12. name=input("请输入姓名:")
  13. print(name)
  14. age=input("请输入年龄:")
  15. print(type(age))
  16. age=age+ //年龄加2岁
  17. print("你输入的姓名:%s,你输入的年龄:%s" %(name,age))
  18. <class 'str'>
  19. Traceback (most recent call last):
  20. File "02输入.py", line , in <module>
  21. age=age+
  22. TypeError: must be str, not int //age为字符串,第10行只能字符串相加
  23. [root@localhost python]# vim 02输入.py
  24. name=input("请输入姓名:")
  25. print(name)
  26. age=input("请输入年龄:")
  27. print(type(age))
  28. age=int(age)+2 //转换类型后进行相加
  29. print("你输入的姓名:%s,你输入的年龄:%s" %(name,age))
  30. [root@localhost python]# python3 02输入.py
  31. 请输入姓名:zyj
  32. zyj
  33. 请输入年龄:
  34. <class 'str'>
  35. 你输入的姓名:zyj,输入的年龄:20

运算符:

赋值运算符=:把=右边的结果给左边的变量。注意区别与==,==为数学中的等于号;

+-*/%//**  加减乘除取余取整除幂

  1. In []: **
  2. Out[]:
  3.  
  4. In []: //
  5. Out[]:
  6.  
  7. In []: /
  8. Out[]: 1.5
  9.  
  10. In []: %
  11. Out[]:

复合赋值运算符:前后先操作再赋值

+= -= *= /= %= **= //=

  1. In []: a=
  2. In []: b=
  3. In []: b+=a //b=b+a
  4. In []: b
  5. Out[]:
  1. In []: a=
  2.  
  3. In []: a*=-+-7 //a=6*(29)
  4.  
  5. In []: a
  6. Out[]:

条件判断


  1. if<条件判断1>:
  2. 执行1
  3. elif<条件判断2>:
  4. 执行2
  5. elif<条件判断3>:
  6. 执行3
  7. else
  8. 执行4 

举例:

  1. [root@localhost python]# vim 04.py
  2. 1 age=input("age:")
  3. 2 age=int(age)
  4. 3 sex=input("sex:")
  5. 4 # and or not
  6. 5 if age >=18 and sex == "man" :
  7. 6 print('you can do it')
  8. 7 elif age <18 or sex == "woman" :
  9. 8 print("you can't do it")
  10. 9 elif not (sex == "man" or sex == "woman"):#sex != "woman" and sex != "man" //<>也是不等于
  11. 10 print("人妖")
  12. 11 else:
  13. 12 pass #以后填充代码,为了保证不出现语法错误,当由else:又没有写其他的时候会出现语法错误
  14. [root@localhost python]# python3 04.py
  15. age:19
  16. sex:man
  17. you can do it
  18. [root@localhost python]# python3 04.py
  19. age:14
  20. sex:woman
  21. you can't do it
  22. [root@localhost python]# vim 04.py
  23. [root@localhost python]# python3 04.py
  24. age:13
  25. sex:dsfdsf
  26. you can't do it
  27. [root@localhost python]# python3 04.py
  28. age:19
  29. sex:dfdsf
  30. 人妖

小技巧:age:dfsdfsdf^H^H^H^H^H^H^H 当键盘输入删除符时会当作字符,此时需要按ctrl+backspace键可以删除。

特殊的真和假

真:非0 

假:0 “” None [] () {}

  1. [root@localhost python]# vim 05.py
  2. 1 age=int(input('age:'))
  3. 2 if age:
  4. 3 print("age不为0")
  5. 4 else:
  6. 5 print("age为0")
  7. 6 name=""
  8. 7 if name:
  9. 8 print("name 不是空字符串")
  10. 9 else:
  11. 10 print("name 是空字符串")
  12. [root@localhost python]# python3 05.py
  13. age:0
  14. age0
  15. name 是空字符串

练习:

1、根据BMI公式(体重除以身高的平方)计算输入人的BMI指数,低于18.5过轻,18.5-25正常,25-28过重,高于32严重肥胖,用if-elif输出打印出来

  1. [root@localhost python]# vim 06.py
  2. 1 h=float(input("input your height:"))
  3. 2 w=float(input("input your weight:"))
  4. 3 print("your height is %.2f,weight is %.2f"%(h,w))
  5. 4 BMI=w/(h**2)
  6. 5 if BMI <18.5 :
  7. 6 print("you are too light")
  8. 7 elif BMI >=18.5 and BMI <25:
  9. 8 print("you are nomal")
  10. 9
  11. 10 elif BMI >=25 and BMI <32:
  12. 11 print("you are weight")
  13. 12
  14. 13 else:
  15. 14 print("you are too weight")
  16. 15 print("your BMI is:%.2f"%BMI)
  17. [root@localhost python]# python3 06.py
  18. input your height:1.50
  19. input your weight:48
  20. your height is 1.50,weight is 48.00
  21. you are nomal
  22. your BMI is:21.33
  23. [root@localhost python]#

2、情节描述:输入公交卡余额,超过2元就可以上车,如果空位置数量大于0,就可以坐下;

  1. [root@localhost python]# vim 07.py
  2. 1 b=float(input("please input your balance:"))
  3. 2 s=int(input("please input the number of the empty seat:"))
  4. 3 print("your balance is:%.2f,the empty seat is:%d"%(b,s))
  5. 4 if b >= 2 and s > 0:
  6. 5 print("please get on and you have a seat")
  7. 6 elif b >= 2 and s <= 0: //第一次忘记写:号,第二次调式的时候与预期不一致,忘记写=号,输入0时没有匹配到。边界值测试
  8. 7 print("you can get on but there is no seat for you")
  9. 8 else:
  10. 9 print("sorry you can't get on,because your balance is too short")
  11. 10 print("goodluck to you")
  12. [root@localhost python]# python3 07.py
  13. File "07.py", line 6
  14. elif b >= 2 and s < 0
  15. ^
  16. SyntaxError: invalid syntax
  17. [root@localhost python]# vim 07.py 6 //进去后光标在报错行
  18. #运行测试,查看结果
  19. [root@localhost python]# python3 07.py
  20. please input your balance:2 #边界值测试
  21. please input the number of the empty seat:0 #边界值
  22. your balance is:2.00,the empty seat is:0
  23. you can get on but there is no seat for you
  24. goodluck to you
  25. [root@localhost python]# python3 07.py
  26. please input your balance:1 #正常值测试
  27. please input the number of the empty seat:3 #正常值
  28. your balance is:1.00,the empty seat is:3
  29. sorry you can't get on,because your balance is too short
  30. goodluck to you
  31. [root@localhost python]# python3 07.py
  32. please input your balance:0
  33. please input the number of the empty seat:0
  34. your balance is:0.00,the empty seat is:0
  35. sorry you can't get on,because your balance is too short
  36. goodluck to you
  37. [root@localhost python]# python3 07.py
  38. please input your balance:10
  39. please input the number of the empty seat:10
  40. your balance is:10.00,the empty seat is:10
  41. please get on and you have a seat
  42. goodluck to you
  43. [root@localhost python]# python3 07.py
  44. please input your balance:-1 #非法值测试
  45. please input the number of the empty seat:-1#非法值测试
  46. your balance is:-1.00,the empty seat is:-1
  47. sorry you can't get on,because your balance is too short
  48. goodluck to you
  49. [root@localhost python]# python3 07.py
  50. please input your balance:1000000000000000000000000 #超大值测试
  51. please input the number of the empty seat:1000000000000000000000000
  52. your balance is:999999999999999983222784.00,the empty seat is:1000000000000000000000000
  53. please get on and you have a seat
  54. goodluck to you
  55. [root@localhost python]#

使用if嵌套实现,更符合正常逻辑。

  1. [root@localhost python]# python3 07.py
  2. 1 m = float(input("money:"))
  3. 2 s = int(input("seat:"))
  4. 3 if m >= 2:
  5. 4 print("you can get on")
  6. 5 if s > 0:
  7. 6 print("there is a seat for you")
  8. 7 else:
  9. 8 print("sorry there is no seat for you")
  10. 9 else:
  11. 10 print("sorry,you can't get on ")
  12. [root@localhost python]# python3 08.py #边界值测试
  13. money:2
  14. seat:0
  15. you can get on
  16. sorry there is no seat for you
  17. [root@localhost python]# python3 08.py #正常值测试
  18. money:2
  19. seat:1
  20. you can get on
  21. there is a seat for you
  22. [root@localhost python]# python3 08.py #非法值测试
  23. money:-1
  24. seat:-1
  25. sorry,you can't get on
  26. [root@localhost python]# python3 08.py #较大的值测试
  27. money:100000000000000
  28. seat:1000000000000000000000
  29. you can get on
  30. there is a seat for you

循环:在程序中做相同的事情,需要使用循环。


while循环

  1. while 条件:
  2. 条件满足时,做的事情1
  3. 条件满足时,做的事情2
  4. 条件满足时,做的事情3
  5. .......

举例:

求1加到100的和:

  1. [root@localhost python]# vim 09.py
  2. 1 i = 1
  3. 2 s = 0
  4. 3 while i <= 100: #满足条件的才进入while循环
  5. 4 s += i
  6. 5 #i++ python不支持这种写法
  7. 6 i+=1 #如果i不变的时候,一直为1,则会一直循环,出现死循环。
  8. 7 print("从1加到100的和为:%d" %s)
  9. [root@localhost python]# python3 09.py
  10. 1加到100的和为:5050

求1-100之间偶数的和,包含1和100。

  1. [root@localhost python]# vim 10.py
  2. 1 i=1
  3. 2 s=0
  4. 3 while i <= 100:
  5. 4 even = i % 2
  6. 5 if even == 0 :
  7. 6 s+=i
  8. 7 else:
  9. 8 pass
  10. 9 i+=1
  11. 10 print("1和100之间的偶数和为:%d" %s)
  12. [root@localhost python]# python3 10.py
  13. 1100之间的偶数和为:2550

打印下面的图形:

  1. [root@localhost python]# vim 12.py
  2. 1 i=1
  3. 2 while i <= 5 :
  4. 3 print(i*"*")
  5. 4 i+=1
  6. [root@localhost python]# python3 12.py
  7. *
  8. **
  9. ***
  10. ****
  11. *****
  1. 1 i=1
  2. 2 while i <= 10 :
  3. 3 print(10*"*")
  4. 4 i+=1
  5. [root@localhost python]# python3 12.py
  6. **********
  7. **********
  8. **********
  9. **********
  10. **********
  11. **********
  12. **********
  13. **********
  14. **********
  15. **********

while循环嵌套

  1. while 条件1
  2. 条件1满足时,做的事情1
  3. 条件1满足时,做的事情2
  4. 条件1满足时,做的事情3
  5. .......
  6.  
  7. while 条件2
  8. 条件2满足时,做的事情1
  9. 条件2满足时,做的事情2
  10. 条件2满足时,做的事情3
  11. ......

举例:用星号打印矩形

版本1:按照逻辑初次编写

  1. 1 x=1 #x为矩形长
  2. 2 y=1 #y为矩形宽
  3. 3
  4. 4 while y <= 10 :#输出10
  5. 5 while x <= 10 :#在一行中输出10个*
  6. 6 print("*")
  7. 7 x += 1
  8. 8 y += 1
  9. 9 print("pass")
  10. [root@localhost python]# python3 11.py
  11. *
  12. *
  13. *
  14. *
  15. *
  16. *
  17. *
  18. *
  19. *
  20. *
  21. pass #输出结果不符合预期

版本2:解决输出一行时,进行了换行的问题。

  1. 1 x=1 #x为矩形长
  2. 2 y=1 #y为矩形宽
  3. 3
  4. 4 while y <= 10 :#输出10
  5. 5 while x <= 10 :#在一行中输出10个*
  6. 6 print("*",end="")#print函数默认输出后就会换行,如果不换行,end=""
  7. 7 x += 1
  8. 8 y += 1
  9. 9 print("pass")

[root@localhost python]# python3 11.py
**********pass  #输出不符合预期

版本3:解决只打印了一行星号的问题。

  1. 1 x=1 #x为矩形长
  2. 2 y=1 #y为矩形宽
  3. 3
  4. 4 while y <= 10 :#输出10
  5. 5 x=1 #每行执行完成后,x为11,若不重新赋值,则第二次大循环的时候不会进到小循环里面,因此只会输出一行*,因此这里需要重新赋值。
  6. 6 while x <= 10 :#在一行中输出10个*
  7. 7 print("*",end="")#print函数默认输出后就会换行,如果不换行,end=""
  8. 8 x += 1
  9. 9 y += 1
  10. 10 print("pass")
  11. [root@localhost python]# python3 11.py
  12. ****************************************************************************************************pass #还是不满足预期

版本4:解决多行星号没有换行的问题

  1. 1 x=1 #x为矩形长
  2. 2 y=1 #y为矩形宽
  3. 3
  4. 4 while y <= 10 :#输出10
  5. 5 x=1 #每行执行完成后,x为11,若不重新赋值,则第二次大循环的时候不会进到小循环里面,因此只会输出一行*
  6. 6 while x <= 10 :#在一行中输出10个*
  7. 7 print("*",end="")#print函数默认输出后就会换行,如果不换行,end=""
  8. 8 x += 1
  9. 9 print("")#每行结束后输出一个换行,print函数默认会输出换行
  10. 10 y += 1
  11. 11 print("pass")
  12. [root@localhost python]# python3 11.py
  13. **********
  14. **********
  15. **********
  16. **********
  17. **********
  18. **********
  19. **********
  20. **********
  21. **********
  22. **********
  23. pass

说明:查看python中函数的帮助文档:

  1. [root@localhost ~]# ipython3
  2. In [1]: help(print) #查看print()函数的帮助文档。
  3.  
  4. Help on built-in function print in module builtins:
  5.  
  6. print(...)
  7. print(value, ..., sep=' ', end='\n', file=sys.stdout, flush=False)
  8. #默认输出后由换行符。
  9. Prints the values to a stream, or to sys.stdout by default.
  10. Optional keyword arguments:
  11. file: a file-like object (stream); defaults to the current sys.stdout.
  12. sep: string inserted between values, default a space.
  13. end: string appended after the last value, default a newline.
  14. flush: whether to forcibly flush the stream.
  15. (END)

练习2:打印九九乘法表

  1. [root@localhost python]# vim 13.py
  2. 1 x = 1 #行数
  3. 2 while x <= 9 : #一共循环9次,才能打印9行,每行打印的列数和行号一样
  4. 3 y = 1 #代表列数
  5. 4 while y <= x:
  6. 5 print("%d*%d=%d\t"%(y,x,y*x),end="")
  7. 6 y+=1
  8. 7 print("")
  9. 8 x+=1
  10. [root@localhost python]# python3 13.py
  11. 1*1=1
  12. 1*2=2 2*2=4
  13. 1*3=3 2*3=6 3*3=9
  14. 1*4=4 2*4=8 3*4=12 4*4=16
  15. 1*5=5 2*5=10 3*5=15 4*5=20 5*5=25
  16. 1*6=6 2*6=12 3*6=18 4*6=24 5*6=30 6*6=36
  17. 1*7=7 2*7=14 3*7=21 4*7=28 5*7=35 6*7=42 7*7=49
  18. 1*8=8 2*8=16 3*8=24 4*8=32 5*8=40 6*8=48 7*8=56 8*8=64
  19. 1*9=9 2*9=18 3*9=27 4*9=36 5*9=45 6*9=54 7*9=63 8*9=72 9*9=81

循环要点

1、循环次数:while中循环次数由条件决定;如九九乘法表中循环9次;

2、循环体:在循环过程中做什么,如打印一行九九乘法表;

3、变量怎么变化:如变量增加或者递减

break和continue


在循环中,break语句可以提前退出循环;

  1. 1 age = int(input("please input your age:"))
  2. 2 i = 1
  3. 3 while True :
  4. 4 if i == age:
  5. 5 print("your age is %d" %i)
  6. 6 break #不知道要循环几次的时候,可以使用break防止死循环。
  7. 7 else:
  8. 8 print("wrong")
  9. 9 i += 1
  10. [root@localhost python]# python3 14.py
  11. please input your age:3
  12. wrong
  13. wrong
  14. your age is 3

在循环过程中,continue语句,可以跳过当前的这次循环,直接开始下一次循环。终止当前的循环,开启下次循环。

  1. 1 i = 0
  2. 2 while i < 10 :
  3. 3 i += 1
  4. 4 if i % 2 == 0:
  5. 5 print("%d is enev" %i) #i是偶数的时候打印
  6. 6 continue
  7. 7 print("i is %d" %i) #i不是偶数的时候打印
  8. 8 else:
  9. 9 print("else 表示不满足条件时调试的代码,这时i为%d" %i) #不满足条件的时候执行else,注意while和这里的else是一个整体,这种方式不常用。
  10. 10 print("结束")
  11. [root@localhost python]# python3 15.py
  12. i is 1
  13. 2 is enev
  14. i is 3
  15. 4 is enev
  16. i is 5
  17. 6 is enev
  18. i is 7
  19. 8 is enev
  20. i is 9
  21. 10 is enev
  22. else 表示不满足条件时调试的代码,这时i10
  23. 结束

练习:使用*打印倒等边三角形,并且行号由用户输入。

  1. 1 i = int(input("请输入行数:"))
  2. 2
  3. 3 a = 0
  4. 4 while a < i :#假设i=4,打印4行,
  5. 5 b = 0 #定义空格
  6. 6 while b < a :#打印当前行前面的空格,第一行不打印空格,第二行打印1个,后面增加一个;
  7. 7 print(" ",end="")
  8. 8 b += 1
  9. 9 c = i-a
  10. 10 while c > 0 : #打印星号,第一行打印4个*,后面的行减1
  11. 11 print("*",end=" ")#打印*不换行,后面跟一个空格
  12. 12 c -= 1
  13. 13 print("")
  14. 14 a += 1
  1. [root@localhost python]# python3 16.py
  2. 请输入行数:3
  3. * * *
  4. * *
  5. *
  6. [root@localhost python]# python3 16.py
  7. 请输入行数:4
  8. * * * *
  9. * * *
  10. * *
  11. *
  12. [root@localhost python]# python3 16.py
  13. 请输入行数:5
  14. * * * * *
  15. * * * *
  16. * * *
  17. * *
  18. *
  19. [root@localhost python]#
  20. [root@localhost python]# python3 16.py
  21. 请输入行数:10
  22. * * * * * * * * * *
  23. * * * * * * * * *
  24. * * * * * * * *
  25. * * * * * * *
  26. * * * * * *
  27. * * * * *
  28. * * * *
  29. * * *
  30. * *
  31. *
  32. [root@localhost python]#
  1. 1 i = int(input("请输入行数:"))
  2. 2 a = 1 #控制行数,打印i行,初始值根据规律选择0或者1都可以。
  3. 3 while a <= i :
  4. 4 b = i - a #定义空格
  5. 5 while b > 0 :
  6. 6 print(" ",end="")
  7. 7 b -= 1
  8. 8 c = 0 #定义星号
  9. 9 while c < a :
  10. 10 print("*",end=" ")
  11. 11 c += 1
  12. 12 a += 1
  13. 13 print("")
  14. [root@localhost python]# python3 17.py
  15. 请输入行数:4
  16. *
  17. * *
  18. * * *
  19. * * * *

for循环

  1. for 临时变量 in 集合或字符串等:
  2. 循环满足条件时执行的代码
  3. else
  4. 循环不满足条件时执行的代码

举例1:

  1. 1 for i in "abcdefg":
  2. 2 print(i)
  3. 3 else:
  4. 4 print("没有内容")
  5. [root@localhost python]# python3 19.py
  6. a
  7. b
  8. c
  9. d
  10. e
  11. f
  12. g
  13. 没有内容
  14. [root@localhost python]#

举例2:

  1. 7 for i in range(1,10):
  2. 8 print(i)
  3. 9 else:
  4. 10 print("没有内容")
  5. [root@localhost python]# python3 19.py
  6. 1
  7. 2
  8. 3
  9. 4
  10. 5
  11. 6
  12. 7
  13. 8
  14. 9
  15. 没有内容
  16. [root@localhost python]#

python数据类型、输入输出、运算符、条件判断、循环的更多相关文章

  1. python笔记五(条件判断/循环/break和continue)

    一 条件判断 if <条件判断1>: <执行1> elif <条件判断2>: <执行2> elif <条件判断3>: <执行3> ...

  2. 【01】Python 环境变量、条件判断、循环、基本运算符

    1 环境变量 1.1 Windows下环境变量 系统变量Path中要加入Python安装路径: C:\xxxx\Python36;C:\xxxx\Python36\Scripts; 2 条件判断 2. ...

  3. python 输入输出 条件判断 循环

    1.条件判断 score = int(input("请输入学生成绩:"))if score>100 and score <0: print("请输入正确的成绩 ...

  4. 【Python⑥】python的缩进,条件判断和循环

    缩进 Python的最大特色是用缩进来标明成块的代码. 这点和其他语言区别很明显,比如大家熟悉的C语言里: ) { num+=; flag-=; } 而在python中: if flag>= 0 ...

  5. Python 条件判断 循环

    age = 20 if age >= 18: print('your age is', age) print('adult') 根据Python的缩进规则,如果if语句判断是True,就把缩进的 ...

  6. Python入门基础之条件判断、循环、dict和set

    Python之if语句 比如,输入用户年龄,根据年龄打印不同的内容,在Python程序中,可以用if语句实现: age = 20 if age >= 18: print 'your age is ...

  7. 值类型之间的相互转化,运算符,if条件判断,循环,函数

    值类型之间的相互转化 number | string | boolean 一.转换为boolean=>Boolean(a); var num = 10; var s = '123'; var b ...

  8. Python基础:条件判断 &&循环

    1:条件判断 2:循环 2.1:for 2.2  while 小结: continue :跳出本次循环 进行下次循环,  break :结束循环体.

  9. python基础-编码_if条件判断

    一.第一句Python代码 在 /home/dev/ 目录下创建 hello.py 文件,内容如下: [root@python-3 scripts]# cat hello.py #!/usr/bin/ ...

  10. Python学习笔记五--条件和循环

    5.1 if语句 没什么好说,if语句语法如下: if expression: expr_true_suit 5.1.1多重条件表达式 单个if语句可以通过布尔操作符and,or,not实现多重条件判 ...

随机推荐

  1. vue前端+java后端 vue + vuex + koa2开发环境搭建及示例开发

    vue + vuex + koa2开发环境搭建及示例开发 https://segmentfault.com/a/1190000012918518 vue前端+java后端 https://blog.c ...

  2. Git for Windows,TortoiseGit支持WinXP的最后版本及下载方法

    TortoiseGit兼容Windows XP和Windows Server 2003的最后版本: TortoiseGit 1.8.16.0 (https://download.tortoisegit ...

  3. JavaScript学习系列5 ---ES6中的var, let 和const

    我们都知道JavaScript中的var,在本系列的 JavaScript学习系列2一JavaScript中的变量作用域 中,我们详细阐述了var声明的变量的作用域 文章中提到,JavaScript中 ...

  4. hdu1063

    #include<iostream> #include<string> using namespace std; struct BigReal //高精度实数 { int le ...

  5. Codeforces Round #179 (Div. 2) B. Yaroslav and Two Strings (容斥原理)

    题目链接 Description Yaroslav thinks that two strings s and w, consisting of digits and having length n  ...

  6. Algorithms - Fibonacci Number

    斐波那契数列(Fibonacci Number)从数学的角度是以递归的方法定义的: \(F_0 = 0\) \(F_1 = 1\) \(F_n = F_{n-1} + F_{n-2}\) (\(n \ ...

  7. SuperSocket使用 IRequestInfo 和 IReceiveFilter 等对象实现自定义协议

    为什么你要使用自定义协议? 通信协议用于将接收到的二进制数据转化成您的应用程序可以理解的请求. SuperSocket提供了一个内置的通信协议“命令行协议”定义每个请求都必须以回车换行"\r ...

  8. Asp.Net 遍历 循环 显示所有COOKIS,SESSION,Applocation

    在C#中循环显示SESSIOn: Response.Write("<br>Session的所有值:<br>"); foreach (string obj i ...

  9. jquery事件之事件

    事件名 说明 语法 (events 事件类型,data数据,handler 事件处理函数,selector 选择器) blur() 获得失去鼠标光标焦点事件 jQueryObject.blur( [ ...

  10. Ruby and gnuplot installation on Ubuntu 16.04

    sudo apt install ruby gnuplot