1 Star 0 Fork 0

chengy/python_demo01

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
slice.py 2.21 KB
一键复制 编辑 原始数据 按行查看 历史
chengy 提交于 2019-02-15 11:47 . init commit
#!/usr/bin/env python3
# _*_ coding:utf-8 _*_
# 高级特性 切片
import os
from collections import Iterable
L = list(range(100))
print(L)
print(L[:10])
print(L[-10:-1])
print(L[:10:2])
print(L[::5])
print('asdgasdasdf23232'[:3])
print(' 我会中文,因为为是中国人 '[1:4])
def trim(s):
if (s[:1] == ' '):
return trim(s[1:-1])
if (s[-2:-1] == ' '):
return (s[0:-2])
return s
print(trim(' sdf zas '))
# 迭代
d = {'a': 1, 'b': 2, 'c': 3}
for k, v in d.items():
print(k, v)
print(isinstance('abc', Iterable))
print(isinstance('[1,2,3]', Iterable))
print(isinstance(12, Iterable))
# 列表生成式 (List Comprehensions)
print(list(range(1, 11)))
# 生成[1x1,2x2,3x3,....,10x10]
L = []
for x in range(1, 11):
L.append(x * x)
print(L)
print([x * x for x in range(1, 11)])
L1 = [x * x for x in range(1, 11) if x % 2 == 0]
print(L1)
L2 = [m + n for m in 'ABC' for n in '123']
print(L2)
# 列出当前目录中所有的文件和目录名
L3 = [d for d in os.listdir('.')]
print(L3)
L4 = ['Hello', 'World', 'IBM', 'Apple', 12]
L5 = [isinstance(s, str) and s.lower() for s in L4]
print(L4)
print(L5)
d = {'x': 'A', 'y': 'B', 'z': 'C'}
L6 = [x + '=' + y for x, y in d.items()]
print(L6)
# 生成器(genterater) 生成器保存的是算法
g = (x * x for x in range(10))
# print(g)
for n in g:
print(n)
def fib(max):
n, a, b = 0, 0, 1
while n < max:
# print(b)
yield b
a, b = b, a + b
n = n + 1
return 'done'
for x in fib(6):
print(x)
def odd():
print('step 1')
yield 1
print('step 2')
yield 3
print('step 3')
yield 7
o = odd()
next(o)
next(o)
print('*' * 20, '杨辉三角')
def triangles():
n = 0
L1 = [1]
L2 = [1, 1]
while n < 10:
if n == 1:
print(L1)
if n == 2:
print(L2)
else:
print()
# L1, L2 = L2, [m + n for m in L1 for n in L2]
n = n + 1
# triangles()
# n = 10
# results = []
# for t in triangles():
# print(t)
# results.append(t)
# n = n + 1;
# if n == 10:
# break
it = iter([1, 2, 3, 4, 5])
while True:
try:
x = next(it)
print(x)
except StopIteration:
break
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Python
1
https://gitee.com/nocoding/python_demo01.git
[email protected]:nocoding/python_demo01.git
nocoding
python_demo01
python_demo01
master

搜索帮助