zoukankan      html  css  js  c++  java
  • python

    print打印

    换行:

    #1.加\n:
    print("hello world\nhaha")
    #2.字符串加三引号''':
    print('''hello world
    haha''')

    不换行:

    #1.\:
    print("hello world \
    haha")
    #2.多行用一个括号括住:
    print(
        "hello world"
        '''haha'''
        'ok')
    #3.python3中用end="":
    print('hello world', end="")
    print("haha")

    注释

    多行注释:三引号或者三个单引号

    变量

    变量的交换:

    #python中可直接交换两个变量:
    a=1
    b=2
    print(a, b)
    a,b=b,a
    print(a, b)
    #结果为:
    (12)
    (21)

    变量的类型:

    有数字、字符串、列表、元组、字典、集合

    #1.通过type()内置函数得知变量类型
    height=1.5
    print(type(height))
    <type 'float'>
    #2.转换类型:
    height=str(1.5)
    print(type(height))
    <type 'str'>

    类型的转换:

    整数:int();浮点:float();字符串:str();列表:list();元组:tuple();字典:dict();集合:set();整数转ASCII码:chr();ASCII码转整数:ord()

    输入输出

    输入:

    #pyton3为input()函数:
    name=raw_input("what is your name:")
    print(name)
    what's your name:haha
    haha

    输出:

    #连续打印10个=:
    print("="*10)
    ==========


    格式化输出:

    %s:字符串

    %d整数

    %f浮点数

    %%输出%

    #以下四个结果一样:
    name=raw_input("what's your name:")
    age=raw_input("what's your age:")
    num=int(input("what's your phone number:")) #因为input输入的是str类型,所以用int()转成int类型
    print(name,"you are %s years old,and your phone number is %d" %(age,num)) #按顺序对应
    print(name,"you are {} years old,and your phone number is {}".format(age,num)) #以下三个都格式化了,不用纠结是%s还是%d,直接对应顺序
    print(name,"you are {1} years old,and your phone number is {0}".format(num,age)) #0代表第一个,1代表第二个
    print("{} you are {} years old,and your phone number is {}".format(name,age,num))
    what's your name:haha
    what's your age:25
    what's your phone number:18811881188
    ('haha''you are 25 years old,and your phone number is 18811881188')
    ('haha''you are 25 years old,and your phone number is 18811881188')
    ('haha''you are 25 years old,and your phone number is 18811881188')
    haha you are 25 years old,and your phone number is 18811881188

    判断语句

    if:

    #例子:判断输入的是否为纯数字:
    char = raw_input("input char:")
    if char.isdigit():
        print(char,"is digit")

    for循环:

    #例子:
    for in 1,2,3,4,5:
        print(i)

    while循环:

    #例子:
    i=1
    while i <=10:
        print(i)
        += 1


    continue:跳出本次循环,直接执行下次循环

    break:退出循环,执行循环外的代码

    exit():退出程序,可指定返回值

    字符串

    定义:

    python中引号引起来的、input()输入的、str()转换的等

    拼接:

    #以下三个结果一样:
    name="haha"
    str1="==="+name+"==="
    str2="===%s===" %(name)
    str3="==={}===".format(name)
    print(str1)
    print(str2)
    print(str3)
    ===haha===
    ===haha===
    ===haha===

    下标:

    字符串、列表、元组都属于序列,都有下标。

    切片、倒序:

    a="hello,world"
    #从第1个到倒数第二个:
    print(a[0:-1])
    hello,worl
    #从第二个到最后一个:
    print(a[1:])
    ello,world
    #全取:
    print(a[:])
    hello,world
    #从第一个到第5个,布长为2:
    print(a[0:5:2])
    hlo
    #倒序:
    print(a[::-1])
    dlrow,olleh

    常见操作:

    a="hello,world haha"
    #计算字符串长度,以下两个结果一样,前者用len()函数,后者用字符串的__len__()方法:
    print(len(a))
    print(a.__len__())
    16
    16
     
    #首字母大写:
    print(a.capitalize())
    Hello,world haha
    #每个单词的首字母大写:
    print(a.title())
    Hello,World Haha
    #全大写:
    print(a.upper())
    HELLO,WORLD HAHA
    #全小写:
    print(a.lower())
    hello,world haha
    #大小写互换:
    print("Hello".swapcase())
    hELLO
     
    #共50个字符,字符串放中间,不够的两边补*:
    print(a.center(50,"*"))
    *****************hello,world haha*****************
    #共50个字符,字符串放中间,不够的左边补*:
    print(a.ljust(50,"*"))
    hello,world haha**********************************
    #共50个字符,字符串放中间,不够的右边补*:
    print(a.rjust(50,"*"))
    **********************************hello,world haha
     
    #删除字符串左边和右边的空格或换行:
    print(" haha\n".strip())
    haha
    #删除字符串左边的空格或换行:
    print(" haha\n".lstrip())
    haha
     
    #删除字符串右边的空格或换行:
    print(" haha\n".rstrip())
     haha
     
    #判断是否以you开头:
    print(a.startswith(("you")))
    False
    #判断是否以you结尾:
    print(a.endswith("you"))
    False
     
    #统计有多少e:
    print(a.count("e"))
    1
     
    #找出world在的第一个下标,找不到前者返回-1,后者报错:
    print(a.find("world"))
    print(a.index("world"))
    6
    6
    #找出最后一个h字符的下标,找不到前者返回-1,后者报错:
    print(a.rfind("h"))
    print(a.rindex("h"))
    14
    14
     
    #替换(数字、字符串、元组是不可变类型):
    b=a.replace('l','L',2)
    print(a)
    print(b)
    hello,world haha
    heLLo,world haha
     
    #分割(前者以:分割成列表,后者以\n分割成列表):
    print("root:x:0:0".split(":"))
    print("root:x\n:0:0".splitlines())
    ['root''x''0''0']
    ['root:x'':0:0']
     
    #联合(把列表里的元素以空格为分隔符组成字符串):
    print(" ".join(['df','-h']))
    df -h

    文件IO操作

    三步走:

    1.open要操作的文件

    2.操作(读、写、追加等)

    3.close文件

    只读模式:

    #默认为只读模式(第二遍读没有结果,因为从上往下读第一遍后,第二次从最后开始读,所以没结果):
    f=open('./2.py')
    a=f.read()
    b=f.read()
    f.close()
    print(a)
    print("="*10)
    print(b)
    hello
    world
    haha
    ==========
     
     
    #告诉你光标在哪儿(第一个字符的光标位置为0,每行尾的换行符占一个字符),移动光标,打印从光标位置开始到文件尾:
    f=open('./2.py',"r")
    print(f.tell())
    f.seek(2)
    print(f.tell())
    print(f.read())
    f.close()
    0
    2
    llo
    world
    haha
    #打印从光标位置开始,到行尾:
    printprint(f.readline())
    llo
    #打印从光标位置开始,到文件尾,且把每一行区分做成列表:
    printprint(f.readlines())
    ['llo\n''world\n''haha']

    文件读的循环方法;

    #strip用来处理换行、取消空行:
    f=open("./2.py","r")
    for index,line in enumerate(f.readlines()):
        print(index,line.strip())
    f.close()
    (0'hello')
    (1'world')
    (2'haha')
    #第二种写法,结果一样,效率高,是一行一行的读,而不是全读完:
    for index,line in enumerate(f):
        print(index,line.strip())

    只写模式:

    #如文件不存在则创建,存在则覆盖原内容;写内容,默认不换行,得加\n;截断,就是保留前三个字符(0为全删,空为不删);刷新缓存:
    f=open("./2.py",'w')
    f.write("hello\n")
    f.truncate(3)
    f.flush()
    f.close()
    hel

    追加模式:

    #追加模式任何情况下只会在最后追加;也有truncate用来截取前面的数据:
    f=open("./2.py",'a')
    f.write("world")
    f.truncate(10)
    f.close()
    helworld

    函数

    传参:

    #形参:
    def test(a,b):
        print(a,b)
    #实参:
    test(1,2)
    #位置参数(即实参不同位置上的参数)要写在关键字参数前面:
    test(1,b=2)
    #全为关键字参数,那么与顺序无关:
    test(b=2,a=1)
    #默认值参数:
    def test(a,b=2):
    #可变长参数(参数名前加*):
    def test(name,sex,age,*haha):
        print(name,sex,age,haha)
    test("zhangsan""man"30991)
    ('zhangsan''man'30, (991))

    函数的返回值:

    #如果是0,则后面的代码不继续执行;也可以自定义:
    return 0

    模块

    定义:

    就是一个.py文件,写好的、拿给别人用。

    模块主要在/usr/local/lib/python3.6下。

    模块的导入:

    #导入多个模块:
    import hello,os
    #导入模块中的函数,三种写法:
    hello.function1()
    from hello import funct1,funct2
    from hello import *
    #这句话的意思是如果执行的原模块(不是被调用):
    if __name=="__main__":

    标准库之os模块:

    import os
     
     
    #切换路径:
    os.chdir("/Users")
    #查看当前路径:
    print(os.getcwd())
    /Users

    面向对象编程

    面向过程编程思想与面向对象编程思想:

    面向对象仍然是有过程的;

    面向对象三大特性:封装、继承、多态

    类与对象:

    类是抽象的概念,是创建对象的模板。

    对象是一个实体。

    理解:类不是一个函数,而相当于一个模块;而对象相当于模块下面的函数。

    类的构成:

    名称:类名

    属性:一组参数数据

    方法:操作的方式或行为

    类的创建:

  • 相关阅读:
    普通锁和分布式锁
    java 正则表达式
    java 字符串转date 格式转换
    消息中间件 kafka
    数据的存储方式:对象存储、文件存储、块存储
    Exceptional Control Flow(6)
    Exceptional Control Flow(5)
    Exceptional Control Flow(4)
    Exceptional Control Flow(3)
    Exceptional Control Flow(2)
  • 原文地址:https://www.cnblogs.com/lidowson/p/12651852.html
Copyright © 2011-2022 走看看