python 入门快速学习整理

Python 入门学习 

1  : 对象类型 1

1.1 列表 1

1.2 字典 2

1.3 元组 2

1.4 元组 2

1.4 文件 3

2  : 条件和循环语句 3

2.1  if else语句 3

2.2  while语句 4

2.3  for 语句 4

2.4  break语句 4

2.5  continue 4

2.6  pass 5

4  : 方法(函数)、类的定义使用 5

4.1 普通函数 5

4.2 主函数 5

4.3 类的定义、构造函数 实例 继承 5

5  : 数据库连接、导入模块 6

5.1  mysql 6

5.2  sqlite 6

6  : 异常处理 6

6.1  try except 语句 6

 

1  : 对象类型

1.1 列表

list=[1,2,3,4]

print list

print list[1]

print list[:2]

print list[2:]

1.2 字典

字典

diction={id:1,'name':'小马','password':123,'age':11}

print diction

print diction[id]

print diction['name']

diction['name']=11

print diction

1.3 元组

元组

tuple=(1,2,3,4,'你好','hello')

print tuple

print tuple[1]

print tuple[4]

tuple[4]='不高'

print tuple

1.4 元组

tuple=(1,2,3,4,'你好','hello')

print tuple

print tuple[1]

print tuple[4]

tuple[4]='不高'

print tuple

1.5 组合类型

组合使用  列表字典

list=[1,2,3,{'name':'xiaoma','age':11},6,7]

print list

dictionary=list[3]

print dictionary['name']

元组 字典

list=(1,2,3,{'name':'xiaoluo','pass':122123})

print list

dictionary=list[3]

print dictionary

print dictionary['name']

1.4 文件

文件 

os=open('D:uif_loglogs.log','r')

读取内容

filetext=os.readlines() # 或是用read() 方法

print filetext

for x in filetext:

    print x

    

os.close()

#os.readinto('D:uif_log1.txt')

读取一行

os=open('D:uif_loglogs.log','r')

filetext=os.readline()

print filetext

os.close()

2  : 条件和循环语句

2.1  if else语句

# if else 语句

a=1

b=0

if a>1:

    print 'True'

else:

    print 'False'

if a>0 and b>0:

    print a

elif a<0 and b<0:

    print b

else:

    print a,b

2.2  while语句

# while 语句

a=2

while a>0:

    print 'hello'

    a-=1

2.3  for 语句

# for 语句

list=[1,2,34,67]

for x in list:

    print x

2.4  break语句

# break 语句

a=2

while a>0:

    if a==2:

        break

    print 'hello'

    a-=1

2.5  continue

# continue 语句

a=2

while a>0:

    if a==2:

        continue

    print 'hello'

    a-=1

2.6  pass

占位符

4  : 方法(函数)、类的定义使用

4.1 普通函数

函数的定义

def getstr():

    print '这是一个普通的函数'

getstr()

4.2 主函数

主函数

def getstr():

    print '这是一个普通的函数'

if __name__=='__main__':

    getstr()

    print '这是一个主方法'

4.3 类的定义、构造函数 实例 继承

类的定义

class testclass():

    # 构造函数

    def __init__(self):

        print '我是一个构造方法'

        

类的实例

t=testclass()

类的继承

class test_clild(test):

    # 构造函数

    def __init__(self):

        print '我是子类中的一个构造方法'

tc=test_clild()

print tc.a

5  : 数据库连接、导入模块

5.1  mysql

数据库连接

import MySQLdb

conn = MySQLdb.Connect(host='127.0.0.1',user='root',passwd='root',db='uif',charset='utf8')

print conn

5.2  sqlite

#在调用connect函数的时候,指定库名称,如果指定的数据库存在就直接打开这个数据库,如果不存在就新创建一个再打开。

import sqlite3

cx = sqlite3.connect("D:/configcenter.db")

#也可以创建数据库在内存中。

con = sqlite3.connect(":memory:")

6  : 异常处理

6.1  try except 语句

普通异常处理

try:

    list=[1,2,3]

    print list[9] 

except :

    print '操作失败'

引用python自带的异常包处理

import exceptions

try:

    list=[1,2,3]

    print list[9] 

except exceptions.Exception,e:

    print e

finally:

    print '我是finally模块

##########################注释自己整理的知识注重快速入门 比较简单 是开发人员在非常短的时间内就可以开发python  作者马锡亮    有学习者可以交流

原文地址:https://www.cnblogs.com/keanuyaoo/p/3292023.html