• Python基本操作


    Python是一种简单易学,且功能强大的编程语言。它是面向对象的编程语言。

    对象的意义:

      对象可以通过一个“.” 的方式来调用这个对象的方法。

    Python环境安装配置:

         Python是一种通用的计算机编程语言,它可以应用于web,Desktop以及game中,在使用Python时最好使用

    数据类型:整型,

    可变数据类型:列表    字典

    不可变数据类型:整型  元组     字符串一旦创建,就不能修改

    Python变量:

      变量不能以数字,空格,特殊字符开头

      变量不能以关键字开头

    Python数字:

      整型:整数    在Python中,只有长整型。

      浮点型:小数   科学计数法

          float   单精度

          double 双精度

      布尔类型:TRUE     FALSE

      运算符:

        算数运算符:“+”     “-”    “*”    “/”

        赋值运算符:“=”   “+=”  “-=”   “*=”   “/=”

        比较运算符:“==”  “>=”   “<=”  “!=”  “<”  “>”  比较运算是布尔值

        逻辑运算符:and   一假全假   与

              or     一真全真 或

              not  非

        关系运算符:in    判断元素是否在列表或集合中

              not in

        位运算符:

    Python切片查找:[ : ] 前取后不取

      s1=“hello world”

      print(s1[1:4])    取索引值1到4   为 “ell”

    查找:[:]

     s1="hello world"

     print(s1[1:4])

     print(s1[1:4:2])

     print(s1[-1])

     print(s1[:])

     print(s1[:8])

     print(s1[1:-1])

     print(s1[1:])

     print(s1[-3:-1])

    Python三元运算:

      a=2

      b=5

      c=a if a<b else b

      意思是:如果 if判断a<b正确,则将a的值赋予c,如果判断不正确,就将b的值赋予c。

      a和b可以再换成三元运算,就是说三元里套三元。

    Python字符串操作:

     字符串

    转义符号

    s='Let\'s go'

     

    print(r"\fsdghlfjdk.")

    字符串的格式化输出

    %s:字符串  %d:整型  %f:浮点型

     print("hello %s,%s"%("sb","egon"))

     print("hello %s, his age is %d"%("sb",35))

     print("hello %s, his age is %.4f"%("sb",35.53452345))

     print("hello {0}, his age is {1}".format("alex",34))

     print("hello {0}, his age is {1}".format(34,"alex"))  

     print("hello {name}, his age is {age}".format(age=30,name="wusir")) 

     print("hello {name}, his age is {age}".format_map({"name":"egon","age":1000})) 

     print("".isdecimal())

     print("".isdigit())

     print("".isnumeric())

     print("hello world".capitalize())

     print("hello world".title())

     print("HELLO world".casefold())

     print("HELLO world".lower())

     print("HELLO\tworld")

     print("HELLO world".expandtabs())

     "HELLO world".rsplit()

     print("HELLO\n wor\nld\n".splitlines())

     print("HELLO\n wor\nld\n".split("\n"))

    print("HELLo world".zfill(10))

      操作的主要方法:

        拼接:拼接次数多用“+”号效率不高。推荐用join方法。 

            # s="hello"+"world"+"I"+"am"+"python"

            # print(s)

            # print(" ".join(["I","am","world!"]))

        string

        分割split():括号中的参数是用什么来分割开字符串。 

            # s="hello world".split("l",1)     

            # ["he","lo world"]

            # print(s)

        strip()  把字符串开头和结尾的空格及\n   ()中可以加去除的条件,比如是“*”

          s=“   hello\nworld\n”

          s1="  hello\nworld\n".strip()

          s2="  hello\nworld\n****".strip("*")

          print(s)

          print(s1)

          print(s2)

        center:居中显示

            # print("hello world".center(50,"*")) 居中对齐

            # print("hello world".ljust(50,"*")) 左对齐

        count

        find:查找字符

          # print("hello world".find("a",4))

             # print("hello world".rfind("l"))

             # print("hello world".index("e"))   index方法没有找到对应的索引值会报错。

        lower

        casefold

        upper

        join

        split

        endwith

        startwith

        replace:替换

          完全匹配:print(“hello world”.replace("world","python"))

        index

      name = "george\tgeorge"

      name2 = "GEORGE"

      name3 = "my name is {0} , i am {1} year old"

      name4 = "my name is {name} , i am {age} year old"

         print(name.capitalize())   #首字母大写

           print(name2.casefold()) #大写变小写 if choice == "Y" or choice == "y"

           print(name.center(50,'*'))    长度为多少,不过填充*

         print(name.count('e',2,5))    统计字符出现的次数。数字的意思是从第几位开始统计到哪里,是个区间。    

          print(name4.ljust(50,"-"))   左对齐,宽度为50 ,不足补齐补“-”。

          print(name4.rjust(50,"\"))右对齐,道理和左对齐一样。

         print(name.endswith("e"))    以什么结尾

         print(name.expandtabs(3))   #设置\t的长度

           print(name.find("e",3)) #返回找的的第一个值的索引,找不到就返回-1

             print(name3.format("Alex",22))

             print(name4.format(name="george",age=25)) #格式化输出

     

           print(name4.format_map({'name':'Alex','age':23}))    也可以传字典进去。

         print(name4.index("is"))   返回is的索引值。

     

      join使用:把列表拼接成字符串,将字符串用任意指定的字符隔开。

        print( "\\\\\".join(["george","jack","rain"]) )

     

        

     

        print(name4.lstrip("My name"))   移除左边的元素,“my name”。   默认是换行和空格。

          print(name4.swapcase()) #大小写互换

       

      翻译:

        IN = "abcde"        输入的内容

        OUT = "!@#$%"  输出的内容

          

            in和out 就是翻译表,in中的元素对应翻译成out中的元素,是索引值相对应。

        trans_table = str.maketrans(IN,OUT) #translate =翻译         

        print(name4.translate(trans_table))  #字符翻译   一一对应后,将字符串做一个转换。

     

     

        print(name4.zfill(50))   

        print(name4.replace('name','NAME',1))    替换,将原来的值,替换成新的值,数字的意思是替换几次。

            

     

            # s="hello world"

     

            # print(s.replace("world","Python"))

     

            # print(s)

     

     

     

     

        print(name4.lower())   大写变小写

        print(name4.rfind('e'))   从右边开始查找,返回右边的的一个对应元素的索引值。

     

          print('aA'.isalpha()) #是不是字母

          print('-a'.isidentifier())  #identifier 关键字 ,是不是合法的关键字,是不是合法的变量名

          print('A'.islower())     是不是小写

          print('A'.isupper())  是不是大写

            print('123.3'.isnumeric())  纯数字返回true,否则返false 

            print('a'.isprintable())   可否打印

          print(' '.isspace())   是不是空格

            print('Today Headline'.istitle()) #是不是英文标题

        print('a1a'.isalnum())    #a-z   A-Z   0-9

        print("12342342".isdecimal())    # 是不是一个正整数

      

    Python列表:

       大时代发的说法

    Python元组(tuple):

      元祖:和列表一样,用来存储数据,但是元组只能读列表,元组是用小括号()表示。可以被切片,因为它和列表一样。

        元组的作用:

        明确表示元组里存储的数据是不应该被修改的。

        元组和列表之间进行切换,

        list(tuple元组):变成列表

        tuple(list列表):变成元组  

    Python集合(set):特点,集合的元素无法重复。天然去重。无序。

      Linux = [ "george","jack","rain","show" ]

      Python = [ "george","show","mack","land" ]

      求出Linux和Python中相同的元素。

      Linux_and_Python = []

      for i in Python:

        if i in Linux:

          Linux_and_Python.append(i)

      print(Linux_and_Python)

      以上是用一个for循环以逐一的便利,在将相同的元素写入一个新的列表。

    集合:关系测试

        分为交集、差集、并集和反向差集

           交集(intersection):两个或多个集合都有的元素。

           intersection 和“ & ” 是相同的效果。

            Linux.intersection(Python)

            print(Linux.intersection(Python))

            print(linux & python)

            print(linux.isdisjoint(python)) 是如果两个集合有一个为空null 的话,返回一个布尔值,true。

                          就是说两个集合没有任何的关联,就返回true。

           差集(difference):在列表a里有,b里没有。

           difference 和 “ - ” 是相同的效果。

            eg:Linux - Python 是Python中没有的Linux项,Jack和rain。

           Linux.difference(python)

           print(Linux.difference(Python))

           注意:Linux.difference(python)中的Linux和Python是有区别的,Linux在前的意思是以Python为基准,去对比Linux,输出的结果是Linux中没有而Python中有的。

          print(linux.difference(python))

        out: [ "jack","rain" ]

           并集(union):将两个或多个列表里的元素,合并在一起,组成一个新的集合。并且,并集有去重的功能。

          print(linux.union(python))

          print(linux | python)

          判断所要元素是不是在集合中,只能是:print("geoge" in linux)打印。如何元素在所在的集合,其打印的结果是布尔值true/false。

          反向差集/对称(symmetric)差集(symmetric_difference):

          打印结果是两个集合中相互都没有的元素。

          print(linux.symmetric_difference(python))

          print(linux ^ python)

          结果是 [ "jack","rain","land","mack" ]

       集合的增删改查:

        改:

          Linux.update(python)     将Linux和Python集合合并。但是是将Python的元素合到Linux中。

           update和union的区别是union不会修改原集合,这是出一个并集的结果。而update是改原集合,是Linux集合彻底的变化。

          Linux.difference_update(python)    是将Linux和Python的差集取出后,将结果放入Linux中,彻底改变Linux集合。

        删:

          Linux.clear()    清空

          Linux.discard("要删除的元素")  删除  但是,元素不存在的话,不会 报错

          Linux.pop 随机删除  

          Linux.remove("要删除的元素")  删除   但是,元素不存在的话,会报错。

        增:

          Linux.add("添加的元素")

        linux.copy()和列表字典的是一样的。

        查:

          Linux.issubset(python)  issubset 判断Linux是不是Python的子集

          返回的结果是布尔值。如果留Linux是Python的子集,是true。否则是false。

          Linux.issuperset()    issuperset  判断超集,也就是父集。

        子集和超集也可以用大于号和小于号表示。

    Python字典:用“{ }”表示

      发生地方都是f

    Python字符编码:

         ASCII 英文

        GB2312     1980年

          GBK    1995年

          GB18030

        1990  Unicode

              utf - 8   中文占三个字节。

          

        Japan   JK3000

        

      Python2里的默认编码是ASCII码。

        # -*- coding:utf-8 -*-

     

        name = "中国" #utf-8 格式的编码

     

        print "utf-8",len(name) #

        print [name.decode("utf-8")] # #你现在是什么编码,

        print "unicode",len(name.decode("utf-8") )  # #你看到的并不是uinicode , 你看到的是unicode-->你的终端屏幕的编码格式

     

        gbk = name.decode("utf-8").encode("GBK") #写要转成的目标编码

        print gbk

     

        print len(gbk.decode("GBK").encode("gb2312"))

      Python3里的所有字符在内存里都是Unicode

        解释器读取文件的默认编码是utf -8

        但是有个文件,编码是gbj,读到内存里,还要需要解码。

      

        dir ()  把传入的数据类型的所有方法,以列表的形式返回。  打印某个变量的所有可用的方法。 

      

     

  • 相关阅读:
    bzoj4321
    bzoj1800
    codeforces396C
    codeforces400C
    codeforces271D
    关于jsp中jstl-core标签循环遍历的使用
    hibernate坑边闲话2
    hibernate坑边闲话
    hibernate中实体与数据库中属性对应的类型
    MySQL的常用命令:添加外键,修改字段名称,增加字段 设置主键自增长等
  • 原文地址:https://www.cnblogs.com/george92/p/6627853.html
Copyright © 2020-2023  润新知