参考资料:Python网红Alex b站
文章目录一、编程语言介绍二、Python介绍&生态圈三、Python开发环境安装四、第一个Python程序五、变量与内存
电脑的三大件的关系变量
变量名定义规则变量命名规范变量的修改与删除常量 六、基本数据类型之数字、字符串、列表、布尔
整数和浮点
基本数据类型数字类型 字符串str布尔类型 bool列表 七、常用运算符八、读取用户指令九、流程控制之if、else十、本日作业
一、编程语言介绍计算机从原始到文明:
编译型——执行速度快——跨平台差
解释型——执行速度慢——跨平台
Windows安装Python,官网下载exe包:
# 安装结束,在shell输入python PS C:Userstaohy> python Python 3.10.2 (tags/v3.10.2:a58ebcc, Jan 17 2022, 14:12:15) [MSC v.1929 64 bit (AMD64)] on win32 Type "help", "copyright", "credits" or "license" for more information. >>> # 出现提示,说明安装成功了
Windows查看文件位置:四、第一个Python程序
1、交互器模式执行py代码
# 直接在shell输入python,进入编译器,输入代码
PS C:Userstaohy> python
Python 3.10.2 (tags/v3.10.2:a58ebcc, Jan 17 2022, 14:12:15) [MSC v.1929 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> print("Hello World!")
Hello World!
2、代码文件执行 需要代码编辑器IDE 流行的Pycharm和VsCode # shell打开,输入python 文件名执行 PS C:Userstaohy> python C:UserstaohyDesktophello_2022.py Hello World!五、变量与内存 电脑的三大件的关系
# 扫盲 电脑的三大件的关系:变量
# id 查看变量的具体内存地址 >>> name = 'taohy' >>> age = 18 >>> id(name) 3169943961520变量名定义规则
1. 变量名只能是 字母、数字或下划线的任意组合 2. 变量名的第一个字符不能是数字 3. 关键字不能声明为变量名变量命名规范
# 常用定义方式: # 驼峰体 AgeOfBlackGirl = 56 NumberOfStudents = 80 # 下划线 (python官方推荐) age_of_balck_girl = 56 number_of_students = 80 # 定义变量不好的方式剧烈 变量名为中文、拼音 变量名过长 变量名词不达意变量的修改与删除
# 变量修改后,内存地址也会随之改变 >>> name = 'taohy' >>> age = 18 >>> id(name) 3169943961520 >>> name = 'dabao' >>> id(name) 3169943995056
# 变量删除用del >>> del(name) >>> id(name) Traceback (most recent call last): File "常量", line 1, in NameError: name 'name' is not defined >>>
# 定义成常量,就是全部大写 >>> PI =3.1415923六、基本数据类型之数字、字符串、列表、布尔 整数和浮点 基本数据类型 数字类型
# 检测数据类型 type >>> type(PI)字符串str# python3 都是int型,没有长整型 >>> 2**70 1180591620717411303424 >>> type(2**70)
# 字符串特性 不可修改,实际上是name指向了另外的内存地址 >>> name = "taohy" >>> id(name) 3169943995440 >>> name = "dabao" >>> id(name) 3169943995568
# 查看str的方法 help(str)
>>> help(str.format)
Help on method_descriptor:
format(...)
S.format(*args, **kwargs) -> str
Return a formatted version of S, using substitutions from args and kwargs.
The substitutions are identified by braces ('{' and '}').
# 多行字符串 >>> msg = ''' ... Hello! ... My name is Taohy! ... ''' >>> msg 'nHello!nMy name is Taohy!n' >>> print(msg) Hello! My name is Taohy!
# 字符串拼接 >>> name = 'Taohy' >>> job = ' is developer.' >>> name + job 'Taohy is developer.'
# 字符串内引用外部变量
# python3 字符串前面加f,用{}表示变量
name = 'Taohy'
age = 18
job = 'developer'
msg = f'''
-------- {name} ShowTime --------
name : {name}
age : {age}
job : {job}
--------- End --------
'''
print(msg)
布尔类型 bool
列表
# 列表增加元素 # names.append(x) >>> girl_friend = ['aa','bb','cc'] >>> type(girl_friend)>>> girl_friend.append('dd') >>> print(girl_friend) ['aa', 'bb', 'cc', 'dd'] # name.insert(i,x) >>> girl_friend.insert(0,'aa1') >> print(girl_friend) ['aa1', 'aa', 'bb', 'cc', 'dd']
# 修改列表元素 >>> girl_friend[0]='aa2' >>> print(girl_friend) ['aa2', 'aa', 'bb', 'cc', 'dd']
# 查询列表元素
>>> girl_friend.index("aa")
1
# 删除列表元素
# names.remove(x)
>>> girl_friend.remove('aa2')
>>> print(girl_friend)
['aa', 'bb', 'cc', 'dd']
# del names
>>> del girl_friend[1]
>>> print(girl_friend)
['aa', 'cc', 'dd']
# 列表切片 下标索引从0开始 >>> girl_friend[0:1] ['aa']
# 列表嵌套 >>> girl_friend.append(['a',1,'developer']) >>> print(girl_friend) ['aa', 'cc', 'dd', ['a', 1, 'developer']]七、常用运算符
# 算术运算符
# 比较运算符
# 赋值运算符
# 逻辑运算
# 成员运算 # in 是否在序列中,如果在,返回True,否则就是False # 注意:只能用来测试字符串、列表、元组、字典、集合,但是不能测试数字类型 >>> print(girl_friend) ['aa', 'cc', 'dd', ['a', 1, 'developer']] >>> 'aa' in girl_friend True >>> aa in girl_friend Traceback (most recent call last): File "八、读取用户指令", line 1, in NameError: name 'aa' is not defined
# 如何实现读取用户输入,且打印在屏幕? 用 input 实现九、流程控制之if、else 十、本日作业
# 作业1
# 作业2



