欢迎光临散文网 会员登陆 & 注册

python基础使用

2022-06-03 12:22 作者:大一_新生  | 我要投稿

file one  (main)

如果有编程语言基础,可以直接看带有注释的实例学习python

# ------------------------第一部分:库的使用------------------------------

# import learn_fun                     #这种是把整个模块调入进来

# import learn_fun as cv               #在调入的过程进行重命名为cv

from learn_class import User        #从learn_class 文件中只调用User,节省空间

import openpyxl                         #第三方库的调用,使用之前pip下载对应库

import os                               #调用build in (内置)库,不需要像第三方使用pip命令下载

from learn_fun import print_fun,v_to_main       #可以导入函数以及变量

from bilibili_api import video                              #B站的api接口,也属于第三方库

# ----------------------变量的定义:不需要声明类型------------------------

user_of_nume="Li jialong"

buff = "begin"

#-------------------------注释的两种方法--------------------------------

# """ 这种注释好用

"""

这个是用来注释的地方

"""

# -------基本输出输入函数:" " / ' ' 都可使用,当有两个“” 把其中一个换成‘’,否则报错

print(os.name)

#下面这个while很神奇 ,它的停止地方是根据缩进来判断的

# ---------------------程序结构:while for if---------------------------

while buff!="exit":

    #输入函数,默认返回类型是string 要转化为int才可以计算

    buff = input("please input a number for testing \n")

    # if a in a_group:

    # if a not in a_group: 这两个也是判断语句

    if buff.isdigit(): #这个语句使用了类和对象

        test_number = int(buff)

    else:

        print("Not a negative number")

        test_number = 666

    #函数的调用

  1. 使用 import module_name ,就是全部调入,具体函数名前加 module_name.

  2.   #如果使用 form module import fun.name 不需要前面有模块名

# for example

    # y_fun = learn_fun.print_fun(test_number,user_of_nume)

    y_fun = print_fun(test_number,user_of_nume)


    #只有有f 基本{}里面的就是你想要的,而不是字符串

# ---------------新的数据类型 list/set/dictionary----------------------

list_test = [1,2,3,4]       #python里面的list其实不是数组,它的element可以是不同类型

i = 0

for list_test_element in list_test:

    print(f"the for loop is {list_test_element*1} == {list_test[i]}")     #list的引用

    i = i+1

dictionary_test={"color":list_test[0],"number":list_test[1]}

print(f"{dictionary_test} if based on index "+str(dictionary_test["color"]))

print(type(dictionary_test["color"]))


# --------------list/set 对此数据类型进行增加或减少数据-----------------

list_test.append(5)     #很奇怪,这个只可以添加一个

print(list_test)

list_test.remove(1)     #添加list element

print(list_test)

set_test = {1,2,3,2,1}  #set 使用的是{} 不可以重复,输出也是随机顺序(string)

print(f"set is different {set_test}")


# -------------------多文件使用:变量的传递 :v_to_main ---------------

print(f"build in / build in based on Data type{v_to_main} ")


# 下面是excel数据处理的练习,有用的一些函数

print(type(range(5)))       # range 75 就是 0~74  

print(range(4,8))

# print(list_test.max_row)  #这个不可以使用是因为对象必须是excel的一个工作普

# ----------自定义类的使用:创建对象并且使用

user1 = User(1,123)

print(f"the id is {user1.id}")

user1.change_id(111)

user1.display()


# -------------------api接口:blibli-------------------------------

# ------------------json:可以

v = video.Video("BV1RR4y1G7Eh")

id = v.get_info()

id1=v.get_chargers()


print(id)

print(id1)

print(type(id))

"""

get_video_info(bvid="BV1RR4y1G7Eh")

print(v)

"""

file two(被调用自定义函数)

#简单定义函数,这个python好像上面的变量这个函数里面可以直接使用

from cgi import test


def print_fun(number,user_of_nume):

    bool_test=number>0

    print(type(bool_test))

    if number>0:

        print(f"{user_of_nume} input the number of {number}")

        return number

    elif number==0:

        print("input a zero")

        return number

    else:

        print("error negative input")

        return -number


v_to_main=0

file three(自定义 : 类)

# 默认这个类的名字首字母大写:属性/方法

class User:

    def __init__(self,id,password):

        self.id = id

        self.password = password

    def change_id(self,id):

        self.id = id

    def display(self):

        print(f"the user id is {self.id}  password is {self.password}")

python基础使用的评论 (共 条)

分享到微博请遵守国家法律