zoukankan      html  css  js  c++  java
  • python基础编程

    1.if else

    var1 = 100
    if var1:
       print ("1 - if 表达式条件为 true")
       print (var1)
    
    #为0时,条件不成立
    var2 = 0
    if var2:
       print ("2 - if 表达式条件为 true")
       print (var2)
    else:
        print("2 - if条件不成立")
    print ("Good bye!")

    打印:
    1 - if 表达式条件为 true
    100
    2 - if条件不成立
    Good bye!
    age = int(input("input your dog’s age:"));
    
    if age < 0:
        print("你逗我呢吧...");
    elif age == 1:
        print("相当于14岁的人");
    elif age == 2:
        print("相当于22岁的人");
    elif age > 2:
        #计算年龄
        humanAge = 22 + (age - 2)*5;
        print("对应人类年龄:", humanAge);

    1 - if 表达式条件为 true
    100
    2 - if条件不成立
    Good bye!

    2.字符串操作

    '''
    Created on 2016年12月2日
    
    @author: 
    '''
    # 字符串操作
    
    a = "Hello";
    b = "Python";
    
    print("a+b=", a + b);#字符串连接    HelloPython
    print("a*2=", a * 2);#重复输出字符串    HelloHello
    print("a[2]=", a[2]);#通过索引获取字符串中字符    l
    print("a[:4]", a[:4]);#截取字符串中的一部分    Hell
    print("o in a:", "o" in a);#如果字符串中包含给定的字符返回 True
    print("xx not in a:", "xx" not in a);#如果字符串中不包含给定的字符返回 True
    #"r"或者"R" : 原始字符串,没有转义或者特殊字符
    print(r"
    ");#
    
    # % : 格式字符串
    print("%s 是字符串, %d 是数字" % (b, 12));#Python 是字符串, 12 是数字
    
    # 3个引号
    hi = ''' hai how are you,
            第二行
        ''';
    print("3引号hi:", hi);
    #Unicode 字符串
    print("Unicode 字符串:" + u"Hellou0020World !");
    
    #======================================================
    
    mystr = "this is string example....wow!!!";
    capstr = mystr.capitalize();#首字母大写
    print("原字符串:", mystr);
    print("首字母大写:", capstr);
    print("居中:", mystr.center(4));
    print("统计【i】出现的次数:", mystr.count("i", 0 ,len(mystr)));

    a+b= HelloPython
    a*2= HelloHello
    a[2]= l
    a[:4] Hell
    o in a: True
    xx not in a: True

    Python 是字符串, 12 是数字
    3引号hi: hai how are you,
    第二行

    Unicode 字符串:Hello World !
    原字符串: this is string example....wow!!!
    首字母大写: This is string example....wow!!!
    居中: this is string example....wow!!!
    统计【i】出现的次数: 3

    3.函数

    #函数定义
    def printStr(mystr):
        "函数功能:打印传入的字符串"
        print("传入参数为:", mystr);
        return;
    
    #函数调用
    printStr("helloPython");

    传入参数为: helloPython
    #lambda函数的语法只包含一个语句,如下:lambda [arg1 [,arg2,.....argn]]:expression
    
    sum = lambda arg1, arg2 : arg1 + arg2;
    
    print("相加:",sum(20, 100));
    print("相加2:",sum(-20, -100));

    相加: 120
    相加2: -120

    4.for用法

    #基本for循环
    languages = ["C", "C++", "Perl", "Python"];
    for i in languages:
        print(i);
    
    #Break
    print("--------------------------");
    sites = ["Baidu", "Google","Runoob","Taobao"]
    for s in sites:
        if s == "Runoob":
            print("到Runoob,循环停止");
            break;
        else:
            print(s);
    print("===end===");

    C
    C++
    Perl
    Python
    --------------------------
    Baidu
    Google
    到Runoob,循环停止
    ===end===

    #for和数列
    #如果你需要遍历数字序列,可以使用内置range()函数。它会生成数列
    #0~n-1的值
    for i in range(10):#0 1 2 ... 9
        print(i);
        
    #区间的值
    print("----------------")
    for j in range(3, 11):
        print(j);

    0
    1
    2
    3
    4
    5
    6
    7
    8
    9
    ----------------
    3
    4
    5
    6
    7
    8
    9
    10

    5.模块调用

    #_Module 新建一个模块
    #模块方法1:
    def print_info():
        print("模块_Module.print_info()被调用了");
        return;
    
    #模块方法2:加法计算
    def add(a, b):
        print("结果:", a + b);
        return a + b;
    #_ModuleCall 导入其他模块,可使用所有函数
    import _Module
    
    #调用其他模块的函数
    _Module.print_info();
    _Module.add(3, 5);

    模块_Module.print_info()被调用了
    结果: 8

    #_ModuleCall2 只导入其他模块的其中部分函数
    from _Module import add
    
    add(10, 9);

    结果: 19

    6.日历

    #日期
    
    import calendar;
    
    cal_2016_1 = calendar.month(2016, 1);
    print("2016年1月日历:");
    print(cal_2016_1);

    2016年1月日历:
    January 2016
    Mo Tu We Th Fr Sa Su
    1 2 3
    4 5 6 7 8 9 10
    11 12 13 14 15 16 17
    18 19 20 21 22 23 24
    25 26 27 28 29 30 31

    逃避不一定躲得过,面对不一定最难过
  • 相关阅读:
    下载超过 28762W 次的 Java面试题库(附答案)
    后端程序员必备:SQL高性能优化方案!50条优化,建议马上收藏!
    [ABP教程]第七章 作者:数据库集成
    [ABP教程]第六章 作者:领域层
    [ABP教程]第五章 授权
    [ABP教程]第四章 集成测试
    [ABP教程]第三章 创建、更新和删除图书
    [ABP教程]第二章 图书列表页面
    [ABP教程]第一章 创建服务端
    [Skill] git下载助手
  • 原文地址:https://www.cnblogs.com/yangzhenlong/p/6134217.html
Copyright © 2011-2022 走看看