学Python的第一天

# 这是一行注释
"""
这是多行注释
这是多行注释
这是多行注释
"""

'''
这也是多行注释
这也是多行注释
这也是多行注释
'''

# 声明变量
MyNum = 1
myNumber = 1
_Number = 1
_number = 1
the_name = "name"

# 赋值
# 整型
a = 1
# 浮点型
b = 1.2
# 字符串型
c = "world"
d = 'world'
# 布尔型
e = False
f = True

# 四种复合数据类型
# 列表型
aArray = [1, 2, 3, 4]

# 集合
bList = {1, 2, 3, 4}

# 元组
cTuple = (1, 2, 3, 4)

# 字典
dDict = {'n': 1, 'm': 2}

# 输出一个变量的值
print(f"hello {c}")

import random

# 获得一个从1到100的随机数,整型,并且包含1和100
aR = random.randint(1, 100)
# 获得一个从1到100的随机数,小数,
bR = random.uniform(1, 100)
# 如果想直接获得从0~1之间的一个小数
cR = random.random()
print("从1到100的随机数,包含1和100:", aR)
print("获得一个从1到100的随机数,小数:", bR)
print("获得从0~1之间的一个小数", cR)

# 元祖元组是 Python 中的一种不可变序列类型,与列表类似,
# 但其元素一旦创建便无法修改。元组使用小括号 () 定义,
# 元素之间用逗号分隔。由于其不可变性,元组具有可哈希性,可以作为字典的键。
# 元组虽然不可变,但支持多种操作,如连接、复制、查找等
# 不可变性:元组的元素无法修改,但可以包含可变对象(如列表)。
# 多值赋值:元组支持将多个值同时赋给多个变量。
# 高效性:由于不可变,元组在内存和性能上比列表更优
tup1 = (1, 2, 3)
tup2 = (4, 5, 6)
# 元组连接与复制
print("元组的连接", tup1 + tup2)
print("元组的复制", tup1 * 2)
# 元组查找
tup3 = (10, 20, 30, 40, 20)
# 输出:1,首次出现的索引
print("首次出现的索引:", tup3.index(20))
# 输出:2,出现次数
print("元素20出现次数:", tup3.count(20))

# 多值赋值
x, y = (1, 2)
print(x, y)


# 函数返回多个值
def get_coordinates():
    return 10, 20


# 输出(10, 20)
coords = get_coordinates()
print("函数返回多个值:", coords)

# 元组的内置函数
# 返回元组长度
print("返回元组长度:", len(tup1))
# 返回元组中最大值和最小值
print("返回元组中最大值和最小值:", max(tup2), min(tup3))

# 将序列转换为元组
tuple(tup3)
# 五种序列的创建
s = "hello"  # 字符串 String -> 单引号/双引号/三引号
lst = [1, 2, 3]  # 列表 List -> 方括号 [] 可变序列
tup = (1, 2, 3)  # 元组 Tuple -> 圆括号 () 不可变序列
rng = range(1, 5)  # range 对象 -> range() 函数 表示一个整数序列,使用 range(start, stop[, step]) 创建
r = range(0, 10, 2)  # 0,2,4,6,8      也就是0到10,从0开始,隔2个生成一个
st = {1, 2, 3}  # 集合 Set -> 花括号 {}  无序不重复元素集合,使用花括号 {} 或 set() 创建
#  空列表 []、空元组 ()、空集合 set()、空字符串 "" 都是合法的空序列。
#
#  集合 {} 在 Python 中默认是空字典,要创建空集合需用 set()。
#
#  除集合外,其余四种都是有序可索引的

# 数学运算
# 如果一个整型经过运算后是小数,那么结果类型会自动变为浮点型
print("5÷2=", 5 / 2)

# 两个星号表示开多少次方
print("2的5次方 =", 2 ** 5)

# 布尔的三种运算 NOT AND OR
aBool = False
bBool = not aBool
print("aBool:", aBool)
print("aBool的not:", bBool)

# 字符串运算,或者可以看作是拼接
s1 = "O"
s2 = "H"
print("OH:", s1 + s2)
print("HH:", s2 * 2)
print("OHH:", s1 + s2 * 3)

# 求余运算%
print("10%2=", 10 % 2)

# 判断是否为余数
aNum = 11
bNum = a % 2
c = b == 0
if c:
    print("偶数")
else:
    print("不是偶数")

# aInt = int(input("请输入你的分数"))
# if aInt < 0 or aInt > 100:
#     print("请输入合法分数")
# elif aInt < 60:
#     print("不合格")
# elif aInt < 80:
#     print("合格")
# else:
#     print("优秀")


# for循环遍历
for i in [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]:
    print("for循环遍历数组:", i)

for i in "ABCDEFGHIJKLMNO":
    print("for循环遍历字符串", i)

for i in (0, 1, 2, 3, 4, 5, 6, 7, 8, 9):
    print("for循环遍历元组", i)

for i in {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}:
    print("for循环遍历集合", i)

# 字符串切片,包含左边却不包含右边
aString = "my name is xxxx"
print("输出my name is xxxx从[1:5]的内容:", aString[1:5])
print("输出my name is xxxx从[:5]的内容:", aString[:5])
print("输出my name is xxxx从[1:]的内容:", aString[1:])
print("替换了字符串中的XXX,将其替换为NP:", aString.replace("xxxx", "NP"))
print("将my name is xxxx进行分割,生成一个链表", aString.split(" "))
aString1 = "-".join(aString.split(" "))
print("my name is xxxx分割后的链表重新拼合为一个字符串", aString1)
print("将hello第一个字母转换成大写", "hello".capitalize())
print("将hello world每个单词的首字母转换成大写", "hello world".title())
print("将HELLO WORLD中大写转小写", "HELLO WORLD".lower())
print("将HELLO WORLD中小写转大写", "hello world".upper())

# 删除空白字符
print("删除  hello  左侧空白字符", "  hello  ".lstrip())
print("删除  hello  右侧空白字符", "  hello  ".rstrip())
print("删除  hello  两侧空白字符", "  hello  ".strip())

array = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
print("获取 [[1, 2, 3], [4, 5, 6], [7, 8, 9]] 里的第一组元素", array[0])
print("获取 [[1, 2, 3], [4, 5, 6], [7, 8, 9]] 里的第二组的第三个元素", array[1][2])

 

运行结果:

 1 D:\Coding\PythonDemo\pythonProject\.venv\Scripts\python.exe D:\Coding\PythonDemo\pythonProject\LeetCode\test.py 
 2 hello world
 3 从1到100的随机数,包含1和100: 44
 4 获得一个从1到100的随机数,小数: 51.6605857247243
 5 获得从0~1之间的一个小数 0.21285018432779446
 6 元组的连接 (1, 2, 3, 4, 5, 6)
 7 元组的复制 (1, 2, 3, 1, 2, 3)
 8 首次出现的索引: 1
 9 元素20出现次数: 2
10 1 2
11 函数返回多个值: (10, 20)
12 返回元组长度: 3
13 返回元组中最大值和最小值: 6 10
14 5÷2= 2.5
15 2的5次方 = 32
16 aBool: False
17 aBool的not: True
18 OH: OH
19 HH: HH
20 OHH: OHHH
21 10%2= 0
22 不是偶数
23 for循环遍历数组: 0
24 for循环遍历数组: 1
25 for循环遍历数组: 2
26 for循环遍历数组: 3
27 for循环遍历数组: 4
28 for循环遍历数组: 5
29 for循环遍历数组: 6
30 for循环遍历数组: 7
31 for循环遍历数组: 8
32 for循环遍历数组: 9
33 for循环遍历字符串 A
34 for循环遍历字符串 B
35 for循环遍历字符串 C
36 for循环遍历字符串 D
37 for循环遍历字符串 E
38 for循环遍历字符串 F
39 for循环遍历字符串 G
40 for循环遍历字符串 H
41 for循环遍历字符串 I
42 for循环遍历字符串 J
43 for循环遍历字符串 K
44 for循环遍历字符串 L
45 for循环遍历字符串 M
46 for循环遍历字符串 N
47 for循环遍历字符串 O
48 for循环遍历元组 0
49 for循环遍历元组 1
50 for循环遍历元组 2
51 for循环遍历元组 3
52 for循环遍历元组 4
53 for循环遍历元组 5
54 for循环遍历元组 6
55 for循环遍历元组 7
56 for循环遍历元组 8
57 for循环遍历元组 9
58 for循环遍历集合 0
59 for循环遍历集合 1
60 for循环遍历集合 2
61 for循环遍历集合 3
62 for循环遍历集合 4
63 for循环遍历集合 5
64 for循环遍历集合 6
65 for循环遍历集合 7
66 for循环遍历集合 8
67 for循环遍历集合 9
68 输出my name is xxxx从[1:5]的内容: y na
69 输出my name is xxxx从[:5]的内容: my na
70 输出my name is xxxx从[1:]的内容: y name is xxxx
71 替换了字符串中的XXX,将其替换为NP: my name is NP
72 将my name is xxxx进行分割,生成一个链表 ['my', 'name', 'is', 'xxxx']
73 my name is xxxx分割后的链表重新拼合为一个字符串 my-name-is-xxxx
74 将hello第一个字母转换成大写 Hello
75 将hello world每个单词的首字母转换成大写 Hello World
76 将HELLO WORLD中大写转小写 hello world
77 将HELLO WORLD中小写转大写 HELLO WORLD
78 删除  hello  左侧空白字符 hello  
79 删除  hello  右侧空白字符   hello
80 删除  hello  两侧空白字符 hello
81 获取 [[1, 2, 3], [4, 5, 6], [7, 8, 9]] 里的第一组元素 [1, 2, 3]
82 获取 [[1, 2, 3], [4, 5, 6], [7, 8, 9]] 里的第二组的第三个元素 6
83 
84 进程已结束,退出代码为 0

 

posted @ 2026-07-09 21:04  NO-PATS  阅读(10)  评论(0)    收藏  举报