Python入门基础

简介:
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. 模块
import 模块名


模块名.函数()
模块名.变量()
目录
相关文章
|
28天前
|
Java Python 开发者
Python 学习之路 01基础入门---【Python安装,Python程序基本组成】
线程池详解与异步任务编排使用案例-xian-cheng-chi-xiang-jie-yu-yi-bu-ren-wu-bian-pai-shi-yong-an-li
77 2
Python 学习之路 01基础入门---【Python安装,Python程序基本组成】
|
1月前
|
存储 安全 API
【Python 基础教程 21】Python3 文件操作全面指南:从入门到精通的综合教程
【Python 基础教程 21】Python3 文件操作全面指南:从入门到精通的综合教程
82 0
|
1天前
|
Python
Python从入门到精通:深入学习面向对象编程——2.1.2继承、封装和多态的概念
Python从入门到精通:深入学习面向对象编程——2.1.2继承、封装和多态的概念
|
1天前
|
存储 索引 Python
Python从入门到精通——1.3.1练习编写简单程序
Python从入门到精通——1.3.1练习编写简单程序
|
1天前
|
开发框架 前端开发 数据库
Python从入门到精通:3.3.2 深入学习Python库和框架:Web开发框架的探索与实践
Python从入门到精通:3.3.2 深入学习Python库和框架:Web开发框架的探索与实践
|
1天前
|
数据采集 数据可视化 数据处理
Python从入门到精通的文章3.3.1 深入学习Python库和框架:数据处理与可视化的利器
Python从入门到精通的文章3.3.1 深入学习Python库和框架:数据处理与可视化的利器
|
1天前
|
Java 数据库连接 数据处理
Python从入门到精通:3.1.2多线程与多进程编程
Python从入门到精通:3.1.2多线程与多进程编程
|
1天前
|
存储 网络协议 关系型数据库
Python从入门到精通:2.3.2数据库操作与网络编程——学习socket编程,实现简单的TCP/UDP通信
Python从入门到精通:2.3.2数据库操作与网络编程——学习socket编程,实现简单的TCP/UDP通信
|
7天前
|
机器学习/深度学习 数据可视化 数据挖掘
《Python 简易速速上手小册》第9章:数据科学和机器学习入门(2024 最新版)
《Python 简易速速上手小册》第9章:数据科学和机器学习入门(2024 最新版)
19 1
|
7天前
|
人工智能 数据挖掘 程序员
《Python 简易速速上手小册》第1章:Python 编程入门(2024 最新版)
《Python 简易速速上手小册》第1章:Python 编程入门(2024 最新版)
35 0

热门文章

最新文章