• Python---基础


    python
    诞生:
    python的创始人为吉多·范罗苏姆(龟叔)

    python2X和python3X的比较:
    python2x: 源码不规范,源码混乱,重复代码较多,python2x: 默认的编码方式ascii,python2X不支持中文,显示中文首行要加:- * - encoding: utf - 8 -
    *-
    python3x: 重整源码,源码规范,优美,清晰,简单
    变量:
    # 变量:将计算的中间结果存储起来,以便后续代码使用。
    变量设定规则:
    1,必须是字母,数字下划线任意组合。
    2,不能是数字开头。
    3,不能是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']
    4,变量不能是中文。
    5,变量不能太长。
    6,变量有具有可描述性。
    AgeOfOldboy = 56
    NumberOfStudents = 80
    # 下划线
    age_of_oldboy = 56
    number_of_students = 80

    # -*- encoding: utf-8 -*-
    print('中国你好')

    print(1 + 2 + 3 + 6 + 7 + 8 + 101)
    print((1 + 2 + 3 + 6 + 7 + 8 + 101) * 3 / 5)
    print(((1 + 2 + 3 + 6 + 7 + 8 + 101) * 3 / 5) * 4 / 8)
    # 变量:将计算的中间结果存储起来,以便后续代码使用。
    a = 1 + 2 + 3 + 7 + 8 + 101
    b = a * 3 / 5
    c = b * 4 / 8
    print(c)
    age1 = 12
    age2 = age1
    age3 = age2
    age2 = 23
    print(age1, age2, age3)
    # 常量:一直不变的量。默认全部大写的变量为常量。
    # 身份证号
    # 注释:帮助你理解别人的代码。
    # 单行注释: # '''被注释内容''' """被注释内容"""
    # 基础数据类型:
    print('这是字符串')
    print("这是字符串")
    msg = '''床前明月光,
    疑是地上霜。
    '''
    print(msg)

    # 连接符
    msg1 = '老男孩教育'
    msg2 = ' 是最好的培训机构'
    print(msg1 + msg2)
    msg = '坚强'
    print(msg * 8)

    # bool值 True False 两种状态:判定代码的真假。
    print(3 > 2)
    print(3 > 4)
    print('True')
    print(True)

    # 判断此对象是什么数据类型 type()
    print('True', type('True'))
    print(True, type(True))
    '''
    name=input('请输入你的名字:')
    age=input('请输入你的年龄:')
    print(name,age,type(name),type(age))
    '''

    # if
    # 单独if
    '''
    print(111)
    if 3>2:
    print(666)
    print(333)

    #if else
    name=input('请输入你的名字:')
    if name == '王爷':
    print('老铁,没毛病')
    else:
    print('有病得治....')


    #if elif ...
    num =int(input('请输入你的选择:')) #str--->int str 全部由数字组成
    if num == 4:
    print('中午饭我请')
    elif num == 5:
    print('晚饭我请')
    elif num == 6:
    print('大保健,走起')


    #if elif ... else
    num =int(input('请输入你的选择:')) #str--->int str 全部由数字组成
    if num == 4:
    print('中午饭我请')
    elif num == 5:
    print('晚饭我请')
    elif num == 6:
    print('大保健,走起')
    else:
    print('给你机会抓不住....')


    #嵌套
    num1=input('请输入数字')
    if num1 =='3':
    num2=input('请输入数字:')
    if num2 == '5':
    print('这都能猜对')
    else:
    print('继续努力')

    score = int(input("输入分数"))
    if score>100:
    print("我擦,最高分才100...")
    elif score>=80:
    print("B")
    elif score >= 90:
    print("A")
    elif score >= 60:
    print("C")
    elif score >= 40:
    print("D")
    else:
    print("太笨了...E")

    #while 循环
    #while 条件:
    # 结果

    while True:
    print('精忠报国')
    print('粉红的回忆')
    print('凉凉')
    flag=False

    flag = True
    while flag:
    print('精忠报国')
    print('粉红的回忆')
    print('凉凉')
    flag=False
    print('第一次')

    count = 1
    flag = True
    while flag:
    print(count)
    count=count+1 #count +=1
    if count ==101:
    flag=False

    count = 1
    while count<101:
    print(count)
    count=count +1
    count = 0
    while count<101:
    if count%2 ==0:
    print(count)
    count=count+1

    #break continue 打断循环

    while True:
    print(111)
    print(222)
    break
    print(333)
    print(666)


    while True:
    print(111)
    print(222)
    continue
    print(333)
    print(666)
    '''

    # break while 循环 计算出1+2+3+4....+100
    count = 1
    sum = 0
    while True:
    sum = sum + count
    count = count + 1
    if count == 101: break
    print(sum)
  • 相关阅读:
    排序算法:冒泡排序
    排序算法: 插入排序
    排序算法:折半插入排序
    排序算法:快速排序
    浅谈C/C++回调函数(Callback)& 函数指针
    仿函数
    回溯法:求和
    动态规划:数字和为sum的方法数
    字典序
    剑指offer15 二进制中1的个数
  • 原文地址:https://www.cnblogs.com/weilaixiaochaoren/p/8955925.html
Copyright © 2020-2023  润新知