Python的基础语法

  1. 注释:在Python中,使用井号(#)表示单行注释,三个单引号(''')或三个双引号(""")表示多行注释。

  2. 变量:在Python中,不需要声明变量类型,直接赋值即可。例如:

a = 10

b = "Hello, World!"

  1. 数据类型:Python有多种数据类型,如整数(int)、浮点数(float)、字符串(str)、列表(list)、元组(tuple)、集合(set)和字典(dict)。

  2. 条件语句:使用if、elif和else关键字进行条件判断。例如:

age = 18

if age >= 18:

print("成年")

else:

print("未成年")

  1. 循环语句:Python中有两种循环语句,分别是for循环和while循环。例如:

for循环

for i in range(5):

print(i)

while循环

count = 0

while count < 5:

print(count)

count += 1

  1. 函数:使用def关键字定义函数。例如:

def add(a, b):

return a + b

result = add(1, 2)

print(result)

  1. 类和对象:使用class关键字定义类,通过类创建对象。例如:

class Person:

def init(self, name, age):

self.name = name

self.age = age

def say_hello(self):

print(f"Hello, my name is {self.name} and I am {self.age} years old.")

person = Person("Tom", 30)

person.say_hello()

相关推荐
向上的车轮1 天前
基于go语言的云原生TodoList Demo 项目,验证云原生核心特性
开发语言·云原生·golang
The Chosen One9851 天前
C++ : AVL树-详解
开发语言·c++
PH_modest1 天前
【Qt跬步积累】—— 初识Qt
开发语言·qt
怀旧,1 天前
【C++】18. 红⿊树实现
开发语言·c++
多恩Stone1 天前
【3DV 进阶-2】Hunyuan3D2.1 训练代码详细理解下-数据读取流程
人工智能·python·算法·3d·aigc
xiaopengbc1 天前
在 Python 中实现观察者模式的具体步骤是什么?
开发语言·python·观察者模式
Python大数据分析@1 天前
python用selenium怎么规避检测?
开发语言·python·selenium·网络爬虫
ThreeAu.1 天前
Miniconda3搭建Selenium的python虚拟环境全攻略
开发语言·python·selenium·minicoda·python环境配置
偷心伊普西隆1 天前
Python EXCEL 理论探究:格式转换时处理缺失值方法
python·excel