Python快速入门教程1:基本语法、数据类型、运算符、数字字符串
wptr33 2025-06-23 22:40 2 浏览
Python3的基础教程,涵盖了基本语法、数据类型、类型转换、解释器、注释、运算符、数字和字符串等内容,并附有使用实例场景。Python3的基础教程,涵盖了基本语法、数据类型、类型转换、解释器、注释、运算符、数字和字符串等内容,并附有使用实例场景。通过实例场景,让你快速掌握Python编程,轻松上手,即刻开启编程之旅,拿起键盘就能实战演练。
一、Python3 基本语法
1. 缩进
Python使用缩进来表示代码块,通常使用4个空格作为一级缩进。
if True:
print("Hello, World!")
2. 行与缩进
每行语句以换行符结束。多行语句可以用反斜杠\连接。
total = 1 + \
2 + \
3
print(total) # 输出 6
3. 多条语句在同一行
可以使用分号;将多条语句放在同一行。
x = 5; y = 10; print(x + y) # 输出 15
二、Python3 基本数据类型
1. 数字(Number)
- 整数(int)
- 浮点数(float)
- 复数(complex)
integer_value = 42
float_value = 3.14
complex_value = 1 + 2j
print(type(integer_value)) # <class 'int'>
print(type(float_value)) # <class 'float'>
print(type(complex_value)) # <class 'complex'>
2. 字符串(String)
single_quote_string = 'Hello'
double_quote_string = "World"
multi_line_string = """This is a
multi-line string."""
print(single_quote_string) # 输出 Hello
print(double_quote_string) # 输出 World
print(multi_line_string)
3. 列表(List)
fruits = ['apple', 'banana', 'orange']
print(fruits[0]) # 输出 apple
4. 元组(Tuple)
coordinates = (10, 20)
print(coordinates[0]) # 输出 10
5. 字典(Dictionary)
person = {'name': 'Alice', 'age': 25}
print(person['name']) # 输出 Alice
6. 集合(Set)
unique_numbers = {1, 2, 3, 4, 5}
print(unique_numbers) # 输出 {1, 2, 3, 4, 5}
三、Python3 数据类型转换
1. 显式转换
# 整数转浮点数
num_int = 10
num_float = float(num_int)
print(num_float) # 输出 10.0
# 浮点数转整数
num_float = 10.5
num_int = int(num_float)
print(num_int) # 输出 10
# 字符串转整数或浮点数
str_num = "123"
num_int = int(str_num)
num_float = float(str_num)
print(num_int, num_float) # 输出 123 123.0
# 列表转集合
list_items = [1, 2, 2, 3, 4]
set_items = set(list_items)
print(set_items) # 输出 {1, 2, 3, 4}
2. 隐式转换
result = 10 + 3.5 # 自动将整数转换为浮点数
print(result) # 输出 13.5
四、Python3 解释器
交互式解释器
可以在命令行中输入python进入交互式解释器,直接执行Python代码。
$ python
>>> print("Hello, World!")
Hello, World!
>>> exit()
脚本文件
编写Python脚本并保存为.py文件,然后通过命令行运行。
$ python script.py
五、Python3 注释
单行注释
使用#符号进行单行注释。
# 这是一个单行注释
print("Hello, World!") # 输出 Hello, World!
多行注释
使用三个引号'''或"""包裹多行注释。
"""
这是一个多行注释。
可以跨越多行。
"""
print("Hello, World!")
六、Python3 运算符
1. 算术运算符
运算符 | 描述 | 示例 |
+ | 加法 | a + b |
- | 减法 | a - b |
* | 乘法 | a * b |
/ | 除法 | a / b |
// | 整除 | a // b |
% | 取模 | a % b |
** | 幂 | a ** b |
a = 10
b = 3
print(a + b) # 输出 13
print(a - b) # 输出 7
print(a * b) # 输出 30
print(a / b) # 输出 3.3333333333333335
print(a // b) # 输出 3
print(a % b) # 输出 1
print(a ** b) # 输出 1000
2. 比较运算符
运算符 | 描述 | 示例 |
== | 等于 | a == b |
!= | 不等于 | a != b |
> | 大于 | a > b |
< | 小于 | a < b |
>= | 大于等于 | a >= b |
<= | 小于等于 | a <= b |
a = 10
b = 5
print(a == b) # 输出 False
print(a != b) # 输出 True
print(a > b) # 输出 True
print(a < b) # 输出 False
print(a >= b) # 输出 True
print(a <= b) # 输出 False
3. 逻辑运算符
运算符 | 描述 | 示例 |
and | 逻辑与 | a and b |
or | 逻辑或 | a or b |
not | 逻辑非 | not a |
x = True
y = False
print(x and y) # 输出 False
print(x or y) # 输出 True
print(not x) # 输出 False
4. 成员运算符
运算符 | 描述 | 示例 |
in | 如果在序列中返回True | a in b |
not in | 如果不在序列中返回True | a not in b |
fruits = ['apple', 'banana', 'orange']
print('apple' in fruits) # 输出 True
print('grape' not in fruits) # 输出 True
5. 身份运算符
运算符 | 描述 | 示例 |
is | 如果两个变量是同一个对象返回True | a is b |
is not | 如果两个变量不是同一个对象返回True | a is not b |
a = [1, 2, 3]
b = [1, 2, 3]
c = a
print(a is c) # 输出 True
print(a is b) # 输出 False
print(a == b) # 输出 True
七、Python3 数字(Number)
1. 整数(int)
x = 10
print(type(x)) # <class 'int'>
2. 浮点数(float)
y = 3.14
print(type(y)) # <class 'float'>
3. 复数(complex)
z = 1 + 2j
print(type(z)) # <class 'complex'>
4. 数学函数和常量
Python的math模块提供了丰富的数学函数和常量。
import math
print(math.pi) # 输出 3.141592653589793
print(math.sqrt(16)) # 输出 4.0
八、Python3 字符串(String)
1. 创建字符串
single_quote_string = 'Hello'
double_quote_string = "World"
multi_line_string = """This is a
multi-line string."""
2. 字符串操作
访问字符
greeting = "Hello, World!"
print(greeting[0]) # 输出 H
print(greeting[-1]) # 输出 !
切片
greeting = "Hello, World!"
print(greeting[0:5]) # 输出 Hello
print(greeting[7:]) # 输出 World!
修改和连接
greeting = "Hello"
name = "Alice"
message = greeting + ", " + name + "!"
print(message) # 输出 Hello, Alice!
格式化字符串
name = "Alice"
age = 25
formatted_message = f"Name: {name}, Age: {age}"
print(formatted_message) # 输出 Name: Alice, Age: 25
字符串方法
text = "hello world"
print(text.upper()) # 输出 HELLO WORLD
print(text.lower()) # 输出 hello world
print(text.capitalize()) # 输出 Hello world
print(text.replace("world", "Python")) # 输出 hello Python
print(len(text)) # 输出 11
未完待续,加入知识星球, 领取完整资料
相关推荐
- 十年之重修Redis原理(redis重试机制)
-
弱小和无知并不是生存的障碍,傲慢才是。--------面试者...
- Redis 中ZSET数据类型命令使用及对应场景总结
-
1.zadd添加元素zaddkeyscoremember...
- redis总结(redis常用)
-
RedisTemplate封装的工具类packagehk.com.easyview.common.helper;importcom.alibaba.fastjson.JSONObject;...
- 配置热更新系统(如何实现热更新)
-
整体设计概览┌────────────┐┌────────────────┐┌────────────┐│配置后台服务│--写入-->│Red...
- java高级用法之:调用本地方法的利器JNA
-
简介JAVA是可以调用本地方法的,官方提供的调用方式叫做JNI,全称叫做javanativeinterface。要想使用JNI,我们需要在JAVA代码中定义native方法,然后通过javah命令...
- SpringBoot:如何优雅地进行响应数据封装、异常处理
-
背景越来越多的项目开始基于前后端分离的模式进行开发,这对后端接口的报文格式便有了一定的要求。通常,我们会采用JSON格式作为前后端交换数据格式,从而减少沟通成本等。...
- Java中有了基本类型为什么还要有包装类型(封装类型)
-
Java中基本数据类型与包装类型有:...
- java面向对象三大特性:封装、继承、多态——举例说明(转载)
-
概念封装:封装就是将客观的事物抽象成类,类中存在属于这个类的属性和方法。...
- java 面向对象编程:封装、继承、多态
-
Java中的封装(Encapsulation)、继承(Inheritance)和多态(Polymorphism)是面向对象编程的三大基本概念。它们有助于提高代码的可重用性、可扩展性和可维护性。...
- 怎样解析java中的封装(怎样解析java中的封装文件)
-
1.解析java中的封装1.1以生活中的例子为例,打开电视机的时候你只需要按下开关键,电视机就会打开,我们通过这个操作我们可以去间接的对电视机里面的元器件进行亮屏和显示界面操作,具体怎么实现我们并不...
- python 示例代码(python代码详解)
-
以下是35个python代码示例,涵盖了从基础到高级的各种应用场景。这些示例旨在帮助你学习和理解python编程的各个方面。1.Hello,World!#python...
- python 进阶突破——内置模块(Standard Library)
-
Python提供了丰富的内置模块(StandardLibrary),无需安装即可直接使用。以下是一些常用的内置模块及其主要功能:1.文件与系统操作...
- Python程序员如何调试和分析Python脚本程序?附代码实现
-
调试和分析Python脚本程序调试技术和分析技术在Python开发中发挥着重要作用。调试器可以设置条件断点,帮助程序员分析所有代码。而分析器可以运行程序,并提供运行时的详细信息,同时也能找出程序中的性...
- python中,函数和方法异同点(python方法和函数的区别)
-
在Python中,函数(Function)...
- Python入门基础命令详解(python基础入门教程)
-
以下是Python基本命令的详解指南,专为初学者设计,涵盖基础语法、常用操作和实用示例:Python基本命令详解:入门必备指南1.Python简介特点:简洁易读、跨平台、丰富的库支持...
- 一周热门
-
-
C# 13 和 .NET 9 全知道 :13 使用 ASP.NET Core 构建网站 (1)
-
因果推断Matching方式实现代码 因果推断模型
-
git pull命令使用实例 git pull--rebase
-
面试官:git pull是哪两个指令的组合?
-
git 执行pull错误如何撤销 git pull fail
-
git fetch 和git pull 的异同 git中fetch和pull的区别
-
git pull 和git fetch 命令分别有什么作用?二者有什么区别?
-
git pull 之后本地代码被覆盖 解决方案
-
还可以这样玩?Git基本原理及各种骚操作,涨知识了
-
git命令之pull git.pull
-
- 最近发表
- 标签列表
-
- git pull (33)
- git fetch (35)
- mysql insert (35)
- mysql distinct (37)
- concat_ws (36)
- java continue (36)
- jenkins官网 (37)
- mysql 子查询 (37)
- python元组 (33)
- mybatis 分页 (35)
- vba split (37)
- redis watch (34)
- python list sort (37)
- nvarchar2 (34)
- mysql not null (36)
- hmset (35)
- python telnet (35)
- python readlines() 方法 (36)
- munmap (35)
- docker network create (35)
- redis 集合 (37)
- python sftp (37)
- setpriority (34)
- c语言 switch (34)
- git commit (34)