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

浅拷贝、深拷贝

 

debug

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

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

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

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

 

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

 

dir() 函数

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

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

 

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

 

...

python3基础:基本语句的更多相关文章

  1. 2. Python3 基础入门

    Python3 基础入门 编码 在python3中,默认情况下以UTF-8编码.所有字符串都是 unicode 字符串,当然也可以指定不同编码.体验过2.x版本的编码问题,才知道什么叫难受. # -* ...

  2. python002 Python3 基础语法

    python002 Python3 基础语法 编码默认情况下,Python 3 源码文件以 UTF-8 编码,所有字符串都是 unicode 字符串. 当然你也可以为源码文件指定不同的编码: # -* ...

  3. Python3基础语法和数据类型

    Python3基础语法 编码 默认情况下,Python3源文件以UTF-8编码,所有字符串都是unicode字符串.当然你也可以为原码文件制定不同的编码: # -*- coding: 编码 -*- 标 ...

  4. oracle(sql)基础篇系列(一)——基础select语句、常用sql函数、组函数、分组函数

        花点时间整理下sql基础,温故而知新.文章的demo来自oracle自带的dept,emp,salgrade三张表.解锁scott用户,使用scott用户登录就可以看到自带的表. #使用ora ...

  5. Mysql(Mariadb) 基础操作语句 (持续更新)

    基础SQL语句,记录以备查阅.(在HeiDiSql中执行) # 创建数据库 Create Database If Not Exists VerifyIdear Character Set UTF8; ...

  6. HQL基础查询语句

    HQL基础查询语句 1.使用hql语句检索出Student表中的所有列 //核心代码 @Test public void oneTest() { Query query=session.createQ ...

  7. mysql使用基础 sql语句(一)

    csdn博文地址:mysql使用基础 sql语句(一)  点击进入 命令行输入mysql -u root -p,回车再输入密码,进入mysql. 终端命令以分号作为一条语句的结束,可分为多行输入,只需 ...

  8. VBA基础——循环语句

    VBA基础之循环语句 Sub s1() Dim rg As Range For Each rg In Range("a1:b7,d5:e9") If rg = "&quo ...

  9. python3基础视频教程

    随着目前Python行业的薪资水平越来越高,很多人想加入该行业拿高薪.有没有想通过视频教程入门的同学们?这份Python教程全集等你来学习啦! python3基础视频教程:http://pan.bai ...

  10. 【MySQL】MySQL基础操作语句

    mysql基础操作语句,包括数据库的增.删.切换,以及表的增.删.改.查.复制. 创建数据库 mysql> create database tem; 使用数据库 mysql> use te ...

随机推荐

  1. 01串LIS(固定串思维)--Kirk and a Binary String (hard version)---Codeforces Round #581 (Div. 2)

    题意:https://codeforc.es/problemset/problem/1204/D2 给你一个01串,如:0111001100111011101000,让你改这个串(使0尽可能多,任意 ...

  2. MySQL的库、表的详细操作

    目录 MySQL的库.表的详细操作 一 库操作 二 表操作 MySQL的库.表的详细操作 本节目录 一 库操作 1.创建数据库 1.1 语法 CREATE DATABASE 数据库名 charset ...

  3. Redis: 缓存过期、缓存雪崩、缓存穿透、缓存击穿(热点)、缓存并发(热点)、多级缓存、布隆过滤器

    Redis: 缓存过期.缓存雪崩.缓存穿透.缓存击穿(热点).缓存并发(热点).多级缓存.布隆过滤器 2019年08月18日 16:34:24 hanchao5272 阅读数 1026更多 分类专栏: ...

  4. @RequestMapping-限定参数映射

    限定参数映射 测试:

  5. VUE项目中使用this.$forceUpdate()强制页面重新渲染

    在使用Vue框架开发时,在函数中改变了页面中的某个值,在函数中查看是修改成功了,但在页面中没有及时刷新改变后的值,我是在使用多层v-for嵌套时出现这种问题的, 解决方法:运用 this.$force ...

  6. python-装饰器案例1

    python-装饰器案例1 高阶函数+嵌套函数=装饰器 例1: import time def timer(func): def deco(): start_time=time.time() func ...

  7. vm安装ubantu的详细过程(转载)

    这里转载一个非常实用的vm虚拟机安装linux系统的文章有需要的可以看下面链接: https://blog.csdn.net/u013142781/article/details/50529030

  8. VMware的linux虚拟机配置ip后无法ping通宿主机

    VMware的linux虚拟机配置ip(使用eth0)后无法ping通宿主机,同样宿主机无法ping通linux虚拟机. 可能原因:linux虚拟机使用的网卡,与本机使用的网卡不同,配置成与本机一致的 ...

  9. Codeforces Round #581 (Div. 2) B. Mislove Has Lost an Array (贪心)

    B. Mislove Has Lost an Array time limit per test1 second memory limit per test256 megabytes inputsta ...

  10. Kay and Snowflake CodeForces - 686D (树的重心性质)

    After the piece of a devilish mirror hit the Kay's eye, he is no longer interested in the beauty of ...