print语句输出
print('hello world!')
x=3;y=6
x,y=y,x #交换变量
print( x, end=", " )
print( y)
a=10;b=388;c=98
print(a,b,c,sep='@')
print('{0}--{1}'.format('One', 1))
print('%s--%s'%('One',str(1)))
str= """这是一个多行字符串实例
这是接着第二行
这是接着第三行
"""
print(str)
hello world!
6, 3
10@388@98
One--1
One--1
这是一个多行字符串实例
这是接着第二行
这是接着第三行
基本函数
数字操作
a=666
isInt=isinstance(a,int) #判断是否为数字
print(isInt)
print(5**3) #乘方
print(5//3) #取整除的整数部分
True
125
1
for语句range迭代
for i in range(5,9) :
print(i,end=';')
print()
for i in range(0, 10, 3) :
print(i,end=';')
print()
a = ['One', 'Two', 'Three']
for i in range(len(a)):
print(i, a[i])
list=[1,2,3,4]
it = iter(list) # 创建迭代器对象
for x in it:
print (x, end=";")
print()
5;6;7;8;
0;3;6;9;
0 One
1 Two
2 Three
1;2;3;4;
逻辑运算
a = 10
b = 20
print(float(a)) #float(x) 将x转换到一个浮点数
if ( a and b ):
print ("1 - 变量 a 和 b 都为 true")
else:
print ("1 - 变量 a 和 b 有一个不为 true")
if ( a or b ):
print ("2 - 变量 a 和 b 都为 true,或其中一个变量为 true")
else:
print ("2 - 变量 a 和 b 都不为 true")
# 修改变量 a 的值
a = 0
if ( not a ):
print ("3 - 变量 a false")
else:
print("3 - 变量 a true")
10.0
1 - 变量 a 和 b 都为 true
2 - 变量 a 和 b 都为 true,或其中一个变量为 true
3 - 变量 a false
高效函数
Counter
from collections import Counter
#定义两个字符串变量
Var1 = "1116122137143151617181920849510"
Var2 = "1987262819009787718192084951"
#以字典的形式,输出每个字符串中出现的字符及其数量
print (Counter(Var1))
print (Counter(Var2))
函数
“传值”与“传引用”
1)不可变类型:类似 c++ 的值传递,如 整数、字符串、元组。如fun(a),传递的只是a的值,没有影响a对象本身。比如在 fun(a)内部修改 a 的值,只是修改另一个复制的对象,不会影响 a 本身。
2)可变类型:类似 c++ 的引用传递,如 列表,字典。如 fun(la),则是将 la 真正的传过去,修改后fun外部的la也会受影响python 中一切都是对象,严格意义我们不能说值传递还是引用传递,我们应该说传不可变对象和传可变对象
#传值
def ChangeInt( a ):
a = 10
b = 2
ChangeInt(b)
print( b ) # 结果是 2
#传引用
def changeme(mylist):
#"修改传入的列表"
for i in range(0,len(mylist)):
mylist[i]+=1
print("函数内部修改后: ", mylist)
return
# 调用changeme函数
mylist = [10, 20, 30];
print("调用前列表取值: ", mylist)
changeme(mylist);
print("调用后列表取值: ", mylist)
2
调用前列表取值: [10, 20, 30]
函数内部修改后: [11, 21, 31]
调用后列表取值: [11, 21, 31]
名字: zou;年龄: 25
名字: zou;年龄: 25
输出:
10
输出:
One
Two;Three;
相加后的值为 : 30
函数传参
#--函数参数的使用不需要使用指定顺序
def printinfo(name, age):
"打印任何传入的字符串"
print("名字: ", name,end=';');
print("年龄: ", age);
return;
# 调用printinfo函数
printinfo(age=25, name="zou");
printinfo('zou',25)
#加了星号(*)的变量名会存放所有未命名的变量参数。如果在函数调用时没有指定参数,它就是一个空元组。我们也可以不向函数传递未命名的变量
def printinfo(arg1, *vartuple):
#"打印任何传入的参数"
print("输出: ")
print(arg1)
for var in vartuple:
print(var,end=';')
return;
# 调用printinfo 函数
printinfo(10);
printinfo('One', 'Two', 'Three');
名字: zou;年龄: 25
名字: zou;年龄: 25
输出:
10
输出:
One
Two;Three;
正则表达式
#lambda 函数的语法只包含一个语句,如下:lambda [arg1 [,arg2,.....argn]]:expression
sum = lambda arg1, arg2: arg1 + arg2;
# 调用sum函数
print("相加后的值为 : ", sum(10, 20))
相加后的值为 : 30
作用域
作用域:Python 中只有模块(module),类(class)以及函数(def、lambda)才会引入新的作用域,其它的代码块(如 if/elif/else/、try/except、for/while等)是不会引入新的作用域的,也就是说这这些语句内定义的变量,外部也可以访问,如下代码:
yy=10
if True:
yy=50
print(yy)
#当内部作用域想修改外部作用域的变量时,就要用到global和nonlocal关键字了
num = 1 #这是一个全局变量
def fun1():
global num # 需要使用 global 关键字声明
num = 123
print(num)
fun1()
print()
#如果要修改嵌套作用域(enclosing 作用域,外层非全局作用域)中的变量则需要 nonlocal 关键字了,如下实例:
def outer():
num = 10
def inner():
nonlocal num # nonlocal关键字声明
num = 100
print(num)
inner()
print(num)
outer()
50
123
100
100
辛苦总结:)。如果写的好,请支持我下面的资源
pycharm工程python调用OpenCV实现USB摄像头实时人脸检测
http://download.csdn.net/download/zou19900101/10208407
pycharm工程pyQt5使用matplotlib绘图源码
http://download.csdn.net/download/zou19900101/10207077
pyqt5快速入门教程
http://download.csdn.net/download/zou19900101/10197371
Qt编写高逼格弹幕
http://download.csdn.net/download/zou19900101/10145524
Qt高仿360界面设计
http://download.csdn.net/download/zou19900101/10145518
Qt编写跨平台串口通信(Window+Linux)
http://download.csdn.net/download/zou19900101/10145513
OpenCV两种方法显示中文
http://download.csdn.net/download/zou19900101/10010181
wrote by zoushaoyuan 2018-01-20