zoukankan      html  css  js  c++  java
  • Python基础一

    Python基础一(Python基础介绍)

     

    1.计算机基础

      cpu:相当于人的大脑,用于计算。

      内存:储存数据,4G,8G,16G,32G,成本高,断电即消失。

      硬盘:1T,固态硬盘,机械硬盘,储存数据,应该长久保持数据,重要文件,小电影等等。

      操作系统:

      应用程序:

     

    2,python历史

      宏观上,python2 与 python3 区别:

        python2 源码不标准,混乱,重复代码太多,
        python3 统一 标准,去除重复代码。

     

    3,python的环境

      编译型:一次性将所有程序编译成二进制文件。
        缺点:开发效率低,不能跨平台。
        优点:运行速度快。
        C,C++等等。
      解释型:当程序执行时,一行一行的解释。
        优点:开发效率高,可以跨平台。
        缺点:运行速度慢。
        python ,php,等等。


    4,python的发展

     

    5,python种类

      运行第一个py文件:
        python3x :python 文件路径 回车
        python2x :python2 文件路径 回车
        python2 python3 区别:python2默认编码方式是ascii码
        解决方式:在文件的首行:#-*- encoding:utf-8 -*-
        python3 默认编码方式utf-8

    #-*- encoding:utf-8 -*-
    print('我爱中国')

    6,pycharm下载安装

      官网下载安装,购买收费版激活码

      创建项目-文件夹-文件

      鼠标调整字体、快捷键

     

    7,变量
      定义:就是将一些运算的中间结果暂存到内存中,以便后续代码调用。
      --必须由数字,字母,下划线任意组合,且不能数字开头。
      --不能是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']
      --变量具有可描述性(见名知意)
      --不能是中文或拼音

     

    8,常量

      一直不变的量,如 π
      全大写表示:BIR_OF_CHINA = 1949


    9,注释
      方便自己方便他人理解代码。
      单行注释:#
      多行注释:'''被注释内容'''      """被注释内容"""

     

    10,用户交互:input
      等待输入,
      将你输入的内容赋值给了前面变量
      input出来的数据类型全部是str

    name = input('请输入你的名字:')
    age = input('请输入你的年龄:')
    print('我的名字是'+name,'我的年龄'+age+'')

    11,基础数据类型初识

      数字:

        int(整型)-- 12,3,45
        + - * /  //
        % 取余数、** 幂、// 整除
        ps:type()--查看类型

        在32位机器上,整数的位数为32位,取值范围为-2**31~2**31-1

        在64位系统上,整数的位数为64位,取值范围为-2**63~2**63-1

        Python3里不再有long类型,全是int型

        还有float--浮点型, 复数型:(略)

    print(100,type(100))
    print('100',type('100'))  

      字符串:

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

        可相加:  字符串的拼接,双方都只能是字符串。

        可相乘:str * int,重复字符串

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

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

    print('坚强'*8)

      bool型:  布尔值    True    False

        数字转换bool:0--False    非0--True   

        bool转换数字:False--0    True--1

    12,条件语句

      if 条件:
        结果

      if...else

      if...elif...else

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

      pass关键字:占位不执行

     

    13,循环语句

      while 条件:
        循环体

    count = 1
    while count <= 100:
        print(count)
        count = count + 1

      无限循环:  while True
      终止循环:

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

    count = 1
    flag = True
    while flag:
        print(count)
        count = count + 1
        if count > 100 :
            flag = False

        2,break

    count = 1
    while True:
        print(count)
        count = count + 1
        if count > 100:
        break    

      continue用法:跳出单次循环,继续下次循环

      while...else:

        当循环被break打断,就不会执行else语句

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

     

    14、格式化输出

      %占位符--s字符串、d数字

      %%单纯显示%

    name = input('请输入姓名:')
    age = input('请输入年龄:')
    job = input('请输入工作:')
    hobbies = input('你的爱好:')
    msg = '''------------ info of %s -----------
    Name   : %s
    Age    : %d
    Job    : %s
    Hobbies: %s
    ------------- end -----------------''' % (name, name, int(age), job, hobbies)
    print(msg)

    15、编码字符集

      电报,电脑的传输,存储都是二进制01010101

      最早'密码本' -ascii 涵盖了英文字母大小写,特殊字符,数字,只能表示256种

      只有8位(7+1),一个字节,最左位全是0, '0'--48 , 'A'--65 , 'a'--97 

      为了解决全球化问题,创建万国码--Unicode

      最开始:Unicode

        1个字节--表示所有英文、字符、数字

        2个字节--16位表示一个中文,不够9万种

        所以4个字节(32位)表示中文,浪费空间资源

      升级:UTF-8

        1个字节--表示所有英文、字符、数字

        2个字节--16位表示一个欧洲文字

        3个字节(24位)表示一个中文

      UTF-8、UTF-16、UTF-32

      GBK--中国自己发明,一个中文用两个字节 16位去表示,包含ASCII码

       1bit 位   8bit = 1byte
      1byte字节  1024byte = 1KB
      1KB 千字节 1024KB = 1MB
      1MB      1024MB = 1GB
      1GB       1024GB = 1TB

    16、运算符 

      计算机可以进行的运算有很多种,可不只加减乘除这么简单。

      运算按种类可分为算数运算、比较运算、逻辑运算、赋值运算、成员运算、身份运算、位运算。

      今天我们暂只学习算数运算、比较运算、逻辑运算、赋值运算

      算数运算:+ - * / % ** //

      比较运算:==  !=(<>)  >  <  >=  <= 

      赋值运算:=  +=  -=  *=  /=  %=  **=  //=

      逻辑运算:and 、or 、not

        优先级关系为( )>not>and>or,同一优先级从左往右计算。

    3>4 or 4<3 and 1==1  # F
    1 < 2 and 3 < 4 or 1>2  # F
    2 > 1 and 3 < 4 or 4 > 5 and 2 < 1  # T
    1 > 2 and 3 < 4 or 4 > 5 and 2 > 1 or 9 < 8  # F
    1 > 1 and 3 < 4 or 4 > 5 and 2 > 1 and 9 > 8 or 7 < 6  # F
    not 2 > 1 and 3 < 4 or 4 > 5 and 2 > 1 and 9 > 8 or 7 < 6  # F

        0--表示假;其他数--表示真。

        x or y  --  x为真,值就是x;x为假,值是y。

        x and y -- x为真,值是y;x为假,值是x。

    8 or 4  # 8
    0 and 3  # 0
    0 or 4 and 3 or 7 or 9 and 6  # 3
    0 or 1 < 2  # T
    1 or 1 < 2  # 1
    1 < 2 and 3  # 3
    1 > 2 and 3  # F
    1 > 2 and 3 or 4 and 3 < 2  # F

        in,not in --判断子元素是否在原字符串(字典,列表,集合)中

    print('喜欢' in 'dkfljadklf喜欢hfjdkas')
    print('a' in 'bcvd')
    print('y' not in 'ofkjdslaf')

     相关练习题

      1、使用while循环输入 1 2 3 4 5 6     8 9 10

    count = 0
    while count < 10:
        count += 1
        if count == 7:
            continue
        print(count)
    View Code

      2、求1-100的所有数的和

    count = 1
    summer = 0
    while count <= 100:
        summer += count
        count += 1
    print(summer)
    View Code

      3、输出 1-100 内的所有奇数

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

      4、输出 1-100 内的所有偶数

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

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

    count = 1
    summer = 0
    while count < 100:
        if count % 2 == 0:
            summer -= count
        else:
            summer += count
        count += 1
    print(summer)
    View Code

      6、计算 1 - 2 + 3 ... + 99 中除了88以外所有数的总和

    i = 1
    sum_ = 0
    while i < 100:
        if i == 88:
            pass
        elif i % 2 == 1:
            sum_ = sum_ + i
        else:
            sum_ = sum_ - i
        i += 1
    print(sum_)
    View Code

      7、计算 1 - 2 + 3 ... - 99 中除了88以外所有数的总和

    i = 1
    sum_ = 0
    while i < 100:
        if i < 88:
            if i % 2 == 1:
                sum_ = sum_ + i
            else:
                sum_ = sum_ - i
        elif i == 88:
            pass
        else:
            if i % 2 == 0:
                sum_ = sum_ + i
            else:
                sum_ = sum_ - i
        i += 1
    print(sum_)
    View Code

      8、用户登陆(三次机会重试)

    count = 0
    while True:
        name = input('请输入用户名:')
        password = input('请输入密码:')
        if name == 'william' and password == '123':
            print('欢迎登录!')
            break
        else:
            print('用户名或密码不对!')
        count += 1
        if count == 3:
            print('用户账号已锁!')
            break
    View Code

      9、用户登录(提示三次机会、格式化输出)

    i = 3
    username = "william"
    password = "123456"
    name = input("请输入你的用户名:")
    while i > 0:
        i -= 1
        if name == username:
            word = input("请输入你的密码:")
            if word == password:
                print("登录成功!")
                print('''
                username: %s
                password: %s
                ''' % (username, password))
                break
            else:
                print("你的密码错误!")
                if i > 0:
                    print("你还有%s次机会" % i)
        else:
            print("你的用户名错误!")
            if i > 0:
                print("你还有%s次机会" % i)
                name = input("请输入你的用户名:")
    else:
        print("三次机会已用完!")
    View Code
  • 相关阅读:
    正则表达式
    webfrom 母版页
    repeater的command事件用法
    JVM进程cpu飙高分析
    @Transactional导致无法动态数据源切换
    mysql分页查询优化(索引延迟关联)
    MAC下安装Homebrew 和 @权限的问题
    RabbitMQ安装以及集群部署
    RabbitMQ 延时消息队列
    java 实现生产者-消费者模式
  • 原文地址:https://www.cnblogs.com/William-Shaw/p/9559822.html
Copyright © 2011-2022 走看看