python3基础:基本语句

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)

 

 

 


  

迭代器(略)

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 * 的时候就把这个列表中的所有名字作为包内容导入。

 

浅拷贝、深拷贝

 View Code

 

debug

http://docs.pythontab.com/interpy/Debugging/README/

 View Code
  • c:      继续执行
  • w:     显示当前正在执行的代码行的上下文信息
  • a:     打印当前函数的参数列表
  • s:     执行当前代码行,并停在第一个能停的地方(相当于单步进入)
  • n:     继续执行到当前函数的下一行,或者当前行直接返回(单步跳过)

 

 

 

 

 

 


python3的内置函数:    http://www.runoob.com/python3/python3-built-in-functions.html

map()   map()会根据提供的函数对指定序列做映射。第一个参数 function 以参数序列中的每一个元素调用 function 函数,返回包含每次 function 函数返回值的新列表。

 View Code

 

filter(function, iterable)  function -- 判断函数。 iterable -- 可迭代对象。

 View Code

 

dir() 函数

内置的函数 dir() 可以找到模块内定义的所有名称。以一个字符串列表的形式返回

 

min()  max()  round()  len()

 View Code

 

list中的方法  index()  count()  .append()

 View Code

 

 

...

posted @ 2019-08-01 14:45  不夜男人  阅读(311)  评论(0编辑  收藏  举报