zoukankan      html  css  js  c++  java
  • python学习笔记入门

    Python学习笔记

    1. 循环次数限制

    lucky_num = 19
    guess_count = 0

    while guess_count<3:
        print("guess count:",guess_count)
        input_num = int(input("Input the guess num:"))
        if input_num>lucky_num:
            print("the real number is smaller.")
        elif input_num<lucky_num:
            print("the real num is bigger...")
        else:
            print("Bingo!")
            break

        guess_count += 1
    else:
        print("Too many retrys!")

    2. for循环

    lucky_num = 19

    for i in range(3):
        input_num = int(input("Input the guess num:"))
        if input_num>lucky_num:
            print("the real number is smaller.")
        elif input_num<lucky_num:
            print("the real num is bigger...")
        else:
            print("Bingo!")
            break

    else:
        print("Too many retrys!")

    3. %号表示【】

    name = input("name:")
    age = input("age:")
    job = input("job:")

    print("Information of %s: Name:%s Age:%s Job:%s" %(name,name,age,job))

    不建议使用+,占空间

    三个单引号注释

    在使用pycharm时,经常会需要多行代码同时缩进、左移,pycharm提供了快捷方式

    1、pycharm使多行代码同时缩进

       鼠标选中多行代码后,按下Tab键,一次缩进四个字符

    2、pycharm使多行代码同时左移

      鼠标选中多行代码后,同时按住shift+Tab键,一次左移四个字符

    实例:

    name = input("name:")
    age = input("age:")
    job = input("job:")

    msg = '''
    Information of %s:
        Name:%s
        Age:%s
        Job:%s
    ''' %(name,name,age,job)
    print(msg)

    Strip()

    Append追加''clear'清除空间, 'copy'复制, ' count() 方法用于统计字符串里某个字符出现的次数。可选参数为在字符串搜索的开始与结束位置。

    ', 'extend 函数用于在列表末尾一次性追加另一个序列中的多个值(用新列表扩展原来的列表)。

    ', 'index'索引, 'insert 函数用于将指定对象插入列表的指定位置。

    ', 'pop' 函数用于移除列表中的一个元素(默认最后一个元素),并且返回该元素的值。

    , 'remove' 函数用于移除列表中某个值的第一个匹配项。

    , 'reverse函数用于反向列表中元素。

    ', 'sort函数用于对原列表进行排序,如果指定参数,则使用比较函数指定的比较函数。

    '实例:

    name_list = ["alex","65brother","tenglan"]
    name_list.append("ERIC")
    name_list.insert(2,"66brother")
    name1 = name_list[3]
    print(name1)
    print(dir(name_list))
    print(name_list.index("65brother"))
    print(name_list.count("65brother"))
    print(name_list[2])
    print(name_list.pop())
    print(name_list.reverse())
    name_list.append("*")

    实例2

    name_list = [1,2,3,4,"Alex","John"]
    a = name_list
    name_list2 = ["Kevin","Mike"]
    b = name_list2
    a.extend(b)
    name = "Nebula"
    a.extend(name)
    print(a)

    4. 元组

    5. 运算符

    +%余数,**返回xy次幂,//返回商的整数部分

    &,与;|,或;^,异或,一真一假即为真;<<左移除2>>右移×2

    实例:

    a = [1,2,3,4,5,6]
    b = (1,2,3,4)
    print(type(a))
    print(type(b))

    6. 嵌套循环

    for j in range(5):
        for i in range(10):
            if i<5:
               continue

            if j>3:
               break
            print(i)

    7. 文件读写

    '''f = open("test.log","w")

    f.write("This is the first line ")
    f.write("This is the second line ")
    f.write("This is the third line ")
    f.write("This is the fourth line ")
    '''
    f = open("test.log","r")
    print(f.read())
    f.close()

    f = open("test.log","r")
    for line in f:
        print(line)
    f.close()

    Tips:Write直接覆盖原来文件

    写读实例:

    f = open("test.log","w+")

    f.write("new line ")
    print(f.readline())
    print("data:",)
    f.close()

    8. 字典

    9. 不同种类的Python

    (1)Cpython,C解释器,.pyc(字节码) 机器码 CPU

    (2)jpython,java解释器,java字节码 机器码 CPU

    (3)ironpython. C#

     sys.argv:是一种列表类型,用于获取命令行参数的列表。

  • 相关阅读:
    node-sass 安装失败
    js中复制功能总结
    设置NODE_ENV=test环境变量
    js eslint语法规范错误提示代码
    npm安装node包时怎么显示安装进度
    前端面试题总结三
    5种方式将数字转成千分位
    前端面试题总结二(js原型继承)
    前端面试题总结一(js变量和函数声明提前相关)
    1109 Group Photo (25分)
  • 原文地址:https://www.cnblogs.com/resort-033/p/12198269.html
Copyright © 2011-2022 走看看