zoukankan      html  css  js  c++  java
  • python入门

    1.什么是编程?

    就是程序用计算机可以理解的表达方式(编程语言)把自己的思维逻辑写下来,编程结果是一堆文件 

     什么是语言?

    事物与事物之间沟通的介质(编程语言就是程序员与计算机交流沟通的介质)

    2.编程语言

    编程语言分类

    (1)机器语言:用二进制指令编程 本质 直接操作硬盘

      优点:执行效率高

      缺点:学习难度大 开发效率低

     (2)汇编语言:用英文标签代替二进制指令 本质也是直接操作硬盘

       优点:执行效率较高

       缺点:学习难度大 开发效率较低

    (3)高级语言:用人类的字符去编写程序 人类的字符计算机无法理解 需要翻译成机器语言 才能执行

             高级语言分为两种:解释型和编译型

             编译型:类似于谷歌翻译 --->gcc

             特点 一次翻译 可以重复使用 

             翻译程序--->gcc--->机器语言--->cpu

             优点:执行效率高 

             缺点:调试程序复杂 跨平台性差

             解释型:类似于同声传译 解释器(python所用)

             特点 边翻译 边执行

             程序--->解释器--->机器语言--->cpu

             优点:调试程序简单 跨平台性强 开发效率高

             缺点:执行效率低

    3.第一个python的两种调试方式

    进入解释器的交互式模式:调试方便,无法永久保存代码   


    脚本文件的方式(使用nodpad++演示):永久保存代码

    4.变量

    变量是一种可以反映出状态变化的一种机制,程序的执行本质就是一系列状态的变化

    变量的定义规范

    1. 变量名只能是 字母、数字或下划线的任意组合
    2. 变量名的第一个字符不能是数字
    3. 关键字不能声明为变量名

    定义方式:

    驼峰体

    AgeOfOldboy=18

    下划线

    age_of_oldboy=18

    定义变量会有:id type value

    x=1

    y=x

    id(x)

    1234567

    id(y)

    1234567

    x is y

    ture

    x==y

    ture

    type(x)

    str

    type(y)

    str

    还有一种情况

    x=1

    y=1

    id(x)

    1234567

    id(y)

    1234568

    所以变量值相同但id不一定相同

    5.用户与程序交换

    在python3中
    input:用户输入任何值,都存成字符串类型

    在python2中
    input:用户输入什么类型,就存成什么类型
    raw_input:等于python3的input

    注释:单行注释#内容 多行注释'''  内容  '''

    6.基本数据类型

    数字 

    整型int:如 age=18,qq=12345678等等。。。

    浮点型float:如stature=1.72,weight=77.4等等。。。

    字符串;如 name=‘egon’,字符串的单引号 双引号 三引号 并没有什么区别 

    字符串可以进行加法和乘法的运算如:name=‘usek’ ,age=18 name+age=('usek'+'18')='usek18'

    列表  在[]内用逗号分隔,可以存放n个任意类型的值

    如:name=【‘usek’,‘egon’,‘alex’】

    字典 在{}内用逗号分隔,可以存放多个key:value的值,value可以是任意类型

    如:info={'name':‘geon’,'age':18,'stature':170‘,'hobbies':’play‘}

    因为列表和字典都是可以存放多个值 如何取出我们想要的值举例说明;

    teacher[{’name':'egon','age':19,'hobbies'['play','music','computer']}

                 {'name':'usek','age':28,'hobbies'['basketball','read']}]

    print(teacher[1],['hobbies'][2])取第二个老师的第三个兴趣

    布尔 布尔值,一个True一个False

    举例说明:

    x=1

    y=2

    x<y

    ture

    x>y

    false

    特点:所有值都自带布尔值  除了 none 0 空 其余的都是真

    7.格式化输出

    占位符,如:%s、%d

    举例说明:

    print('my name is %s','my age is %s' %('usek',18))

    注意:%d只能接受数字

    8.基本运算符

    逻辑运算

    and 逻辑与,and用于连接左右两个条件,只有在两个条件判断的结果都是Ture的情况下,and运算最终的结果才是Ture

    or逻辑 或 or 只要两边有一边判断的结果是Ture or运算最终的结果就是Ture

    not逻辑非 not 如果x为Ture,返回False。如果x为False,它返回Ture

    身份运算

    is比较的是id
    而==比较的是值

    9.流程控制之if...else

    单独使用if的:

    score=70
    if score<=70:
    print('pass')
    if和else 一起使用的:
    score=80
    if score<=70:
    print('pass')
    else:
    print('good')

    
    
    age_of_girl=18
    height=171
    weight=99
    is_pretty=True

    if age_of_girl >= 18 and age_of_girl < 22 and height > 170 and weight < 100 and is_pretty == True:

    print('love')
    else:
    print('aunt')

    if套if的:
    age_of_girl=18
    height=171
    weight=99

    success=False

    if age_of_girl >= 18 and height < 175 and weight > 80:
    if success:
    print('love')
    else:
    print('sad')
    else:
    print('aunt')
    if elif else的组合:
    age=input('>>:')
    age=int(age)

    if age <= 20:
    print('a')
    elif age <= 30:
    print('b')
    elif age <= 40:
    print('c')
    else:
    print('d')
    10.流程控制之while循环
    while的语法如下:
    count=1
    while count <= 100:
        if count%2 == 0:
            print(count)
        count+=1

    break用于退出本层循环
    continue用于退出本次循环,继续下一次循环
    while break continue 的语法使用:

    name='seven'
    password='123'

    while True:
    inp_name=input('your name:')
    inp_pwd=input('password: ')
    if inp_name == name and inp_pwd == password:
    while True:
    cmd=input('>>: ')

    if cmd == 'quit':
    break
    print('run <%s>' %cmd)
    else:
    print('fleas')
    continue
    break

  • 相关阅读:
    MongoDB-基础-limit-skip-sort
    MongoDB-基础-条件操作符
    mongodb-基础-update-remove
    Linq to sql-存储过程
    SQL Server-游标使用
    JavaScript-求时间差
    HTTP 错误 500.21
    .NET错误The 'targetFramework' attribute in the <compilation> element of the Web.config file is used only to target version 4.0 and later of the .NET Framework
    HTTP 错误 500.21
    WebApi&MVC对比
  • 原文地址:https://www.cnblogs.com/yftzw/p/8597667.html
Copyright © 2011-2022 走看看