python基础语法

    科技2025-05-16  51

    python基础语法

    一、计算机组成原理

    1.什么是计算机?

    2.计算机是由什么组成的?

    一个完整的计算机系统,是由硬件系统和软件系统两大部分组成的。

    3.硬件系统

    4. 软件系统

    5.计算机是如何处理程序的?

    6.编程语言是什么?

    7.什么是Python?

    Python就是一门编程语言,而且是现在世界上最流行的编程语言之一。

    二、python简介

    1.Python介绍

    Python发展历史:链接

    2.Python版本

    3.总结

    三、python解释器

    1.解释器的作用

    2.下载Python解释器

    下载地址:https://www.python.org/downloads/release/python-372/

    3.安装Python解释器

    4.总结

    四、注释

    1.注释的作用

    2.注释的分类及语法

    3.快速体验

    注意:解释器不执行任何的注释内容。

    # 单行:输出hello world print('hello world') print('hello Python') # 简单注释内容 # 单行注释 """ 第一行注释 第二行注释 第三行注释 """ ''' 注释1 注释2= 注释3 '''

    4.总结

    五、变量

    1.变量的作用

    2.定义变量

    标识符

    命名习惯

    使用变量

    """ 1. 定义变量 语法:变量名 = 值 2. 使用变量 3. 看变量的特点 """ # 定义变量:存储数据TOM my_name = 'TOM' print(my_name) # 定义变量:存储数据 黑马程序员 schoolName = '我是黑马程序员,我爱Python' print(schoolName)

    认识bug

    # 定义变量:存储数据TOM my_name = 'TOM' print(my_name) # 定义变量:存储数据 黑马程序员 schoolName = '我是黑马程序员,我爱Python' print(schoolName)

    3.Debug工具

    打断点

    Debug调试

    Debug输出面板分类

    4.认识数据类型

    """ 1. 按经验将不同的变量存储不同的类型的数据 2. 验证这些数据到底是什么类型 -- 检测数据类型 -- type(数据) """ # int -- 整型 num1 = 1 # float -- 浮点型,就是小数 num2 = 1.1 print(type(num1)) print(type(num2)) # str -- 字符串,特点:数据都要带引号 a = 'hello world' print(type(a)) # bool -- 布尔型,通常判断使用,布尔型有两个取值 True 和 False b = True print(type(b)) # list -- 列表 c = [10, 20, 30] print(type(c)) # tuple -- 元组 d = (10, 20, 30) print(type(d)) # set -- 集合 e = {10, 20, 30} print(type(e)) # dict -- 字典 -- 键值对 f = {'name': 'TOM', 'age': 18} print(type(f))

    检测数据类型的方法:type()

    """ 1. 按经验将不同的变量存储不同的类型的数据 2. 验证这些数据到底是什么类型 -- 检测数据类型 -- type(数据) """ # int -- 整型 num1 = 1 # float -- 浮点型,就是小数 num2 = 1.1 print(type(num1)) print(type(num2)) # str -- 字符串,特点:数据都要带引号 a = 'hello world' print(type(a)) # bool -- 布尔型,通常判断使用,布尔型有两个取值 True 和 False b = True print(type(b)) # list -- 列表 c = [10, 20, 30] print(type(c)) # tuple -- 元组 d = (10, 20, 30) print(type(d)) # set -- 集合 e = {10, 20, 30} print(type(e)) # dict -- 字典 -- 键值对 f = {'name': 'TOM', 'age': 18} print(type(f))

    5.总结

    六、输出

    1.格式化输出

    所谓的格式化输出即按照一定的格式输出内容。

    格式化符号

    """ 1. 准备数据 2. 格式化符号输出数据 """ age = 18 name = 'TOM' weight = 75.5 stu_id = 1 stu_id2 = 1000 # 1. 今年我的年龄是x岁 -- 整数 %d print('今年我的年龄是%d岁' % age) # 2. 我的名字是x -- 字符串 %s print('我的名字是%s' % name) # 3. 我的体重是x公斤 -- 浮点数 %f print('我的体重是%.3f公斤' % weight) # 4. 我的学号是x -- %d print('我的学号是%d' % stu_id) # 4.1 我的学号是001 print('我的学号是%03d' % stu_id) print('我的学号是%03d' % stu_id2) # 5. 我的名字是x,今年x岁了 print('我的名字是%s,今年%d岁了' % (name, age)) # 5.1 我的名字是x,明年x岁了 print('我的名字是%s,明年%d岁了' % (name, age + 1)) # 6. 我的名字是x,今年x岁了,体重x公斤,学号是x print('我的名字是%s,今年%d岁了,体重%.2f公斤,学号是%06d' % (name, age, weight, stu_id))

    2.体验

    格式化字符串除了%s,还可以写为f’{表达式}’ f-格式化字符串是Python3.6中新增的格式化方法,该方法更简单易读。

    name = 'TOM' age = 18 weight = 75.5 # 我的名字是x,今年x岁了,体重x公斤 print('我的名字是%s,今年%s岁了,体重%s公斤' % (name, age, weight)) name = 'TOM' age = 18 # 我的名字是x,今年x岁了 print('我的名字是%s,今年%s岁了' % (name, age)) # 语法 f'{表达式}' print(f'我的名字是{name},今年{age}岁了')

    3.转义字符

    print('hello') print('world') print('hello\nPython') print('\tabcd')

    4.结束符

    print('hello', end="\n") print('world', end="\t") print('hello', end="...") print('Python')

    5.总结

    七、 输入

    1.输入的语法

    """ 1. 书写input input('提示信息') 2. 观察特点 2.1 遇到input,等待用户输入 2.2 接收input存变量 2.3 input接收到的数据类型都是字符串 """ password = input('请输入您的密码:') print(f'您输入的密码是{password}') print(type(password))

    2.输入的特点

    3.总结

    八、数据类型转换

    1.转换数据类型的作用

    2.转换数据类型的函数

    3.快速体验

    """ 1. input 2. 检测input数据类型str 3. int() 转换数据类型 4. 检测是否转换成功 """ num = input('请输入数字:') print(num) print(type(num)) # str print(type(int(num))) # int

    4.实验

    # 1. float() -- 将数据转换成浮点型 num1 = 1 str1 = '10' print(type(float(num1))) # float print(float(num1)) # 1.0 print(float(str1)) # 10.0 # 2. str() -- 将数据转换成字符串型 print(type(str(num1))) # str # 3. tuple() -- 将一个序列转换成元组 list1 = [10, 20, 30] print(tuple(list1)) # 4. list() -- 将一个序列转换成列表 t1 = (100, 200, 300) print(list(t1)) # 5. eval() -- 计算在字符串中的有效Python表达式,并返回一个对象 str2 = '1' str3 = '1.1' str4 = '(1000, 2000, 3000)' str5 = '[1000, 2000, 3000]' print(type(eval(str2))) print(type(eval(str3))) print(type(eval(str4))) print(type(eval(str5)))

    5.总结

    九、运算符

    1.算数运算符

    2.赋值运算符

    3.复合赋值运算符

    a = 10 a += 1 # a = a + 1 print(a) b = 10 b -= 1 # b = b - 1 print(b) # 注意: 先算复合赋值运算符右面的表达式; 算复合赋值运算 c = 10 # c = 10 + 1 + 2 # c += 3 -- c = c + 3 c += 1 + 2 print(c) d = 10 d *= 1 + 2 print(d)

    4.比较运算符

    5.逻辑运算符

    a = 0 b = 1 c = 2 # 1. and: 与: 都真才真 print((a < b) and (c > b)) print(a > b and c > b) # 2. or:或 : 一真则真,都假才假 print(a < b or c > b) print(a > b or c > b) # 3. not: 非: 取反 print(not False) print(not c > b)

    拓展

    6. 总结

    Processed: 0.016, SQL: 8