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
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 u盘文件读不出来怎么办 u盘突然读不出来怎么办 u盘读取不出来怎么办 蛋糕卷底部粘纸怎么办 美瞳没有护理液怎么办 没有带护理液了怎么办 护理液忘记带了怎么办 康宁玻璃锅裂了怎么办 带隐形牙套喝酒怎么办 牙齿保持器丢了怎么办 牙套保持器坏了怎么办 牙齿磕掉了一小块怎么办 牙齿裂掉了一半怎么办 大牙缺了一小块怎么办 牙根碎了一小块怎么办 门牙碎了一小块怎么办 孩子门牙长歪了怎么办 儿童门牙长歪了怎么办 大门牙长歪了怎么办 下面牙齿长歪了怎么办 只有一颗牙齿歪怎么办 牙齿突然长歪了怎么办 有个牙齿长歪了怎么办 一颗大牙长歪了怎么办 一颗牙齿长偏了怎么办 一个门牙长歪了怎么办 儿童牙齿长歪了怎么办 孩子牙齿长歪了怎么办 宝宝门牙长歪了怎么办 一颗牙齿挤歪了怎么办 后槽牙掉了一块怎么办 最里面的牙掉了怎么办 成年了牙齿掉了怎么办 我的门牙豁牙子怎么办 小孩牙齿长得稀怎么办 后槽牙掉了一颗怎么办 前门牙掉了一颗怎么办 牙齿黄怎么办小苏打美白牙齿 1岁宝宝牙齿发黄怎么办 宝宝一岁牙齿黄怎么办 宝宝出的牙黄怎么办