> 文章列表 > 1-python基础

1-python基础

1-python基础

1.字面量
被写在代码中固定的值
1-python基础

2.注释

# 单行注释"""
多行注释1:三对双引号
"""'''
多行注释2:三对单引号
'''

3.变量与输出

age=10 # 变量赋值
print("我今年",age,"岁") 

1-python基础

(1)print()可当换行使用

print(1)
print()
print(2+4)

1-python基础

(2)print打印完自动换行
1-python基础

如果不需要换行,使用end=“空格”

age=10
print(age,end=" ")

1-python基础

4.基本数据类型与转换

counter= 100 # 整型变量
miles= 1000.0 # 浮点型变量
name= "jack" # 字符串
a = b = c = 1 # 多个赋值
a, b, c = 1, 2, "jack" # 多个赋值

(1)查看变量类型type()

a=111.21
b="ddw"
c=24324
d=type(c)
print(type(a),type(b),type(c),type(23.65),d)

1-python基础
(2)自动类型转换-隐式

a = 123 # int
b = 1.23 # float 
c = a + b # c自动转为float
print(c) # 123.23
print(type(c)) # float

(3)自动类型转换-显示

x="3" # x为字符串3
y = int(2.8) # 将2.8强制转换为int型的2,并赋值给y
z = int("3") # 将字符串3强制转换为int型的3,并赋值给z
print(x+y)  # error,字符串x和整型y无法相加
print(z+y) # 整型z和整型y相加得到3+2=5
m = str(2) # 将数字2转换为字符串2
a=1
print(a+m) # error
print(int("a")) # error,字符型不能转整型,但任何类型都可以转换成字符串

5.标识符
在编程时使用一些名字来为变量、类、方法命名

命名规则:
(1)英文、中文、数字、下划线
(2)不能以数字开头
(3)区分大小写
(4)不可使用关键字。如:False、True、None、class、and等。但可以修改大小写使用,如可以使用:false、true、Class等

命名规范:
(1)变量:见名知意、尽量简洁、多个字符组合时使用下划线命名、英文字母全小写。如:student_name=“jack”

6.运算符
(1)取整除//

print(9/2) # 4.5
print(9.0/2.0) # 4.5
print(16/4) #4.0 除法默认得到小数
print(9//2) # 4 取整除保留整数部分
print(9.0//2.0) # 4.0
print(9.0//2) # 4.0
print(9//2.0) # 4.0

(2)取余%

print(5%2) # 1

(3)指数**

print(5**2) #幂运算5^2=25

(4)复合赋值运算符
即对c做a运算,结果存回c
c+=a→c=c+a
c%=a→c=c%a
c**=a→c=c**a
c//=a→c=c//a
因此c必须是变量,而a可以是变量或者数字
[例]

a=2
b=3
a**=b
print(a) # a=a^b=2^3=8
a**=2
print(a) # 8^2=64
2**=b # error

(5)逻辑运算

print(3*3.75/1.5) # 7.5  不同类型的数混合运算时会将整数转换为浮点数
&按位与
|按位或
^按位异或(相同为0,不同为1~对二进制按位取反
<<左移(高位丢弃,低位补0>>右移

右移(>>):
①正数:低位丢弃,高位补0
②负数:负数在计算机中以补码形式存储,先计算补码,进行算术右移(低位丢弃,高位补1),求得原码即为所求
[例1] -72>>2
原码:11001000
反码:10110111
补码:10111000
补码右移两位:11101110
原码:10010010(补码除符号位全部取反加1)
即-18
[例2] 72>>2
原码:01001000
右移两位:00010010
即18

按位取反(~):
①正数:正数是按照原码形式存储,按位取反(包括符号位)后变为负数,将其视为补码形式,求得原码即为所求
②负数:负数在计算机中以补码形式存储,先计算补码,再按位取反(包括符号位),求得原码即为所求
[例1] -72按位取反
原码:11001000
反码:10110111
补码:10111000
按位取反:01000111
原码:01000111(正数原码与补码相同)
即71
[例2] 72按位取反
原码:01001000
按位取反:10110111(补码形式)
原码:11001001
即-73
③总结:x的按位取反结果为:~x= -(x+1)

7.字符串
(1)定义方式
①三种定义方式

a='hello'
b="hello"
c="""hello""" #在等号左侧有变量接收的三引号不属于注释

②三引号的换行

a="""h
e
llo"""
print(a)

1-python基础
③单双引号的换行,需要加 \\

a="a" \\"b" \\"c" \\"d"
print(a)

1-python基础

④字符串包含引号

想要输出’haha’,使用双引号定义

a="'haha'"
print(a)

想要输出"haha",使用单引号定义

a='"haha"'
print(a)

使用转义字符\\解除引号
如:通过\\,将引号改为普通字符"

1-python基础

(2)字符串拼接
+无空格
,有空格

print("hel"+"lo") # helloa="hel"
print(a+"lo"+"nihao") # hellonihao
print(a,"lo","nihao") # hel lo nihao 逗号拼接有空格

字符串无法和非字符串通过加号拼接

a=1
print("hello"+a) #error

(3)字符串格式化(针对str和非str的拼接)
①%
%d作为占位符,使用%隔开,用后面的12替换

a="我今年%d岁" % 12
print(a) #输出:我今年12岁

两个占位后面需要加括号,用逗号分开,按顺序写

a="%s今年%d岁" % ("我",12)
print(a) # 输出:我今年12岁

常见的占位:%s字符串、%d整数、%f浮点型

②快速格式化
在引号前加f,变量用{}标记,快速格式化不做精度控制

a="hello"
b=123
c=5.8
print(f"这是{a},啊啊{b},哦哦{c}")
# 输出:这是hello,啊啊123,哦哦5.8

(4)格式化的精度控制

a="%f" % 1.2
print(a) #输出1.200000

%5d表示数字宽度为5,123为三位数,前面会再输出两个空格

b="%5d" % 123
print(b)

1-python基础
若指定宽度小于当前宽度,正常输出(即无效宽度设置)

b="%2d" % 123
print(b)

1-python基础

对于%7.3
小数点前的7表示输出的整数+小数+小数点位数+输出结果前面的空格数=7,小数点后的3表示小数点后保留3位

a="%7.3f" % 12.3
print(a)

1-python基础
若小数点前的数小于实际位数,会正常输出

a="%2.3f" % 12.3
print(a) # 输出12.300

小数部分截断采取四舍五入的方式,宽度若不设置可省略

a="%.1f" % 1.55
print(a) # 输出1.6

(5)对表达式进行格式化

print("%d" % (1+1)) # 2
print(f"sasasa{1+2}") # sasasa3
print("%s" % type("dsad")) # <class 'str'>

(6)字符串分析

str = 'Runoob'
print (str)          # 输出字符串:Runoob
print (str[0:-1])    # 下标从0开始,从0号输出到-1号(左闭右开),-1号表示从右边数第一个字符,输出:Runoo
print (str[0])       # 输出字符串第一个字符:R
print (str[2:5])     # 从2号输出到5号(左闭右开):noo
print (str[2:])      # 从2号到底:noob
print (str * 2)      # 输出字符串两次:RunoobRunoob
print (str + "TEST") # 连接字符串:RunoobTEST

1-python基础
[练习]

1-python基础
字符串更新
(加号连接没空格,逗号连接有空格)

a='hello '
print("new="+a[:6],"world") # new=hello world

(7)字符串不能直接修改某个元素的值

a="hello"
print(a[0]) # h
a[0]="d" # error

(8)索引index
index(value,start,end)

a="hello"
b=a.index("l",0,-1) # 查找第一个l,从0开始,到-1(不含)为止
print(b) # 2
a="hello world haha"
print(a.index("world")) # 6
#找到world的w的下标,空格也算一个元素

(9)替换replace
replace(被替换的字符串,新的字符串)
将字符串内全部的 字符串1 替换为 字符串 2
通过替换,通过返回值生成了一个新的字符串,而不是修改原字符串(原字符串的值不变)

a="haha dlsajdsak haha jdsapdsaj haha pdasdoj"
b=a.replace("haha","hehe")
print(a) # haha dlsajdsak haha jdsapdsaj haha pdasdoj
print(b) # hehe dlsajdsak hehe jdsapdsaj hehe pdasdoj

(10)字符串分割split
split(“分隔符”)
通过指定分隔符进行分割,结果分别存入列表(后面讲到)

a="hello world sajisas"
b=a.split(" ") # 按照空格切分
print(b) # ['hello', 'world', 'sajisas']

(11)字符串的规整操作strip
strip() 去除前后空格
strip(“字符串”) 去除前后指定字符串(按单个字符计(含空格))

a="  hello world hsaihsa     "
print(a) #   hello world hsaihsa     
print(a.strip()) # hello world hsaihsa
a="  hello world hsaihsa     "
print(a) #   hello world hsaihsa     
print(a.strip("hel")) #   hello world hsaihsa     
# 无影响,因为原字符串是以空格开头,不能从中间元素删除
print(a.strip(" hel")) # o world hsaihsa
# 删除开头和结尾连续的" "、"h"、"e"、"l"
a="hello world hsaihseahl"
print(a.strip("hel")) # o world hsaihsea
# 删除开头和结尾连续的"h"、"e"、"l",结尾到a断开,不再往前检查

(12)统计字符串中某元素出现的次数count

a="hello world hsaihseahl worldsasc"
print(a.count("l")) # 5
print(a.count("world")) # 2

(13)统计字符串的长度len

a="a b "
print(len(a)) # 4

8.缩进
不使用{},而按照对齐方式划分代码块,未对齐可能导致error

if True:print("1")print("2")
else:print("3")

9.数据输入input
input默认将输入转为str,可直接进行字符串拼接

a=input() # 若输入6
print(a) # 6
print("您的输入是"+a) # 您的输入是6
print("请输入:")
a=input()
#等价于
a=input("请输入:")