算法 & 基础

Python 基础

原创 · 语言基础

print打印函数

pythonprint("hello world!");

字符串变量声明,也可以使用单引号

pythonprint("hello world!");
message="hello world !";

title()函数,作用将每个单词的首字母大写

pythonprint("hello world!");
message="hello world !";

upper()函数,作用将所有字母大写 lower()函数,作用将所有字母大写 字符串拼接使用+号,\t是缩进两个字符,\n是换行

pythonprint("单词首字母大写:"+message.title()+"\t所有字母大写:"+message.upper()+"\t所有字母小写"+message.lower());

四则运算(结果是1.5 与java和.net中不一样)

pythonprint("单词首字母大写:"+message.title()+"\t所有字母大写:"+message.upper()+"\t所有字母小写"+message.lower());
result=3/2;print(result);
result=0.2 + 0.1;#结果是0.30000000000000004,在运算过程中我们要截取精度,运算中保留4位,结果保留2位小数
print(result);print("想要的结果要经过四舍五入:"+str(round(result,2)));#ceil(x) 进一,floor(x)抹零

字符串在与变量进行拼接时候要注意:变量要使用str()函数转换成字符串,java和.net中可以智能判断不需要转换

pythonprint("单词首字母大写:"+message.title()+"\t所有字母大写:"+message.upper()+"\t所有字母小写"+message.lower());
result=3/2;print(result);
result=0.2 + 0.1;#结果是0.30000000000000004,在运算过程中我们要截取精度,运算中保留4位,结果保留2位小数
print(result);print("想要的结果要经过四舍五入:"+str(round(result,2)));#ceil(x) 进一,floor(x)抹零
age=34;welcomeMsg="牛牛今年"+str(age)+"岁了"; print(welcomeMsg);

列表学习,通常意义就是数组,通过下角标索引(从0开始)访问列表元素

pythonprint("单词首字母大写:"+message.title()+"\t所有字母大写:"+message.upper()+"\t所有字母小写"+message.lower());
result=3/2;print(result);
result=0.2 + 0.1;#结果是0.30000000000000004,在运算过程中我们要截取精度,运算中保留4位,结果保留2位小数
print(result);print("想要的结果要经过四舍五入:"+str(round(result,2)));#ceil(x) 进一,floor(x)抹零
age=34;welcomeMsg="牛牛今年"+str(age)+"岁了"; print(welcomeMsg);
mylist=["张三","李四","王五"];
i=0;#此种方式通过java或.net中的语言特点迁移
for li in mylist:
    print(mylist[i]);#python要注意子语句是通过缩进来体现的
    print("foreach取值:"+li);#python语法中没有的,相当于其他语言中的foreach循环
    i=i+1;
print("下面为Python的写法:");
for i in range(len(mylist)):
    print(mylist[i]);#通过列表的下角标访问列表值

列表的增删改

pythonemptylist=[];
emptylist.append("李艾特");#向列表中新增元素,即增删改中的增
emptylist.append("马海江");
emptylist.append("王志权");
emptylist.append("刘元博");
emptylist.insert(2,"牛牛");#插入函数的索引代表原索引位置的前面
print(emptylist[2]);#列表通过索引访问,索引为-1代表最后一位,-2代表倒数第二位,以此类推

从列表中删除元素,注意del是语句而非方法,即增删改中的删

pythondel emptylist[2];
names='';
for o in emptylist:
    if names!='':
        names+="-";
    names+=o;
print(names);

如果不传递参数,默认是从最后一个元素删除,参数索引正数代表从开头,负数代表从结尾

pythondelo=emptylist.pop(-2);
print("您刚才删除的学生姓名是:"+delo);
remove_obj=emptylist[0];
emptylist.remove(remove_obj);#remove只会删除第一个遇见的值,重复值需要通过循环来删除
print("您刚才通过remove删除的学生姓名是:"+remove_obj);

操作列表(排序、反转、聚合)

pythondelo=emptylist.pop(-2);
print("您刚才删除的学生姓名是:"+delo);
remove_obj=emptylist[0];
emptylist.remove(remove_obj);#remove只会删除第一个遇见的值,重复值需要通过循环来删除
print("您刚才通过remove删除的学生姓名是:"+remove_obj);
cars=["auto","toyota","bmw","audi"];
cars.reverse();print(cars);#倒着打印,而非排序,都是改变原有的列表
cars.sort();print(cars);#此函数无返回值,永久性的改变原有列表的元素顺序
cars.sort(reverse=True);print(cars);#倒序
print("临时排序的列表:"+str(sorted(cars)));print(cars);#临时排序,返回值就是排序后的结果列表
numlist=[1,2,3];print("列表的最大值是:"+str(max(numlist))+"\t"+"列表的最小值:"+str(min(numlist))+"\t"+"列表的和的值:"+str(sum(numlist))+"\t"+"列表的平均值:"+str(sum(numlist)/len(numlist)));
for i in range(1,5): #range函数[start,end)参数为左闭右开区间,如果省略start参数,则代表从0开始
    print(i);

列表的动态语句声明方式,切片a:b代表左闭右开区间,和range类似,从第a+1个元素开始取,取到第b(含)个元素

pythonsquarelist=[value**2 for value in range(5)];
print(squarelist);
print("列表的切片(取第三到第五个元素前面的元素):"+str(squarelist[2:4])+"列表省略起始参数代表从0开始:"+str(squarelist[:2])+"列表省略截止参数代表到最后一个元素:"+str(squarelist[3:])+"列表省略起始参数负数代表从右开始:"+str(squarelist[-3:])+"复制列表结果:"+str(squarelist[:]));

元组

pythonsquarelist=[value**2 for value in range(5)];
print(squarelist);
print("列表的切片(取第三到第五个元素前面的元素):"+str(squarelist[2:4])+"列表省略起始参数代表从0开始:"+str(squarelist[:2])+"列表省略截止参数代表到最后一个元素:"+str(squarelist[3:])+"列表省略起始参数负数代表从右开始:"+str(squarelist[-3:])+"复制列表结果:"+str(squarelist[:]));
yuanzu=(10,100,1000);#元组不能使用for表达式
print(yuanzu);
pythona=70;
if a>=80:
    print("您的技能等级为:A");
elif a>=60 and a<80:
    print("您的技能等级为:B");
elif a>=30 and a<60:
    print("您的技能等级为:C");
else:
    print("您的技能等级为:D");
pythonstudentlist=[];
for i in range(1,6):
    studnet={};
    if i==4:
        continue;
    studnet["id"]=i;
    studnet["name"]="学生"+str(i);
    studentlist.append(studnet);
print(studentlist);
pythonfor student in studentlist:
    if student["name"]=="学生2":

del student["name"];#删除的不是一个整体字典,而是字典中的一个key/value键值对

python        studentlist.remove(student);
print(studentlist);
pythoncourse={
    'firstbook':"JQuery",
    'secondbook':"MySQL",
    'thirdbook':"JSP"
}
print("本学期的课程和书名如下:\n");
for k,v in course.items():
    print("\t"+k+"的书名是:"+v+"\n");
print("本学期的课程如下:\n");
for k in course.keys():
    print(k);
print("本学期的书名如下:\n");
for v in course.values():
    print(v);

人机交互

pythonmsg=input("请您输入一些消息,我将会为您打印此消息:");
print("刚才您通过键盘键入的内容为:"+str(int(msg)>18));
i=1;
while i<11:
    print(i);
    i=i+1;

函数定义

pythondef showMsg(username,weltime="晚上好"):#username是形参,默认值全部放到形参的最后
    print("hello"+"\t"+username+"\t"+weltime);
    return "hello"+"\t"+username+"\t"+weltime;

showMsg("牛牛","早上好");#"牛牛"是实参,通过位置实参传递
showMsg(weltime="中午好",username="牛牛");#通过形参名字传参,无关参数顺序
showMsg(username="牛牛");
pythonprint("我是利用函数返回值打印出来的:"+showMsg("牛牛","早上好"));
pythonmethodlist=["张三","李四","王五"];
def optList(list):
    for mtd in list:
        if mtd=='李四':
            list.remove(mtd);
            break;
    return list;
print(optList(methodlist[:]));#传递实参时候可以传递列表副本
print(methodlist);

向函数传递字符串元组names

pythondef makeCake(size,*names):
    rtn='';
    for name in names:
        if rtn!='':
            rtn+=",";
        rtn+=name;
    return rtn+"订做了"+str(size)+"寸的蛋糕";
print(makeCake(12,'张三','李四','马六'));

向函数传递字典

pythondef showUserInfo(username,usersex,**other):
    userEntity={};
    userEntity["username"]=username;
    userEntity["usersex"]=usersex;
    for k,v in other.items():
        userEntity[k]=v;
    print(userEntity);
showUserInfo("张三","男",age=20,banji="电信1701");