首页 > 编程语言 > 详细

【Pyhthon开发】:初识Python

时间:2019-09-09 00:29:44      阅读:73      评论:0      收藏:0      [点我收藏+]

一、变量

声明变量

#!/Library/Frameworks/Python.framework/Versions/3.7/bin/python3.7    
# -*- coding:utf-8 -*-     
# caidongsheng
# 2019-09-08 21:21
# example20.py
# PyCharm

name = "caidongsheng"

变量定义的规则

  • 变量名只能是 字母、数字或下划线的任意组合
  • 变量名的第一个字符不能是数字
  • 以下关键字不能声明为变量名。[‘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‘]

变量的赋值

#!/Library/Frameworks/Python.framework/Versions/3.7/bin/python3.7    
# -*- coding:utf-8 -*-     
# caidongsheng
# 2019-09-08 21:21
# example20.py
# PyCharm

name = "caidongsheng"
name2 = name
print(name,name2) ## caidongsheng caidongsheng
name = "lixue"
print("name2 is ",name2) ##name2 is caidongsheng

二、流程控制

#!/Library/Frameworks/Python.framework/Versions/3.7/bin/python3.7    
# -*- coding:utf-8 -*-     
# caidongsheng
# 2019-09-08 21:21
# example20.py
# PyCharm


# 提示输入用户名和密码
# 验证用户名和密码
#     如果错误,则输出用户名或密码错误
#     如果成功,则输出 欢迎,XXX!


import getpass

name = input(请输入用户名:)     ##input输入获取的值都为字符串!!!
pwd = getpass.getpass(请输入密码:)

if name == "caidongsheng" and pwd == "cmd":
    print("欢迎,caidongsheng")
else:
    print("用户名和密码错误")

三、while循环

语法规则

while 条件:

# 循环体
# 如果条件为真,那么循环体则执行
# 如果条件为假,那么循环体不执行

break

  break用于退出所有循环

#!/Library/Frameworks/Python.framework/Versions/3.7/bin/python3.7    
# -*- coding:utf-8 -*-     
# caidongsheng
# 2019-09-08 21:37
# example21.py
# PyCharm

while True:
    print("123")
    break
    print("456")


输出结果:
123

continue

  continue用于退出当前循环,继续下一次循环

#!/Library/Frameworks/Python.framework/Versions/3.7/bin/python3.7    
# -*- coding:utf-8 -*-     
# caidongsheng
# 2019-09-08 21:37
# example21.py
# PyCharm

while True:
    print("123")
    continue
    print("456")

输出结果:
123 123 ...

 

四、练习题

1、使用while循环输入 1 2 3 4 5 6 8 9 10

#!/Library/Frameworks/Python.framework/Versions/3.7/bin/python3.7    
# -*- coding:utf-8 -*-     
# caidongsheng
# 2019-09-08 10:07
# example19.py
# PyCharm

num = 1
while num <= 10:
    if num == 7:
        pass
    else:
        print(num)
    num += 1

2、求1-100的所有数的和

#!/Library/Frameworks/Python.framework/Versions/3.7/bin/python3.7    
# -*- coding:utf-8 -*-     
# caidongsheng
# 2019-09-08 10:07
# example19.py
# PyCharm


sum = 0

i = 1
while  i <= 100:
    sum = sum + i
    i = i + 1

print(sum)

3、输出 1-100 内的所有奇数

#!/Library/Frameworks/Python.framework/Versions/3.7/bin/python3.7    
# -*- coding:utf-8 -*-     
# caidongsheng
# 2019-09-08 10:07
# example19.py
# PyCharm


sum01 = 0
j = 1
while  j <= 100:
    if j % 2 == 1:
        sum01 = sum01 + j
    else:
        pass
    j = j + 1

print("1到100,奇数和是: ",sum01)

4、输出 1-100 内的所有偶数

#!/Library/Frameworks/Python.framework/Versions/3.7/bin/python3.7    
# -*- coding:utf-8 -*-     
# caidongsheng
# 2019-09-08 10:07
# example19.py
# PyCharm


sum02 = 0
j1 = 1
while  j1 <= 100:
    if j1 % 2 == 0:
        sum02 = sum02 + j1
    else:
        pass
    j1 = j1 + 1

print("1到100,偶数和是: ",sum02)

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

#!/Library/Frameworks/Python.framework/Versions/3.7/bin/python3.7    
# -*- coding:utf-8 -*-     
# caidongsheng
# 2019-09-08 10:07
# example19.py
# PyCharm


sum03 = 0
sum04 = 0

j2 = 1

while  j2 <= 99:
    if j2 % 2 == 0:
        sum03 = sum03 - j2
    else:
        sum04 = sum04 + j2
    j2 = j2 + 1

print("和是: ",sum04+sum03)

6、用户登陆(三次机会重试)

 

#!/Library/Frameworks/Python.framework/Versions/3.7/bin/python3.7    
# -*- coding:utf-8 -*-     
# caidongsheng
# 2019-09-08 10:07
# example19.py
# PyCharm
user_name = input("请输入用户名:")
loop = 1
while loop <= 3:
user_name = input("请输入用户名:")
user_pass = input("请输入密码: ")
if user_name == "caidongsheng" and user_pass =="123456":
print("欢迎进入!!")
break
else:
print("用户或者密码错误!!")
loop = loop + 1
 

 

【Pyhthon开发】:初识Python

原文:https://www.cnblogs.com/caidongsheng94/p/11488745.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!