栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

Python语言基础知识

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Python语言基础知识

1.1 编译器 1.1.1 定义

人生苦短,我用Python

cython

机器只能理解机器语言

程序员编写代码(Java c c++ c# python go)等------>翻译成机器语言---->

1.1.2 编译器的编译方式

 

2.0 解释型语言-Python 2.1 输出语句-print 

2.2 注释
#
快捷键 ctrl+/
注释的语句依旧存在,但是不运行

2.3 输入语句-input

阻塞代码,不允许接着运行,等待用户的输入

 

2.4 变量名

1.不允许使用关键字(Python中变色了的,不允许使用)

2.由数字,字母,下划线组成,不能有其他的特殊符号,不允许有空格,不允许有括号

3.不能以数字开头

4.可以使用下划线开头

5.常量可以使用全大写命名

2.5 赋值运算符
符号案例说明
+=a=1 a+=1 ====>a=a+1加法赋值运算符
-=a=1 a-=1 ====>a=a-1减法赋值运算符
**=幂赋值运算符
/=除法赋值运算符
2.6 随机数 2.6.1 随机生成一个整数

2.6.1.1 源码

2.6.1.2 注意点

1.randint生成的是一个随机的整数
2.需要传递两个参数,一个数是起始值,一个数是结束值
3.这个范围是左闭右闭
2.6.2 随机选择

2.6.2.1 注意点

1.需要传递的参数是一个非空的数据集合
2.随机返回一个集合中的数据
3.整数或者小数不可以作为输入数据,字符串可以,列表可以
3.0 列表 3.1 表达形式
[数据1,数据2,数据3]
list2 = [1,2,3,4,5,6,7,'asd123','123',['new1','new32']]
列表中的元素可以是不同的数据类型,甚至可以嵌套列表
3.2 列表的下表取值

下标从0开始

取出第一位 列表名[下标]

3.2.1 案例:取出一位数据
list1 = ['剪刀','石头','布']

print(list1[0])
print(list1[2])
3.2.2 案例:取出连续多位
# todo 需求 取出剪刀和石头
# print(list1[0])
# print(list1[1])
# print(list1[起始位置:结束位置])
print(list1[0:2])

列表名[起始位置:结束位置],是左闭右开的,不包含结束位置
3.2.3 案例:取出不连续多位
list2 = [1,2,3,4,5,6,7,'asd123','123',['new1','new32']]
# print(list2)
# 3-7
# print(list2[2:7])

# 需求,取出奇数位的数据元素
print(list2[1::2])
print(list2[::2])

列表名[起始位置:结束位置:步长]
3.3 列表的增加 3.3.1 源码

3.3.2 注意点
1.append没有返回值,不需要变量承接
2.append将数据追加到列表的末尾,说明修改的是原始列表
3.3.3 extend 源码

3.3.4 insert 在指定的位置之前追加元素 

3.4 列表的删除 3.4.1 remove
def remove(self, value): # real signature unknown; restored from __doc__
        """
        L.remove(value) -> None -- remove first occurrence of value.
        Raises ValueError if the value is not present.
        """
        pass
        
     注意点:
     1.可以依靠值来进行删除
     2.如果存在多个重复值,只能删除第一个
     3.如果值不存在,主动抛出错误
     4.没有返回值
3.4.2 del
del list1[20] # 按照下标删除
print(list1)


del list1   # 删除整个列表
注意点:
1.del可以通过下标来进行删除
2.下标不能超出范围
3.删除的也是原始数据
4.del 列表的名字  从内存中删除
3.4.3 clear
def clear(self): # real signature unknown; restored from __doc__
    """ L.clear() -> None -- remove all items from L """
    pass
    
注意点:
1.清空列表中的数据,保留列表
3.4.4 pop
def pop(self, index=None): # real signature unknown; restored from __doc__
        """
        L.pop([index]) -> item -- remove and return item at index (default last).
        Raises IndexError if list is empty or index is out of range.
        """
        pass
	
	注意点:
	1.删除并且返回一个索引对应的值
	2.index用来指定需要删除的下标,如果index没有传递,默认按照从末尾删除
	3.如果列表是空的,或者index超出范围,主动抛出错误
	4.pop 也是修改原始列表
3.5 列表的修改 3.5.1 通过下标来进行修改 

# 修改 将末尾位置的4改为40
list1[-1] = 40
print(list1)
注意点:
1.通过下标来进行定位
2.格式 列表名[下标] = 值

3.6 列表的查找 3.6.1 index
def index(self, value, start=None, stop=None): # real signature unknown; restored from __doc__
    """
    L.index(value, [start, [stop]]) -> integer -- return first index of value.
    Raises ValueError if the value is not present.
    """
    return 0
    
 注意点:
 1.按照值来进行查找,返回的是当前值出现的第一个下标
 2.如果值不存在,主动抛出错误
 3.如果需要使用start 或者stop,直接传递参数,不需要以start=下标这种形式
3.6.2 count
def count(self, value): # real signature unknown; restored from __doc__
    """ L.count(value) -> integer -- return number of occurrences of value """
    return 0
    
 注意点:
 1.返回的是当前值出现的次数
 2.如果值不存在,返回的是0
3.7 copy复制 3.8 逆置 3.8.1 reverse
def reverse(self): # real signature unknown; restored from __doc__
    """ L.reverse() -- reverse *IN PLACE* """
    pass
    
注意点:
1.当前方法,用来逆置列表
2.修改的是原始列表

3.8.2 内置reversed
class reversed(object):
    """
    reversed(sequence) -> reverse iterator over values of the sequence
    
    Return a reverse iterator
    """
    
  注意点:
  1.没有修改原始数据
  2.返回的是一个逆置对象,如果需要的数据是列表格式,需要进行强制类型转换list()
print('原始数据',list1)
# list1.reverse()
ret = list(reversed(list1))
print(ret)
print('逆置之后的数据',ret)
3.9 sort 3.9.1 sort
def sort(self, key=None, reverse=False): # real signature unknown; restored from __doc__
    """ L.sort(key=None, reverse=False) -> None -- stable sort *IN PLACE* """
    pass
注意点:
1.用来对列表进行排序,默认是升序
2.修改的是原始列表
3.如果需要降序,reverse=True
3.9.2 sorted
def sorted(*args, **kwargs): # real signature unknown
    """
    Return a new list containing all items from the iterable in ascending order.
    
    A custom key function can be supplied to customize the sort order, and the
    reverse flag can be set to request the result in descending order.
    """
    pass
    
注意点:
1.对可迭代对象进行排序,默认升序
2.如果使用降序,reverse=True可以完成
list2 = sorted(list1,reverse=True)
print(list2)

4.0 字符串 4.1 表达形式
'字符串'
"字符串"
"""字符串"""(这种情况使用较少)
单引号和双引号,在Python中没有任何区别
单引号和双引号,在Java中有区别

4.2 内存中的存储

 

4.3 增加 4.3.1 +

两个字符串相加,将两个字符串合成一个字符串

4.3.2 join
 def join(self, iterable): # real signature unknown; restored from __doc__
        """
        S.join(iterable) -> str
        
        Return a string which is the concatenation of the strings in the
        iterable.  The separator between elements is S.
        """
        return ""
        
        
    注意点:
    1.传入的参数必须是可迭代对象
    2.iterable里面的元素,必须是字符串类型
4.4 删除 4.4.1 del

删除整个字符串

格式:

del 字符串名

4.5 修改 4.5.1 replace
def replace(self, old, new, count=None): # real signature unknown; restored from __doc__
    """
    S.replace(old, new[, count]) -> str

    Return a copy of S with all occurrences of substring
    old replaced by new.  If the optional argument count is
    given, only the first count occurrences are replaced.
    """
    return ""

注意点:
1.old代表的是需要替换的旧字符串
2.new代表的是新的字符串
3.count是进行替换的次数,count如果不填写,默认替换所有的目标字符串
4.返回的是替换过后的新字符串,不影响原始字符串

4.6 查找 4.6.1 index
def index(self, sub, start=None, end=None): # real signature unknown; restored from __doc__
        """
        S.index(sub[, start[, end]]) -> int
        
        Return the lowest index in S where substring sub is found, 
        such that sub is contained within S[start:end].  Optional
        arguments start and end are interpreted as in slice notation.
        
        Raises ValueError when the substring is not found.
        """
        return 0
        
        
注意点:
1.查询的目标字符串,返回的是当前字符串的最低下标
2.start,end 指定的是范围
3.如果查询不到数据,则报错
4.6.2 rindex
def rindex(self, sub, start=None, end=None): # real signature unknown; restored from __doc__
        """
        S.rindex(sub[, start[, end]]) -> int
        
        Return the highest index in S where substring sub is found,
        such that sub is contained within S[start:end].  Optional
        arguments start and end are interpreted as in slice notation.
        
        Raises ValueError when the substring is not found.
        """
        return 0
        
        
注意点:
1.查询的目标字符串,返回的是当前字符串的最高下标
2.start,end 指定的是范围
3.如果查询不到数据,则报错
4.6.3 find
def find(self, sub, start=None, end=None): # real signature unknown; restored from __doc__
        """
        S.find(sub[, start[, end]]) -> int
        
        Return the lowest index in S where substring sub is found,
        such that sub is contained within S[start:end].  Optional
        arguments start and end are interpreted as in slice notation.
        
        Return -1 on failure.
        """
        return 0
        
 注意点:
1.查询的目标字符串,返回的是当前字符串的最低下标
2.start,end 指定的是范围
3.如果查询不到数据,则返回的是-1
4.6.4 rfind
def rfind(self, sub, start=None, end=None): # real signature unknown; restored from __doc__
        """
        S.rfind(sub[, start[, end]]) -> int
        
        Return the highest index in S where substring sub is found,
        such that sub is contained within S[start:end].  Optional
        arguments start and end are interpreted as in slice notation.
        
        Return -1 on failure.
        """
        return 0

注意点:
1.查询的目标字符串,返回的是当前字符串的最高下标
2.start,end 指定的是范围
3.如果查询不到数据,则返回的是-1
4.6.5 count

4.7 判断以什么开头和以什么结尾 4.7.1 开始
def startswith(self, prefix, start=None, end=None): # real signature unknown; restored from __doc__
        """
        S.startswith(prefix[, start[, end]]) -> bool
        
        Return True if S starts with the specified prefix, False otherwise.
        With optional start, test S beginning at that position.
        With optional end, stop comparing S at that position.
        prefix can also be a tuple of strings to try.
        """
        return False
        
        
注意点:
1.需要指定进行判断的字符串
2.可以指定判断的范围,不指定默认是全部
3.返回的是布尔值
4,如果需要判断的是多个字符串,需要(字符串1,字符串2,字符串3......)

4.7.2 结束

4.7.2.1 案例

 

4.8 额外常用的方法
类型说明
upper将字符串全大写,并且返回新的字符串
lower将字符串全小写,并且返回新的字符串
capitalize将字符串首字母大写,并且返回新的字符串
strip删除空格
split切割,默认以空格的形式来进行切割,也可以指定分隔符,maxsplit指定的是切割的次数,返回值是切割之后的列表
5.0 数字 5.1 内存中的存储

5.2 Python中为了运行效率

存在小整数池

面试题Python 小整数池,[-5,256]

pycharm 为了运行,扩大了这个范围

6.0 遍历for循环 6.1 功能

挨个取出对象的每一个元素

6.2 案例
# 遍历
for i in lsit2:
    print(i)
    lsit.append(i)
注意点:

7.0 break 终止循环 7.1 作用

在满足条件的情况下,自动断开循环

7.1.1 案例
import random
​
while True:
    # todo 1.0 提示用户输入选择的手势
    player1 = int(input('请输入:0(剪刀) 1(石头)  2(布) 3(停止):'))
​
    # todo  1.1 判断用户是否停止游戏
    if player1 == 3:
        print('----------------->用户停止游戏')
        break

8.0 while else
# while   else 一起配合,什么作用
i = 0
while i<3: # 3<3
    print('-------->满足while循环条件执行的代码')
    i += 1
else:
    print('-------->不满足进入else')
    
 注意点:
 1.条件成立,进入while的语句
 2.条件不满足,进入else,只进入一遍

8.1 进程与程序的区别
  • 进程:跑起来的程序

  • 程序:以py结尾的文件

9.0 continue终止当前循环,进入下一个循环 9.1 案例
# 需求 打印0-10个数字,但是不打印5和6
for i in range(0,11):
    # 条件判断,数字不能位5,6
    if i==5 or i==6:
        # break
        # exit()
        continue
    print('此时的数字是',i)

10.0 range 10.1 range源码
class range(object):
    """
    range(stop) -> range object
    range(start, stop[, step]) -> range object
    
    Return an object that produces a sequence of integers from start (inclusive)
    to stop (exclusive) by step.  range(i, j) produces i, i+1, i+2, ..., j-1.
    start defaults to 0, and stop is omitted!  range(4) produces 0, 1, 2, 3.
    These are exactly the valid indices for a list of 4 elements.
    When step is given, it specifies the increment (or decrement).
    """

10.2 注意点
1.左闭右开
2.起始位置,结束位置,步长

10.3 如何使用range

 

11.0 元组 11.1 表现形式
t1 = (1,2,3,3,4,['asd123','qwe'],'123',(1,2))
print(t1)

注意点:
1.元组的表现形式是()
2.元素可以是整数,列表,字符串,元组

11.2 增加 11.2.1 创建的是新元组

t3 = t1+t2
print(t3)

11.3 删除 11.3.1 del
del元组名 删除整个元组 不支持del元组[下标]不支持删除一个

11.4 修改

不支持修改

11.5 查找 11.5.1 index
def index(self, value, start=None, stop=None): # real signature unknown; restored from __doc__
    """
    T.index(value, [start, [stop]]) -> integer -- return first index of value.
    Raises ValueError if the value is not present.
    """
    return 0
注意点:
1.返回的是值出现在元组中的下标,第一次出现的下标
2.如果值不存在,报错
11.5.2 count
def count(self, value): # real signature unknown; restored from __doc__
    """ T.count(value) -> integer -- return number of occurrences of value """
    return 0
    
注意点:
1.统计的是值在元组中出现的次数

11.6 下标与切片
print(t1[0]) print(t1[-2]) print(t1[1:5])

11.7 元组和列表的区别
列表元组
相同点支持下标取值,可以使用for
不同点list()将元组转化为列表tuple()将列表转化为元组
为了保证数据安全,元组不可以被修改,可以将列表转化为元组

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/283261.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号