zoukankan      html  css  js  c++  java
  • python学习记录(持续更新)--最最最基础的一部分(变量、字符串、运算符、常见函数)

    写在前面

    本系列教程针对有一定编程经验的伙伴快速入门python基础,一些涉及开发的常识问题,本文并不涉及。

    几个常用的函数

    print 打印 

    print('*' * 10) =>  打印出10个*

    input 获取输入

    name = input('What is your name?')

    int  float bool str 类型转换

    num=int('12345')
    

     len upper lower  replace in 

    course='Python for Beginners'
    print(len(course)) =>20
    print(course.upper())=>PYTHON FOR BEGINNERS
    print(course.lower())=> python for beginners
    print(course.title())=> Python For Beginners 首字母大写
    print(course)=>Python for Beginners upper 并不会更改原有字符串
    print(course.find('P')) => 0 找到第一个出现的索引 区分大小写
    print(course.replace('Beginners','Absolute Beginners')) =>Python for Absolute Beginners  替换字符
    
    
    print('Python' in course)=> True 是否包含某字符串
     

     range

    for item in range(10):
        print(item) =>0~9
    for item in range(5,10):
    print(item) =>5~9
    
    
    for item in range(5,10,2): 步长
    print(item) => 5,7,9
     

     split

    message="My name is Jaychou"
    words=message.split(' ')
    print(words)
    =>['My', 'name', 'is', 'Jaychou']

    几个细节

    字符串输入的几种方式

    print(' ')
    print(" ")
    print('''  
    这种可换行输入
    很方便
    
    ''')
    

      索引

    coutse='my name is Jay Chou'
    print(coutse[0])
    
    => m
    print(coutse[-1])
    => u
    coutse='myname is Jay Chou'
    print(coutse[0:3])
    =>myn 从0开始算数3个字符打印
    print(coutse[0:])
    =>myname is Jay Chou 不指定结尾打印所有字符
    print(coutse[:5])
    =>mynam 开头不指定默认从0开始
    coutse='myname is Jay Chou'
    another = coutse[:]
    print(another)
    =>myname is Jay Chou 用于clone字符串

    格式化输入

    first= 'Jay'
    last= 'Chou'
    msg = f'{first}[{last}] is a coder'
    print(msg) =>Jay[Chou] is a coder

    运算符

    print(10/3) =>3.3333333333333335
    print(10//3) =>3
    print(10 % 3)=> 1
    print((10**3)) =>1000 幂次运算
    x = 10
    x +=3
    print(x) =>13

    print(round(2.5)) => 2
    print(round(2.51)) =>3 
    round(80.23456, 2) :  80.23
    round(100.000056, 3) :  100.0
    round(-100.000056, 3) :  -100.0
    print(abs(-2.5)) => 2.5 

    运算函数模块 math

    
    
    import  math 
    print(math.ceil(2.1)) 天花板函数 向上取整 => 3
    print(math.floor(2.1)) 向下取整 =>2


  • 相关阅读:
    48-最长不含重复字符的子字符串
    51-数组中的逆序对
    字符串的排列
    二叉树转链表
    求根
    构造二叉树
    二叉树中序遍历
    反转链表系列
    斐波那契系列
    f.lux
  • 原文地址:https://www.cnblogs.com/dcxy/p/12362477.html
Copyright © 2011-2022 走看看