python来历_Python的由来与使用介绍

创建字典:

#!/usr/binl/env python

#encoding: utf-8

#author: YangLei

user_info = {"name":"yanglei","age":23,"job":"IT"}

常用操作:索引

新增

删除

键、值、键值对

循环

长度

七、数据运算

算数运算:

ae5bc562f906b47be5a1fa8436af552d-1.png

比较运算:

ae5bc562f906b47be5a1fa8436af552d-2.png

赋值运算:

ae5bc562f906b47be5a1fa8436af552d-3.png

逻辑运算:

eb1fc2b2e49abded4f7e9dcb70c45d31-4.png

成员运算:

eb1fc2b2e49abded4f7e9dcb70c45d31-5.png

身份运算:

eb1fc2b2e49abded4f7e9dcb70c45d31-6.png

位运算:

eb1fc2b2e49abded4f7e9dcb70c45d31-7.png

运算符优先级:

0bce7e545edab8d6f834aa19b475251a-8.png

八、if判断

场景一、用户登陆验证

#!/usr/binl/env python

#encoding: utf-8

#author: YangLei

input_user = input("Please enter your user name: ")

input_password = input("Please enter your password: ")

if input_user == "yanglei" and input_password == "123456":

print("\033[32;1m%s login successfully\33[0m" % input_user)

else:

print("\033[31;1mThe user name or password error,please try again\033[0m")

场景二、猜年龄游戏

在程序里设定好你的年龄,然后启动程序让用户猜测,用户输入后,根据他的输入提示用户输入的是否正确,如果错误,提示是猜大了还是小了

#!/usr/binl/env python

#encoding: utf-8

#author: YangLei

guess_age = 50

input_age = int(input("Please enter your guess age: "))

if input_age > guess_age:

print("\033[31;1mCan you guess what big\33[0m")

elif input_age < guess_age:

print("\033[31;1mCan you guess what small\33[0m")

else:

print("\033[32;1mYou guessed it\33[0m")

外层变量,可以被内层代码使用

内层变量,不应被外层代码使用

九、break和continue的区别

continue:

#!/usr/binl/env python

#encoding: utf-8

#author: YangLei

count = 1

while count <= 10:

if count == 5:

count += 1

continue

print(count)

count += 1

break:

#!/usr/binl/env python

#encoding: utf-8

#author: YangLei

count = 1

while count <= 10:

if count == 5:

count += 1

break

print(count)

count += 1

由此可以看出continue是跳出当前循环,而break是跳出本层循环。

十、while循环

循环语句,计算机的一种基本循环模式。当满足条件时进入循环,不满足跳出。while语句的一般表达式为:

while(表达式)

{

循环体

}

场景一、用户登陆验证升级

#!/usr/bin/env pyhon

#encoding: utf-8

#auth: yanglei

count = 0

while count < 3:

input_user = input("Please enter your user name: ")

input_password = input("Please enter your password: ")

if input_user == "yanglei" and input_password == "123456":

print("\033[32;1m%s login successfully\33[0m" % input_user)

break

elif count == 2:

print("\033[31;1mThe user name or password mistake,three chances to use up,the program exits\33[0m")

break

else:

count += 1

print("\033[31;1mThe user name or password error,please try again\033[0m")

场景二、猜年龄游戏升级

#!/usr/bin/env pyhon

#encoding: utf-8

#auth: yanglei

guess_age = 50

count = 0

while count <= 3:

if count == 3:

input_choose = input("Do you want to continue to play?(Y or y|N or n)")

if input_choose == "Y" or input_choose == "y":

count = 0

continue

elif input_choose == "N" or input_choose == "n":

break

else:

print("\033[31;1mAre you input errors!\33[0m")

continue

input_age = int(input("Please enter your guess age: "))

if input_age > guess_age:

print("\033[31;1mCan you guess what big\33[0m")

count += 1

elif input_age < guess_age:

print("\033[31;1mCan you guess what small\33[0m")

count += 1

else:

print("\033[32;1mYou guessed it\33[0m")

break

  • 0
    点赞
  • 0
    评论
  • 0
    收藏
  • 一键三连
    一键三连
  • 扫一扫,分享海报

表情包
插入表情
评论将由博主筛选后显示,对所有人可见 | 还能输入1000个字符
©️2020 CSDN 皮肤主题: 深蓝海洋 设计师:CSDN官方博客 返回首页
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值