赞
踩
- a = 2 ** 3
- b = 3 // 2
- c = 3 / 2
- print(a)
- print(b)
- print(c)
- a = 10
- print(type(a))
-
- a = 2.0
- print(type(a))
-
- a = 'hello'
- print(type(a))
-
- a = True
- print(type(a))
- e = "My name is 'tangzhong'"
- print(e)
-
- a = 10
- print(f"a = {a + 10}")
- # 我们希望把数字和字符串混在一起来打印~
- a = int(input('请输入第一个整数: '))
- b = input('请输入第二个整数: ')
-
- print(type(a))
- print(type(b))
-
- b = int(b)
- print(type(b))
-
- c: int = a
- print(type(c))
-
- print(f'a + b = {a + b}')
- a = 10
- b = 20
- c = 30
-
- print(a < b < c)
- print(a < b and b < c)
-
- print(a > b or b < c)
-
- print(not a < b)
- a, b = 10, 20
-
- # 使用多元赋值, 直接一步到位完成交换.
- a, b = b, a
- print(a, b)
- a = 10
- ++a
- print(a)
- --a
- print(a)
- a = 'hello'
- b = 'world'
- print(a + b)
-
- # a = 'hello'
- # b = 10
- # print(a + b)
-
- a = 10
- b = 5.5
- print(a + b)
-
- a = 10
- b = False
- print(a + b)
-
- a = 10; print(a)
- choice = input("输入 1 表示愿意认真学习, 输入 2 表示躺平摆烂: ")
-
- if choice == '1':
- print('你能够找到好工作')
- elif choice == '2':
- print("你可能毕业就失业了")
- else:
- # 除了 1 和 2 其他的情况都算非法情况
- print("您的输入有误!")
- a = input("请输入第一个整数: ")
- b = input("请输入第二个整数: ")
-
- if a == '1':
- if b == '2':
- print("aaa")
- print("bbb")
- print("ccc")
- a = input("请输入一个数字: ")
- if a != '1':
- # 啥都不做
- pass
- else:
- print('hello')
- # 打印 1-10
- for i in range(1, 11):
- print(i)
-
- # 打印 2, 4, 6, 8, 10
- for i in range(2, 12, 2):
- print(i)
-
- # 打印从 10 到 1
- num = 10
- while num >= 1:
- print(num)
- num -= 1
-
- for i in range(10, 0, -1):
- print(i)
- # 假设我要吃 5 个包子
- for i in range(1, 6):
- if i == 3:
- # 发现第三个包子, 有一只虫~
- continue
- print(f'吃第 {i} 个包子')
-
- # 还是要吃 5 个包子
- for i in range(1, 6):
- if i == 3:
- # 发现第三个包子, 有半只虫
- break
- print(f'吃第 {i} 个包子')
- import random
- import time
- # 让系统休眠一秒
- time.sleep(1)
- # 生成随机数
- point = random.randint(1, 3)
- print(point)
- # 定义一个求和函数
- def calcSum(beg, end):
- theSum = 0
- for i in range(beg, end + 1):
- theSum += i
- print(theSum)
-
- calcSum(1, 100)
- calcSum(300, 400)
- calcSum(1, 1000)
- def add(x, y):
- return x + y
-
- print(add(10, 20))
- print(add(1.5, 2.5))
- print(add('hello', 'world'))
- # print(add(10, 'hello'))
- def getPoint():
- x = 10
- y = 20
- return x, y
-
- a, b = getPoint()
- _, b = getPoint()
- x = 10
- def test():
- global x
- # 修改全局变量
- x = 20
-
- test()
- print(f'x = {x}')
- def add(x, y, debug=False):
- if debug:
- print(f'x = {x}')
- return x + y
-
- result = add(10, 1,True)
- print(result)
- def test(x, y):
- print(f'x = {x}')
- print(f'y = {y}')
-
- test(x=10, y=20)
- test(y=100, x=200)
- # 1. 直接使用字面值来创建
- a = []
- print(type(a))
-
- # 2. 使用 list() 来创建
- b = list()
- print(type(b))
-
- # 3. 可以在创建列表的时候, 在 [ ] 中指定列表的初始值.
- # 元素之间使用 , 来分割
- a = [1, 2, 3, 4]
- print(a)
-
- # 4. 可以在同一个列表里放不同类型的变量
- a = [1, 'hello', True, [4, 5, 6]]
- print(a)
- # 8. 可以使用内建函数 len 来获取到列表的长度(元素个数) , 和字符串类似.
- a = [1, 2, 3, 4]
- print(len(a))
-
- # 9. Python 中的下标, 其实还可以写成负数!!
- a = [1, 2, 3, 4]
- print(a[len(a) - 1])
- # -1 就是倒数第一个元素!
- print(a[-1])
- # 1. 切片操作的基本使用
- a = [1, 2, 3, 4]
- print(a[1:3])
-
- # 2. 使用切片的时候, 省略边界.
- a = [1, 2, 3, 4]
- # 省略后边界, 意思是从开始位置, 一直取到整个列表结束.
- print(a[1:])
- print(a[:2])
- print(a[:-1])
- print(a[:])
-
- # 3. 带有步长的切片操作
- a = [1, 2, 3, 4, 5, 6, 7, 8, 9, 0]
- print(a[::2])
- print(a[1:-1:2])
- # 1. 使用 for 循环来遍历列表
- a = [1, 2, 3, 4, 5]
- for elem in a:
- elem = elem + 10
-
- print(a)
-
- # 2. 使用 for 循环遍历, 通过下标的方式.
- a = [1, 2, 3, 4, 5]
- for i in range(0, len(a)):
- # print(a[i])
- a[i] = a[i] + 10
-
- print(a)
- # 3. 使用enumerate变量
- a = ["早上好", '中午好', '晚上好']
- for index, val in enumerate(a):
- print(index, val)
- # 1. 使用 append 往列表末尾新增一个元素.
- a = [1, 2, 3, 4]
- a.append(5)
- print(a)
-
- b = [5, 6, 7, 8]
- b.append('world')
- print(b)
-
- # 2. 还可以使用 insert 方法, 往列表的任意位置来新增元素~
- a = [1, 2, 3, 4]
- a.insert(1, 'hello')
- a.insert(100, 'hello')
- print(a)
- a = [1, 2, 3, 4]
- print(1 in a)
- print(10 in a)
- print(1 not in a)
- print(10 not in a)
-
- a = [1, 2, 3, 4]
- print(a.index(2))
- print(a.index(3))
- a = [1, 2, 3, 4]
- a.pop()
- print(a)
-
- a = [1, 2, 3, 4]
- a.pop(1)
- print(a)
-
- a = ['aa', 'bb', 'cc', 'dd']
- a.remove('cc')
- print(a)
- a = [1, 2, 3, 4]
- b = [5, 6, 7, 8]
- c = b + a
- print(c)
- print(a)
- print(b)
-
- a = [1, 2, 3, 4]
- b = [5, 6, 7, 8]
- a.extend(b)
- print(a)
- print(b)
-
- a = [1, 2, 3, 4]
- b = [5, 6, 7, 8]
- a += b
- print(a)
- print(b)
- # 1. 创建元组
- a = ()
- print(type(a))
-
- b = tuple()
- print(type(b))
-
- # 2. 创建元组的时候, 指定初始值.
- a = (1, 2, 3, 4)
- print(a)
-
- # 3. 元组中的元素也可以是任s意类型的.
- a = (1, 2, 'hello', True, [])
- print(a)
元组除了内部元素无法被修改之外,其他的一系列操作和列表一样
- # 1. 创建字典
- a = {}
- print(type(a))
- b = dict()
- print(type(b))
-
- # 2. 创建字典的同时设置初始值.
- a = {
- 'id': 1,
- 'name': 'zhangsan',
- }
- print(a)
- a = {
- 'id': 1,
- 'name': 'zhangsan'
- }
- print('id' in a)
- print('classId' in a)
-
- print('id' not in a)
- print('classId' not in a)
-
- a = {
- 'id': 1,
- 'name': 'zhangsan',
- 100: 'lisi'
- }
-
- print(a['id'])
- print(a['name'])
- print(a[100])
- a = {
- 'id': 1,
- 'name': 'zhangsan'
- }
-
- a['score'] = 90
- print(a)
-
- a['score'] = 100
- print(a)
-
- a.pop('name')
- print(a)
- # 直接使用 for 循环来遍历字典
- a = {
- 'name': 'zhangsan',
- 'id': 1,
- 'score': 90
- }
-
- for key in a:
- print(key, a[key])
-
- for key, value in a.items():
- print(key, value)
-
- print(a.keys())
- print(a.values())
- print(a.items())
- # 使用 open 打开一个文件.
- f = open('./record.txt', 'r')
- print(f)
- print(type(f))
-
- f.close()
- flist = []
- count = 0
- while True:
- f = open('./record.txt', 'r')
- flist.append(f)
- count += 1
- print(f'打开文件的个数: {count}')
- f = open('./record.txt', 'w')
- f.write('hello')
- f.close()
-
- f = open('./record.txt', 'w')
- f.write("1111\n")
- f.close()
-
- f = open('./record.txt', 'a')
- f.write("2222")
- f.close()
- f = open('./record.txt', 'r', encoding='utf8')
- result = f.read(3)
- print(result)
- f.close()
-
- # 2. 更常见的需求, 是按行来读取~~
- f = open('./record.txt', 'r', encoding='utf8')
- for line in f:
- print(f'line = {line}', end='')
- f.close()
-
- f = open('./record.txt', 'r', encoding='utf8')
- lines = f.readlines()
- print(lines)
- f.close()
- def func():
- with open('./record.txt', 'r', encoding='utf8') as f:
- pass
- # 代码执行逻辑
- import datetime as dt
-
- date1 = dt.datetime(year=2012, month=2, day=14)
- date2 = dt.datetime(year=2016, month=2, day=3)
- print(date2 - date1)
- def reverseWords(s: str):
- tokens = s.split()
- tokens.reverse()
- return ' '.join(tokens)
- print(reverseWords("I am a student."))
- def rotateString(s, goal):
- if len(s) != len(goal):
- return False
- return goal in (s + s)
- print(rotateString("abcde", "edcba"))
- def countPrefixes(words: list, s: str):
- count = 0
- for word in words:
- if s.startswith(word):
- # s 是以 word 开头
- count += 1
- return count
-
- print(countPrefixes(['a', 'b', 'c', 'ab', 'bc', 'abc'], 'abc'))
- print(countPrefixes(['a', 'a'], 'aa'))
输入要查找的路径, 输入要搜索的文件名(一部分)
自动的在指定的路径中进行查找~
- import os
-
- inputPath = input('请输入要搜索的路径:')
- pattern = input('请输入要搜索的关键词:')
-
- for dirpath, _, filenames in os.walk(inputPath):
- for f in filenames:
- if pattern in f:
- print(f'{dirpath}/{f}')
使用 os.walk 即可实现目录的递归遍历.
os.walk 返回一个三元组, 分别是 当前路径 , 当前路径下包含的目录名 (多个), 当前路径下包含的文件名 (多个)
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。