赞
踩
import sys;sys.stdout.write("hello world\n"); out:hello world |
print("%s is number %d" % ("ten",10)); out:ten is number 10 |
注:不同于print(),write()不会自动在字符串后面添加换行符
import sys;user=input("enter loginname:");print("your login is",user);
pystr='spyder_python_3.5';print(pystr[0:6]);print(pystr[6]);print(pystr[-3:-1],pystr[-1]); |
pystr='spyder_python_3.5';print(pystr[0:6]);print(pystr[6]);print(pystr[-3:]); |
Out:spyder
_
3.5
注:切片不包含结束坐标。因此常用:[-3:]
列表,运算符[],元素可以增删改。
元祖,运算符(),只读的列表。
共同点:切片操作,运算符[],[:]
aList=[1,"a",2,"b"]; aTuple=("rebots",77,93,"try"); |
aList[2:] Out[17]: [2, 'b'] |
aTuple[0:3] Out[18]: ('rebots', 77, 93) |
Python的映射数据类型,键-值(key-value)对构成;一般来说,数字或者字符串作为键key会最为常用;运算符为{}。
#创建字典: aDict={'host':'beijing'}; aDict['tel']='18888888888'; aDict['name']='Clark'; aDict[1]='etl'; aDict[2]='oracle'; aDict[3]='sql tuning'; |
aDict Out[25]: {1: 'etl', 2: 'oracle', 3: 'sql tuning', 'host': 'beijing', 'tel': '18888888888', 'name': 'Clark'} |
#字典的查询 aDict.keys() Out[26]: dict_keys([1, 2, 3, 'host', 'tel', 'name'])
for key in aDict: print(str(key)+"\t "+aDict[key]) |
def stastics(y):
import sys;
x=int(input("enter a number less then 30:"))+y;
if x<=10:
print("class_1");
elif x<=20:
print("class_2");
elif x<=30:
print("class_3");
else:
print("you enter a number more then 30.");
def test_while(counter):
while counter<10:
print("loop #%d" % (counter));
counter+=1;
test_while(8)
loop #8
loop #9
for item in aDict.keys():
print(item,"\t",aDict[item])
for item in [1,2,'a','b']:
print(item);
class range(object)
| range(stop) -> range object
| range(start, stop[, step]) -> range object
range()函数创建range对象
range(i, j) produces i, i+1, i+2, ..., j-1.
range(4).__contains__(2)
Out[33]: True
range(4).__contains__(4)
Out[34]: False
[x ** 2 for x in range(4)]
Out[35]: [0, 1, 4, 9]
[x for x in range(4)]
Out[36]: [0, 1, 2, 3]
持久存储。
Open:
open(file, mode='r', buffering=-1,encoding=None, errors=None, newline=None, closefd=True, opener=None)
'r' open for reading (default)
'w' open for writing,truncating the file first
'x' create a new file andopen it for writing
'a' open for writing,appending to the end of the file if it exists
'b' binary mode
't' text mode (default)
'+' open a disk file for updating (readingand writing)
open(r'E:\库-技术研究库\branch-stage01\程序设计-python\方向-pythoncore\专题-【Python核心编程(第二版)】Wesley J. Chun\1.txt', mode='x', encoding=None)
Out[42]: <_io.TextIOWrapper name='E:\\库-技术研究库\\branch-stage01\\程序设计-python\\方向-pythoncore\\专题-【Python核心编程(第二版)】Wesley J. Chun\\1.txt' mode='x' encoding='cp936'>
file=open(r'E:\库-技术研究库\branch-stage01\程序设计-python\方向-pythoncore\专题-【Python核心编程(第二版)】Wesley J. Chun\1.txt', mode='w', encoding=None)
file.writelines([str(item)+"\t"+aDict[item]+"\n"for item in aDict.keys()])
file.close()
try:
file=open(r'E:\库-技术研究库\branch-stage01\程序设计-python\方向-python core\专题-【Python核心编程(第二版)】Wesley J. Chun\2.txt', mode='x', encoding=None);
except IOError:
print("file openerror:",IOError);
else:
print("file open error:other");
file open error: <class'OSError'>
try:
file=open(r'E:\库-技术研究库\branch-stage01\程序设计-python\方向-python core\专题-【Python核心编程(第二版)】Wesley J. Chun\2.txt', mode='x', encoding=None);
except IOError as e:
print("file open error:",e);
else:
print("file open error:other");
file open error: [Errno 17] File exists:'E:\\库-技术研究库\\branch-stage01\\程序设计-python\\方向-python core\\专题-【Python核心编程(第二版)】Wesley J. Chun\\2.txt'
def addMe2(x):
return(x+x)
addMe2(2.5)
Out[81]: 5.0
addMe2('python')
Out[82]: 'pythonpython'
addMe2(['1','a',2,'b'])
Out[83]: ['1', 'a', 2, 'b', '1', 'a', 2,'b']
def foo(debug=True):
if debug:
print("in debug mode");
else:
print("out debug mode");
foo()
in debug mode
foo(False)
out debug mode
类是面向对象编程的核心,它扮演数据及逻辑容器的角色。
********************************************************************
** 欢迎转发,原文:http://blog.csdn.net/clark_xu [徐长亮的专栏]
** 感兴趣的代码在:https://github.com/clark99
** 感谢支持公众号:clark_blog
********************************************************************
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。