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

python语言基础-入门笔记1

2016-07-30 15:19 393 查看
http://edu.51cto.com/course/course_id-1413.html

模块导入

方法(1)
import m

print m.plus(1, 2)

方法(2)
from m import plus

print plus(1, 2)

导入系统模块
import sys.path

from os import *

import string, re

import time,random

import socket, threading

添加到系统目录
sys.path.append(“./dir/”)

第一次赋值创建变量

python中一切皆对象
dir(var)

查看对象类型:变量名._class_

调用对象方法:变量名.方法()

内建数据类型:

boolean,

integer,

float, 

string ,“this is a test”

 list, [1, 2, 3]

tuple,(1, 2, 3)

dict, {'aa': 18, 'bb': 16}

set,set([0, 1, 3])

表达式比较

=   ==   >   <   >=   <=  

表达式组合

and    or    not

if 1==1:

    print "hello"

else:

    print "wrong"

if sex== 'boy':

    print 'boy'

elif sex== 'girl':

    print 'girl'

else:

    print 'weather is good'

python 中不存在switch

python 中没有三目运行( a>b?a:b)

python中的循环
for i in ['bob', 'lucy', 'jim']:

    print i

for i in range(10):

    if i<8:

        continue

    print i,

    break

else:

    print 'no break/exception'

i = 0

while i<10:

    print i,

    i += 1

obj = range(5)

iterator = iter(obj)

try:

    while True:

         print iterator.next(),

except StopIteration:

    pass

函数的定义和调用

#函数名
def welcome():

    print(' hello')

#函数名
def welcome(who, action):

    print(who + ' ' + action)

python变长参数
def welcome(*args)

    print args

>>>welcome('jim', 'is', 'thinking') 元祖

def welcome(**args)
    print args

>>>welcome(who=jim', state='is', action='thinking')
 字典

参数默认值
def welcome(who, state='is', action='sleeping'):

    print who,state, action

函数的返回值

#函数定义

def 函数名(参数列表):

    函数体

    return obj
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: