跳转至

基础语法用例

百行代码总结 Python 基础语法

每当我在复习 Python 语法时就想写一个例子,一个可以包含python大部分基础语法的例子,便于自己复习语法。

写在程序之前

整个程序包含了138个知识点,哈哈,不要看着知识点多,其实每个知识点在代码中我就写了一句。所以这些知识点也就138行代码。

在python,可以利用#号进行注释,我的程序里面也都会采取#来讲解代码含义。同时给予知识点的编号。

并且,python利用代码间的缩进表示隶属关系。python 中的缩进一般是4个空格,也可以是一个tab键。但是新手不要轻易混用,否则可能会报错。 简单来说如下:

i = int(input())
if i > 0:
    print('hello world')  # 一定要有之前的缩进,以表示这条语句属于if
else:
    print('world hello')

好了,接下来给出我的300行例子吧👇

注:如果是初学,可以将每个函数单独截取阅读并运行查看。 当然咯,代码不能光读不写,一定要动手去编。如果不动手编,可能看完运行完你依旧还是不是很懂(本例子可以直接跟着指令直接运行)

代码

1、导入包

import os  # 1、利用 import 语句进行导入模块,用逗号分隔可以导入多个包
import math,copy,random,time
from collections import Counter  # 2、利用 from...import ....进行导入
import numpy as np  # 3、利用as关键字重命名包名,以后再使用就可以直接用np了

2、定义函数

def hello_world():  # 4、利用 def 关键字创建函数,简单来说:函数就是将具有独立功能的代码块组织成一个模块,需要的时候调用
    # 创建函数格式: def name(参数1,参数2....):
    yourname = input('你好,请输入你的名字:')  # 5、输入函数, input(), 若input中有字符串可以输出
    print('欢迎来到Python的世界,',yourname)  # 6、输出函数, print(), 若要输出多个对象,利用逗号分隔
    print('让我们开始学习吧~')

def hello_twice():
    global yourname, yourheight, yourweight  # 7、利用 global 关键字定义全局变量,使之在整个程序运行周期能够被调用
    yourname = input('请输入你的名字:')
    yourheight = input('请输入你的身高:')
    yourweight = input('请输入你的体重:')

字符串操作

# python 中字符串的部分操作
def deviding_line():
    word1 = 'i am line'  # 8、字符串的创建,利用单引号' 或者双引号" 进行创建
    word2 = word1.upper()  # 9、字符串的函数,利用运算符.进行调用,该语句中的upper()可以将字符串转换为全大写字母
    word3 = word1.lower()  # 10、lower()函数,将字符串转换为全小写
    word4 = word1.title()  # 11、title()函数,可以将字符串标题化
    # 以上三个函数仅为字符串的部分函数
    words = [word1, word2, word3, word4]  # 12、[]可以创建一个列表,列表可以存放很多的对象
    line = '-' * 40  # 13、利用*运算符创建串,这里就是有40个-符号

    endReturn = line + words[random.randint(0, 3)] + line  # 14、字符串可以利用 + 号直接相连
    # 15、上面出现的 random.randint() 可以创建随机整数, 0 和 3 为随机数的上下限
    return endReturn  # 16、函数返回值,可以在被调用时将这个值返回

数字操作

# 学习 python 中的数字模型
def study_number():
    num1 = input('请输入一个数字:')
    print('你输入的是数字%s'%num1,'可它的类型为:', type(num1))  # 17、输出函数格式控制
    # 18、type()函数可以返回该值的类型
    num2 = int(input('再输入一个数字:'))  # 19、利用 int() 函数进行数值类型转换,将数字转换为 int 整型
    print('你输入的是数字%s' % num2, '它的类型为:', type(num2))
    num3 = float(input('再输入一个数字:'))  # 20、float() 函数可以转换为浮点数类型
    print('你输入的是数字%s' % num3, '它的类型为:', type(num3))
    print('num1 + num2 = {}'.format(int(num1) + num2))  # 21、数字加法
    # 22、format() 函数格式化输出,在字符串中的{}符号将被替换为 format() 的参数
    print('num1 - num2 = {}'.format(int(num1) - num2))   #23、数字减法
    print('num1 * num2 = {}'.format(num1 * num2))  # 24、num1 * num2 并不会是你想要的数据,因为 input() 函数,默认输入为字符类型
    print('num1 * num2 = {}'.format(int(num1) * num2))  # 25、数字乘法
    print('num2 // num3 = {:.3f}'.format(num2 // num3))  # 26、数字整除,同时 {:.3f} 表示输出格式小数点后面保留三位
    print('num2 / num3 = {:.4f}'.format(num2 / num3))  # 27、数字除法,保留小数点后四位
    print('num2 % num3 = {:.4f}'.format(num2 % num3))  # 28、求余数
    print('num2 % num3 = {:.4%}'.format(num2 % num3))  # 29、求余数,{:.4%}输出格式为百分比格式
    print('num1 ** num2 = {}'.format(int(num1) ** num2))  # 30、幂运算
    print('This is the {a}, and {b}'.format(a = 'numbers', b = 'some operations'))  # 31、format 多参数,标记位置对应输出

布尔值

def study_bool():
    one, two, three = True, True, False  # 32、bool 值
    print(one, two, three)
    print('and 运算符:', one and two, one and three)  # 33、and 运算,当两个值同时为真时才为真
    print('or 运算符:', one or two, one or three)  # 34、or 运算符,当两个值同假时为假
    print('not 运算符:', not one, not two,not three)  # 35、not 运算符,得到相反的值

列表

#学习python中的列表模型
def study_list(length): # 36、带有参数的函数
    l1 = [1, 2, 3, 4, 5, 9.0]   # 37、创建列表, 利用符号[]
    l2 = list(range(10, 10 + length))  # 38、创建列表, 也可以用list()
    # 39、range() 函数,可以创建一个整数列表, 格式为 range(start, end, step), start为开始位置, end为结束位置, 前闭后开, step为步长
    print('l1的类型为: ',type(l1))
    print(l1[1], l2[1])  # 40、访问列表值, 可以直接用 list[num] 的方式进行访问
    l3 = l2  # 41、将 l2 的引用赋给 l3
    print(id(l1), id(l2), id(l3))  # 42、id()函数可以获取对象的内存地址, 在这里可以看到 l3 的的地址和 l2 是一样的
    l3[0] = 99  # 43、更新列表值
    print('l2 == l3 么?', l2 == l3)  # 44、更新 l3 后依旧等于 l2, 因为 l3 和 l2 本来就是一个对象, 不过换了个名字
    l4 = l2.copy()  # 45、复制一个 l2 给 l4, copy() 创建一个一模一样的列表
    l4[0] = 999
    print('l4 == l2么?', l4 == l2)  # 46、此时 l4 不等于 l2
    print('删除前', l4)
    del l4[0]  # 47、del 语句进行删除列表值, 在 python 中 del 可以删除所有的变量
    print('删除后', l4)
    l4.append(30)  # 48、给列表添加值
    l4.extend(l1)  # 49、给列表追加一个序列多个值
    print('添加l1后:', l4)
    l4.reverse()  # 50、列表反转
    print('反转后:', l4)
    l4.sort()  # 51、sort()函数,将列表进行排序
    print('排序后:', l4)

元组

#学习python中的元组模型
def study_tuple(length: int) -> bool:  # 52、解释参数类型的函数创建, `:` int 表示参数类型为整型, `->` bool 表示返回值为布尔值
    tuple1 = (1, 2, 3, 4)  # 53、创建元组, 利用 `()` 符号, 元组的特性是不可以改变
    tuple2 = tuple(range(10, 10 + length))  #54、利用 tuple 创建元组

    print(tuple1.count(1))  # 55、元组函数 count(), 用于输出某个值的数量
    print(tuple1.index(1)) #56、元组函数 index(), 可以按照索引得到值
    try:  # 57、python 中的异常处理, try: 语句内部如果出现错误则会转入到 except 中
        tuple1[0] = 9  # 58、因为元组的不可改变性,所以该语句会出错
    except TypeError:
        print('元组插入失败')
    finally:  # 59、finally 内语句不管是否出现错误都会执行
        print('不管插入成不成功我都会执行')

    try:
        print(id(tuple1), id(tuple2))
    except:
        return False
    else:
        tuple3 = tuple1 + tuple2  # 60、元组虽然不可改变, 但是可以通过 `+` 号进行合并为另一个元组
        print(tuple3, id(tuple3))
    return True

字典

def study_dict():  # 学习python中的字典模型, 字典是 键 -> 值 的映射
    # 61、以下为创建字典的5种方法
    dict1 = {1: '一', 2: '二', 3: '三', 4: '四'}  # 1、利用 {} 创建字典
    dict2 = dict(one=1, two=2, three=3)  # 2、利用 dict() 函数和关键字创建字典
    dict3 = dict(zip([6, 7, 8, 9], ['Six', 'Seven', 'Eight', 'Nine']))  # 3、利用 zip() 函数创建字典
    dict4 = dict([('One', 1), ('Two', 2), ('Three', 3)])  # 4、利用 dict() 函数和列表创建字典
    dict5 = dict({1:'一',2:'二',3:'三',4:'四'})  # 5、利用 dict() 函数和字典创建字典

    print(type(dict1), dict1 == dict5)  # 62、可以看到, dict1 和 dict5 是等价的
    print(dict1[1], dict2['one'], dict3[6], dict4['One'], dict5[1])  # 63、通过字典的键访问
    print(dict1.get(4))  # 64、通过get函数访问内容

    dict1[1] = '壹' # 65、修改字典内容
    dict1[5] = '五' # 66、添加字典
    print(dict1)
    print(1 in dict1, 6 in dict1, 7 not in dict1) # 67、in 和 not in 关键字,可以判断值是否在序列中

    dict6 = dict1.copy()  # 68、字典的复制
    dict6[1] = 'One'
    print(dict1, '<dict1------------dict6>', dict6)

    dict1.clear() # 69、字典的清空
    print(dict1)
    del dict1, dict2, dict3, dict4, dict5, dict6  # 70、删除字典, 也可以用 del dict[key] 的方式删除某个键

集合

def study_set():  # python中集合的学习, 集合中不存在相等的值
    set1 = set(['You', 'Are', 'Not', 'Beautiful'])  # 71、利用 set() 函数进行创建集合
    set2 = {'You','Are','So','Beautiful'}  # 72、利用 {} 创建集合, 创建空集合的时候不能用 {}, 因为 {} 表示字典
    set3 = set2.copy() # 73、集合的复制

    print(type(set1))  # 输出集合类型
    print(set1, set2)  # 输出集合
    print(set1 | set2)  # 74、集合`或`运算符, 得到两个集合中所有元素
    print(set1 & set2)  # 75、集合`与`运算符, 得到两个集合共同元素
    print(set1 ^ set2)  # 76、集合`异或`运算符, 得到两个集合中不同元素
    print(set1 - set2)  # 77、集合`差`运算, 得到 set1 有, set2 没有的元素
    print(set1 <= set2, set3 <= set2, set3 < set2)  # 78、<=符号, 判断是否为子集, <符号, 判断是否为真子集

    set1.add('Me too') # 79、集合添加元素
    print('is语句用法', set3 == set2, set3 is set2, set1 is not set2)  # 80、is 和 is not语句, is 语句用于判断对象是否一样, == 判断值是否一样
    set3.clear()  # 81、清空集合,集合变为空
    print(set3)
    del set3

常用函数

def study_Some_functions(): # python 中一些常用函数
    list1 = [1, 2, 3, 4, 5, 6]  # 眼熟不, 这就是之前的列表, 下面的这些都认认是啥
    tuple1 = (11, 12, 13, 14, 15, 16)  # 元组
    set1 = set(list1)  # 集合
    dict1 = dict(zip([1, 2, 3, 4, 5], ['one', 'Two', 'Three', 'Four', 'Five']))  # 字典, 如果 zip 中的两个序列长度不一样, 则以短的为准

    print(max(list1), max(tuple1), max(set1), max(dict1))  # 82、max() 函数,得到序列中最大值
    print(min(list1), min(tuple1), min(set1), min(dict1))  # 83、min() 函数,得到最小值
    print(sum(list1), sum(tuple1), sum(set1), sum(dict1))  # 84、sum() 函数,得到序列和
    print(len(list1), len(tuple1), len(set1), len(dict1))  # 85、len() 函数,得到序列长度
    print(divmod(list1[0], tuple1[0]))  # 86、divmod() 函数,计算两个数的商和余数, 结果两个格式为(商, 余数)
    print(list(enumerate(tuple1)))  # 87、enumerate(), 给元组添加一个索引

    list2 = list(tuple1)  # 88、利用 list() 将元组, 字典等等转换为列表
    list3 = list(set1)
    list4 = list(dict1)
    tuple2 = tuple(list1)  # 89、利用 tuple() 将列表, 字典等转换为元组

    print(list2,list3,list4)

    for i in range(len(list1)):  # 90、for循环语句
        print(list1[i], end=' ')  # 91、print的属性 end, 可以使输出格式为 end 的内容, 而不是默认换行
    print()
    for i in dict1:  # 92、for 循环遍历
        print(i, dict1[i], end=' ')

    list5 = list(reversed(list1))  # 93、reversed() 函数,可以反转序列
    print('\n',list5)  # 94、`\n` 换行符

    testStr = "The mountains and rivers are different, the wind and the moon are the same"
    words = testStr.split(' ')  # 95、split() 函数, 以 split() 内参数分割字符串, 返回一个列表
    print(words)
    words.sort(key=len)  # 96、sort() 函数, 进行排序, 参数 key=len 时, 以字符串长度为标准排序
    print('以长度排序:',words)
    words.sort(key=len, reverse=True)  # 97、reverse 参数, 结果反转
    print('以长度排序并且反转:', words)
    words.sort(key=str)  # 98、以字典序进行排序
    print('以字典序排序:', words)

    ct = Counter(testStr)  # 99、collections 模块中的 Counter, 可以得到字符串中每个数字出现次数
    print(ct)
    ct.update('eeeexxxxxlllll')  # 100、更新
    print(ct)
    print(ct.most_common(5))  # 101、得到字符数最多的前五位

切片

def study_Slice():  # python的切片操作, 得到序列的部分内容
    str1 = 'I hope one day, I can find you, my sweet dream'
    list1 = list(range(10))
    tuple1 = tuple(list1)

    print(str1[:])  # 102、切片格式为 `str[start:end:step]`, 前闭后开, step 可为正负, 默认步长为1
    print(str1[::-1])  # 103、当步长为负数的时候, 反转
    print(str1[:15])  # 104、只有 end 时, 截取最开始到 end
    print(str1[15:])  # 105、只有 start 时, 截取从 start 到末尾的所有字符
    print(str1[::2])  # 106、步长为 2
    print(str1[1::2])

    print(list1[:])  # 107、和 str 一样
    print(list1[2:])
    print(list1[:2])
    print(list1[::-1])

    list1[1:5] = [10] # 切片赋值, 右边必须为一个可以遍历的序列
    #list1[1:5] = 10   这样就会报错
    print(list1)

循环语句和条件语句

def study_loop_select():  # python中的循环和选择语句
    list1 = [1, 2, 3, 4, 5]
    num = int(input('while循环, 输入你想要循环的次数:'))
    i = 1
    while i <= num:  # 108、while expression: 当 expression 为真的时候进行循环
        if i < 5:  #109、if...elif...else 选择语句, 如果判断结果只有两个,可以使用if...else
            print('我打印了',i,'次')
        elif i < 10:
            print('打印了',i,'次,真累啊')
        elif i < 15:
            print('打印太多啦,再打印我就要停止了...')
        elif i < 20:
            print('continue...')
            i += 1
            continue   # 110、continue 语句, 用在循环中, continue 后的所有语句都不允许, 直接进入下次循环
            print('我想我可能输出不了了')
        else:
            print('累死我了,休息。 都', i, '次了~_~')
            break  # 111、break语句,运用在循环中,直接退出循环,所以,在本例子中,这个循环最多循环20次
        i+=1
        time.sleep(0.5)  # 112、time 库为 python 中的时间库, time.sleep(second) 可以使程序暂停运行 second 秒
    else:  # 113、while 循环后接一个 else 语句,当执行完所有循环后执行一次,可以省略(个人感觉用处不大)
        print('while结束了')

    # 113、for循环, 上面代码有用到过
    for i in list1:
        print(i,end=' ')
    print()

    for i in range(5):
        print(i)

python推导式

def study_expression_deduction(): # 114、python表达式推导
    list1 = [i for i in range(10)]   # 115、列表推导,可以将for循环的结果直接赋值给列表, 利用该语句推导出列表
    list2 = [x for x in range(20) if x % 2 == 0]  # 116、列表推导,可以在for循环中加入if语句, 满足 if 条件的才会被添加到列表中
    print(list1,'<list1--------------list2>',list2)

    print(deviding_line())  # 117、函数可以在任何地方被调用,如果是自己调用自己就可以称为递归调用

    list3 = [['_'] * 3 for i in range(3)]
    print(list3)

    fruits = ['Apple', 'Banana', 'Pear']
    colors = ['Red', 'Yellow', 'Green']
    suitcolor = [(color, fruit) for color, fruit in zip(colors, fruits)] # 118、两个列表合并
    print(suitcolor)  # 输出结果为:[('Red', 'Apple'), ('Yellow', 'Banana'), ('Green', 'Pear')]
    cartesian = [(color, fruit) for color in colors for fruit in fruits] # 119、两个列表的笛卡尔积
    print(cartesian)  # 输出结果为:[('Red', 'Apple'), ('Red', 'Banana'), ('Red', 'Pear'), ('Yellow', 'Apple'), ('Yellow', 'Banana'), ('Yellow', 'Pear'), ('Green', 'Apple'), ('Green', 'Banana'), ('Green', 'Pear')]

    dict1 = {fruit:color for fruit,color in suitcolor}  # 120、字典推导, 只要是带有键值对的任何序列, 都可以推导出字典
    print(dict1)  # 输出结果为:{'Red': 'Apple', 'Yellow': 'Banana', 'Green': 'Pear'}

文件操作

def study_files():
    filepath = input('请输入你的文件路径(输入quit退出):')
    if filepath == 'quit':
        return True
    try:
        file = open(filepath, 'w')  # 121、打开文件,'w'为写格式打开, 若文件不存在则创建
        file.write('哈哈,现在开始写文件')  # 122、向文件写入字符串
        file.close()  # 123、关闭文件
        file = open(filepath, 'r')  # 122、以'r'读格式打开, 若文件不存在则报错
        print('从文件中读出的内容:\n', file.read())  # 123、read() 函数可以得到文件内容
    except FileNotFoundError:
        print('文件未找见请重新输入')
        study_files()  # 124、这就是上面所说的递归调用
    except:
        print('出现错误,请重新输入路径')
        study_files()
    # 共有以下几种打开文件的方式
    # 'r':读取(默认)
    # 'w':写入(会先截断之前的内容)
    # 'x':独占写入(若文件已存在则报错)
    # 'a':追加写入(将内容写入到已有文件的末尾)
    # 'b':二进制模式
    # 't':文本模式(默认)
    # '+':读写模式(可添加到其他模式中使用)
    # 'U':通用换行符支持
    # 'r+':读写
    # 'w+':写读
    # 'a+':追加读
    # 'rb':二进制读
    # 'wb':二进制写
    # 'ab':二进制追加
    # 'rb+':二进制读写
    # 'wb+':二进制写读
    # 'ab+':二进制追加读
    # ...

class Users():  # 125、面向对象编程, python 中创建类 class, 类包含有属性与方法, 包括有私有变量, 共有变量等等
    def __init__(self, name, height, weight):  # 126、类的构造方法, 创建实例时自动调用
        self.name = name
        self.height = height
        self.weight = weight
        self.yanzhi = 100

    def display(self): # 127、类方法
        print('大家好,我是{},身高{},体重{},颜值超高{}'.format(self.name,self.height,self.weight,self.yanzhi))

    def __private(self):  # 用`__`开头, 约定的私有方法, 只能在类内部调用, 不能在外部调用
        print('我是私有方法')

    def __str__(self):  # 类的魔法方法, 用于返回一个字符串, 该字符串为类的描述
        return '我是一个类, 我的名字叫{}, 我的身高{}, 我的体重{}, 我的颜值{}'.format(self.name, self.height, self.weight, self.yanzhi)

    @classmethod  # 类方法, 用于调用类的属性, 需要传入类或者实例
    def class_method(cls):
        print('我是类方法, 我的类名为{}'.format(cls.__name__))

    @staticmethod  # 静态方法, 用于调用类的属性, 但是不需要传入类或者实例
    def static_method():
        print('我是静态方法')
if __name__=="__main__":  # 128、无论之前有什么,程序都会从这里开始运行
    hello_world()  # 129、所以这是运行的第一句,调用该函数
    deviding_line()
    try:
        print(yourname)  # 130、调用完 hello_world() 函数后,因为在 hello_world() 函数内部有一个 yourname 变量,所以我们进行输出,看在这里能不能找见这个变量
    except:
        print('  未能找见该变量  ')  # 131、不可能找见这个变量的,因为 yourname 是局部变量,只存在于 hello_world() 函数内部
    deviding_line()
    hello_twice()  # 132、因为在该函数中定义了 global 语句,所以该函数中的变量在以下程序中都可以使用

    user = Users(yourname, yourheight, yourweight)  # 133、实例化对象,创建Users类的实例
    user.display()  # 134、对象调用方法
    print(user)  # 对象调用魔法方法 __str__()
    user.class_method()  # 对象调用类方法
    Users.class_method()  # 类调用类方法
    user.static_method()  # 对象调用静态方法
    Users.static_method()  # 类调用静态方法

    # 135、在python中,可以用三引号进行多行注释,但是如果用变量接收注释的话也可以是一个有格式的字符串,如下
    chooseinformation = '''Input the number of the function you want to Run(quit is exit):
    1、study_number    2、study_list
    3、study_tuple    4、study_dict
    5、study_set      6、study_Some_functions
    7、study_Slice    8、study_loop_select
    9、study_expression_deduction
    10、study_files
    '''
    deviding_line()
    while True:  # 136、while 循环进行运行程序, 只有当输入 quit 时才会退出循环(不过你强制退出当然也可以退出)
        input('按键继续')  # 137、为了让输出不那么快,等待按键后才输出以下内容
        print(chooseinformation)
        num = input('输入序号:')
        # 138、在以下 if...elif...else 选择中, 我们来选择运行不同的函数
        if num == 'quit':
            break
        elif num=='1':
            study_number()
        elif num=='2':
            study_list(10)
        elif num=='3':
            study_tuple(10)
        elif num=='4':
            study_dict()
        elif num=='5':
            study_set()
        elif num=='6':
            study_Some_functions()
        elif num=='7':
            study_Slice()
        elif num=='8':
            study_loop_select()
        elif num=='9':
            study_expression_deduction()
        elif num=='10':
            study_files()
    print('哈哈,恭喜你,这个程序结束咯~')

总结

文档中一共记录了138个知识点,这些知识点包括有:

  • 1、python中的注释
  • 2、python中的运算符
  • 3、python中的数据类型
  • 4、python中的函数
  • 5、python中的模块
  • 6、python中的异常处理
  • 7、python中的面向对象编程
  • 8、python中的文件操作

等等