• python基础语法


    1.输入输出循环判断

    num=10
    bingo=False
    while bingo==False:         #False的写法要注意
         answer=input()
         if  answer<num:
               print 'too small'
         if  answer>num:
               print 'too big'
         if  answer==num:
               print 'BINGO'
               bingo=True
    for i in range(1,21):                  #从1循环到20
        print i                             #注意必须有首行缩进

    格式化输出:

    >>> num=18
    >>> print 'My age is %d' %num
    My age is 18
    >>> print 'Today is %s.'%'Friday'
    Today is Friday.
    >>> print "%s 'score is %f" %('Lily',90.2)
    Lily 'score is 90.200000
    >>> print "%s 'score is %.2f" %('Lily',90.2)
    Lily 'score is 90.20
    for i in range(0,5):
        for j in range(0,i+1):
            print '*',                 #要想在同一行输出,后面要打上逗号
        print                       #输出空时,会换行

    2.主要记录一下与C语言不同的地方和特别需要注意的地方:

    // 整除

    ** 乘方

    整数没有长度限制,浮点数长度限制(小数点后16位)

    复数: 

    >>> 1j*1j
    (-1+0j)

    3.导入模块:

    import

    ①import math     #导入math中所有函数 使用时要用 math.sqrt()的形式

    ②from math import *  #导入math中的所有函数, 可直接使用sqrt()

    ③from math import sqrt, tan  #只导入sqrt和tan两个函数 推荐使用

    复制代码
    >>> import math
    >>> math.sqrt(5)
    2.23606797749979
    >>> math.sqrt(2)*math.tan(22)
    0.012518132023611912
    >>> from math import *
    >>> log(25+5)
    3.4011973816621555
    >>> sqrt(4)*sqrt(100)
    20.0
    >>> from math import sqrt, tan
    >>> tan(20)*sqrt(4)
    4.474321888449484
    复制代码

    4.字符串: ‘ ’, “ ”, “““ ”””,''' '''

    len(): 求字符串长度 返回的是整形 不像C有个‘’ 返回的是字符串本身的长度

    +:字符串拼接

    *:多次拼接

    一个字符串如果有换行,那么只能用“““ ”””或者''' '''

    表示为同一行

    复制代码
    >>> len("""No No No""")
    8
    >>> "No"*10
    'NoNoNoNoNoNoNoNoNoNo'
    >>> "No"+' Yeah!'
    'No Yeah!'
    >>> 
    复制代码
    >>> '''this is the 
    same line'''
    'this is the same line'
    str='''
    "What's your name?" I asked.
    "I'm Han Meimei."
    '''
    print str

    得到的结果:

    "What's your name?" I asked.
    "I'm Han Meimei."

    5.帮助:

    dir():括号中是导入模块后的模块名,列出模块的所有函数

    dir(__builtins__):查看Python内置函数清单

    help():括号中是函数名,查看函数的文档字符串

    print():打印函数文档字符串

    如:print(math.tanh.__doc__)

          print(bin.__doc__)

    6.类型转换:

    float(): 把整数和字符串转换为浮点数

    str(): 把整数和浮点数转换为字符串

    int():把浮点数和字符串转换为整数 舍弃小数部分  字符串必须长得像整数 “123.5”是不可以的  int('325')是可以的

    round(): 浮点数转整数 四舍六入五成双 不支持字符串

    bool(): 为0的数字,包括0.0会被视为false;空集合,包括()/{}/[]会被视为false;空字符串会被视为false

    bin():把十进制整数转换为二进制字符串,前面会有0b

    复制代码
    >>> str(3.1415)
    '3.1415'
    >>> float('3')
    3.0
    >>> int("123")
    123
    >>> int("123.5")
    Traceback (most recent call last):
      File "<pyshell#30>", line 1, in <module>
        int("123.5")
    ValueError: invalid literal for int() with base 10: '123.5'
    >>> int(123.5)
    123
    >>> round("123"ArithmeticError)
    SyntaxError: invalid syntax
    >>> 
    复制代码
    >>> bool('ad')
    True
    >>> bool('False')
    True
    >>> bool(False)
    False
    >>> bool('')
    False
    >>> bool(0)
    False
    >>> bool(-123)
    True
    >>> 
    >>> bin(10)
    '0b1010'
    >>> bin(-4)
    '-0b100'
    >>>bin(10.3)  #错误,不能转换小数

    7.多重赋值:

    复制代码
    >>> x,y,z=1,"r",1.414
    >>> x
    1
    >>> y
    'r'
    >>> z
    1.414
    复制代码

    交换变量的值

    >>> y,x=x,y
    >>> x
    'r'
    >>> y
    1

    8.random

    from random import randint
    i=randint(5,10)
    print i
  • 相关阅读:
    相似性算法研究
    CNN实战篇-手把手教你利用开源数据进行图像识别(基于keras搭建)
    主流NoSQL数据库的分析与选择
    CentOS7下防火墙相关命令
    Error:(10, 32) java: 程序包org.springframework.xxxl不存在
    Windows下IDEA上传码云push的时候出现push to origin/master was rejected
    SpringBoot配置mybatis使用的两种方式
    SpringBoot+Mybatis-plus测试时碰到的奇怪时区问题
    Java String 在拼接时的编译器优化
    Java Maven项目更新后jdk版本变为1.5
  • 原文地址:https://www.cnblogs.com/wy1290939507/p/4744431.html
Copyright © 2020-2023  润新知