1.while语句

条件循环控制语句。一般需要和break一起使用,不然会进入死循环。

格式:【 while <条件>:

                 <内容>

                 break    】

x=int(input('请输入一个数字:'))
while x>0:
    print('正数')
    break

2.if语句

流程分支的条件控制,一般和elif和else使用。

x=int(input('请输入一个数字:'))
if x<0:
    print('负数')
elif x==0:
    print('')
else :
    print('正数')

对于简单的 if else语句,可以用三元运算(三目运算)来表示

#书写格式

result = value1 if  条件 else value2

#如果条件成立,把value1的值赋给result,不成立,则把value2的值赋给result
 1 #if一般方法表达
 2 a=1
 3 if a==1:
 4     result='True'
 5 else:
 6     result='False'
 7 print(result)
 8 
 9 #三目运算表达
10 result = 'True' if a==1 else 'False'
11 print(result)
12 
13 #运行结果
14 True
15 True
demo

3.for语句

循环控制语句,可用来遍历某一对象,和in一起使用。

格式: 【 for <> in <对象集合>:】

x=['a','b','c','d']
for i in x :              # i 位置的字符,只要不是关键字,可以随意用字符代表
    print(i)

4.range()函数

数字序列迭代器,当你迭代它时,它是一个能够像期望的序列返回连续项的对象,但为了节省空间,它并不真正构造列表。

格式:  range(stop)  给出结束数值,开始数值默认为0,间隔为1。

           range(start,stop)  给出开始数值和结束数值,间隔为1。

           range(start,stop,step)  给出开始数值和结束数值,间隔为step数值。

 1 class range(object):
 2     """
 3     range(stop) -> range object
 4     range(start, stop[, step]) -> range object
 5     
 6     Return an object that produces a sequence of integers from start (inclusive)
 7     to stop (exclusive) by step.  range(i, j) produces i, i+1, i+2, ..., j-1.
 8     start defaults to 0, and stop is omitted!  range(4) produces 0, 1, 2, 3.
 9     These are exactly the valid indices for a list of 4 elements.
10     When step is given, it specifies the increment (or decrement).
11     """
12     def count(self, value): # real signature unknown; restored from __doc__
13         """ rangeobject.count(value) -> integer -- return number of occurrences of value """
14         return 0
15 
16     def index(self, value, start=None, stop=None): # real signature unknown; restored from __doc__
17         """
18         rangeobject.index(value, [start, [stop]]) -> integer -- return index of value.
19         Raise ValueError if the value is not present.
20         """
21         return 0
22 
23     def __contains__(self, *args, **kwargs): # real signature unknown
24         """ Return key in self. """
25         pass
26 
27     def __eq__(self, *args, **kwargs): # real signature unknown
28         """ Return self==value. """
29         pass
30 
31     def __getattribute__(self, *args, **kwargs): # real signature unknown
32         """ Return getattr(self, name). """
33         pass
34 
35     def __getitem__(self, *args, **kwargs): # real signature unknown
36         """ Return self[key]. """
37         pass
38 
39     def __ge__(self, *args, **kwargs): # real signature unknown
40         """ Return self>=value. """
41         pass
42 
43     def __gt__(self, *args, **kwargs): # real signature unknown
44         """ Return self>value. """
45         pass
46 
47     def __hash__(self, *args, **kwargs): # real signature unknown
48         """ Return hash(self). """
49         pass
50 
51     def __init__(self, stop): # real signature unknown; restored from __doc__
52         pass
53 
54     def __iter__(self, *args, **kwargs): # real signature unknown
55         """ Implement iter(self). """
56         pass
57 
58     def __len__(self, *args, **kwargs): # real signature unknown
59         """ Return len(self). """
60         pass
61 
62     def __le__(self, *args, **kwargs): # real signature unknown
63         """ Return self<=value. """
64         pass
65 
66     def __lt__(self, *args, **kwargs): # real signature unknown
67         """ Return self<value. """
68         pass
69 
70     @staticmethod # known case of __new__
71     def __new__(*args, **kwargs): # real signature unknown
72         """ Create and return a new object.  See help(type) for accurate signature. """
73         pass
74 
75     def __ne__(self, *args, **kwargs): # real signature unknown
76         """ Return self!=value. """
77         pass
78 
79     def __reduce__(self, *args, **kwargs): # real signature unknown
80         pass
81 
82     def __repr__(self, *args, **kwargs): # real signature unknown
83         """ Return repr(self). """
84         pass
85 
86     def __reversed__(self, *args, **kwargs): # real signature unknown
87         """ Return a reverse iterator. """
88         pass
89 
90     start = property(lambda self: object(), lambda self, v: None, lambda self: None)  # default
91 
92     step = property(lambda self: object(), lambda self, v: None, lambda self: None)  # default
93 
94     stop = property(lambda self: object(), lambda self, v: None, lambda self: None)  # default
range
for i in range(3):          #运行结果为0,1,2
    print(i)
for i in range(0,5):       #运行结果为0,1,2,3,4
    print(i)
for i in range(-2,10,2): #运行结果为-2,0,2,4,6,8
    print(i)

5.break和continue语句,以及循环中的else语句

1)break语句和 C 中的类似,用于跳出最近的一级 for 或 while 循环。

while True:
    print('hello')
    break

2)continue语句表示循环继续执行下一次迭代:

for x in range(1, 4):
        print(x, 'for语句')
        continue
        print(x, 'continue语句后')
else:
        print(x, 'else语句')
 
#运行结果
1 for语句
2 for语句
3 for语句
3 else语句

 3)循环中的else

如continue的例子里,有for-else语句,else语句会在循环跳出后执行,但是break跳出循环则不会执行else,所以else可以用来处理循环中的一些异常跳出。

for x in range(1, 4):
        print(x)
else:
        print(x)

#运行结果
1
2
3
3

6.pass语句

pass语句什么也不做。它用于那些语法上必须要有什么语句,但程序什么也不做的场合,通常用于创建最小结构的类。

另一方面,pass可以在创建新代码时用来做函数或控制体的占位符。可以让你在更抽象的级别上思考。

class EmptyClass:
    pass