• 基本运算符与if while详解:


    '''
    基本运算符与if while详解:
    '''

    # 算术运算符
    # + - * / % // **  # 返回一个数值
    
    # 比较运算符
    # > >= < <= == !=  # 返回一个布尔值
    
    # 赋值运算符
    # =
    x = 10
    
    # 逻辑运算符(把多个条件同时叠加)
    
    name = 'nick'
    height = 180
    weight = 140
    
    # # and 左右两个条件都为True,则为True,否则为False
    # print(name == 'nick' and height == 180)  # True
    # print(name == 'nick1' and height == 180)  # False
    #
    # # or 左右两个条件只要有一个满足则为True,否则为False
    # print(name == 'nick' or height == 190)  # True
    # print(name == 'nick1' or height == 190)  # False
    #
    # # not 否,如果条件为True,则为False,如果条件为False,则为True
    # print(not name == 'nick')  # False
    
    # 身份运算符
    # 每一个变量值都有内存地址(身份)
    x= 257
    y = x
    z= 257
    
    print(id(x) == (id(y)))
    print(x is y) # is比较的是内存地址
    print(x is not y)  # is not判断是否不等于
    print(not x is y)
    print(id(x) == id(z))
    print(x is z)
    
    # 位运算符(从来没见过)
    
    # 60 13  十进制 0,1,2,3,4,5,6,7,8,9,10
    
    # 0,1,2,3,4,5,6,7,8,9,逢十进一位,10,11,12,13,...19,20...90,91,92,..99,100
    
    
    # 0和1 二进制
    
    # 0,1,逢二进一位,10,11,100,101,111,1000
    
    # 0 # 0000 0000 --》0
    # 1 # 0000 0001 --》 1
    # 10 # 0000 0010 --》 2
    # 11 # 0000 0011 --》 3
    # 100 # 0000 0100 --》 4
    # 101 --》 5
    # 110 --> 6
    # 111 --> 7
    
    
    # 0100 0011  -->
    # 方法一,计算器:67
    # 方法二:手工计算
    
    # 9892 == 2*10**0 + 9*10**1 + 8*10**2 + 9*10**3
    print(2*10**0 + 9*10**1 + 8*10**2 + 9*10**3)
    # 01000011 == 1*2**0 + 1*2**1 + 0 + 0 + 0 + 0 + 1*2**6 + 0
    print(1*2**0 + 1*2**1 + 0 + 0 + 0 + 0 + 1*2**6 + 0)
    
    
    '''
    a = 0011 1100 # 60
    
    b = 0000 1101 # 13
    
        0000 1100 # 12
    
    # 科学计算(计算原子弹的弹道轨迹)的情况下
    
    '''
    
    
    # 0000 a
    # 0001 b
    # 0010 c
    
    # 0110 e
    
    # 0001 0010 bc
    
    
    
    
    # 成员运算符:判断元素是否在容器类元素里面(字符串)
    class_student_lt = ['s1','s2','s3']
    print('s1' in class_student_lt) # True
    print('s1' not in class_student_lt) # False
    print('s4' in class_student_lt) # False
    
    s = 'nick'
    print('n' in 'nick')
    
    # Python运算符优先级
    # + - * / : 先算* / 再算 + -,就叫做优先级
    
    # 需要优先,就加括号,括号优先级最高 (经验) --》 顿悟,上课的听的萌萌懂,
    
    print((1+2)*3)
    
    # # print(1.2 - 1.0 == 0.2)  # True
    # # #     010101010 01010101010
    # # # 010101010 - 01010101010  # 一些差错
    # # print(round(1.2 - 1.0))  # 浮点数运算时造成的不确定尾数
    # #
    # #
    # # # 12--》 0101010
    # # # 1.2 --》 101001100101010101010100100101
    # # # 1.0 --》 101010110011101100110101001010
    # # #          101010111111111110000000000000
    # #
    # #
    # # # 流程控制 --> 控制 变量变化 的一个方向
    # #
    # # # IPO --> input process output
    # #
    # # # x = 10
    # # # for i in range(10):
    # # #     print(i*10)
    # # #
    # # #
    # # # x = 10
    # # # name = input('')
    # # # if name == 'nick':
    # # #     print(x*10)
    # # # else:
    # # #     print(x*100)
    # #
    # #
    # # x  = 10
    # # x = x + 10
    # # print(x)
    # # x += 10  # x = x + 10  # +=:赋值运算符
    # # '''
    # # -= : x = x -
    # # *= : x = x *
    # #
    # # '''
    # # print(x)
    #
    #
    # # if判断  if(如果)---》判断
    #
    #
    # # # 单分支结构
    # # name = 'nick'
    # # inp = input('name: ')
    # # if name == inp:
    # #     print('撸起袖子加油干')
    # # print(1)
    #
    # '''
    # if 条件: (:表示你接下来的代码需要缩进) # 条件为True运行缩进内代码;不成立不运行缩进内代码
    #     print('撸起袖子加油干')
    #     code1
    #     code2
    #     code3
    #     code3
    #     代码块
    #
    # print(1)
    # '''
    #
    # # 双分支结构
    #
    # '''
    # if 条件:
    #     code1 条件成立执行code1
    # else:
    #     code2 条件不成立执行code2
    # '''
    # # name = 'bzr'
    # # s = input('name: ')
    # # if s == name:
    # #     print('猥琐的班主任')
    # # else:
    # #     print('英俊的nick老师')
    #
    # # 多分支结构
    # '''
    # if 条件1:
    #     code1 条件1成立执行code1
    # elif 条件2:
    #     code2 条件1不成立条件2成立执行code2
    # elif 条件3:
    #     code3 条件1和2不成立,条件3成立执行code3
    # elif可以有无限个。。。
    #     coden
    # else:
    #     code4 所有条件都不成立,执行code4
    # '''
    #
    # '''
    # height  > 130 全票
    # heigh > 70 and height < 130 半票
    # height < 70 免票
    # '''
    #
    # height = int(input('请输入你的身高:'))
    #
    # if height > 130:
    #     print('全票')
    # elif height > 70:
    #     print('半票')
    # else:
    #     print('免票')
    #
    #
    # if height>130:
    #     print('全票')
    # if height <130 and height >70:
    #     print('半票')
    # if height <70:
    #     print('免票')
    #
    # # 变量/if判断/for循环
    #
    # '''
    # if
    #
    # if
    #
    # if
    # '''
    # # 和
    # '''
    # if
    #
    # elif
    #
    # else
    # '''
    # # 的区别
    #
    #
    # '''
    # 如果 成绩>=90,打印"优秀"
    # 如果 成绩>=80 并且 成绩<90,打印"良好"
    # 如果 成绩>=70 并且 成绩<80,打印"普通"
    # 其他情况:打印"差"
    # '''
    #
    # grade = input('请输入你的成绩:')
    #
    # grade_int = int(grade)
    #
    # if grade_int>=90:
    #     print('优秀')
    # elif grade_int >=80:
    #     print('良好')
    # elif grade_int >=70:
    #     print('普通')
    # else:
    #     print('差')
    #
    #
    # # 小伙子,给我做一个登录功能。(模仿)
    #
    # # 难在条件的选择,以及输入/输出的选择
    
    
    # 找bug的绝招,打印变量,查看变量的变化过程 --》 debug的来源
    
    # x = int(input('x:'))
    # print(1,x)
    #
    #
    # if x > 100:
    #     x *= 10  # x = x*10
    #     print(2,x)
    # elif x > 10:
    #     # code1
    #     # code2
    #     # code1
    #     # code2
    #     x /= 10  # x = x /10
    #     print(3,x)
    # else:
    #     pass  # 啥也不做,占个位置
    #
    # print(x)
    
    
    # 前期不要用debug模式运行,而是自己通过print查看变化
    for  i in range(10):
        print(i**2)
    
    
    # 5%语法错误
    # 80%错误来自于逻辑错误(变量的变化方向没有控制好)
    # 10%粗心错误
    
    name ='nick'
    name1= 'jason'
    
    
    # 流程控制:控制变量往一个方向变化
    
    
    # 循环:重复(按照某种规律)干一件事
    
    # print(1)
    # print(2)
    # print(3)
    # print(4)
    # print(5)
    # print(6)
    # print(7)
    # print(8)
    # print(9)
    # print(10)
    
    
    # print(1)
    # print('nick')
    # print(2)
    
    
    # while 当
    
    
    '''
    while 条件: # 条件成立运行代码,不成立结束while循环
        代码 # 代码执行结束后会进入下一次循环(再一次判断条件)
    '''
    # while 1:
    #     print(1)
    #
    # print(2)
    
    # while + break
    # count = 0
    # while 1:
    #     if count == 100:
    #         break  # break终止循环
    #     count += 1
    #     print(count)
    #
    # print('bzr')
    
    # while + continue 不打印50
    # count = 0
    # while 1:
    #     if count == 100:
    #         break  # break终止循环
    #     count += 1
    #     if count == 50:
    #         continue  # continue跳出本次循环,不执行下面的代码
    #     print(count)
    #
    # print('bzr')
    
    
    # 打印1-100内偶数(不包括[22,46,68,98])的和
    # 分解题目
    # print(2525 - 22 - 46 - 68 - 98 + 25)
    #
    # count = 0
    # sum_count = 0
    # while True:
    #
    #     if count == 100:
    #         break
    #
    #     count += 2
    #     if count in [22, 46, 68, 98]:
    #         continue
    #
    #     sum_count += count
    #
    # print(sum_count)
    
    # tag(中间变量)控制while循环
    '''
    count = 0
    while count:
        pas
    '''
    
    # count = 0# count = 98 # count = 100
    # sum_count = 0
    # while count < 100: #
    #
    #     count += 2 # count = 100
    #     if count in [22, 46, 68, 98]:
    #         continue
    #
    #     sum_count += count #
    #
    # print(sum_count)
    
    
    # while + else 仅作了解(非用不可可以使用,不要和if。。else混了)
    # count = 0
    # while count < 100:
    #     count += 1
    #     print(count)
    # else:
    #     print('没有被break干掉我就能出来')
    
    # count = 0
    # while count < 50:
    #     if count == 100:
    #         break
    #     count += 1
    #     print(count)
    # else:  # 没有被break干掉就执行,被break终止了就不执行
    #     print('没有被break干掉我就能出来') # 可以判断while是否被break终止
    
    
    # 猜年龄游戏,有三次复活机会
    
    age = 18
    count = 0
    while count < 3:
        age_inp = input('请输入你的年龄:')
    
        age_inp_int = int(age_inp)
    
        if age_inp_int > age:
            print('猜大了')
        elif age_inp_int < age:
            print('猜小了')
        else:
            print('猜对了')
            break
    
        count += 1
    
  • 相关阅读:
    GoWeb-Gin 文件上载
    Node.JS + Mysql数据库
    Node.JS 项目打包 JXCore
    Express web框架 upload file
    hdoj 4430 Yukari's Birthday
    hdoj 4282 A very hard mathematic problem
    hdoj 3750 Guess Game
    hdoj 2199 Can you solve this equation?
    uva 11384 Help is needed for Dexter
    ios中fixed元素在滚动布局中的延时渲染问题
  • 原文地址:https://www.cnblogs.com/jinhongquan/p/11507030.html
Copyright © 2020-2023  润新知