网站首页 > 技术文章 正文
想学习python的朋友们,今天整理了一份python基础语法入门教程,新手友好,欢迎收藏。
1. 变量与数据类型
# 整数
x = 10
print(x) # 输出: 10
# 浮点数
y = 3.14
print(y) # 输出: 3.14
# 字符串
name = "Alice"
print(name) # 输出: Alice
# 布尔值
is_student = True
print(is_student) # 输出: True
2. 列表(List)
# 定义列表
fruits = ["apple", "banana", "cherry"]
print(fruits) # 输出: ['apple', 'banana', 'cherry']
# 添加元素到列表
fruits.append("orange")
print(fruits) # 输出: ['apple', 'banana', 'cherry', 'orange']
# 访问列表元素
print(fruits[1]) # 输出: banana
3. 元组(Tuple)
# 定义元组
coordinates = (10.0, 20.0)
print(coordinates) # 输出: (10.0, 20.0)
# 访问元组元素
print(coordinates[0]) # 输出: 10.0
4. 字典(Dictionary)
# 定义字典
person = {"name": "Alice", "age": 25}
print(person) # 输出: {'name': 'Alice', 'age': 25}
# 访问字典元素
print(person["name"]) # 输出: Alice
# 添加新键值对
person["city"] = "New York"
print(person) # 输出: {'name': 'Alice', 'age': 25, 'city': 'New York'}
5. 集合(Set)
# 定义集合
unique_numbers = {1, 2, 3, 4, 4, 5}
print(unique_numbers) # 输出: {1, 2, 3, 4, 5}
6. 条件语句(if-else)
# 定义一个变量
age = 18
# 使用if-else进行条件判断
if age >= 18:
print("You are an adult.")
else:
print("You are a minor.")
7. 循环(for和while)
# 使用for循环遍历列表
fruits = ["apple", "banana", "cherry"]
for fruit in fruits:
print(fruit)
# 使用while循环
count = 0
while count < 5:
print(count)
count += 1
8. 函数(Functions)
# 定义一个简单的函数
def greet(name):
return f"Hello, {name}!"
# 调用函数
message = greet("Alice")
print(message) # 输出: Hello, Alice!
9. 类(Classes)
# 定义一个简单的类
class Person:
def __init__(self, name, age):
self.name = name
self.age = age
def greet(self):
return f"Hello, my name is {self.name} and I am {self.age} years old."
# 创建类的实例
person = Person("Alice", 25)
print(person.greet()) # 输出: Hello, my name is Alice and I am 25 years old.
10. 文件操作(File Handling)
# 写入文件
with open("example.txt", "w") as file:
file.write("Hello, World!")
# 读取文件
with open("example.txt", "r") as file:
content = file.read()
print(content) # 输出: Hello, World!
这些示例展示了Python中一些更高级的功能,如条件语句、循环、函数、类和文件操作。通过练习这些代码,您可以进一步加深对Python语言的理解。如果您有任何问题或需要更多示例,请随时提问。
猜你喜欢
- 2024-12-27 零基础Python完全自学教程14:Python中的序列知识详解
- 2024-12-27 “零基础也能学会!Python 条件语句全攻略”
- 2024-12-27 零基础自学Python,简单实例,用if-elif-elif-else简洁代码
- 2024-12-27 零基础学Python——简单操作excel
- 2024-12-27 零基础自学Python,自动办公实例,同学你是哪个班的
- 2024-12-27 零基础可以学习Python吗?能不能学会
- 2024-12-27 「实用教程」零基础Python快速入门学习视频
- 2024-12-27 零基础自学Python,简单实例,阶乘(for循环版)
- 2024-12-27 py编程视频教程自学全套Python编程零基础入门到实战网络爬虫课程
- 2024-12-27 最完整的Python动画教程,7天零基础入门Python
- 05-25Python 3.14 t-string 要来了,它与 f-string 有何不同?
- 05-25Python基础元素语法总结
- 05-25Python中的变量是什么东西?
- 05-25新手常见的python报错及解决方案
- 05-2511-Python变量
- 05-2510个每个人都是需要知道Python问题
- 05-25Python编程:轻松掌握函数定义、类型及其参数传递方式
- 05-25Python基础语法
- 257℃Python短文,Python中的嵌套条件语句(六)
- 257℃python笔记:for循环嵌套。end=""的作用,图形打印
- 256℃PythonNet:实现Python与.Net代码相互调用!
- 251℃Python操作Sqlserver数据库(多库同时异步执行:增删改查)
- 251℃Python实现字符串小写转大写并写入文件
- 106℃原来2025是完美的平方年,一起探索六种平方的算吧
- 90℃Python 和 JavaScript 终于联姻了!PythonMonkey 要火?
- 81℃Ollama v0.4.5-v0.4.7 更新集合:Ollama Python 库改进、新模型支持
- 最近发表
- 标签列表
-
- python中类 (31)
- python 迭代 (34)
- python 小写 (35)
- python怎么输出 (33)
- python 日志 (35)
- python语音 (31)
- python 工程师 (34)
- python3 安装 (31)
- python音乐 (31)
- 安卓 python (32)
- python 小游戏 (32)
- python 安卓 (31)
- python聚类 (34)
- python向量 (31)
- python大全 (31)
- python次方 (33)
- python桌面 (32)
- python总结 (34)
- python浏览器 (32)
- python 请求 (32)
- python 前端 (32)
- python验证码 (33)
- python 题目 (32)
- python 文件写 (33)
- python中的用法 (32)