Python简介
Python是一种高级编程语言,由荷兰人Guido van Rossum于1989年发明,第一个公开版本发行于XXXX年X月,Python的设计哲学强调代码的可读性和简洁的语法(尤其是使用空格缩进划分代码块,而非使用大括号或关键字),Python的标准库非常庞大,可以支持很多任务,如网页爬取、数据分析、人工智能等。
Python环境搭建
1、下载Python安装包
访问Python官网(https://www.python.org/)下载适合自己操作系统的Python安装包,建议选择最新的稳定版本。
2、安装Python
运行下载好的安装包,按照提示进行安装,在安装过程中,勾选“Add Python to PATH”选项,将Python添加到系统环境变量中。
3、验证Python安装
打开命令行工具,输入python --version,如果显示出Python的版本号,说明Python已经成功安装。
Python基本语法
1、注释
在Python中,单行注释使用井号(#),多行注释使用三个单引号(''')或三个双引号(""")包围。
这是一个单行注释 ''' 这是一个 多行注释 '''
2、变量和数据类型
Python中有多种数据类型,如整数(int)、浮点数(float)、字符串(str)、列表(list)、元组(tuple)、字典(dict)等,变量可以通过赋值语句来创建和修改。
a = 10 # 整数
b = 3.14 # 浮点数
c = "Hello, Python!" # 字符串
d = [1, 2, 3] # 列表
e = (1, 2, 3) # 元组
f = {"name": "Tom", "age": 18} # 字典
3、控制结构
Python中有if、elif、else条件判断语句,以及for、while循环语句。
if-elif-else条件判断语句
age = 18
if age < 18:
print("未成年")
elif age >= 18 and age < 60:
print("成年")
else:
print("老年")
for循环语句
for i in range(5):
print(i)
while循环语句
count = 0
while count < 5:
print(count)
count += 1
函数和模块
1、函数定义和调用
使用def关键字定义函数,使用函数名加括号的方式调用函数,函数可以有参数和返回值。
def add(a, b): # 定义函数add,接收两个参数a和b
return a + b # 返回a和b的和
result = add(1, 2) # 调用函数add,传入参数1和2,将返回值赋给变量result
print(result) # 输出结果3
2、模块导入和使用
Python中有很多内置模块和第三方模块,可以使用import关键字导入模块,然后通过模块名加函数名的方式调用模块中的函数,导入math模块并使用其中的sqrt函数计算平方根。
import math # 导入math模块 result = math.sqrt(4) # 调用math模块中的sqrt函数,计算4的平方根,将结果赋给变量result print(result) # 输出结果2.0
面向对象编程(OOP)基础
1、类和对象的定义和使用
使用class关键字定义类,类中可以包含属性和方法,通过类名加括号的方式创建对象,通过对象名加方法名的方式调用对象的方法。
class Person: # 定义一个Person类,包含属性name和age,以及方法say_hello和get_age_and_name
def __init__(self, name, age): # __init__方法是类的构造方法,用于初始化对象的属性值
self.name = name # self表示对象本身,这里表示对象的属性name等于传入的参数name
self.age = age # self表示对象本身,这里表示对象的属性age等于传入的参数age
def say_hello(self): # say_hello方法用于打印问候语,接收一个参数self表示对象本身可以省略不写,因为方法的第一个参数默认是self,表示对象本身的意思,这里直接写成name即可。 print(f"Hello, my name is {self.name}!") # f-string是Python3.6以后引入的新特性,可以在字符串中直接插入变量的值或者表达式的结果。{}内的内容会被替换成f后面的变量或表达式的值,注意这里的self.name需要用f-string包裹起来才能正确显示名字。 def get_age_and_name(self): # get_age_and_name方法用于获取对象的年龄和姓名,返回一个包含年龄和姓名的元组 return self.age, self.name # return关键字表示返回一个值,这里返回一个包含self.age和self.name的元组,注意这里的self.age和self.name需要用逗号分隔开才能组成一个元组。 p = Person("Tom", 18) # p是一个Person对象,通过Person类创建,传入参数"Tom"和18作为name和age的值 p.say_hello() # p调用say_hello方法,打印问候语 age, name = p.get_age_and_name() # p调用get_age_and_name方法,获取年龄和姓名,分别赋值给变量age和name print(f"{name} is {age} years old.") # f-string格式化输出姓名和年龄信息 # 如果需要创建一个空的Person对象,可以不传入任何参数给Person类构造方法 person = Person() # person是一个空的Person对象,没有name和age属性 person.say_hello() # person调用say_hello方法会报错,因为没有name属性 person.get_age_and_name() # person调用get_age_and_name方法会报错,因为没有age属性 person = Person("Alice", 20) # person重新赋值为一个新的Person对象,传入参数"Alice"和20作为name和age的值 person.say_hello() # person调用say_hello方法打印问候语 person.get_age_and_name() # person调用get_age_and_name方法获取年龄和姓名,分别赋值给变量age和name print(f"{person.name} is {person.age} years old.") # f-string格式化输出姓名和年龄信息 # 如果需要在创建Person对象时指定初始值,可以在类构造方法中添加默认值 class PersonWithDefaultValues: def __init__(self, name="Unknown", age=0): super().__init__(name, age) def say_hello(self): print(f"Hello, my name is {self.name}!") def get_age_and_name(self): return self.age, self.name p = PersonWithDefaultValues() p.say_hello() p.get_age_and_name() print(f"{p.name} is {p.age} years old.") p = PersonWithDefaultValues("Tom", 18) p.say_hello() p.get_age_and_name() print(f"{p.name} is {p.age} years old.") p = PersonWithDefaultValues("Alice", -5) p.say_hello() p.get_age_and_name() print(f"{p.name} is {p.age} years old.") p = PersonWithDefaultValues("Bob", "twenty") p.say_hello() p.get_age_and_name() print(f"{p.name} is {p.age} years old.") p = PersonWithDefaultValues("Charlie", "thirty") p.say_hello() p.get_age_and_name() print(f"{p.name} is {p.age} years old.") p = PersonWithDefaultValues("David", True) p.say_hello() p.get_age_and_name() print(f"{p.name} is {p.age} years old.")} print(f"{person[0]} is {person[1]} years old."} # f-string格式化输出姓名和年龄信息 # 如果需要在创建Person对象时指定初始值,可以在类构造方法中添加默认值 class PersonWithDefaultValues: def


发表评论