python学习笔记 loop&&raw_input 7&& if

1.首先要说range(x) 

其返回的是一个list:[0,1,2,....x-1]

>>> range(5)

[0,1,2,3,4]

2.Loop 共有两种形式,一种for x in []/(), 一种while ***:

 1 sum = 0;
 2 for x in range(101):
 3     sum += x
 4 print sum
 5 
 6 n = 99
 7 sum = 0
 8 while n > 0:
 9     sum += n
10     n -= 2
11 print sum
View Code

对于类C语言中的这种代码

for(i =3 ;i < n;i+=2){
//
//
//
}
View Code

可以用range的这种形式

1 for i in range(3,n+1)
2     #000
3     # 000
View Code

3.if中出现多个条件

  if 条件1 && 条件2 ----> if 条件1 and 条件2 

 && : and

 || : or 

 1 age=20
 2 if age >= 6:
 3     print 'teenager '
 4 elif age >= 18:
 5     print 'adult'
 6 else:
 7     print 'kid'
 8 age = 13
 9 if age > 12  and age < 18:
10     print 'teenager'
View Code

 4. raw_input 他返回的对象注意,是一个字符串!!!

如果需要改变他的形式那么需要进行强制转换(不知道python里面有没有强制转换这一个东西)

1 m = int(raw_input('age:'))
2 if m < 18 and m > 12:
3     print 'teenager'
4 elif m > 18:
5     print 'adult'
6 else :
7     print 'child'
View Code
原文地址:https://www.cnblogs.com/silence-tommy/p/6476096.html