python学习整理
来源:互联网 发布:修改mysql配置文件 编辑:程序博客网 时间:2024/06/09 15:14
1.基本输出+特殊字符输出+多行打印+多行转义+打印中文
打印效果:
源代码:
# -*- coding: utf-8 -*-#上一行注释表示用UTF-8编码读取源代码#引用包import math#打印语句print 'Hello,world'#打印两数相加print 100+100#打印bool值print 1>2# ','表示输出一个空格print 'i love','music','are you?'#等差数列前100项求和x1 = 1#number 1d = 3#diffn = 100#number 100x100 = 1+3*(100-1)sum100 = ((x1+x100)*100)/2print sum100#输出特殊字符print 'he said \"I\'m sad." '#换行打印print 'line1:1\n line2:2'#打印多行--第二行前的空格也会打印print '''line1:11 line2:22'''#多行转义print r'''hello,"IMOOC".Let's start learn python!'''#打印中文print u'中文'
2.列表:list操作+tuple操作
运行效果:
源代码:
# -*- coding: utf-8 -*-#上一行注释表示用UTF-8编码读取源代码#引用包import math#数据类型是列表:listtestList = ['A',1,'B',2,'C',3]print testList#打印第一个和第二个元素print testList[0],testList[1]#打印最后一个元素print testList[-1]#在list最后插入一个元素testList.append('D')#在list第3个位置插入一个元素testList.insert(2,'T')print testList#删除最后一个元素testList.pop()#删除指定一个元素testList.pop(2)print testList#替换指定位置的元素testList[-1] = '33'print testListprint '\n\n'#创建tuple,创建完成不能修改,使用和list相同testtuple = ('a',1,'b',2)print testtuple[-2]#创建单个元素tuple时记得加,testpuple2 = (1,)print testpuple2
3.if–else使用+for–continue使用+while–break使用
源代码:
testList = ['A',1,'B',2,'C',3]#if--elif--else语句使用age = 12if age >= 18: print 'your age is',age print 'adult'elif age < 6: print 'you are too younger'else: print 'you are not a adult'#for--continue循环使用for sth in testList: if sth==2: continue print sth#while--break循环使用N = 5x = 0;while x<N: print x x += 1 if x==4: break
4.dict使用+set使用
源代码:
#dict的使用testDict = {'a':1,'b':2,'c':3}#访问dictif 'a' in testDict: print testDict['a']print testDict.get('a')print testDict.get('f')#不存在返回None#为dict添加内容testDict['f']=7#存在则替换值,不存在添加#遍历dictfor tempkey in testDict: print tempkey,':',testDict.get(tempkey)#计算任意集合的大小print 'len=',len(testDict)#set集合使用--无序,不重复--会自动去掉重复元素testSet = set(['a','b'])print testSet#判断元素是否在set中print 'c' in testSet#向set中添加元素testSet.add('h')print testSet#从set中删除元素if 'a' in testSet: testSet.remove('a') print testSetelse: print 'no sth in it'
5.int转str + str+int + 函数定义 + 递归函数 + 可变参数
源代码:
# -*- coding: utf-8 -*-#上一行注释表示用UTF-8编码读取源代码#引用包import math#int 转strstr(12)#str 转intint('123')#函数定义--默认参数def myfunction(x=5): print int(x) if int(x): return -int(x) else: return xprint myfunction('10')#递归函数def fact(n): if n==1: return 1 return n * fact(n - 1)print fact(5)#可变参数def average(*args): sum = 0.0 print len(args) return sumprint '\n'print average(2,4)
阅读全文
0 0
- [Python]学习资料整理
- Python学习整理资料
- python学习整理
- python学习资料整理
- python 学习笔记整理
- python学习网站整理
- Python学习笔记整理
- python学习整理
- Python学习整理2
- python学习资料整理
- python学习简要笔记整理
- Python学习简要笔记整理
- Python|爬虫学习资料整理
- Python的C扩展,学习整理
- Python 学习(1)---基本知识整理
- Python 学习(2)---基本知识整理
- python入门学习笔记(待更新 + 整理)
- python学习笔记-常用技巧整理
- 端口分类
- JavaScript——正则表达式大全
- Java静态内部类特性详解
- MapReduce中的二次排序
- Nginx nginx.conf配置文件详解
- python学习整理
- 机器学习项目开发实战,应用
- linux下gdb调试基本命令学习
- 写在开头
- IntelliJ IDEA中怎么全局搜索替换(整个项目)(Eclipse)
- Java(JDK、环境变量、虚拟机、跨平台)
- 差分约束
- Ubuntu操作系统虚拟机下安装使用
- struts2_关于valueStack的数据