1、程序输出:
print(value,..., sep=' ', end='\n', file=sys.stdout, flush=Flase)
默认参数:sep=' ' 表示输出多个value时默认以‘空格’来分隔;
end='\n' 表示输出value时会默认在每一行后面添加 换行符“\n”;
file=sys.stdout 表示默认采用标准输出(即向屏幕输出内容);
flush=Flase 作用....
2、程序输入:
input(prompt=None, /)
3、注释:
单行注释:#
多行注释:''' '''
4、操作符:
+ 、- 、*、/、//、%、** 分别表示:加、减、乘、地板除、求余、乘方;
<、<=、>、>=、==、!=、<>
5、格式化字符串:
%,%s, %d,%f,......
6、列表:
[ ]
7、元组:
()
8、字典:
{ }
9、if语句:
if a<0:
print("a小于0")
elif a>0:
print("a大于0")
else:
print("a等于0")
10、while循环:
count=0
while count<5:
print("count is %d" % count)
count+=1
11、for循环和range()内建函数:
for eachNum in range(5):
print(eachNum)
range()内建函数用来生成列表
12、列表解析:
squared = [x ** 2 for x in range(8) if not x % 2 and x != 0]
for i in squared:
print(i)
13、内建函数open():
open(file, mode='r', buffering=-1, encoding=None, errors=None, newline=None, closefd=True, opener=None)
14、错误和异常:
try:
filename = input("Enter file name:")
fobj = open(filename, 'r')
for eachLine in fobj:
print(eachLine)
fobj.close()
except IOError as e:
print("file open error:", e)
>>>
Enter file name:hhh.txt
file open error: [Errno 2] No such file or directory: 'hhh.txt'
15、函数:
用关键字 def 来定义函数:
def function_name([arguments]):
"optional documentation string"
function_sutie
[arguments] : 表示可选参数。
16、类:
类 就是可以批量生产具有相似 功能、方法(函数)、属性 的 加工厂。
class FooClass(object):
"my very first class:FooClass"
version = 0.1
def __init__(self, nm="Jack Cheng"):
self.name = nm
print("Created a class instance for", nm, self.name)
def showname(self):
print("Your name is", self.name)
print("My name is", self.__class__.__name__)
当一个类实例被创建时,__init__() 方法会自动执行,类似构造器,它的目的是用来执行一些必要的初始化工作。
17、模块:
>>> import sys
>>> sys.stdout.write('Hello World!\n')
Hello World!
13
>>> sys.stdout.write('Hello World! \n')
Hello World!
14
>>> sys.stdout.write('Hello World! \n')
Hello World!
14
>>> sys.stdout.write('Hello World! \n')
Hello World!
15
>>>
dir([obj]) : 显示对象的属性,如果没有提供参数,则显示全局变量的名字;
help([obj]) : 显示对象的帮助文档,如果没有提供参数,则会进入交互式帮助;