Python 基础与笔记(1)
来源:互联网 发布:python xml解析 编辑:程序博客网 时间:2024/05/29 12:32
1. Python支持C/C++的运算符有:
=
+=
-=
*=
/=
%=
Python特有的运算符:
**(乘方)
**=(乘方赋值)
不支持的运算符:前缀、后缀形式的++,--
2. Python数值类型:
int
long
float
complex
3. 字符串
单引号、双引号里面的内容都是字符串。
第一个字符索引下标为0;
最后一个字符索引下标为-1;
字符串可以用“+”进行拼接;
>>> str1 = 'hello'
>>> str2 = 'world'
>>> str1+str2
'helloworld'
字符串可以用“*”进行重复;
>>> str1*5
'hellohellohellohellohello'
>>> str1[0]
'h'
>>> str1[-1]
'o'
>>> str1[0:-1]
'hell'
4. 列表(list)和表列(tuple)
这是Python里自带容器。里面可以承载任意个数的Python对象。
列表用[]定义,其元素和长度可以变化。如
>>> alist = [1,2,3,4]
>>> alist
[1, 2, 3, 4]
>>> alist[0:-1]
[1, 2, 3]
>>> alist[2:]
[3, 4]
>>> alist[:]
[1, 2, 3, 4]
>>> alist[:3]
[1, 2, 3]
>>> alist[1] = 5
>>> alist
[1, 5, 3, 4]
表列用()定义,元素不能再次赋值。其他操作同列表。如
>>> atuple = ('robots',77,93,'try')
>>> atuple
('robots', 77, 93, 'try')
>>> atuple[0]
'robots'
5. 字典
字典就是Python里的hash table数据类型。用{}定义,内容包含key 和 value。key通常是数字或字符串。value可为任意基本数据类型。
>>> adict = {}
>>> adict['host'] = 'earth'
>>> adict['port'] = 80
>>> adict
{'port': 80, 'host': 'earth'}
>>> adict.keys()
dict_keys(['port', 'host'])
>>> adict['host']
'earth'
>>> adict.values()
dict_values([80, 'earth'])
6. 缩进风格
Python代码段是用缩进标识的。
7. 文件操作
handle = open(file_name,access_mode = 'r')
几种模式:
r:只读(默认)
w:只写
a:append,添加内容到末尾
+:读写
b:二进制方式
8. 异常
try :
try_block
except someError:
processing_block
raise:明确引发一个异常。
9. 函数
def function_name([arguments]):
'optional documentation string'
function_suite
##注意:函数所有参数都是以引用方式传递的,因此参数在函数中的任何变化都会影响到原始对象。(貌似3.3有变化了)
def adder2(x,y) :
return (x+y)
参数允许默认值。
10. 类
class class_name[(base_classes_if_any)]:
"optional documentation string"
static_member_declarations
method_declarations
一个例子:
class Cat :
"Hello world, I am a cat, miao miao miao..."
count = 0
def __init__(self, str = "Xiaomei") :
'Constructor'
self.name = str
count += 1
print("My name is : %s" % str)
def show_info(self)
'Show some class info'
print("My class name is :%s" % self.__class__)
print("My name is : %s" % self.name)
print("I have %d companies" % self.count)
__class__内建变量显示类名,这里是__main__.Cat
__doc__内建变量显示类声明时的documentation string
11. 模块
模块名.变量()
=
+=
-=
*=
/=
%=
Python特有的运算符:
**(乘方)
**=(乘方赋值)
不支持的运算符:前缀、后缀形式的++,--
2. Python数值类型:
int
long
float
complex
3. 字符串
单引号、双引号里面的内容都是字符串。
第一个字符索引下标为0;
最后一个字符索引下标为-1;
字符串可以用“+”进行拼接;
>>> str1 = 'hello'
>>> str2 = 'world'
>>> str1+str2
'helloworld'
字符串可以用“*”进行重复;
>>> str1*5
'hellohellohellohellohello'
>>> str1[0]
'h'
>>> str1[-1]
'o'
>>> str1[0:-1]
'hell'
4. 列表(list)和表列(tuple)
这是Python里自带容器。里面可以承载任意个数的Python对象。
列表用[]定义,其元素和长度可以变化。如
>>> alist = [1,2,3,4]
>>> alist
[1, 2, 3, 4]
>>> alist[0:-1]
[1, 2, 3]
>>> alist[2:]
[3, 4]
>>> alist[:]
[1, 2, 3, 4]
>>> alist[:3]
[1, 2, 3]
>>> alist[1] = 5
>>> alist
[1, 5, 3, 4]
表列用()定义,元素不能再次赋值。其他操作同列表。如
>>> atuple = ('robots',77,93,'try')
>>> atuple
('robots', 77, 93, 'try')
>>> atuple[0]
'robots'
5. 字典
字典就是Python里的hash table数据类型。用{}定义,内容包含key 和 value。key通常是数字或字符串。value可为任意基本数据类型。
>>> adict = {}
>>> adict['host'] = 'earth'
>>> adict['port'] = 80
>>> adict
{'port': 80, 'host': 'earth'}
>>> adict.keys()
dict_keys(['port', 'host'])
>>> adict['host']
'earth'
>>> adict.values()
dict_values([80, 'earth'])
6. 缩进风格
Python代码段是用缩进标识的。
7. 文件操作
handle = open(file_name,access_mode = 'r')
几种模式:
r:只读(默认)
w:只写
a:append,添加内容到末尾
+:读写
b:二进制方式
8. 异常
try :
try_block
except someError:
processing_block
raise:明确引发一个异常。
9. 函数
def function_name([arguments]):
'optional documentation string'
function_suite
##注意:函数所有参数都是以引用方式传递的,因此参数在函数中的任何变化都会影响到原始对象。(貌似3.3有变化了)
def adder2(x,y) :
return (x+y)
参数允许默认值。
10. 类
class class_name[(base_classes_if_any)]:
"optional documentation string"
static_member_declarations
method_declarations
一个例子:
class Cat :
"Hello world, I am a cat, miao miao miao..."
count = 0
def __init__(self, str = "Xiaomei") :
'Constructor'
self.name = str
count += 1
print("My name is : %s" % str)
def show_info(self)
'Show some class info'
print("My class name is :%s" % self.__class__)
print("My name is : %s" % self.name)
print("I have %d companies" % self.count)
__class__内建变量显示类名,这里是__main__.Cat
__doc__内建变量显示类声明时的documentation string
11. 模块
import 模块名
模块名.变量()
阅读全文
0 0
- Python 基础与笔记(1)
- Python 基础与笔记(2)
- PYTHON基础笔记(1)
- Python 基础笔记(1)
- python基础笔记1
- python基础笔记-1
- python基础笔记1
- Python学习笔记(一)——入门与基础
- Python 学习笔记(一):基础与入门
- python 基础学习笔记(1)
- python基础入门学习笔记(1)
- 《Python基础教程》学习笔记(1-2基础、列表与元组)
- python数据分析与挖掘学习笔记(1)-基础及准备
- 【学习笔记】Python基础-数据类型与变量
- Python基础--学习笔记1
- python 笔记1--基础类型
- Python笔记 基础篇1
- python 基础学习笔记1
- 输入一个int型的正整数,计算出该int型数据在内存中存储时1的个数。
- 【jzoj5220】【GDOI2018模拟7.10】【C】【动态规划】
- redisson使用第一次碰到的坑
- BZOJ 1060: [ZJOI2007]时态同步
- GFP_ATOMIC or GFP_KERNEL?
- Python 基础与笔记(1)
- 访问Oracle数据库的四款工具软件介绍
- 【简记】Java Web 内幕——AOP源码
- MATLAB实现PFFM算法
- Vim技能修炼教程(7)
- [bzoj3698]XWW的难题 有源汇的上下界最大流
- 【bzoj1731】[Usaco2005 dec]Layout 排队布局
- 解决SurfaceView调用setZOrderOnTop(true)遮挡其他控件的问题
- String所有方法