Python初学(一)
来源:互联网 发布:js设置input边框颜色 编辑:程序博客网 时间:2024/06/05 20:26
1.基础变量定义弱类型语言
# coding=utf-8message = " hello python " #定义字符串age=23;print (message.title()); #相应的字符串操作print (message.lower());print (message.upper());print (message.strip());print (message.lstrip());print (message.rstrip());print ("age="+str(age)); #str 整形转字符串2.集合的定义及操作
定义集合输出
listdemo = ['1', '2', '3', '4'];print (listdemo);Paython的集合是可以动态追加的例如:
listdemo.append("5");删除操作使用del函数删除不能得到删除的元素,使用pop(intdex)出栈不但可以指定位置删除的还可以获取删除删除的内容,pop()
默认删除栈最后一个元素
del listdemo[4]; # 删除指定的一个数据print (listdemo);# 最后一个数据出栈删除,可以加参数deleData = listdemo.pop();print (deleData);集合的排序,及循环输出,len()函数获取长度。Paython中for循环如:for str in strs:只要是for循环体 内部的语句不论有多少条都要进行首行缩进。无首行缩进表示不在for循环内部
listdemo1 = ['2', '1', '4', '3'];# listdemo1.sort(reverse=False); #排序 reverse控制正反顺序 列表数据永久性变化print (sorted(listdemo1, reverse=False)); # 排序,但原有列表的顺序不发生变化print (listdemo1)listdemo1.reverse(); # 倒叙排序print (listdemo1); # 倒叙打印print (len(listdemo1)); # 数组长度for a in listdemo1: # for 循环使用,不用大括号,只要首行缩进就是在循环体里 print (a);集合操作的一些函数,其中元组的定义后,不能赋值改变,想要改变,除非重新定义覆盖
#创建数字列表for a in range(1,5): print a;for a in range(1,5,2):#按照+2不断输出不太与5 print a;#要将这些数字转换为一个列表,可使用list():print "----------------------------------";number=list(range(1,5));print number;print min(number);#最小值print max(number);#最大值print sum(number);#和print number[0:3]; #切片截取操作 从0开始输出3个copenumber=number[:];#复制列表num=copenumber;#复制列表print copenumber;print "----------------#定义元祖------------------";#定义元祖 元祖中的值一旦定义只能读取不能改变numbers=(200,50);print numbers[0];下面是输出结果
123413----------------------------------[1, 2, 3, 4]1410[1, 2, 3][1, 2, 3, 4]----------------#定义元祖------------------200
3.if 结构 if -elif-else结构
if结构定义 ,if中条件的定义 A and B 或者 A or B
#if语句cars = ['audi', 'bmw', 'subaru', 'toyota'];for car in cars: if(car=='audi'and len(car)==4): print ("this is"+car); else: print ("other");一些if中条件语句的书写方式
#检查值是否在类表中if("audi" in cars): print "true";else : print "false"; #检查值不包含在类表中if("audi" not in cars): print "true";else : print "false";
if-elif-if 结构中最后不一定要包含else语句
if("audi" in cars): print "true";elif("a" not in cars) : print "a不包含在cars";else: print "其他";
阅读全文
0 0
- 初学Python(一)
- 初学Python(一)
- Python初学(一)
- 初学Python(一)
- 初学python(一)
- 初学Python笔记(一)
- Python初学(一)DocString
- Python初学笔记(一)
- python 初学基础(一)
- python+django初学之安装(一)
- 初学Python(一)——数据类型
- python 初学一问题
- Python初学(一)
- Python初学一
- python初学基础知识一
- Python(一)初学小实验
- 初学python一:零碎知识点
- python初学零碎记一
- 台大Hung-Yi Lee Machine Learning 学习笔记——Regression
- 前端Cookie应用
- 中文逗号转英文逗号
- pandas数据分析0723
- Eclipse常用快捷键
- Python初学(一)
- nginx内存池
- SuperMap iDesktop Cross 9D TOP 5新特性,总有一个惊艳到你!
- 【二分+贪心】Codeforces830A[Office Keys]题解
- Spring整合JMS(消息中间件)
- Java常见集合框架(六):List之Vector
- poj3723 Conscription
- springboot初步使用RocketMQ
- zzuli oj 1002: 简单多项式求值