zoukankan      html  css  js  c++  java
  • python

    1. python历史。

    2008 年出现 python 2.7 与 python 3.0 两个版本,后来 吉多·范罗苏姆 大叔决定停用 python 2.7,但因用户反对,于是决定于 2020 年停止 python 2.7 的更新

     宏观上:

    python2 与 python3 区别:python2 源码不标准,混乱,重复代码太多,python3 统一 标准,去除重复代码

    2. python的环境

     编译型:一次性将所有程序编译成二进制文件

      缺点:开发效率低,不能跨平台

      优点:运行速度快

      比如:C,C++等等

    解释型:当程序执行时,一行一行的解释

      缺点:运行速度慢

      优点:开发效率高,可以跨平台

      比如:python ,php,等等

    3. python种类

    运行第一个py文件:

      python3x :python 文件路径 回车  

      python2x :python2 文件路径 回车  

      python2 python3 区别:python2默认编码方式是ascii码

      解决方式:在文件的首行:#-*- encoding:utf-8 -*-

      python3 默认编码方式utf-8

    4. 变量

     变量:就是将一些运算的中间结果暂存到内存中,以便后续代码调用  

      1. 必须由数字,字母,下划线任意组合,且不能数字开头

      2. 不能是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']

      3. 变量具有可描述性

      4. 不能是中文

    x=1+2+3+4+5
    print(x)
    print(1+2+3+4+5)
    print(x*5)
    print(x*5-234+2)
    
    x='sjd'
    print(x)
    名字='亦双弓'
    print(名字)
    
    
    age1 = 12
    age2 = 100
    age2 = age1
    age3 = age2
    #age2 = 100
    print(age1,age2,age3)

    5. 常量

    常量:一直不变的量 ,如:圆周率 π  

      BIR_OF_CHINA = 1949 ,python中没有常量,约定俗成的大写为常量

    6. 注释

    注释: 方便自己方便他人理解代码

      单行注释:#

      多行注释:'''被注释内容'''  """被注释内容"""

    当引用换行的语句时应该用 3 个单引号或双引号进行引用 
    
    """
    我今天写了一首小诗
    歌颂我爱人
    你看她那乌黑亮丽的头发
    啊!好像一只炸毛鸡
    '''
    mg='''我今天写了一首小诗
    歌颂我爱人
    你看她那乌黑亮丽的头发
    啊!好像一只炸毛鸡
    '''
    print (mgr,mg)

    7. 用户交互:input

      1.待输入

      2.你输入的内容赋值给了前面变量

      3. input 出来的数据类型全部是 str   

    print (True,type(True))
    print ('True',type('True'))
    
    name = input('you name:')
    age = input("you age:")
    print ("my name is "+name,",age is  "+age)
    print (name,age,type('age'))

    8. 基础数据类型初始

    数字:int 12,3,45 + - * / **  % 取余数  

      ps:type()  

        字符串转化成数字:int(str) 条件:str必须是数字组成的

        数字转化成字符串:str(int)

    字符串:str,python当中凡是用引号引起来的都是字符串

      可相加:字符串的拼接

      可相乘:str * int

    bool:布尔值, True False

    int = 100
    print(100,type(100))
    print(100.3,type(100.3))
    print('100', type('100'))
    print("100", type("100"))
    
    print("I am ysg")
    #print('i'm ysg')
    print("I'm ysg,i'm ysg")
    
    a="ysg"
    b="peipei"
    c=a+b
    e=100
    f=100
    print(c)
    print(a+b)
    #print(a+b)
    print("坚强"*4)
    #print("坚强坚强坚强坚强"/2)
    #print(e=f)

    9.  if 判断语句

      if 条件:  结果

    #语法结构
    # if
    if True :
        print(666)
    if 4<5 :
        print(777)
    if 5<3 :
        print(555)
    if False :
        print(222)
    
    
    if 9<7 :
        print("你请我喝酒")
    else:
        print('喝什么酒')
    
    num=input("请输入你选择的数字:")
    if int(num)<5 :
        print ("")
    elif 5<int(num)<10 :
        print("")
    elif int(num) == 5:
        print("")
    else :
        print("输错了")
    
    num=int(input("请输入你选择的数字:"))
    if num<5 :
        print ("")
    elif 5<num<10 :
        print("")
    elif num == 5:
        print("")
    else :
        print("输错了")
    
    print("111")
    while True:
        print('我们不一样')
        print('丑八怪')
    print('222')

    10. while 循环语句

      while 条件:  

        循环体:无限循环

      终止循环:

        1. 改变条件,使其不成立

        2. continue 结束本次循环

        3. break 跳出循环结构

    #while 循环
    count = 1
    flag=True
    while flag:
        print(count)
        count=count+1
        if count > 10:
            flag=False
    
    count =1
    while count<10:
        print(count)
        count=count+1
        
    
    count = 1
    sun=0
    while count <=100:
        sun=sun+count
        count=count+1
        print(sun)
    
    #break
    print("111")
    while :
        print("222")
        #break
        print("333")
    
    count=0
    while True:
        count = count+1
        if count==5:continue
        print(count)
        if count >10:break
    count = 0
    while count <100:
        count += 1
        if count > 5 and count < 95:
            continue
        print("loops:",count)
    print ("loop---------loop")

    相关练习

     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
    sun=0
    while count<=100:
        sun +=count
        count +=1
        print(sun)

    3. 输出 1-100 的所有奇数

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

    4. 输出 1-100 的所有偶数

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

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

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

    6.用户登录(三次机会重试)

    i=0
    while i<3:
        username=input('请输入用户名:')
        userpw=input('请输入密码:')
        if username=="亦双弓" and userpw=='123123':
            print('欢迎'+username+'回来')
            break
        elif i==2:
            print('密码输入次数超限,已被冻结')
        else:
            print('---密码输入错误,请重新输入---')
        i+=1
  • 相关阅读:
    程序员通过什么渠道接外包项目
    中小型软件项目开发一般流程建议
    DevExpress GridControl功能总结
    页面UI注意事项,你在乎吗?
    加密,解密
    本地化(国际化)
    AutoLayout(自动布局)
    UItableView与UICollectionView
    分享iOS开发常用(三方类库,工具,高仿APP,实用网站,技术干货)
    NSPredicate
  • 原文地址:https://www.cnblogs.com/ysging/p/9610298.html
Copyright © 2011-2022 走看看