python3 快速上手(总结)
来源:互联网 发布:加强网络管理 防范 编辑:程序博客网 时间:2024/06/05 08:18
今天上班,没有什么实际的任务,闲着无聊抽空在网上搜了搜python3的内容。这篇就是copy+总结。自己动手尝试了一下
1,print
s1=input("Input your name:")print("hello,%s" % s1)
* input("某字符串")函数:显示"某字符串",并等待用户输入.
* print()函数:如何打印.
2,字符串和数字
a = 2b = "test"c = str(a)+ bprint("c is %s" % (c))d = "1111"e = a + int(d)print("e is %i" % (e) )
用int和str函数将字符串和数字进行转换
3,列表
#定义元组word=['a','b','c','d','e','f','g']#如何通过索引访问元组里的元素a=word[2]print ("a is: "+a)b=word[1:3]print ("b is: ")print (b) # index 1 and 2 elements of word.c=word[:2]print ("c is: ")print (c) # index 0 and 1 elements of word.d=word[0:]print ("d is: ")print (d) # All elements of word.#元组可以合并e=word[:2]+word[2:]print ("e is: ")print (e) # All elements of word.f=word[-1]print ("f is: ")print (f) # The last elements of word.g=word[-4:-2]print ("g is: ")print (g) # index 3 and 4 elements of word.h=word[-2:]print ("h is: ")print (h) # The last two elements.i=word[:-2]print ("i is: ")print (i) # Everything except the last two charactersl=len(word)print ("Length of word is: "+ str(l))print ("Adds new element")word.append('h')print (word)#删除元素del word[0]print (word)del word[1:3]print (word)
列表长度是动态的,可任意添加删除元素.
注意 []左边包括,右边不包括。eg:b=word[1:3] #word[1],word[2]没有3。
4,字典(前些天编了一个vba真是用够这个数据类型了,大学期间都没有用过几次)
x = {1:'可爱多',2:'妙脆角',3:'果粒橙'} #这三个好像被没有什么联系print(x[1])print(x[2])print(x[3])for i in x : print ( "key is %i and item is %s" %(i,x[i]))
这个没什么好说的 字典类型和for循环的调用
5,字符串
word="abcdefg"a=word[2]print ("a is: "+a)b=word[1:3]print ("b is: "+b) # index 1 and 2 elements of word.c=word[:2]print ("c is: "+c) # index 0 and 1 elements of word.d=word[0:]print ("d is: "+d) # All elements of word.e=word[:2]+word[2:]print ("e is: "+e) # All elements of word.f=word[-1]print ("f is: "+f) # The last elements of word.g=word[-4:-2]print ("g is: "+g) # index 3 and 4 elements of word.h=word[-2:]print ("h is: "+h) # The last two elements.i=word[:-2]print ("i is: "+i) # Everything except the last two charactersl=len(word)print ("Length of word is: "+ str(l))
python的字符串有没有发现和元组的操作很像~
类似Java,在python3里所有字符串都是unicode,所以长度一致.
6,条件与循环
#条件和循环语句x=int(input("Please enter an integer:"))if x<0: x=0 print ("Negative changed to zero")elif x==0: print ("Zero")else: print ("More")a = ['cat', 'window', 'defenestrate']for x in a: print (x, len(x))
7,函数
def sum(a,b,c,d) return a+b+c-dfunc = sumr = func(5,6,7,8)print(r)答案是10
还有一个python好用的内置函数:range
a =range (1,10)for i in a: print(i)
range函数:
函数原型:range(start, end, scan):
参数含义:start:计数从start开始。默认是从0开始。例如range(5)等价于range(0, 5);
end:技术到end结束,但不包括end.例如:range(0, 5) 是[0, 1, 2, 3, 4]没有5
scan:每次跳跃的间距,默认为1。例如:range(0, 5) 等价于 range(0, 5, 1)
8,异常处理
#! /usr/bin/pythons=input("Input your age:")if s =="": raise Exception("Input must no be empty.")try: i=int(s)except Exception as err: print(err)finally: # Clean up action print("Goodbye!")
9,文件处理
#! /usr/bin/pythonspath="D:/download/baa.txt"f=open(spath,"w") # Opens file for writing.Creates this file doesn't exist.f.write("First line 1.\n")f.writelines("First line 2.")f.close()f=open(spath,"r") # Opens file for readingfor line in f: print("每一行的数据是:%s"%line)f.close()
open的参数:r表示读,w写数据,在写之前先清空文件内容,a打开并附加内容.
0 0
- python3 快速上手(总结)
- python3 爬虫 零基础快速上手(爬虫示例)
- Axure 7.0 快捷键及快速上手总结
- Kotlin快速上手,基础知识点总结
- Python快速上手(一)
- Python快速上手(二)
- Python快速上手(三)
- Django 快速上手(一)
- 快速上手
- SVN快速上手(一)安装
- (原创)单元测试Junit快速上手
- Enterprise Library 4.1 快速上手(图)
- Enterprise Library 4.1 快速上手(图)
- Enterprise Library 4.1 快速上手(图)
- axis2快速上手指南(转载)
- ExtAspNet控件库快速上手(一)
- CEGUI配置 快速上手(VS2008)
- F# 20分钟快速上手(一)
- Stasm构建表情识别系统的局限性
- jQuery的each循环
- 耶鲁博弈论笔记
- Spring AOP的两种实现方式
- suse下gdb调试无debug信息的glibc--配置zypper的源
- python3 快速上手(总结)
- C++学习篇(续)
- 几种边缘检测算子的比较Roberts,Sobel,Prewitt,LOG,Canny
- 黑马程序员——多线程-整理笔记
- android事件拦截处理机制详解
- 转载和积累系列 - 内存对齐
- 设置UIView圆角 cornerRadius 圆角有性能问题,用贝塞尔曲线代替
- libmad mp3 decode example code
- 黑马程序员_反射