02_if.py

name = 'westos'
password = 'westos'
print(name, password)
name = input("Name: ")
password = input("Password:")
#分支语句的条件:name == 'root' and password == 'westos'
#如果满足条件, 执行第7行语句;
#如果不满足条件, 执行第9行语句;
if name == 'root' and password == 'westos':
print("%s登录成功" %(name))
else:
print("%s登录失败" %(name))

03_loop.py

#循环语句
try_count = 0

while循环的原理:
如果表达式为真, 执行while循环里面的语句;
如果表达式不为真, 结束循环, 不再执行循环里面的语句.
while try_count < 3:
guess_num = input("Num: ")
try_count += 1
print("用户已经尝试了%s次" %(try_count))

#try_count = 0 input_num =1
#try_count = 1 input_num =2
#try_count = 2 input_num =3
#try_count = 3

04_circle.py

导入数学模块
import math

py3中, input接收的是什么类型? 字符串类型
r = float(input("输入圆的半径: "))
C = 2 math.pi r
S = math.pi * pow(r, 2)

print("半径为%.1fcm的圆的周长为%.1f ,面积为 %.1f 。" %(r, C, S))

05_if_else.py

name = input("Name: ")
password = input("Password:")
#分支语句的条件:name == 'root' and password == 'westos'
#如果满足条件, 执行第7行语句;
#如果不满足条件, 执行第9行语句;
if name == 'root' and password == 'westos':
print("%s登录成功" %(name))
else:
print("%s登录失败" %(name))

06_if_no_else.py

age = int(input("Age:"))

if age < 18:
print("未成年")

07_get_score_grade.py

#多分支语句
score = float(input("成绩: "))

if 85 <= score < 100:
print('A')
#elif score >= 70 and score < 85:
elif 70 <= score < 85:
print("B")
elif 60 <= score < 70:
print('C')
elif 0 < score < 60:
print('D')
else:
print('invaild score')
#a = 10
#b = 5
#if a > b:
#max = a
#else:
#max = b
#print(max)

08_三元运算符.py

#三元运算符, if...else的简写
a = 10
b = 5
print(a if a > b else b)
#嵌套分支语句

09_嵌套分支语句.py

import getpass
print("**用户登录*****")
name = input("Name: ")
passwd = getpass.getpass("Password: ")

if name == 'root':

占位符:pass

if passwd == 'westos':
    print("%s登录成功" %(name))
else:
    print("%s登录失败: 密码错误" %(name))

else:
print("帐号不存在")

10_random拓展使用.py

"""

random.random()
0.8100583312773273
random.uniform(10, 100)
89.2102811051263
random.randint(10, 20)
11
random.choice('abcdefg')
'a'
nums = list(range(1, 10) )
nums
[1, 2, 3, 4, 5, 6, 7, 8, 9]
#将的元素打乱顺序
random.shuffle(nums)
nums
[5, 1, 7, 2, 4, 9, 8, 3, 6]

"""
import random

print("随机生成的年份: ", random.randint(2000, 2200))
height = random.uniform(100, 160)
#round设置浮点数的精度
print("随机生成的身高: ", round(height, 1))

11_闰年判断器.py

#随机生成一个年份,判断是不是闰年
#import random
#year = random.randint(1900,2000)
#if (year % 4 == 0 and year % 100 != 0) or (year % 400 == 0):
#print("%d年份是闰年"%(year))
#else:
#print("%d年份不是闰年"%(year))

import random
year = random.randint(1900,2000)
isleap = (year % 4 == 0 and year % 100 != 0) or (year % 400 == 0)
print("%d年份是闰年"%(year) if isleap else "%d年份不是闰年"%(year))

12_偶数判断器.py

import random
num = random.randint(0, 100)
is_even = (num % 2 == 0)
print( "数值%d是偶数" %(num) if is_even else "数值%d不是偶数" %(num) )

13_一元二次方程求解.py

#解一元二次方程,用户一次输入a,b,c的值
import math
a = float(input("输入a="))
b = float(input("输入b="))
c = float(input("输入c="))
delta = pow(b,2)-4ac

if delta == 0:
x1 = (-b+math.sqrt(delta))/(2a)
print("该方程有一个解,解为 x1=%.1f"%(x1))
elif delta > 0:
x1 = (-b+math.sqrt(delta))/(2
a);
x2 = (-b-math.sqrt(delta))/(2*a)
print("该方程有两个解,解为x1=%.1f,x2=%.1f"%(x1,x2))
else:
print("该方程无解")

14_while计数循环.py

count = 0
while(count < 9):
print("the index is : ", count)
count += 1
#循环打印了9次。
#count = 0 print(0) 1
#count = 1 print(1) 2
#count = 2 print(2) 3
#..........
#count = 8 print(8) 9
#count = 9

15_while死循环.py

#用户一直输入命令, 打印命令的执行结i果.

#导入os模块, 封装了操作系统常用的方法, 比如: 创建/删除文件, 执行shell命令等.
import os
while True:
cmd = input("cmd>> ") # date
#os.system方法执行shell命令并打印命令的执行结果。
os.system(cmd)

16_for循环.py

#for、in: 关键字
#for item in 'hello': item的值是变化的, 并依次赋值,
#item = 'h'
#item = 'e'
#item = 'l'
#item = 'l'
#item = 'o'
"""
for item in 'hello':
print("字符显示: ", item)
"""

#["粉条", "粉丝", "粉带"]: 列表
for name in ["粉条", "粉丝", "粉带"]:
print("猫的姓名是: ", name)

17_for结合range的循环.py

#需求: 循环10次
#需求: 循环100次
"""
range的用法:
range(stop) 从0开始到stop-1结束
range(4) [0, 1, 2, 3]
range(start, end) 从start开始到end-1结束
range(1,6) [1, 2, 3, 4, 5]
**range(start, end, step) 从start开始到end-1结束, 步长是指定的
range(0,10,2) [0, 2, 4, 6, 8]
"""
for count in range(100):
print("循环第%d次" %(count+1))

#4! = 1234
#5! = 1
234*5

"""
#通过for循环实现阶乘操作
result = 1
for num in range(1,4):

num=1, 2, 3, 4, 5

#result = result * num  
#result = 1 * 1   result = 1*1*2  result=1*1*2*3
result *= num

print(result)
"""

18_循环求阶乘.py

#通过while循环实现阶乘操作
result = 1
num = int(input('N:'))
index = 1

while index <= num:
result *= index
index += 1
print(result)

#index result
#1 1
#2 2
#3 6
#4

19_循环求偶数和.py

"""
result = 0
for num in range(1, 101):
if num % 2 == 0:
result = result + num
print(result)
"""

#sum是内置方法, 求和
#range(2, 101, 2): 获取1-100之间所有的偶数
print(sum(range(2, 101, 2)))

20_break与continue跳出循环.py

"""
for count in range(10):
if count == 2:

break跳出循环,不再执行循环语句

    break
print(count)

#count if
#0 False
#1 False
#2 True
"""

for count in range(10):
if count == 2:

跳出本次循环,会再次执行循环语句

    continue
print(count)

21_九九乘法表.py

#i: 外层循环

#i是指外层循环, 循环9次
for i in range(9, 0, -1):

j是内层循环, 循环i次;

for j in range(1, i+1):
    # print('sddedd', end=' '), 不换行打印
    print("%d*%d=%d" %(j, i, i*j), end=' ')
#每层打印结束, 则换行.
print()

#i是指外层循环, 循环9次
for i in range(1, 10):

j是内层循环, 循环i次;

for j in range(1, i+1):
    # print('sddedd', end=' '), 不换行打印
    print("%d*%d=%d" %(j, i, i*j), end=' ')
#每层打印结束, 则换行.
print()

22_防***暴力破解的用户登录系统.py

import getpass

try_count = 0

while try_count < 3:
try_count += 1
print("第%d次登录系统" %(try_count))
name = input("Name: ")
password = getpass.getpass("Password:")

if name == 'root' and password == 'westos':
    print("%s登录成功" %(name))
    #登录成功后, 不需要再循环, break跳出循环
    break
else:
    print("%s登录失败" %(name))

#当while后面的表达式返回False, 执行else里面的语句。while....else结合再python中是特有的。
else:
print("尝试次数超过三次, 请稍后再试")

23_draw_flower.py

#Turtle库是Python语言中一个很流行的绘制图像的函数库
import turtle
import random
"""

  1. 设置画笔颜色和填充颜色, 并设置画笔移动速度
  2. turtle.begin_fill()准备开始填充图形
  3. 向当前画笔方向移动distance像素长为200
  4. 逆时针(向左)移动170°
  5. 循环3、4步50次
  6. turtle.end_fill()实现填充完成
  7. turtle.done()完成绘制
    """

#1. 设置画笔颜色和填充颜色, 并设置画笔移动速度
turtle.color('red', 'yellow')
turtle.speed(10)

#2. turtle.begin_fill()准备开始填充图形
turtle.begin_fill()

#5. 循环3、4步50次
for count in range(50):
#3. 向当前画笔方向移动distance像素长为200
turtle.forward(200)
#4. 逆时针(向左)移动170°
turtle.left(170)
#6. turtle.end_fill()实现填充完成
turtle.end_fill()
#7. turtle.done()完成绘制
turtle.done()

24_greater_flower.py

from turtle import *
from random import randint
speed(0)
bgcolor('white')
x = 1
while x < 1000:

三原色: red, green, blue

#(255, 0, 0) === red
#(0, 255, 0) ==== green
#(0, 0, 255) ==== blue
r = randint(0,255) #让颜色随机变化
g = randint(0,255)
b = randint(0,255)
colormode(255)
pencolor(r,g,b) # 根据r,g,b的值改变笔触颜色
#向前运行50+x个像素
fd(50 + x)
#向右旋转多少度
rt(90.911)
x = x+1

exitonclick()