PY--变量、if、for、while

Python变量

变量为存储在内存中值,意味着在创建变量时会在内存中开辟一个空间,基于变量的数据类型,解释器会分配指定内存,并决定什么数据可以被存储在内存中。因此,变量可以指定不不同的数据类型,这些变量可以存储整数,小数或字符。

格式:变量名 = 值

命名规则:

1.数字、字母、下划线(_)组成

2.不能以数字开头

3.不能用Python关键字

特别注意:

不要汉字当变量

全局变量为全大写(CHINA = 1949)

变量、函数用下划线。类似驼峰体。

1 ['and', 'as', 'assert', 'break', 'class', 'continue', 'def', 'del', 'elif', 'else', 'except', 'exec', 'finally', 'for', 'from', 'global', 'if', 'import', 'in', 'is', 'lambda', 'not', 'or', 'pass', 'print', 'raise', 'return', 'try', 'while', 'with', 'yield']
 1 [root@hyz ~]# python
 2 Python 2.7.5 (default, Aug  4 2017, 00:39:18) 
 3 [GCC 4.8.5 20150623 (Red Hat 4.8.5-16)] on linux2
 4 Type "help", "copyright", "credits" or "license" for more information.
 5 >>> counter = 100   # 赋值整型变量
 6 >>> area = 89.7       # 浮点型
 7 >>> name = 'Peter'   # 字符串
 8 >>> print(counter)
 9 100
10 >>> print(area)
11 89.7
12 >>> print(name)
13 Peter
变量

多个变量赋值

Python允许同时为多个变量 赋值

1 >>> a = b = c = 1
2 >>> print(a)
3 1
4 >>> print(c)
5 1
6 >>> 
多变量赋值

创建一个整型对象,值为1 ,三个变量被分配到相同的内存空间上。

多个对象指定多个变量

1 >>> a,b,c, = 0,2,'Peter'
2 >>> print(a,b,c)
3 (0, 2, 'Peter')
多对象多赋值

 

if语句

  基本格式

 1 if 条件:
 2      pass
 3 elif 条件:
 4      pass 
 5 else:
 6      pass
 7 
 8 #!/usr/bin/python
 9 # -*- coding:utf-8 -*-
10 number =  9
11 numbers = int(input('输入数字:'))
12 if numbers == number:
13     print('yes')
14 elif numbers < number:
15     print('small')
16 else:
17     print('big')
18 
19 执行结果:
20 [root@hyz python3]# 
21 [root@hyz python3]# python 1.py 
22 输入数字:1
23 small
24 [root@hyz python3]# python 1.py 
25 输入数字:9
26 yes
27 [root@hyz python3]# python 1.py 
28 输入数字:10
29 big
30 [root@hyz python3]# 
if

  if语句自上而下匹配条件,最多匹配一次,可以匹配不上

whlie循环

  基本格式

1 while 条件:
2     pass 
3 
4 #!/usr/bin/python
5 # -*- coding:utf-8 -*-
6 while True:
7     print('Peter is good')   #死循环
while

  while True 死循环。

  1.条件不给True

  2.满足某种条件内部破坏循环

for 循环

  基本格式

  

1 for i in 可迭代对象:
2     print(i)
3 
4 #!/usr/bin/python
5 # -*- coding:utf-8 -*-
6 for i in [1,2,3,4,5,6]:
7     print(i)           
for 循环

continue 和break

  continue # 跳出当前这一次循环

  break # 跳出当前循环

  

1 #!/usr/bin/python
2 # -*- coding:utf-8 -*-
3 for i in [1,2,3,4,5,6]:
4     if i == 1:
5         continue
6     if i == 6:
7         break
8     print(i)
continue和break

  此参数制度循环语句(while和for生效),遇到好几个语句、循环嵌套的情况,当运行结果与预期不符时,用print打印排查

基本练习:

求1-100的所有数的和

 1 count = 1
 2 sum = 0
 3 while count < 101:
 4     sum += count
 5     count += 1
 6     print(sum)
 7 
 8 
 9 sum = 0
10 for i in range(1,101):
11     sum += i
12     print(sum)
求和

输出1-100内的所有奇数

 1 for i in range(1,101):
 2     if i %2 == 0:
 3         continue
 4     print(i)
 5 
 6 
 7 count = 1
 8 while count < 101:
 9     if count % 2 == 1:
10         print(count)
11     count += 1
奇数

输出1-100内的所有偶数

 1 for i in range(1,101):
 2     if i %2 == 1:
 3         continue
 4     print(i)
 5 
 6 count = 1
 7 while count < 101:
 8     if count % 2 == 0:
 9         print(count)
10     count += 1
偶数

求1-2+3-4+5......99的所有数的和

 1 sum = 0
 2 for i in range(1, 100):
 3     if i % 2 == 0:
 4         sum -= i
 5     else:
 6         sum += i
 7 print(sum)
 8 
 9 count = 1
10 sum = 0
11 while count < 100:
12     if count % 2 == 0:
13         sum -= count
14     else:
15         sum += count
16     count += 1
17 print(sum)
1-2+3-4..99和

用户登录(三次机会)

 1 import time
 2 count = 0
 3 account = 'Peter'
 4 password = 'xqd521'
 5 while count < 3:
 6     user_account = input('Please enter the account:')
 7     user_password = input('Please enter your password:')
 8     if user_account == account:
 9         print('Welcome!')
10         break
11     else:
12         print('Account password does not match, please re-enter')
13     count += 1
14     if count == 3:
15         print('There are too many input errors. Please reenter after 15 seconds!')
16         time.sleep(15)
17 
18 
19 
20 
21 
22 # !/usr/bin/python
23 # -*- coding:utf-8 -*-
24 
25 import time
26 user_list = [
27 {'username':'root','password':'123'},
28 {'username':'administrator','password':'123'},
29 {'username':'god','password':'123'}
30 ]
31 count = 1
32 while True:
33         user_name = input('UserName:')
34         user_password = input('Password:')
35         flag = False
36         user_info = {'username':user_name,'password':user_password}
37         if user_info in user_list:
38             print('Welcome!')
39             break
40         else:
41             print('Account password does not match, please re-enter!')
42         count += 1
43         if count == 4:
44             print('There are too many input errors. Please reenter after 15 seconds!')
45             time.sleep(15)
46             break
三次登录

猜你喜欢

转载自www.cnblogs.com/H-xqd/p/9101136.html
py
今日推荐