Python入门
一、input
1、永远等待,直到用户输入了值,就会将输入的值赋值给一个东西
二、变量
1、变量名只能是字母、数字、下划线的组合,单纯的一类或者两类也可以
2、变量名的第一个字符不能是数字
3、以下关键字不能作为变量名
['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']
Ps:变量名不能是关键字
不能和Python内置的名字重复
4、声明变量
#!/usr/bin/env python # -*- coding: utf-8 -*- name = "zhoubin"
三、if条件语句
1、基本语句
if "Albert" == "Albert": print("正确") else: print("错误") #注意:缩进用四个空格
2、多条条件
if "Albert" == "Albert": print("第一个条件") elif: print("第二个条件") elif: print("第三个条件") else: print("第四个条件") print("end")
四、while循环
1、基本循环
#while 条件: # 循环体 # 如果条件为真,那么循环体则执行 # 如果条件为假,那么循环体不执行
2、break
break用于退出所有循环
while True: print("123") break print("456")
3、continue
continue用于退出当前循环,继续下一次循环
while True: print("123") continue print("456")
五、for循环
# 例: # test = "Albert" # for i in test: # print(i) # 输出: # A # l # b # e # r # t # ps:所有可迭代对象都能用for循环 # dic = { # "k1":"v1", # "k2":"v2" # } # for i in dic.values(): # print(i) # 输出: # v1 # v2 # ps:循环输出字典的值
练习题
1、使用while循环输入 1 2 3 4 5 6 8 9 10
2、求1-100的所有数的和
3、输出 1-100 内的所有奇数
4、输出 1-100 内的所有偶数
5、求1-2+3-4+5 ... 99的所有数的和
6、用户登陆(三次机会重试)