基础语法(IDE:PyCharm)

------------------------------------------------------------------

print(变量名) ----输出变量

------------------------------------------------------------------

print(type(变量名)) ----查看变量类型

------------------------------------------------------------------

a == b    ----对两个值进行比较

a is b    ----对两个内存地址进行比较
------------------------------------------------------------------

新建变量 = input("提示语") ----使用input方法接收用户输入作为变量

------------------------------------------------------------------

int(变量名) ----将变量转换成整数类型

float(变量名) ----将变量转换成浮点类型

str(变量名) ----将变量转换成字符串型

(并不会原变量的类型,如果a原来是int型,使用str(a)后,再输出a依旧是int)

------------------------------------------------------------------

print("格式化字符串" % 变量名) ----格式化输出变量

    %s ----字符串

    %d ----整数(可以用%.5d表示5位数,不足5位用0补齐)

    %f ----浮点数(可以用%.2f表示保留两位小数)

    %% ----%(在使用格式化输出变量后要输出%得用%%)

------------------------------------------------------------------

if    ----条件的使用

if 条件a:

    a条件下执行的命令

    if 条件b:

        符合条件a与条件b下执行的命令

    else:

        符合条件a不符合条件b执行的命令

elif 条件c and 条件d:

    同时符合条件cd(c & d)下执行的命令

elif 条件e or 条件f:

    符合条件e或条件f(e | f)下执行的命令

else:

    不属于上述条件下执行的命令

-------------------------------------------------------------------

while ----while循环初步使用(使用while打印99乘法表)

i = 1

j = 1

while i < 10:

    while j < 10:

        print("%d*%d=%d" % (i,j,i*j),end = " ")

        j += 1

    j = 1

    i += 1

    print("")

-------------------------------------------------------------------

break&continue ----两种跳出循环的方式以及使用

i = 0

j = 0

while i < 10:

    if i == 8:

        break

    j += i

    i += 1

print("j")

i = 0

j = 0

while i < 10:

    if i == 8:

        i += 1

        continue    #在使用continue时要跳出死循环,不会一直停在 i == 8 而无法执行后续代码

    j += i

    i += 1

print("j")

-------------------------------------------------------------------

 def ----定义函数

def 函数1():      -------定义的函数要调用才会执行

    """xxxxx"""    -----三个双引号可以对函数功能进行说明,在使用函数时按快捷键Ctrl+q查看

    print("321")

def 函数2():

    print("123")

    函数1()

    print("1234567")

def 函数2()

(定义函数时要空两行)

-------------------------------------------------------------------

def 函数名(形参1,形参2,...) ----函数参数的定义

    """

    函数说明

    :param 形参1:参数说明

    :param 形参2:xxx

    :return:

    """

    return ----返回值

变量 = 函数名(实参1,实参2,...) ----调用函数并给出实参执行得到返回值赋给变量

(灵活使用参数可以让函数变得灵活)

-------------------------------------------------------------------

a = [x,x,x,...] ----列表

b = [x,x,x,...]

a.sort(reverse=01) ----列表排序,reverse=1降序

a.extends(b) ----列表合并

a.insert(位置int,x)

a.remove(x) ----删除列表中出现的第一个x

del a[3] ----删除列表中第三个元素

a.pop(3) ----删除列表中第三个元素

a.clear ----清空列表

a.len()

-------------------------------------------------------------------

a = {"a1": xxx,

       "b1": xxx,

       "...": ...}        ----建立字典

a["c1"] = xxx       ----在字典内新增

a["a1"] = xxx       ----修改字典属性

-------------------------------------------------------------------

print(str[::-1])    ----输出字符串str逆序

-------------------------------------------------------------------

a = 10

b = 5

a,b = b,a    ----利用元组来交换ab的值

-------------------------------------------------------------------

def yoo(sex=True):    ----使用缺省参数

    if sex:

        print("性别:男")

    if not set:

        print("性别:女")

-------------------------------------------------------------------

def yoho(*tuple,**dic)    ----定义方法

    print(tuple)

    print(dic)

a = (1,2,3,4)

b = {"name":"xiaoming",

        "age":19}

yoho(a,b)    ----直接传入数据输出都为元组

yoho(*a,**b)    ----拆包语法传入数据可以分别传入元组和字典

-------------------------------------------------------------------

def rec(num):    ----递归

    if num == 0:    ----定义跳出递归条件

        return num

    a = rec(num-1)

    return a + num

b = rec(100)

print(b)

-------------------------------------------------------------------

捕获异常

while True:

    try:

        num = int(input("请输入一个整数")

        result = 10 / num

        print(result)

    except ValueError:    ----针对ValueError的处理

        print("请输入正确的数字")

    except Exception as result:    ----针对未知错误的处理

        print("未知错误 %s " % result)

    else:    ----程序运行正常执行的操作

        f_result = 100 * result

        print(f_result)

        break

    finally:    ----无论程序正不正常进行都会进行的操作

        print("输出完毕")

---------------------------------------------------------------------------------

文件读写

file = open("FileName","a")    ----在文件末尾写入

file.write("want append")    ----要写入的文本

file.close()

---------------------------------------------------------------------------------

逐行读取文件

file = open("FileName")

while True:

    text = file.readline()

    if not text:

        break

    print(text)

file.close()

---------------------------------------------------------------------------------

复制小文件

file1 = open("File1Name")

file2 = open("CopyFile1","w")

text = file1.read()

file2.write(text)

file1.close

file2.close
---------------------------------------------------------------------------------

 逐行复制文件

file1 = open("FileName")

file2 = open("CopyFile","w")

while True:

    text = file1.readline()

    if not text:

        break

    file2.write(text)

file1.close()

file2.close()

---------------------------------------------------------------------------------

原文地址:https://www.cnblogs.com/mengxinteriri/p/10332363.html