• Python学习之路基础篇--02Python基础+小作业


    1 变量  

      变量就是将一些运算的中间结果暂存到内存中,以便后续代码调用。必须由数字,字母,下划线任意组合,且不能数字开头。不能是python中的关键字,如['and', 'as', 'assert', 'break', 'class', 'continue','def', 'del', 'elif', 'else', 'except', 'exec','finally', 'for', 'from', 'global', 'if', 'import', 'in', 'is', 'lambda', 'not', 'or', 'pass', 'print', 'raise', 'return', 'try', 'while', 'with', 'yield'],变量具有可描述性。不能是中文。

    2 常量  

      常量就是一直不变的量。在python中约定用大写表示 ,如 BIR_OF_CHINA = 1949

    3 注释  

      方便自己方便他人理解代码。单行注释:#。  多行注释:'''被注释内容''' """被注释内容"""(将多行注释赋给一个变量,相当于一个长的字符串)

    4 用户交互

      用input  等待输入  将你输入的内容赋值给了前面变量  注意:input出来的数据类型全部是str类型

    5 基础数据类型初始。  

      数字:int 12,3,45   + - * / **   % 取余数  ps:type(),查看数据类型  字符串转化成数字:int(str) 条件:str必须是数字组成的。数字转化成字符串:str(int)  

      字符串:str,python当中凡是用引号引起来的都是字符串。可相加,字符串的拼接。可相乘:str * int 

      bool:布尔值。 True 和False

    6 If 语句

      if 条件:
        结果

    7 While语句  

        while 条件:    

          循环体

      这将无限循环。终止循环:改变条件,使其不成立;使用break。

      continue:跳出本次循环,进入下次循环。

    8 While... else... 语句

      如果程序正常走完,就会输出else后面的语句

    作业:

    1、使用while循环输入 1 2 3 4 5 6     8 9 10

    count = 0
    while count < 10:
        count += 1
        if count ==7:
            continue
        print(count)

    2、求1-100的所有数的和

    count = 1
    sum = 0
    while count <= 100 :
        sum = sum + count
        count += 1
    print(sum)

    3、输出 1-100 内的所有奇数

    for i in range(1,100,2):
        print(i)

    4、输出 1-100 内的所有偶数

    for i in range(0,101,2):
        print(i)

    5、求1-2+3-4+5 ... 99的所有数的和

    sum = 0
    count = 1
    while count < 100:
      if count %2 == 0:
        sum -= count
      else:
        sum += count
      count += 1
    print(sum)

    6、用户登陆(三次机会重试)

    i = 0
    while i < 3:
    user_name = input('Please, input your count:')
    password = input ('please, input your password:')
    if user_name == 'eli' and password =='eli123':
    print('wlecome~~~')
    break
    elif i != 2:
    print('You are fail to log in...you have %d' %(2-i))
    i += 1
  • 相关阅读:
    最小生成树(卡鲁斯卡尔)
    最小生成树(kruskal模版 Prim模板)
    hdu1247(Hat’s Words)
    hdu1671Phone List(字典树)
    hdu1305Immediate Decodability(字典树)
    hdu1251(统计难题)
    oj1500(Message Flood)字典树
    oj2892(字典树)
    数论公式
    2504(多项式求和)
  • 原文地址:https://www.cnblogs.com/YS-0717/p/9359936.html
Copyright © 2020-2023  润新知