zoukankan      html  css  js  c++  java
  • python day1

    一、python介绍:

    1.官网:https://www.python.org/

    2.发展历史:创始人为荷兰人吉多·范罗苏姆 [2]  (Guido van Rossum),脚本解释程序。

    二、变量:存储接下来调用的东西。

    变量定义的规则:只能是字母、数字、下划线的任意组合;数字不能开头;关键字不能声明为变量。

    三、字符编码:

    ASCII:美国标准信息交换码,最多可以给256个字符分配数值,即0~255,最多只能用8位来表示。

    GB2312 :支持汉字少,一般手机、MP3支持。

    GBK:汉字扩展。

    GB18030:汉字扩展,PC平台支持。收录少数民族文字。

    编码方法时从上向下兼容。

    Unicode:万国码。解决传统的字符编码的局限而产生。他为每种语言的每个字符设定了统一并且唯一的二进制编码。规定最少由16位来表示。

    UTF-8:对Unicode的压缩和简化。将所有的字符和符号进行分类:ASCII中的用1个字节保存,欧洲字符用两个字节保存,东亚用3个保存。。。

    ## pycharm 默认用utf-8

    四、用户交互

    字符串拼接:

    1.加号

    name =input("name:")
    age=input("age:")
    job=input("job")
    salary=input("salary:")
    print(name,password)
    info='''
    ------------info of'''+ name +'''-----------
    Name:'''+name +'''
    Age:'''+ age +'''
    Job:'''+ job +'''
    Salary:'''+ salary +'''
    '''
    print(info)

    2.格式化字符串

    name =input("name:")
    age=input("age:")
    job=input("job")
    salary=input("salary:")
    print(name,password)
    info='''
    ------------info of %s-----------
    Name:%s
    Age:%s
    Job:%s
    Salary:%s
    '''%(name,name,age,job,salary)
    print(info)

    :从键盘输入的都是字符串类型数据

    name =input("name:")
    age=input("age:")
    print(type(age))
    age=int(age)
    print(type(age))
    job=input("job")
    salary=input("salary:")
    print(name,password)
    info='''
    ------------info of %s-----------
    Name:%s
    Age:%d  
    Job:%s
    Salary:%s
    '''%(name,name,age,job,salary)
    print(info)

    3.format格式化函数

    name =input("name:")
    age=input("age:")
    print(type(age))
    age=int(age)
    print(type(age))
    job=input("job")
    salary=input("salary:")
    print(name,password)
    info='''
    ------------info of {_name}-----------
    Name:{_name}
    Age:{_age}
    Job:{_job}
    Salary:{_salary}
    '''.format(_name=name,
               _age=age,
               _job=job,
               _salary=salary)
    print(info)
    name =input("name:")
    age=input("age:")
    print(type(age))
    age=int(age)
    print(type(age))
    job=input("job")
    salary=input("salary:")
    print(name,password)
    info='''
    ------------info of {0}-----------
    Name:{0}
    Age:{1}
    Job:{2}
    Salary:{3}
    '''.format(name,age,job,salary)
    print(info)

    五、if else 流程判断

    1.密文:

    输入密码时,想用密文是导入模块getpass,getpass在pycharm中不好使

    import getpass
    username = input("username:")
    password=getpass.getpass("password:")
    print(username,password)

    2.流程判断:

    if elif else

    _username="humiao"
    _password="123456"
    username = input("username:")
    password=input("password:")
    print(username,password)
    
    if _username==username and _password==password:
        print("WELCOME user {name} login".format(name=username))
    else:
        print("Invalid username or password")

     while

    count=0
    while True:
        print("count=",count)
        count=count+1  #count+=1

    for

    for i in range(10):
        print("loop",i)
    
    for i in range(0,10,2):# 从0开始到10结束,2是步长就是隔一个挑一个,3,是隔两个跳一个
        print("loop",i)

    continue:跳出本次,进行下次循环

    for i in range(0,10):
        if i <3:
            print("loop",i)
        else:
            continue
        print("hehe")

    break:直接跳出循环。

    for i in range(10):
        print("=-------",i)
        for j in range(10):
            print(j)
            if j>5:
                break 

    例:猜年龄

    i.

    age_of_oldboy=56
    guess_age=int(input("guess age:"))
    if guess_age==age_of_oldboy:
        print("yes,you got it")
    elif guess_age> age_of_oldboy:
        print("think smaller")
    else:
        print("think bigger")

    ii.

    猜三次年龄,三次内猜对,三次还猜不对就不再猜

    ##while true 版本
    age_of_oldboy=56 count =0 while True: if count==3: break guess_age = int(input("guess age:")) if guess_age == age_of_oldboy: print("yes,you got it") break # 跳出循环 elif guess_age > age_of_oldboy: print("think smaller") else: print("think bigger") count +=1
    ## while count版本
    age_of_oldboy=56 count =0 while count<3: guess_age = int(input("guess age:")) if guess_age == age_of_oldboy: print("yes,you got it") break # 跳出循环 elif guess_age > age_of_oldboy: print("think smaller") else: print("think bigger") count +=1 if count==3: print("you have tried too many times..fuck off")
    ##while else 版本
    age_of_oldboy=56 count =0 while count<3: guess_age = int(input("guess age:")) if guess_age == age_of_oldboy: print("yes,you got it") break # 跳出循环 elif guess_age > age_of_oldboy: print("think smaller") else: print("think bigger") count +=1 else: print("you have tried too many times..fuck off")
    猜年龄for循环版:

    1.猜三次就不能再猜
    age_of_oldboy=56
    for i in range(3):
        guess_age = int(input("guess age:"))
        if guess_age == age_of_oldboy:
            print("yes,you got it")
            break # 跳出循环
        elif guess_age > age_of_oldboy:
            print("think smaller")
        else:
            print("think bigger")
    else:
        print("you have tried too many times..fuck off")z
    ##2.每猜三次是否还要再猜三次
    age_of_oldboy=56
    count =0
    while count<3:
        guess_age = int(input("guess age:"))
        if guess_age == age_of_oldboy:
            print("yes,you got it")
            break # 跳出循环
        elif guess_age > age_of_oldboy:
            print("think smaller")
        else:
            print("think bigger")
        count +=1 #count=3的时候就已经猜了三次
        if count==3:
            continue_confiemation =input("Do you want to keep guessing?")
            if continue_confiemation !='n':
                count =0
    else:
        print("you have tried too many times..fuck off")
  • 相关阅读:
    C++防止头文件反复包括
    yppasswd, ypchfn, ypchsh
    yes
    Yacc
    xxd
    xpdf -Portable Document Format(PDF)文件阅读器
    xinetd
    xargs
    x25, PF_X25
    write -在一个文件描述符上执行写操作
  • 原文地址:https://www.cnblogs.com/redkongbai/p/10779214.html
Copyright © 2011-2022 走看看