python基础知识一

编译的概念

把名文代码执行前,先转成二进制,再执行, 这个过程就叫编译

sh test.sh #shell 程序的解释器

编译型与解释型

编译型语言包括:c、c++、go 等

特点:运行效率高 依赖编译平台 linux 操作系统 跟cpu交互的接口,与windows不是完全一样,不能跨平台 ,开发效率低

linux: print_to_console

windows: print_to_screen

解释型语言包括:shell、python、php、ruby 等

特点:运行效率低 跨平台,sh 解释器 负责跟不同的平台沟通,开发效率高,解释型的语言一般不需要关注硬件底层,学习成本低

变量

变量说直白一点就相当于一个容器,用来存储数据到内存中。

变量格式:

驼峰体:TrafficCost = windows 程序开发人员喜欢用 c#

官方推荐:traffic_cost = python

trafficcost age_of_oldboy = 56

ageofoldboy = 56

AGE_OF_OLDBOY = 56 #常量 不变的量

“ctrl + ?”  ---->  注释选中的 流程控制

if ...  elif ... else 判断

if ...判断条件

elif ...接着上面的继续判断

……

elif ...接着上面的继续判断

else ...上面的条件都不满足,则执行这一条内容

判断xxx的数据类型

print(type(xxx))

4 = integer = int 整数,整型

4 = string = str 字符串

a = string

改变xxx的类型:

int(“xxx”) ---> 转变为整数、整型 (int)

str(xxx) ---> 转变为字符串 (str)

同一类型的数据类型可以 相互操作, 整数+ 整数,

注意:字符串不能与整型进行计算、比较。

实操一

猜年龄 , 可以让用户最多猜三次

age = 34
for i in range(3):
    age_guess = int(input('input your guess:'))
    if age_guess > age:
        print("try smaller...")
    elif age_guess < age:
        print("try bigger...")
    else:
        print("Congratulations you guessed it")
        break
else:
    print("Input too many times")

age = 34
count = 0
while count<3:
    age_guess = int(input("input your guess:"))
    if age_guess > age:
        print("try smaller...")
    elif age_guess < age:
        print("try bigger...")
    else:
        print("Congratulations you guessed it")
        break
    count += 1
else:
    print("Input too many times")

实操二

猜年龄 ,每隔3次,问他一下,还想不想继续玩,y,n

age = 34
count = 0
while True:
    if count < 3:
        age_guess = int(input("please input your age:"))
        if age_guess > age:
            print("try smaller...")
        elif age_guess < age:
            print("try bigger...")
        else :
            print("Congratulations you guessed it")
            break
        count += 1
    elif count == 3:
        select_ge = "Continue to play don't play?(Y/N)"
        chioce = input("Continue to play don't play?Y/N")
        if chioce == "y" or chioce == "Y":
            print("Keep on talking")
            count = 0
        elif chioce == "n" or chioce == "N":
            print("Do not want to play")
            break 

age = 34
while True:
    for i in range( 3 ):
        age_guess = int(input('input your guess:'))
        if age_guess > age:
            print("try smaller...")
        elif age_guess < age:
            print("try bigger...")
        elif age_guess == age:
            print("Congratulations you guessed it")
            exit()
    else :
        chioce = input("Continue to play don't play?Y/N")
        if chioce == "y" or chioce == "Y":
            print("Keep on talking")
        elif chioce == "n" or chioce == "N":
            print("Do not want to play")
            exit() 

实操3

编写登陆接口
输入用户名密码
认证成功后显示欢迎信息
输错三次后锁定

原文地址:https://www.cnblogs.com/Michael--chen/p/6628676.html