example
example

Python Python 语法

  • 主题发起人 主题发起人 Yann
  • 开始时间 开始时间

Yann

站长
管理成员
GTAOS管理组
认证卖家
认证用户
黄金
192.55G
1.变量声明和使用:

Python:
name = "John"
age = 25
print("My name is", name, "and I am", age, "years old.")

2.条件语句(if-else):
Python:
score = 85
if score >= 60:
    print("You passed the exam!")
else:
    print("You failed the exam.")

3.循环语句(for):
Python:
for i in range(1, 6):
   print("Number:", i)

4.列表(List)声明和访问:
Python:
fruits = ["apple", "banana", "orange"]
print(fruits[0])  # 输出 "apple"

5.函数声明和调用:
Python:
def greet(name):
   print("Hello,", name)

greet("Alice")  # 输出 "Hello, Alice!"

6.类声明和实例化:
Python:
class Person:
    def __init__(self, name):
        self.name = name

   def greet(self):
       print("Hello, my name is", self.name)

person = Person("John")
person.greet() # 输出 "Hello, my name is John!"


7.文件导入和模块使用:
Python:
# 文件1.py
variable = "Hello from file 1!"

# 文件2.py
from 1 import variable
print(variable)  # 输出 "Hello from file 1!"


8.异常处理:
Python:
def divide(numerator, denominator):
    if denominator == 0:
        raise Exception("Division by zero error!")
    return numerator / denominator

try:
   print(divide(10, 0))
except Exception as e:
   print("Error:", str(e))
 
最后编辑:
后退
顶部