qidao123.com技术社区-IT企服评测·应用市场
标题:
python快速直白入门(半新手向,老手复习向)
[打印本页]
作者:
千千梦丶琪
时间:
2023-5-8 16:39
标题:
python快速直白入门(半新手向,老手复习向)
主用python做项目有一段时间,这次简单总结学习下。为后面的项目编写,进行一次基础知识的查缺补漏、
1、变量名和数据类型
"""
变量名,只能由" 数字、大小写字母、_ " 组成,且不能以数字开头
"""
# 整数 int
# hashable,不可变对象
a = 5
# 浮点数 float
# hashable,不可变对象
a1 = 3.14
# 字符串 string
# hashable,不可变对象
a_1 = "哈哈哈"
str_num = '5'
_str_float = """3.14"""
_ = '''hello world''' # 常常用于接收我们不需要使用的值
# 列表 list
# 元素可修改,元素有顺序
# 列表是unhashable, 可变对象
tmp_list = [1, 3.14, "haha", [7, "qq"]]
# 元组 tuple
# 元素不可修改,元素有顺序
# 元组是hashable,不可变对象
tmp_tuple = (1, 3.14, "haha", [7, "qq"])
# 集合 set
# 元素不重复,即使重复,也会自动去重。元素没有顺序
# 元素必须是hashable,不可变对象
# 集合是unhashable, 可变对象
tmp_set = {1, 1, 3.14, "haha"}
# 字典 dict 键值对形式--key:value,key不能重复
# key必须为hashable,不可变对象
# 字典是unhashable,可变对象
tmp_dict = {1: "xy", "a": 123, (1, "a"): [1, "abc", 3]}
# 布尔值 bool
# 布尔值是hashable,不可变对象
x = True
y = False
# None 不是0,不是空字符串"",不是False,啥也不是
# None是hashable, 不可变对象
t = None
# 常量 变量名用大写字母
# 约定俗成,并不是不可变
PI = 3.1415926
INT_LIST = [1, 2, 3, 4, 5]
复制代码
2、注释&格式化输出
# 注释:对代码的说明性文字,不参与代码运行、运算
# 单行注释
"""
多行注释
多行注释
多行注释
"""
'''
多行注释
多行注释
多行注释
'''
# 格式化输入
name = "alex"
age = 100
# 占位符(不推荐)
res = "姓名: %s,年龄: %d" % (name, age) # 字符串就是%s 整数就是%d 浮点数%f
print("占位符输出: %s" % res)
# format(推荐)
res = "姓名:{},年龄:{}".format(name, age)
res = "姓名:{name},年龄:{age}".format(age=age, name=name)
print("format函数输出:{}".format(res))
# f前缀(python3.6以上)
res = f"姓名:{name},年龄:{age}"
print(f"f前缀输出:{res}")
复制代码
3、代码执行顺序
先调用会报错,放在后面即可运行。
"""
从上往下执行
"""
a = 3
def plus(x, y):
return x + y
b = plus(a, 5)
print(b)
复制代码
4、算数运算符&整浮互相转化&精度问题
"""
算术运算符: +、 -、 *、 /、 //、 **、%
"""
# 加
res = 2 + 3
print(f"2 + 3 = {res}")
# 减
res = 2 - 3
print(f"2 - 3 = {res}")
# 乘
res = 2 * 3
print(f"2 * 3 = {res}")
# 除
res = 2 / 3
print(f"2 / 3 = {res}")
# 整除
res = 5 // 2
print(f"5 // 2 = {res}")
# 取余
res = 5 % 2
print(f"5 % 2 = {res}")
# 次幂
res = 2 ** 3
print(f"2 ** 3 = {res}") # 2的三次方
"""
浮点数和整数的互相转换
以及常用函数abs
"""
# 整数转浮点数
a = 5
print("a = 5, a的类型为:{}".format(type(a)))
a = float(a)
print("float(a) = {}, a的类型为:{}".format(a, type(a)))
# 浮点数转整数,直接取整数位,没有四舍五入
b = 3.55
print("b = 3.55, b的类型为:{}".format(type(b)))
b = int(b)
print("int(b) = {}, b的类型为:{}".format(b, type(b)))
# abs 取绝对值
c = -3.14
print("c = -3.14, c的类型为:{}".format(type(c)))
c = abs(c)
print("abs(c) = {}, c的类型为:{}".format(c, type(c)))
# round,精度低,不推荐
b = 3.55
b = round(b, 1)
print("round(b) = {},b的类型为:{}".format(b, type(b))) # 3.5不是3.6
# 标准库decimal
from decimal import Decimal
b = 3.55
b = str(b)
b = Decimal(b).quantize(Decimal("0.1"),rounding="ROUND_HALF_UP") # 四舍五入 Decimal保留小数的位数
print("round(b) = {},b的类型为:{}".format(b, type(b)))
复制代码
5、赋值运算符
"""
赋值运算符:
=, +=, -=, *=, /=, //=, %=, **=
"""
a = 2
a = a + 2 # a = 4
a += 2 # a = 6
a = a - 2 # a = 4
a -= 2 # a = 2
a = a * 2 # a = 4
a *= 2 # a = 8
复制代码
6.1、编码
"""
在计算机中存储的是二进制数 10011011
在计算机最小存储单位,1位二进制数,即1个Bit(比特)
1 Byte(字节) = 8 Bits
1 KB = 1024 Bytes 2**10 = 1024
1 MB = 1024 KB
编码问题:
1、ASCII编码,只有大小写字母、数字、特色符号,
用1个Byte表示一个字符
例如:字母A 01000001
2、GB2312,Euc-kr,Shift_JIS ...
各国有各国的标准,必然会冲突,即乱码
3、Unicode编码(万国码),所有语言统一使用这一套编码
通常用2个Byte表示一个字符(生僻字符要4个Byte)
依然存在问题:
例如用户只用英文的话,采用这种编码,
占用的存储空间是ASCII编码的2倍。
例如:字母A 00000000 01000001
4、UTF-8编码
把Unicode字符集编码成1-6个Byte
(1)大小写英文字母、数字、特殊符号
维持ASCII编码,1个Byte
例如,字母A的ASCII码和UTF-8码是一样的
01000001
(2)中文通常是 3个Byte
(3)生僻的字符 4-6个Byte
5、Python3.X 源码.py文件默认使用UTF-8
Python3.X 默认使用ASCII,所以需要指明
# -*- coding:UTF-8 -*-
6、计算机内存中,使用的是Unicode编码
需要存储或传输时,会吧内存中的Unicode转为UTF-8
"""
复制代码
6.2、字符串常用方法
"""
去除首尾指定字符,默认去除空格,返回值均为字符串
strip() # 左右都去除
lstrip() # 只去除左边
rstrip() # 只去除右边
"""
tmp = "----举个--栗子---"
tmp_lstrip = tmp.lstrip("-")
tmp_rstrip = tmp.rstrip("-")
tmp_strip = tmp.strip("-")
print("tmp_lstrip:{}".format(tmp_lstrip))
print("tmp_rstrip:{}".format(tmp_rstrip))
print("tmp_strip:{}".format(tmp_strip))
"""
startswith() 判断字符串是否以指定的字符串开头
endswith() 判断字符串是否以指定的字符串结尾
返回值均为布尔值,即True或False
"""
tmp = "这次继续举个栗子"
if tmp.startswith("这次"):
print("tmp是以'这次'开头的")
if tmp.endswith("子"):
print("tmp是以'子'结尾的")
"""
分割字符串
split(sep, maxsplit):
1、sep:指定分割符,默认为空格
2、maxsplit:分割次数,默认为-1即全部分割
3、返回值为一个列表list
"""
tmp = "Python,C,C++,Java,Vue"
res = tmp.split(",")
res_2 = tmp.split(",", 2)
print("res:{}".format(res))
print("res_2:{}".format(res_2))
"""
lower() 全转为小写
upper() 全转为大写
"""
tmp = "What's up! Man!"
tmp_l = tmp.lower()
tmp_u = tmp.upper()
print("大写:{}".format(tmp_u))
print("小写:{}".format(tmp_l))
"""
is系列方法,最常用的三个:
isalpha() 判断字符串是否仅含有字母
isdigit() 判断字符串是否仅含有数字
isalnum() 判断字符串是否仅含字母或数字
均返回布尔值,即True或False
"""
tmp_1 = "23"
tmp_2 = "python"
tmp_3 = "python666"
if tmp_1.isdigit():
print("tmp_1中仅含数字")
if tmp_2.isalpha():
print("tmp_2中仅含字母")
if tmp_3.isalnum():
print("tmp_3中仅含字母或数字")
"""
子字符串替换
replace(old, new, count)
1、old:被替换的子字符串
2、new:新的子字符串
3、count:替换次数,默认为-1全部替换
"""
tmp = "Python66666"
# 全部替换
tmp_1 = tmp.replace("6", "9")
# 从左往右,替换指定次数
tmp_2 = tmp.replace("6", "1", 4)
# 找不到指定的子字符串,则不替换
tmp_3 = tmp.replace("java", "go")
print(tmp_1)
print(tmp_2)
print(tmp_3)
"""
获取子字符的索引
1、index(sub,start,end) 从左往右
1)sub,子字符
2)start,查找的起始位置
3)end,查找的终止位置-1
2、rindex() 从右往左
3、只找一个,且若sub存在,则会报错
"""
# 012345678
tmp = "Python666"
# 正向查找
index_1 = tmp.index("6", 5, 7) # 实际是在第5个索引和第6个索引查找
# 反向查找
index_2 = tmp.rindex("6")
print("index_1:{}".format(index_1))
print("index_2:{}".format(index_2))
复制代码
6.3、字符串转义
"""
字符串转义:
\n 换行符,将光标位置移到下一行开头。
\r 回车符,将光标位置移到本行开头。
\t 水平制表符,即Tab键
\a 蜂鸣器响铃。现在的电脑一般都没有蜂鸣器了
\b 退格(Backspace),将光标位置移到前一列。
\\ 反斜杠
\' 单引号
" 双引号
"""
a = "这是换行,\n 这是\r回车符,这是\t制表符," \
"这里是退格\b\n" \
"这是反斜杠\\, 这是单引号\', 这是双引号""
print(a)
复制代码
7、比较运算符&if...else
[code]"""比较运算符: ==, !=, >, >=,
欢迎光临 qidao123.com技术社区-IT企服评测·应用市场 (https://dis.qidao123.com/)
Powered by Discuz! X3.4