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))
debug
http://docs.pythontab.com/interpy/Debugging/README/
import pdb def make_bread():
pdb.set_trace()
return "" print(make_bread())
- 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))
filter(function, iterable) function -- 判断函数。 iterable -- 可迭代对象。
a = filter(lambda x: x % 2 == 0, range(20)) #过滤出偶数
print(list(a))
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
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)
...
python3基础:基本语句(2017)的更多相关文章
- 2. Python3 基础入门
Python3 基础入门 编码 在python3中,默认情况下以UTF-8编码.所有字符串都是 unicode 字符串,当然也可以指定不同编码.体验过2.x版本的编码问题,才知道什么叫难受. # -* ...
- python002 Python3 基础语法
python002 Python3 基础语法 编码默认情况下,Python 3 源码文件以 UTF-8 编码,所有字符串都是 unicode 字符串. 当然你也可以为源码文件指定不同的编码: # -* ...
- Python3基础语法和数据类型
Python3基础语法 编码 默认情况下,Python3源文件以UTF-8编码,所有字符串都是unicode字符串.当然你也可以为原码文件制定不同的编码: # -*- coding: 编码 -*- 标 ...
- oracle(sql)基础篇系列(一)——基础select语句、常用sql函数、组函数、分组函数
花点时间整理下sql基础,温故而知新.文章的demo来自oracle自带的dept,emp,salgrade三张表.解锁scott用户,使用scott用户登录就可以看到自带的表. #使用ora ...
- Mysql(Mariadb) 基础操作语句 (持续更新)
基础SQL语句,记录以备查阅.(在HeiDiSql中执行) # 创建数据库 Create Database If Not Exists VerifyIdear Character Set UTF8; ...
- HQL基础查询语句
HQL基础查询语句 1.使用hql语句检索出Student表中的所有列 //核心代码 @Test public void oneTest() { Query query=session.createQ ...
- mysql使用基础 sql语句(一)
csdn博文地址:mysql使用基础 sql语句(一) 点击进入 命令行输入mysql -u root -p,回车再输入密码,进入mysql. 终端命令以分号作为一条语句的结束,可分为多行输入,只需 ...
- VBA基础——循环语句
VBA基础之循环语句 Sub s1() Dim rg As Range For Each rg In Range("a1:b7,d5:e9") If rg = "&quo ...
- python3基础视频教程
随着目前Python行业的薪资水平越来越高,很多人想加入该行业拿高薪.有没有想通过视频教程入门的同学们?这份Python教程全集等你来学习啦! python3基础视频教程:http://pan.bai ...
- 【MySQL】MySQL基础操作语句
mysql基础操作语句,包括数据库的增.删.切换,以及表的增.删.改.查.复制. 创建数据库 mysql> create database tem; 使用数据库 mysql> use te ...
随机推荐
- 5分钟搭建 nginx +php --------------(LNMP)新手专用
5分钟搭建 nginx +php --------------(LNMP)新手专用 2014-11-14 16:48 88876人阅读 评论(2) 收藏 举报 版权声明:本文为博主原创文章,未经博主允 ...
- ubuntu 阿里云 常出问题 运维工作日志
一.2015-8.26(数据库 error—28) tmp文件临时数据写入不了----解决办法 1.查看临时文件 ls -l 找到了 2.由此可以查看得出来tmp文件有的权限是有的 3.查看tmp 存 ...
- [Android] 配置build.gradle 动态传参
(1)一个Android工程中有一个build.gradle是负责Project范围的,而Module中又有各自的build.gradle是专门负责模块的. (2)在Gradle中Task是一等公民, ...
- pytest.3.Assert
From: http://www.testclass.net/pytest/assert/ Assert就是断言,每个测试用例都需要断言. 与unittest不同,pytest使用的是python自带 ...
- STL基础--String
String 构造 string s1("Hello"); string s2("Hello", 3); //s2: Hel string s3(s1, 2); ...
- Unity3d- 资源
Data与Resources文件夹一般只读文件放到Resources目录Data用于新建的文件或者要修改的文件============================================= ...
- Java-Runoob-高级教程-实例-方法:06. Java 实例 – 方法覆盖
ylbtech-Java-Runoob-高级教程-实例-方法:06. Java 实例 – 方法覆盖 1.返回顶部 1. Java 实例 - 方法覆盖 Java 实例 前面章节中我们已经学习了 Jav ...
- [转][Dapper]参数化查询慢
参考:https://www.cnblogs.com/wy123/p/7190785.html 参考:https://www.cnblogs.com/Irving/p/3951220.html i ...
- [C#][Quartz]添加监听器
namespace Quartz.Listener { public class SchedulerListener : SchedulerListenerSupport { private stat ...
- C语言怎么简单测试为大小端模式
作者:Slience_J 原文地址:https://blog.csdn.net/slience_j/article/details/52048267 1.什么是大小端模式? 大端模式,是指数据的高字节 ...