python语言基础-入门笔记1

来源:互联网 发布:淘宝女包货源免费代理 编辑:程序博客网 时间:2024/06/04 23:35

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

0 0