http://www.cnblogs.com/qq21270/p/4591318.html   字符串、文本文件

http://www.cnblogs.com/qq21270/p/7872824.html   元组 tuple = () 、 列表 list = [] 、 字典 dict = {}

https://juejin.im/post/5a1670b5f265da432b4a7c79  Python 语法速览与实战清单(虽是py2的,值得看)

 

  • http://www.cnblogs.com/melonjiang/p/5083461.html
  • http://www.cnblogs.com/melonjiang/p/5090504.html
  • http://www.cnblogs.com/melonjiang/p/5104312.html  模块
  • http://www.cnblogs.com/melonjiang/p/5096759.html  装饰器
  • http://www.cnblogs.com/melonjiang/p/5135101.html  面向对象编程

 

数学运算

1、整除、取模

a = 36
b = 10
c = d = 0
c = a//b          #取整除 - 返回商的整数部分
d = a % b         #取模 - 返回除法的余数
print (a,"",b,"等于", c,",余",d)          # 36 除 10 等于 3 ,余 6

 

2、and   or

a = True
b = False
if ( a and b ):
   print (" 变量 a 和 b 都为 true")
if ( a or b ):
   print (" 变量 a or b 为 true")

 

3、for循环、if、列表

list = [2, 3, 4, 5 ]
for i in range(10):
    if ( i in list ):
        print("变量",i,"在给定的列表中 list 中")
    else:
        print("变量",i,"不在列表中")

 

4、数学运算

import math
x = 4.56789
a = math.floor(x)   #(要import math)返回数字的下舍整数,如math.floor(4.9)返回 4
b = math.ceil(x)    #(要import math)返回数字的上入整数,如math.ceil(4.1) 返回 5
c = round(x)        #返回浮点数的四舍五入值,如math.floor(4.56789)返回 5
c = round(x ,2)     #返回浮点数的四舍五入值,如给出n值,则代表舍入到小数点后的位数,如math.floor(4.56789,2)返回 4.57
print(a,b,c)

 

5、随机数

import random
a = random.choice(range(10))        # 随机数。在[0,10]范围内,一个整数。
a = random.randint(0, 10)           # 随机数。在[0,10]范围内,一个整数。
a = random.randrange (50 , 70 ,1)  # 随机数。在[50,70]范围内。第三个参数是步长

a = random.random()           # 随机数,在[0,1]范围内,浮点数。
a = random.uniform(0, 10)           # 随机数。在[0,10]范围内,浮点数。

 

6、PI

import math
pi = math.pi
#theta = math.pi / 4            #相当于45度角
theta = math.radians(30)        #radians(x)将角度转换为弧度。degrees(x)将弧度转换为角度
y = math.sin(theta)
x = math.cos(theta)
print(pi)
print(theta)
print(y)
print(x)

 

7、取小数点后2位

b=2222.26622
print('%.2f'%b)

 

 格式化输出

a=123456
print('{0:.2f}'.format(a)) #123456.00

 

 


  

迭代器(略)

http://www.runoob.com/python3/python3-iterator-generator.html

http://docs.pythontab.com/interpy/Generators/Iterable/

 

生成器(略)

http://docs.pythontab.com/interpy/Generators/Generators/

 

 

函数

必需参数:(略)
关键字参数:

def printinfo(name, age):
    print("名字: ", name, ",年龄: ", age);
    return;
printinfo(age=50, name="bob");

默认参数:

def printinfo(name, age = 35):
    print("名字: ", name, ",年龄: ", age);
    return;
printinfo( name="bob");

不定长参数:

def printinfo(*vartuple):
    print("打印任何传入的参数: ")
    for var in vartuple:
        print(var)
    return
printinfo(10)
printinfo(70, 60, 50, 5)

匿名函数:python 使用 lambda 来创建匿名函数。 lambda函数看起来只能写一行

sum = lambda arg1, arg2: arg1 + arg2;
print ("相加后的值为 : ", sum( 1, 2 ))    #相加后的值为 :  3
print ("相加后的值为 : ", sum( 22, 33 ))  #相加后的值为 :  55

 

变量作用域  http://www.runoob.com/python3/python3-function.html

Python的作用域一共有4种,分别是:

  • L (Local) 局部作用域
  • E (Enclosing) 闭包函数外的函数中
  • G (Global) 全局作用域
  • B (Built-in) 内建作用域
  • 以 L –> E –> G –>B 的规则查找

变量作用域的  global 和 nonlocal关键字

num = 0
def outer():
    num = 10
    def inner():
        nonlocal num   # nonlocal关键字声明(把这里的nonlocal换成global,或者注释掉,分别看看运行效果)
        num = 100
        print("inner:",num)
    inner()
    print("outer:",num)
outer()
print("global:",num)

 

print(locals())  #返回局部命名空间里的变量名字
print(globals())  #返回全局变量名字

 

 

 

__name__属性

一个模块被另一个程序第一次引入时,其主程序将运行。如果我们想在模块被引入时,模块中的某一程序块不执行,我们可以用__name__属性来使该程序块仅在该模块自身运行时执行。

#!/usr/bin/python3
# Filename: using_name.py

if __name__ == '__main__':
   print('程序自身在运行')
else:
   print('我来自另一模块')

'''运行输出如下:
$ python using_name.py
程序自身在运行
$ python  a.py       #里面包含此句:   import using_name
我来自另一模块
'''

  

目录只有包含一个叫做 __init__.py 的文件才会被认作是一个包

如果包定义文件 __init__.py 存在一个叫做 __all__ 的列表变量,那么在使用 from package import * 的时候就把这个列表中的所有名字作为包内容导入。

 

浅拷贝、深拷贝

python3基础:基本语句(2017)
x = ["a", "b", "c"]
y = x      # 换成  y = x[:] 再试试
y[1] = "bbb"
print(x)
print(y)
print(id(x))
print(id(y))

import copy
ls1 = range(10)
ls2 = ls1
ls3 = copy.deepcopy(ls1)
print(id(ls1))
print(id(ls2))
print(id(ls3))
View Code

相关文章:

  • 2021-12-29
  • 2021-11-23
  • 2021-11-27
  • 2021-12-26
猜你喜欢
  • 2021-08-24
  • 2021-09-18
  • 2021-06-11
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-11-29
相关资源
相似解决方案