提要
哈喽,大家好!相信有很多在传统软件行业的小伙伴,日常接触JS、Java、C#这类语言多一些,很少用到Python。但是Python确实很香(例如:AI、数学、绘图等),早晚会碰上它。对于我们这些懂编程但不懂Python的“老新手”来说,只有系统、全面地科普一下Python基础知识,才能更好、更高效地搬运的代码。下面是我整理的一些笔记,分享给大家。
py2exe(Windows)、PyInstaller(Linux、UNIX)可以将 Python 程序打包成独立可执行的文件,用于在未安装Python的平台上执行Python编写的应用程序。
DB_USERNAME = "admin"
DB_PASSWORD = "sa"
global_var = 1
def fn():
local_var = 2
a = {"xm": "张三"}
b = {"xm": "张三"}
print(a, id(a), type(a))
print(a == b, a is b)
del a
print(a)
Python 的工具库是呈层级分布的。可作用于多种类型的通用操作都是以内置函数或表达式的形式出现的(例如:len()、dir()、type()等)。但是类型的操作是以方法调用的形式出现的(例如:aString.upper())。
c = '张三'
d = "李四"
e = '''你好,
王五'''
print(c, d ,e)
path1 = 'c:\windows\abc\bcd'
path2 = r'c:\windows\abc\bcd'
print(path1)
print(path2)
a = "hello webj2eedev"
a[0] = "h"
zs = "张三"
ls = "李四"
a = "hello, %s, %s." % (zs, ls)
b = "hello, {}, {}.".format(zs, ls)
c = "hello, {1}, {0}.".format(zs, ls)
d = f"hello, {zs}, {ls}."
print(a)
print(b)
print(c)
print(d)
zs = "张三"
print(zs*3)
a = [1, 2, 3, 4] # 列表,可变
b = (6, 7, 8, 9) # 元组,不可变
c = {"xm": "张三", "age": 18} # 字典
d = {"张三", "李四"} # 集合
a = range(10)
b = [item ** 2 for item in a]
c = [item ** 2 for item in a if item % 2 == 0]
d = {item: item ** 2 for item in a}
e = {item for item in a}
print(b)
print(c)
print(d)
print(e)
x = True
y = False
本质是挤在一行中的一条 if 语句
classes = {
"张三": 18,
}
xm = "李四"
r = classes[xm] if xm in classes else "NOT EXIST"
print(r)
map = {
"张三": 18,
"李四": 30
}
xm = "hello world"
print("张三" in map,
"王五" not in map,
"world" in xm)
if(3 > 4 or 1 < 2 and 2<3):
print("yes")
if(not (1 > 2)):
print("no")
def add(a, b):
return a+b
def sum(a, b=100):
return a+b
def sub(*args):
return args[0] - args[1]
def div(**args):
return args["a"]/args["b"]
mul = lambda a, b: a * b
print(add(1, 2))
print(sum(1))
print(sub(1, 2))
print(div(a=100, b=2))
print(mul(4, 5))
try:
1/0
print("here?")
except Exception as e:
print(e)
finally:
print('hohoho')
参考:
Multiple Python interpreters: https://developer.fedoraproject.org/tech/languages/python/multiple-pythons.html Jython: https://www.jython.org/ IronPython: https://ironpython.net/ PyPy: https://www.pypy.org/index.html Stackless Python: https://github.com/stackless-dev/stackless/wiki/ py2exe: https://github.com/py2exe/py2exe