您的位置:首页 > 编程语言 > Python开发

Python3.x快速入门学习

2011-04-27 12:41 561 查看
说明
特殊字符
<space>
表示键盘上的空格
1.
Python命令参数:
-d
提供调试输出
-o
生成优化的字节码(生成.pyo文件)
-s
不导入site模块以在启动时查找Python路径
-v
冗余输出(导入语句详细追踪)
-m mod
将一个模块以脚本形式运行
-Q opt
除法选项
-c cmd
运行以命令行字符串形式提交的Python脚本
file
从给定的文件运行python脚本

2.
程序输出
print("风云雄霸天下");
print("%s在本次考试中位于第%d名"
% ("吴科", 26));
输出重定向
print("内容",file=重定向目的);
#重定向到标准错误输出
import sys
print("重定向到标准错误输出", file=sys.stderr);
#重定向到文件
logfile=open("f:/log.txt","a");
print("重定向到日志文件", file=logfile);
logfile.close();
3.
输入
变量名=input("提示");
mystring=input("请输入您的名字:");
4.
注释

使用"#"注释

文档注释:在模块、类或者函数的起始位置添加一个字符串,起到在线文档的功能

5.
操作符
标准算术操作符
比较操作符
逻辑操作符
+

<
小于
and

-

<=
小于等于
or

*

>
大于
not

/

>=
大于等于
a < b and b < c
相当于 a < b < c
//
整除
==
等于
%

!=
不等于
**
乘方
6.
类型
数字
字符串
列表和元组
字典
有符号整型
短整型int
索引操作符[]
列表
元组
使用{}包裹
长整型long
切片操作符[:]
使用[]包裹
使用()包裹
me = {"name" : "吴科", "age" : 23}#定义

me["name"]
#获取name
#遍历

for key in me:

... print(key, me[key]);
布尔值bool
连接运算符+
元素个数和值可以改变
只读列表,元素个数和值都不可改变
浮点型float
重复运算符*
复数complex
7.
代码块及缩进

代码块通过缩进对齐表达代码逻辑,而不是使用大括号

8.
表达式式结果

非0表示true

0表示flase

9.
if语句
if表达式
if expression:
<space>if_suite;
if-else表达式
if expression:
<space>if_suite;
else:
<space>else_suite;
if-elif-else表达式
if expression:
<space>if_suite;
elif expression_other:
<space>other_suite;
else:
<space>else_suite;

10.
循环语句
while表达式
while expression:
<space>while_suite;
for表达式
for item in array:

<space>for_suite;
for i in range(len(mystring)):
<space>print(i, mystring[i]);
for i, ch in enumerate(mystring):
<space>print(i, ch);
11.
列表解析
for i in [x for x in range(5)]:
<space>print(i);

for i in [x * 2 for x in range(5) if not x % 2]:
<space>print(i);
12.
文件
#打开文件
handle = open(file_name, mode);
mode可使用“r”读取,“w”写,“a”添加

#关闭文件
handle.close();

readfile=open("f:/read.txt", "r");
for ch in readfile:
<space>print(ch);
readfile.close();
13.
错误与异常
try;
<space>try_suite;
except:
<space>except_suite;

try:
<space>filename = input("请输入文件路径:");
<space>fileis=open(filename, "r");
<space>fileis.close();
except:
<space>print("发生异常");
14.
函数
def function_name(param):
<space>"声明"
<space>function_suite;

#定义
def sayHello(name="步惊云"):
<space>"打印Hello
name, 默认情况name为步惊云"
<space>print("Hello %s" % name);

#调用
#默认参数函数调用:
sayHello()

#指定参数函数调用:
sayHello("聂风");
15.

class class_name(base_class):
<space>"描述"
<space>"静态属性定义"
<space>"方法定义"

#定义类
class Person(object):
<space>"定义人"
<space>def __init__(self, name="步惊云"):
<space><space>"构造方法"
<space><space>self.name=name;
<space><space>print("构造人");

<space>def showname(self):
<space><space>"显示人名"
<space><space>print(self.name);

<space>def setname(self, name):
<space><space>"重新设置人名"
<space><space>self.name=name;

<space>def getname(self):
<space><space>"获取人名"
<space><space>return self.name;

#调用
me=Person();
me.showname();
me.setname("聂风");
print(me.getname());

me=Person("聂风");
16.
模块

模块是Python的一种组织方式,将彼此有关系的Python代码组织到一个独立的文件中,模块可包含可执行的代码、函数和类。

模块就是不带.py后缀的Python源文件。

当模块创建后,可使用import语句将这个模块导入到其他模块中使用。
import module_name

module.function();

module.variable
17.
使用函数
dir()
显示对象属性
help()
查询帮助文档
int()
将一个对象转换为整型
len()
计算对象长度
open()
打开文件
range()
返回整数列表
input()
控制台输入
str()
将一个对象转换为字符串
type()
返回对象类型
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: