1. 变量和数据类型:
定义变量:
x = 5
name = "Alice"
常见数据类型:
- 整数(int): 1, -42
- 浮点数(float): 3.14, 2.0
- 字符串(str): "Hello", 'World'
- 布尔值(bool): True, False
2. 运算符:
算术运算符:
a = 5
b = 2
sum_result = a + b
difference = a - b
product = a * b
quotient = a / b
remainder = a % b
power = a ** b
比较运算符:
x = 5
y = 10
result = x > y # 结果为 False
逻辑运算符:
is_sunny = True
is_warm = False
if is_sunny and is_warm:
print("It's a sunny and warm day.")
3. 控制流结构:
条件语句:
x = 10
if x > 0:
print("Positive")
elif x == 0:
print("Zero")
else:
print("Negative")
循环语句:
- for 循环:
fruits = ["apple", "banana", "orange"]
for fruit in fruits:
print(fruit)
- while 循环:
count = 0
while count < 5:
print(count)
count += 1
4. 函数:
定义函数:
def greet(name):
return "Hello, " + name + "!"
result = greet("Alice")
print(result)
5. 列表(List):
numbers = [1, 2, 3, 4, 5]
fruits = ["apple", "orange", "banana"]
mixed_list = [1, "apple", True]
6. 字典(Dictionary):
person = {
"name": "John",
"age": 30,
"city": "New York"
}
print(person["name"]) # 输出 "John"
7. 元组(Tuple):
coordinates = (3, 4)
8. 输入输出:
输入:
name = input("Enter your name: ")
print("Hello, " + name + "!")
输出:
age = 25
print("I am", age, "years old.")
这只是 Python3 基础语法的简要概览。你可以根据需要深入学习更多的概念,例如面向对象编程、异常处理、模块和包等。建议使用官方文档或其他在线教程进行更深入的学习。
转载请注明出处:http://www.pingtaimeng.com/article/detail/13252/Python3