python3基础:基本语句(2017)

 

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

 

浅拷贝、深拷贝

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

 

debug

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

import pdb

def make_bread():
    pdb.set_trace()
    return "11111"

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

 

 

 

 

 

 


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

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

def square(x):
    return x ** 2
x = map(square, [1, 2, 3, 4, 5]) 

x = map(lambda x: x ** 2, [1, 2, 3, 4, 5])  # 或者这么写
print(list(x))
View Code

 

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

a = filter(lambda x: x % 2 == 0, range(20))     #过滤出偶数
print(list(a))
View Code

 

dir() 函数

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

 

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

ls1 = [2.5, 3.6, 88.8, 22]
print(max(ls1)) # list中的最大值 88.8
print(min(ls1)) # list中的最小值 2.5
print(round(3.45678, 2))  # 取小数点后2位,四舍五入。     3.46
print(round(3.45678))   # 只有一个参数时,四舍五入默认取整      3
print(len("abcdefg"))   # 得到字符串的长度  7
View Code

 

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

ls1 = ["aa", "bb", "cc", "dd", "ee", "aa", "aa"]
print(ls1.index("bb"))  # 找到这个元素在list中的位置
# print(ls1.index("ddd")) # 找不到就会出错
print(ls1.count("aa"))
ls1.append("xx")    # 追加元素
print(ls1)
View Code

 

 

...

posted on 2017-10-07 02:27 bjhhh 阅读( ...) 评论( ...) 编辑 收藏

转载于:https://www.cnblogs.com/qq21270/p/7634025.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值