学习Python第一天记录

本文介绍了Python中的基本赋值运算,如算术运算符和**=运算,以及逻辑运算符的使用,包括and和or条件判断。此外,展示了如何运用if-else和多分支结构进行选择流程控制,涵盖了输入输出、循环遍历、名片显示和游戏逻辑。
摘要由CSDN通过智能技术生成

赋值运算

# 赋值运算
# += -= *= \= %= **=
a,b,c,d=23,18,10,3
# a+=c
# print(a)
a**=2
print(a)

逻辑运算符

# 逻辑运算符 and or not
# and 条件比较严格
# 定义四个变量
a,b,c,d=23,18,10,3
print(a + b > c and c < d)
print(c > d and a > b)

输入和输出

# 输出 %占位符
name = '张洋'
classPro = '青川附中一年级三班'
age = 7
print('我的名字是:%s 来自:%s 今年%d岁了'%(name,classPro,age))


# 输入 获取键盘输入的内容
name = input('请输入您的姓名:')
QQ = int(input('请输入您的QQ:'))
phone = input('请输入您的电话:')
address = input('请输入您的地址:')

print("名字:%s"%(name))
print("QQ:%d"%(QQ))
print("电话:{}".format(phone))
print("地址:%s"%(address))

名片显示

print('===================================')
name = '老夫子'
QQ = 66666666
phone = 15000000000
adress = '广州市白云区'
print('姓名:%s\nQQ:%d\n手机号:%d\n公司地址:%s'%(name,QQ,phone,adress))
print('===================================')

选择流程

# 双分支
# if 条件表达式:
#     代码指令
#     ......
score = int(input("您的成绩是:"))
if score <= 60:
    print("您不及格")
else:
    print("您及格了")
    pass
print("加油")


# 多分支
# if 条件表达式:
#       ......
# elif 条件表达式:
#       ......
if score == 10:
    print("您的成绩是10分")
elif score ==20:
    print("您的成绩是20")
elif score ==30:
    print("您的成绩是30")
else:
    print("您的成绩不在10-30分内")


# 多分支 多条件的演练
# 猜拳游戏
# 0:石头 1:剪刀 2:布
import  random # 随机数
# 计算机  人
person = int(input("请出拳:[0:石头 1:剪刀 2:布]"))
computer = random.randint(0,2)
if person == 0 and computer == 1:
    print("人类赢")
elif person ==1 and computer==2:
    print("人类赢")
elif person == 2 and computer ==0:
    print("人类赢")
elif person ==computer:
    print("平手")
else:
    print("机器人赢")

while循环

# 循环的分类
# while 语法结构:
# while 条件表达式:
#     代码指定
#     ......
# 语法特点
# 1.有初始值
# 2.条件表达式
# 3.自增变量(循环体内计数变量) 或者 自减变量,否则会死循环
# 使用条件:循环的次数不确定,是依靠循环条件来结束\
# 目的:为了将相似或者相同的代码操作变的更加简洁,使我们的代码可以重复的利用
# for


# while的使用
# 输出1-100之间的数据
index =1 # 定义一个变量
while index <= 100:
    print(index)
    index+=1
    pass

#猜拳游戏改进
count =1
while count<=10:
    import random  # 随机数
    # 计算机  人
    person = int(input("请出拳:[0:石头 1:剪刀 2:布]"))
    computer = random.randint(0, 2)
    if person == 0 and computer == 1:
        print("人类赢")
    elif person == 1 and computer == 2:
        print("人类赢")
    elif person == 2 and computer == 0:
        print("人类赢")
    elif person == computer:
        print("平手")
    else:
        print("机器人赢")
        pass
    count+=1

打印99乘法表

# 打印99乘法表
row = 1
while row <= 9:
    col = 1
    while col <= row:
        print(" %d * %d =%d " % (row, col, row * col),end="")
        col += 1
    print()
    row += 1

打印直角三角形

# 打印直角三角形
row = 1
while row<=7:
    j=1
    while j<=row:
        print('*',end=" ")
        j+=1
    print()
    row+=1

for循环

# for循环
# 语法特点 : 遍历操作,依次的取集合容器中的每个值
# for 临时变量 in 容器:
#   执行代码块

tags = '我是一个中国人' # 字符创类型本身就是一个字符类型的集合
for item in tags:
    print(item)
    pass


# range 此函数可以生成一个数据集合列表
# range(起始:结束:步长) 步长不能为0
sum =0
for data in range(1,101): # 左边包含 右边不包含
    sum+=data # 求累加和
    pass

print("sum = {}".format(sum))


for data1 in range(50,201):
    if data1%2==0:
        print("{}是偶数".format(data1))
        pass
    else:
        print("{}是奇数".format(data1))

break和continue

# break和continue
# break 代表终端结束的意思  满足条件直接结束本层循环
# continue 结束本次循环继续进行下次循环(当continue的条件满足的时候,本次循环剩下的语句将不再执行
# 后面的循环继续)
# 这两个关键字只能用在循环中

sum =0
for item in range(1,51):
    if sum>100:
        break
        pass
    sum+=item
    pass

print("sum = {}".format(sum))

for item1 in range(1,101):
    if item1%2==0:
        continue
        pass
    print(item1)
    pass

99乘法表for实现

# 99乘法表for实现
for i in range(1,10):
    for j in range(1,i+1):
        print("%d*%d=%d"%(i,j,i*j),end=" ")
        pass
    print()
    pass

for–else

# for--else
for item in range(1, 11):
    print(item, end=" ")
    if item >= 5:
        break
    pass
else:
    print("已经执行完了吗")
    pass

# 三次密码错误提示系统锁定
account = 'wyw'
pwd = '123'
for i in range(3):
    zh = input("请输入账号: ")
    pd = input("请输入密码: ")
    if account == zh and pwd == pd:
        print("恭喜您登陆成功")
        break  # 退出本层循环了
    pass
else:
    print("您的账号已经被系统锁定")

while–else

# while--else
index=1
while index<=10:
    print(index)
    if index ==6:
        break
    index+=1
    pass
else:
    print("执行了吗")
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值