Python教学入门:数字类型与字符串

在这里插入图片描述

字符串元素组成的序列

在这里插入图片描述
字符串元素组成的序列指的是字符串中的每个字符按照一定的顺序排列形成的序列。在 Python 中,字符串是由字符组成的有序序列(Sequence),每个字符在字符串中有其固定的位置(索引),并且可以通过索引来访问字符串中的每个字符。

例如,字符串 “hello” 中的每个字符 ‘h’、‘e’、‘l’、‘l’、‘o’ 就组成了一个序列,它们按照顺序排列在一起。在 Python 中,可以通过索引来访问字符串中的每个字符,索引从 0 开始,表示第一个字符,依次递增。

下面是一个示例:

my_string = "hello"

# 访问字符串中的每个字符
print(my_string[0])  # 输出:h
print(my_string[1])  # 输出:e
print(my_string[2])  # 输出:l
print(my_string[3])  # 输出:l
print(my_string[4])  # 输出:o

数字类型(Numeric Types):

整数(int):
在 Python 中,整数是不可变的,可以表示正整数、负整数和零。
Python 中整数的大小仅受限于计算机的内存。

# 定义整数变量
num1 = 10
num2 = -5
num3 = 0

# 输出整数变量
print(num1)  # 输出:10
print(num2)  # 输出:-5
print(num3)  # 输出:0

# 整数运算
result = num1 + num2
print(result)  # 输出:5

浮点数(float):
浮点数用于表示带有小数点的数字。
浮点数在计算机中是以近似值存储的,因此可能存在精度问题。

# 定义浮点数变量
num1 = 3.14
num2 = -2.5

# 输出浮点数变量
print(num1)  # 输出:3.14
print(num2)  # 输出:-2.5

# 浮点数运算
result = num1 * num2
print(result)  # 输出:-7.85

复数(complex):
复数是由实部和虚部构成的数,形式为 a + bj,其中 a 是实部,b 是虚部,j 是虚数单位(在 Python 中使用 j 表示)。
复数由实部和虚部两部分组成,实部表示实数部分,虚部表示虚数部分。
在 Python 中,可以使用 complex() 函数来创建复数,也可以直接使用实部和虚部的形式表示。
复数支持加减乘除等基本运算,同时也支持求模、取共轭等操作。
两个复数的共轭复数是指虚部符号相反的复数。对于复数 z = a + bj,其共轭复数为 z* = a - bj。

# 定义复数变量
num1 = 2 + 3j
num2 = 1 - 2j

# 输出复数变量
print(num1)  # 输出:(2+3j)
print(num2)  # 输出:(1-2j)

# 复数运算
result = num1 * num2
print(result)  # 输出:(8+1j)

布尔类型(Boolean):
布尔类型表示逻辑值,只有两个取值:True(真)和False(假)。在 Python 中,布尔类型用来表示条件的真假。

a = True
b = False

# 与运算
result_and = a and b
print(result_and)  # 输出:False

# 或运算
result_or = a or b
print(result_or)   # 输出:True

# 非运算
result_not = not a
print(result_not)  # 输出:False

字符串(Strings):

字符串的表示:
在 Python 中,字符串可以使用单引号、双引号或三引号表示。
例如:‘hello’、“world”、‘’‘Hello, world!’‘’。
字符串的表示和基本操作:

# 定义字符串变量
str1 = 'hello'
str2 = "world"
str3 = '''Hello, world!'''

# 输出字符串变量
print(str1)  # 输出:hello
print(str2)  # 输出:world
print(str3)  # 输出:Hello, world!

# 字符串连接
result = str1 + ' ' + str2
print(result)  # 输出:hello world

# 字符串重复
repeated_str = str1 * 3
print(repeated_str)  # 输出:hellohellohello

# 字符串格式化
name = 'Alice'
age = 30
formatted_str = 'My name is %s and I am %d years old.' % (name, age)
print(formatted_str)  # 输出:My name is Alice and I am 30 years old.

字符串的索引和切片:

# 字符串索引
my_string = 'Python'
print(my_string[0])  # 输出:P
print(my_string[-1])  # 输出:n

# 字符串切片
print(my_string[1:4])  # 输出:yth
print(my_string[:3])   # 输出:Pyt
print(my_string[3:])   # 输出:hon

字符串方法的使用:

# 字符串方法示例
my_string = '   hello world   '

# 去除字符串两端的空格
trimmed_str = my_string.strip()
print(trimmed_str)  # 输出:hello world

# 将字符串转换为大写
upper_str = my_string.upper()
print(upper_str)    # 输出:   HELLO WORLD

# 将字符串转换为小写
lower_str = my_string.lower()
print(lower_str)    # 输出:   hello world

# 字符串分割
words = my_string.strip().split()
print(words)        # 输出:['hello', 'world']

字符串的格式化输出的方式
使用 % 运算符进行格式化:这是传统的字符串格式化方法,通过在字符串中使用 % 运算符和格式化说明符来指定要插入的变量和格式。

name = "Alice"
age = 30
formatted_str = "My name is %s and I am %d years old." % (name, age)
print(formatted_str)  # 输出:My name is Alice and I am 30 years old.

使用 str.format() 方法进行格式化:这是一种更加灵活的格式化方法,通过在字符串中使用占位符 {} 并调用 format() 方法来填充变量。

name = "Bob"
age = 25
formatted_str = "My name is {} and I am {} years old.".format(name, age)
print(formatted_str)  # 输出:My name is Bob and I am 25 years old.

使用 f-string 进行格式化:f-string 是 Python 3.6 引入的一种新的字符串格式化方法,通过在字符串前加上 f 或 F 前缀,并在字符串中使用大括号 {} 来表示变量。

name = "Charlie"
age = 35
formatted_str = f"My name is {name} and I am {age} years old."
print(formatted_str)  # 输出:My name is Charlie and I am 35 years old.

python中的占位符

在 Python 中,占位符是指用于表示待填充数据的特殊符号或字符串,它在字符串中留出一个位置,用来动态地填入变量或值。在字符串格式化中,占位符通常以特定的格式表示,例如 %s、{}、{} 等。
常见的 Python 中的占位符有:
%s:用于插入字符串。
%d:用于插入整数。
%o:用于插入整数,并以八进制格式显示。
%x:用于插入整数,并以十六进制格式显示(小写字母)。
%X:用于插入整数,并以十六进制格式显示(大写字母)。
%e:用于插入浮点数,并以科学计数法格式显示(小写字母)。
%f:用于插入浮点数。
在这里插入图片描述

name = "Alice"
age = 30
num = 255
float_num = 3.1415926

formatted_str = "My name is %s, I am %d years old. Number in octal: %o, Number in hex: %x, Number in HEX: %X. Float number in scientific notation: %e, Float number: %f" % (name, age, num, num, num, float_num, float_num)
print(formatted_str)

字符串的拼接,切割,替换
在这里插入图片描述
字符串的拼接(Concatenation):
字符串拼接是将多个字符串连接在一起形成一个新的字符串的操作。

# 使用加号(+)进行字符串拼接
str1 = "Hello"
str2 = "World"
result = str1 + ", " + str2
print(result)  # 输出:Hello, World

字符串的切割(Splitting):
字符串切割是将一个字符串按照指定的分隔符拆分成多个子字符串的操作。
在这里插入图片描述

# 使用 split() 方法进行字符串切割
my_string = "apple,banana,orange"
result = my_string.split(",")
print(result)  # 输出:['apple', 'banana', 'orange']

字符串的替换(Replacement):
在这里插入图片描述

# 使用 replace() 方法进行字符串替换
my_string = "I like apples"
new_string = my_string.replace("apples", "bananas")
print(new_string)  # 输出:I like bananas

去除两侧空格的方法
在这里插入图片描述
使用 strip() 方法:

my_string = "   Hello, World!   "
trimmed_string = my_string.strip()
print(trimmed_string)  # 输出:Hello, World!

使用 lstrip() 和 rstrip() 方法:
lstrip() 方法用于去除字符串左侧的空格,rstrip() 方法用于去除字符串右侧的空格。

my_string = "   Hello, World!   "
left_trimmed_string = my_string.lstrip()
right_trimmed_string = my_string.rstrip()
print(left_trimmed_string)  # 输出:Hello, World!   
print(right_trimmed_string)  # 输出:   Hello, World!

使用 split() 方法和 join() 方法:
先使用 split() 方法将字符串分割成多个部分,然后使用 join() 方法将非空部分重新组合成字符串。

my_string = "   Hello, World!   "
trimmed_string = ' '.join(my_string.split())
print(trimmed_string)  # 输出:Hello, World!

在 Python 中,常见的算术运算符包括:
在这里插入图片描述

加法运算符(+):用于将两个数相加或连接两个字符串。

result = 10 + 5
print(result)  # 输出:15

str_result = "Hello, " + "World!"
print(str_result)  # 输出:Hello, World!

减法运算符(-):用于将一个数减去另一个数。

result = 10 - 5
print(result)  # 输出:5

乘法运算符(*):用于将两个数相乘或重复一个字符串多次。

result = 10 * 5
print(result)  # 输出:50

str_result = "Hello" * 3
print(str_result)  # 输出:HelloHelloHello

除法运算符(/):用于将一个数除以另一个数,结果为浮点数。

result = 10 / 5
print(result)  # 输出:2.0

整除运算符(//):用于将一个数除以另一个数,结果为整数(向下取整)。

result = 10 // 3
print(result)  # 输出:3

取模运算符(%):用于计算两个数相除的余数。

result = 10 % 3
print(result)  # 输出:1

幂运算符()**:用于计算一个数的指数。

result = 2 ** 3
print(result)  # 输出:8

在 Python 中,常见的比较运算符用于比较两个值的大小或判断两个值是否相等。下面是常见的比较运算符:
在这里插入图片描述
等于(==):检查两个值是否相等。

result = (10 == 5)  # False

不等于(!=):检查两个值是否不相等。

result = (10 != 5)  # True

大于(>):检查左边的值是否大于右边的值。

result = (10 > 5)  # True

小于(<):检查左边的值是否小于右边的值。

result = (10 < 5)  # False

大于等于(>=):检查左边的值是否大于或等于右边的值。

result = (10 >= 5)  # True

小于等于(<=):检查左边的值是否小于或等于右边的值。

result = (10 <= 5)  # False

  • 8
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

KennySKwan

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值