一、Python简介
Python是一种高级编程语言,由荷兰人Guido van Rossum于1989年发明,第一个公开发行版发行于1991年,Python的设计哲学是“优雅”、“明确”、“简单”,Python开发者的哲学是“用一种方法,最好是只有一种方法来做一件事”,也因此它和其他语言相比,Python更注重可读性和一致性。
二、Python环境搭建
1、下载Python安装包
访问Python官网()下载适合自己操作系统的Python安装包。
2、安装Python
双击下载好的安装包,按照提示进行安装,在安装过程中,建议勾选“Add Python to PATH”选项,将Python添加到系统环境变量中。
3、验证Python安装
打开命令提示符(Windows)或终端(Mac/Linux),输入以下命令:
python --version
如果显示出Python的版本号,说明Python已经成功安装。
三、Python基础语法
1、注释
在Python中,使用井号(#)表示单行注释,三个单引号(''')或三个双引号(""")表示多行注释。
# 这是一个单行注释 ''' 这是一个多行注释 '''
2、变量和数据类型
Python中有多种数据类型,如整数(int)、浮点数(float)、字符串(str)、列表(list)、元组(tuple)、字典(dict)等。
a = 10 # 整数 b = 3.14 # 浮点数 c = "Hello" # 字符串 d = [1, 2, 3] # 列表 e = (1, 2, 3) # 元组 f = {"name": "Tom", "age": 18} # 字典
3、运算符
Python支持多种运算符,如算术运算符(+、-、*、/、%、**、//)、比较运算符(==、!=、>、<、>=、<=)、逻辑运算符(and、or、not)等。
a = 10 + 5 # 算术运算符 b = a > c # 比较运算符 c = a and b # 逻辑运算符
4、控制结构
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、函数定义和调用
在Python中,使用def关键字定义函数,通过函数名加括号的方式调用函数,函数可以有参数和返回值。
def add(a, b): # 定义函数add,接收两个参数a和b return a + b # 返回a和b的和 result = add(10, 20) # 调用函数add,传入参数10和20,将返回值赋给result变量 print(result) # 输出结果30
2、模块导入和使用
Python提供了丰富的标准库和第三方库,可以通过import关键字导入模块,然后使用模块中的函数和类,导入math模块使用其中的sqrt函数计算平方根。
import math # 导入math模块 result = math.sqrt(4) # 使用math模块中的sqrt函数计算4的平方根,结果为2.0,赋值给result变量并输出结果2.0
五、面向对象编程(OOP)基础
1、类和对象的定义和使用
在Python中,使用class关键字定义类,通过类名加括号的方式创建对象,类中可以定义属性和方法,属性是类的变量,方法是类的函数,通过对象名加属性名或方法名的方式访问属性或调用方法。
class Person: # 定义一个Person类,包含属性name和age,以及方法say_hello和get_age_of_persons_older_than_me()。注意方法名前需要加self表示实例对象自身。 class Person: def __init__(self, name, age): # __init__方法称为构造方法,用于初始化对象的属性 self.name = name # self表示实例对象自身,name表示属性名,age表示传入的参数 self.age = age def say_hello(self): # say_hello方法用于打印问候语 print("Hello, my name is", self.name)def get_age_of_persons_older_than_me(self, persons): # get_age_of_persons_older_than_me方法用于计算比自己年龄大的人的年龄总和 total_age = sum([person.age for person in persons if person.age > self.age]) return total_age persons = [Person("Tom", 18), Person("Jerry", 20)] # 创建一个Person对象的列表 for person in persons: # 遍历Person对象列表 person.say_hello() # 调用每个Person对象的say_hello方法 print("Total age of persons older than me:", persons[0].get_age_of_persons_older_than_me(persons[1:])) # 调用第一个Person对象的get_age_of_persons_older_than_me方法,传入除第一个Person对象之外的其他Person对象作为参数,计算年龄总和并输出结果
class Person: def __init__(self, name, age): self.name = name self.age = agedef say_hello(self): print("Hello, my name is", self.name)def get_age_of_persons_older_than_me(self, persons): total_age = sum([person.age for person in persons if person.age > self.age]) return total_age persons = [Person("Tom", 18), Person("Jerry", 20)] for person in persons: person.say_hello() print("Total age of persons older than me:", persons[0].get_age_of_persons_older_than_me(persons[1:]))
class Person: def __init__(self, name, age): self.name = name self.age = age def say_hello(self): print("Hello, my name is", self.name) def get_age_of_persons_older_than_me(self, persons): total_age = sum([person.age for person in persons if person.age > self.age]) return total_age persons = [Person("Tom", 18), Person("Jerry", 20)] for person in persons: person.say_hello() print("Total age of persons older than me:", persons[0].get_age_of_persons_older_than_me(persons[1:]))
class Person: def __init__(self, name, age): self.name = name self.age = age def say_hello(self): print("Hello, my name is", self.name) def get_age
还没有评论,来说两句吧...