python语言基础-入门笔记1

本文详细介绍了Python的基础语法,包括模块导入方法、系统模块使用、变量创建及数据类型介绍、条件语句、循环语句、迭代操作以及函数定义与调用等核心概念。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值