Python小白学习之路—while、for循环、运算

Python里面的循环跟其他语言里的循环基本一致,只是书写格式不同。

1.for循环: for [循环条件]:[循环语句]  满足循环条件,则执行循环语句,执行一次判断一次,不满足则结束循环。

简单的循环: in range() 表示在什么范围里

复制代码

 1 # 循环 自动累计加一
 2 # 一个参数代表从0开始到多少
 3 for i in range(10):
 4     print("One:",i)
 5 # 两个参数代表范围
 6 for i in range(0,5):
 7     print("Two:",i)
 8 # 三个参数代表范围以及隔几次循环执行语句一次
 9 for i in range(0,10,2):#2表示隔几个输出一次
10     print("Thress",i)

复制代码

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

D:\Python\python.exe D:/Pyproject/Day1/for.py

One: 0

One: 1

One: 2

One: 3

One: 4

One: 5

One: 6

One: 7

One: 8

One: 9

Two: 0

Two: 1

Two: 2

Two: 3

Two: 4

Thress 0

Thress 2

Thress 4

Thress 6

Thress 8

Process finished with exit code 0

2.while循语句: while [True/False]: 循环语句  条件为真则执行循环语句,为假则不进入循环。

复制代码

 1 names = "Sunshine"
 2 # 如果条件返回值为真(True)则执行循环语句
 3 while names == 'Sunshine':
 4     print("条件为真,名字是Sunshine。")
 5     # 执行完语句后退出循环
 6     break
 7 
 8 while names == 'Others':
 9     print("条件为真,名字是Others。")
10     # 执行完语句后退出循环
11     break

复制代码

1

2

3

4

D:\Python\python.exe D:/Pyproject/Day1/while.py

条件为真,名字是Sunshine。

Process finished with exit code 0

  如果不加break的话,因为循环条件一直为真,则会一直循环,执行输出语句。(一般Python有限制,好像最多只能执行998次)。

  break的作用是终断循环,也就是退出整个循环,要注意的时,如果有多个循环嵌套的话,break只能退出当前所在的循环。如果讲话上面的break替换成continuou的话,还是会一直执行循环语句,continuou的作用是结束本次循环,开始下次循环,当前循环下continuou后面的语句不会执行。

Ex:学完循环语句和if条件语句后,可以用来写一个猜年龄的小程序,要求如下:

(1)猜测年龄,猜的年龄大了,就提示bigger

(2)猜测年龄,猜的年龄小了,就提示smaller

(3)猜对了,输出年龄,并祝贺

View Code

结果:

1

2

3

4

5

6

7

8

9

D:\Python\python.exe D:/Pyproject/Day1/for.py

guess age:15

Think bigger...

guess age:57

Think smaller...

guess age:56

Yes,you got it.

Process finished with exit code 0

  

什么是运算?
运算,数学上,运算是一种行为,通过已知量的可能的组合,获得新的量。运算的本质是集合之间的映射
1.逻辑运算(返回值为布尔值类型)
假设:a,b,c = 3,5,7
1.and  Ex:  a > 2 and c < 7 -->flase
2.or  Ex:  a > 2 or c = 8   -->true
3.not  Ex:  if not a > 2 and c < 7:print("ddd")   输出结果:ddd

复制代码

1 # 逻辑运算
2 a,b,c = 3,5,7
3 # and运算 两个条件都满足才为真
4 print("and:",a > 2 and c < 7)
5 # or运算  满足其中任意一个条件即为真
6 print("or:",a > 2 or c == 2)
7 # not运算 如果不是a<2 并且c>5,则不执行
8 if not a < 2 and c > 5:
9     print("执行成功")

复制代码

结果:

1

2

3

4

5

6

D:\Python\python.exe D:/Pyproject/bogle.py

and: False

or: True

执行成功

Process finished with exit code 0

  

2.成员运算(返回值为布尔值类型)
(1)in 运算   判断在不在里面
现在有列表 a[1,2,3,4]
in:  1 in a  表示:1是列表a里面的一员。结果:True
not:  5 not in a  表示:5不是列表a里面的一员。 结果:False
1 # 创建一个列表
2 a = [1,2,3,4]
3 # in 运算
4 print("In运算,1是a的一员:",1 in a)
5 # not 运算
6 print("Not运算,5不是a的一员:",4 not in a)
结果:

1

2

3

4

5

D:\Python\python.exe D:/Pyproject/bogle.py

In运算,1是a的一员: True

Not运算,5不是a的一员: False

Process finished with exit code 0

(2)is 运算   判断是不是什么的一员
a[1,2,3,4]
names = 'hello world!"
is  type(a) is list  表示:a的类型是一个列表-->true
is not  type(names) is not str  表示:names的类型不是一个字符串类型-->false

复制代码

1 # 创建一个列表和字符串
2 a = [1,2,3,4]
3 names = "hello world!"
4 # is 运算
5 print("is运算,a的类型是一个列表:",type(a) is list)
6 # is not 运算
7 print("is运算,names不是字符串类型:",type(names) is not str)

复制代码

 结果:

1

2

3

4

5

D:\Python\python.exe D:/Pyproject/bogle.py

is运算,a的类型是一个列表: True

is运算,names不是字符串类型: False

Process finished with exit code 0

3.位运算(转换成二进制后的运算)
现在有a,b,c三个值分别为60,13,0
a = 60 (00111100), 
b = 13 (00001101),
c = 0 (00000000)
进行如下运算:
(1)&   与运算  a & b =00001100-->12(00001010) (同为1,则为真1)
(2)|     或运算  a | b =00111101-->61(00111101)(有一个为1,则为1)
(3)^   异运算  a ^ b =00110001-->49(0011001)(相同为0,不同为1)
(4)~   取反运算  ~ a =1 11000011-->-195(最前面的为符号位,也会取反) 
(5)<<  左移运算  64 << 2 =256  64(00100000)左移2位后:256(10000000)
(6)>>  右移运算  64 >> 2 =16    64(00100000)右移2位后:16(000001000)

复制代码

1 a = 60
2 b = 13
3 c = 0
4 print("&运算:",a & b )
5 print("|运算:",a | b)
6 print("^运算:",a ^ b)
7 print("~运算:",~a)
8 print(">>运算:",64 >> 2)
9 print("<<运算:",64 << 2)

复制代码

结果:

1

2

3

4

5

6

7

8

9

D:\Python\python.exe D:/Pyproject/bogle.py

&运算: 12

|运算: 61

^运算: 49

~运算: -61

>>运算: 16

<<运算: 256

Process finished with exit code 0

  

计算机中能表示的最小单位,是一个二进制位(bit)
计算机中能存储的最小单位,是一个二进制位(bit)
8 bit(比特) = 1 byte(字节)
计算机i中的字符最少要用一字节来表示
1024byte = 1kbyte
1024kbyte = 1mbyte
1024mbyte = 1gbyte
1024gb = 1Tb

 
三元运算:
三元运算符是软件编程中的一个固定格式,语法是“条件表达式?表达式1:表达式2”。使用这个算法可以使调用数据时逐级筛选。(百度百科参考
1.语法:条件表达式?表达式1:表达式2
说明:问号前面的位置是判断的条件,判断结果为bool型,为true时调用表达式1,为false时调用表达式2。
其逻辑为:“如果为条件成立或者满足则执行表达式1,否则执行第二个。”
2.也可以用if...else...
a,b,c=1,3,5
d = a if a>b else c (如果a>b则d=a,否则d=c) -->d=5

猜你喜欢

转载自blog.csdn.net/qq_39947874/article/details/82744716