zoukankan      html  css  js  c++  java
  • python基础篇

    python基础篇1

    1.python版本

    python的版本有2和3

    python2与python3的区别:python2的源码重复两很多,语法也不够清晰,源码中掺杂着c、php、java等

    python3中几乎是重构后的源码,语法规范,清晰,优美

    2.python的编程语言是什么

    python是一门动态解释型的强类型定义语言

    编程语言的类型有编译型和解释型、静态语言和动态语言、强类型定义语言和弱类型定义语言等

    编译型:指解释器将全部源程序一次性编译成字节码,然后编译成二进制文件,再运行

          优点:程序运行速度快

          缺点:程序的开发效率低,不能跨平台执行

    解释型:指程序在运行时,程序代码是一条一条的执行,并运行

               优点:调试代码很方便,开发效率高,并且可以跨平台

    3.python的优缺点

    优点:简单,开发效率高,可以跨平台,可扩展性强,是一门高级语言

    缺点:运行速度慢,代码不能加密,禁止多线程的并行执行

    4.python的注释

    当行注释:#被注释的内容

    多行注释:'''被注释内容'''或"""被注销内容""""  即三个单引号或三个双引号

    5.变量

    变量:指的是在程序运行时,把程序运行的中间结果临时保存在内存中,以便后续代码的调用

    声明变量

    name = 'tianmao'

    变量定义的规则:

         5.1.变量名只能是字母,数字或下划线的任意组合

         5.2.变量名第一个字符不能是数字

         5.3.变量名不能为关键字,例如('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')

         5.4变量的定义要具有可描述性

    变量的定义方式有两种方式:驼峰体和下划线

    #驼峰体
    
    StudentName = tom
    
    HomeTown = henan
    
    #下划线
    
    student_name = tom
    
    number_id = 80

    6.变量赋值

    name1 = "tom"
    name2 = name1

    7.常量

    常量指的事不变的量,python中的变量全部用大写表示

    8.程序交互

    用户交互是使用input

     name = input('name: ')
     age = input('age: ')
     job = input('job: ')
     hobbie = input('hobbie: ')
     info = '''
     ----info of %s------
     name   :%s
     age    :%d
     job    :%s
     hobbie :%s
     ----------end----------
     '''%(name,name,int(age),job,hobbie)
     print(info)

    如上图中的%代表占位符,s代表字符串(str),d代表数字(digit),如何将字符串转化成数字 int(str)条件:全部由数字组成的字符串,才能转换成数字

    如果需要需要表示百分数时是需要用两个%表示,例79%%;

    另type()可以查看数据类型

    name = input('name: ')
    age = input('age: ')
    print(type(age))

    执行结果是

    Name:Alex
    Age:22
    <class 'str'>

    注意:input()接受的内容默认是字符串格式的

    9.循环之if

    if...esle语句

    单分支

    if 条件:
        满足条件后要执行的代码

    双分支

    if 条件:
        结果
    else:
        结果
    if 条件:
        结果
    elif:
        结果
    elif:
        结果
    else:
        结果
    count = input('请输入数字: ')
    if count == 10:
        print('你好')
    else:
        print('count')
    count = input('请输入数字: ')
    if count == 10:
        print('你好')
    elif count == 5:
        print('hello')
    elif count == 9:
        print('how are you')
    else:
        print('count')

    while循环

    while  条件:
        结果
    while 条件:
        结果
    else:
        结果
    count = 1
    sum = 0
    while count < 100:
        if count % 2 == 1 :
            sum += count
        else:
            sum -= count
        count += 1
    print(sum)
    count = 0
    while count <= 5:
        count += 1
        print('loop',count)
    else:
        print('循环正常执行完了')
    print('out of while loop')

    结束while循环的方法有两种:改变条件或用循环中止语句

    循环中止语句是指在循环的过程中,因为某些原因需要把中止循环。就可以使用break或continue语句来实现中止循环操作。

    break用于完全结束一个循环,跳出循环体执行循环后面的语句。即break结束的事本层循环

    continue是中止本次循环,接着执行后面的循环。即continue结束的是这一次的循环。break是完全中止这个循环

    count = 0
    while count <= 100 : #只要count<=100就不断执行下面的代码
        print("loop ", count)
        if count == 5:
            break
        count +=1 #每执行一次,就把count+1,要不然就变成死循环啦,因为count一直是0
    
    print("-----out of while loop ------")
    count = 0
    while count <= 100 : 
        count += 1
        if count > 5 and count < 95: #只要count在6-94之间,就不走下面的print语句,直接进入下一次loop
            continue 
        print("loop ", count)
    
    print("-----out of while loop ------")

    while...else

    while后面的else作用是指,当while循环正常执行完时,中间也没有被break中止的话,就会执行else后面的语句

    注意:如果while的条件为假时,else也是会被执行的

    count = 0
    while False:
        count += 1
        print('hello')
    else:
        print('OK'

    如上面的例子中的执行结果是OK

    如果执行的过程中被break中止了,就不会执行else的语句了

    count = 0
    while count <= 5 :
        count += 1
        if count == 3:break
        print("Loop",count)
    
    else:
        print("循环正常执行完啦")
    print("-----out of while loop ------")
    Loop 1
    Loop 2
    -----out of while loop ------

     

  • 相关阅读:
    POJ 3970(最小公倍数LCM)
    06005_Jedis入门
    雷林鹏分享:C# 字符串(String)
    雷林鹏分享:C# 类(Class)
    雷林鹏分享:C# 枚举(Enum)
    雷林鹏分享:C# 运算符重载
    雷林鹏分享:C# 多态性
    雷林鹏分享:C# 命名空间(Namespace)
    雷林鹏分享:C# 接口(Interface)
    雷林鹏分享:C# 正则表达式
  • 原文地址:https://www.cnblogs.com/dwenwen/p/7693551.html
Copyright © 2011-2022 走看看