一、注释符
单行注释符:
单行注释以#开头
多行注释符 :
多行注释符用3个双引号""""""或者3个单引号''''''将注释括起来
"""
这是注释符
"""
或者
'''
这是注释符
'''
二、逻辑运算符
a = 3
b = 4
c = a > b or 5
print( c ) # 先判断a>b,a>b 不成立,所以c = 5
print(a>b) # False
三、数字
1、数字类型:
整型int
浮点型float
布尔型bool
复数complex
2、bool(x):把x转化为布尔型
print(0) #False
print(1) #True
print(1.5) #True
3、int(x):把x转化为整型
a = 5.5
print(int(a)) #5
4、float(x):把x转化为浮点型
a = 5
print(float(a)) #5.0
5、如果input()输入的是浮点型,必须先转成浮点型,再转成整型。
a = int(input("请输入一个数:")) #例如输入1.5
print(a) #这时候会报错,ValueError: invalid literal for int() with base 10: '1.5'
b = int(float(input("请输入一个数:"))) #例如输入1.5
print(b) #5
6、进制转换
①、其他进制转化为十进制:
A、十六进制转化为十进制:
print(0xA) #10
print(0xE) #15
B、八进制转化为十进制:
print(0o17) #15
print(0o23) #19
C、二进制转化为十进制:
print(0b0) #0
print(0o1) #1
②、十进制转化为其他进制:
A、hex( x ):把 x 转化为十六进制,并返回字符串:
a = hex(10)
print(a,type(a)) #0xa <class 'str'>
B、oct( x ):把 x 十进制转化为八进制,并返回字符串:
a = oct(10)
print(a,type(a)) #0o12 <class 'str'>
C、bin( x ):把 x 十进制转化为二进制,并返回字符串:
a = bin(10)
print(a,type(a)) #0b1010 <class 'str'>
int(str,num):把num进制的字符串str,转化为十进制的整数型:
a = int("10",16) #把16进制的10转化为十进制
print(a,type( a )) #16 <class 'int'>
b = int("10",8) #把8进制的10转化为十进制
print(b,type( b )) #8 <class 'int'>
c = int("10",2) #把2进制的10转化为十进制
print(c,type( c )) #2 <class 'int'>
7、算术运算符:
① // 表示取整
a = 3 // 2
print(a) #1
b = -3 // 2
print(b) # -2 ,向下取整
②把任意整数,转化为颠倒过来的数,例如123转化为321
a = int(input("请输入一个整数:"))
b = 0
if a > 0:
while a > 0:
b = b * 10 + a % 10
a = a // 10
elif a < 0:
a = -a
while a > 0:
b = b * 10 + a % 10
a // 10
b = -b
print(b)
**3、x,x次方
a = 2**3
print(a) #8
4、+=、-=的运算
a = 6
b = 3
c = 7
d = 9
c += a + b #c = c + ( a + b )
d -= a - b #d = d - ( a - b )
print(c) #16
print(d) #6
a = 4
b = 3
a -= a - b #a = a- (a - b)
print(a) #3
a = 4
b = 3
a *= a + b # a = a * (a + b)
print(a) #28
四、True,False
True的值默认为1,False的值为0
print(True) #True
print(False) #False
print(not True) #False
print(net False) #True
print(True + 1) #2
print(False + 1) #1
print(not 0) #True
print(not 1) #False
五、字符串
1、单引号,双引号的使用
a = '123'
b = "123"
c = "123'123'123"
print( a ) #123
print( b ) #123
print( c ) #123'123'123
2、type( x ):查看x的类型:
a = "1"
b = 1
c = 1.5
print( a ,type( a ) ) #<class 'str'>
print( b ,type( b ) ) #<class 'int'>
print( c ,type( c ) ) #<class 'float'>
3、id(x):查找x的储存位置
①值相同的不可变数据,储存位置相同
a = 1
b = 1
print(id(a)) #8791455994688
print(id(b)) #8791455994688
print(a is b) #True,is判断a是否和b的储存位置相同
②值相同的可变数据,储存位置不同
a = [1,2,3]
b = (1,2,3)
print(id(a)) #30368328
print(id(b)) #30368392
print(a is b) #False
4、变量是没有类型的,变量的值决定了变量的类型:
a = 1
print(type(a)) #<class 'int'>
a = "1"
print(type(a)) #<class 'str'>
5、变量之间的运算:
①对于值是数字的变量来说,是计算:
a = 1 + 2
print(a) #3
a = 1
b = 2
c = a + b
print(c) #3
a = True
b = False
c = a + b
print(c) #1
②对于值是字符串的变量来说,是拼接:
a = "1a"
b = "2b"
c = a + b
print(c) #1a2b
a = "1"
b = "2"
c = a+b
print(c) #12
A、变量*x:输出变量x次
a = "123"
b = a*3
print(b) #123123123
6、字符串不能和数字相加:
a = "1"
b = 1
c = a + b
print( c ) #报错
跳过报错
a = "1"
b = 1
try:
c = a + b
print( c )
except:
print("报错")
7 、input(),用户输入字符串
输入重量,单价,计算总价,当总价大于10时,返回2元:
weight = float(input("请输入重量:")
price = float(input("请输入单价:")
money = weight * price
if money > 10:
money = money - 2
print("应付%f元"%money)
8、count(str,beg = 0,end = len(string)):
返回str在string里出现的次数,如果beg或者end指定,则返回指定范围内str出现的次数:
a = input("请输入字符串:")
b = input("请输入要查找的字符:")
count = 0
i = 0
while i <= len(a) - len(b):
if a[i] == b[0]:
j = 0
while j < len(b):
if a[i +1] != b[j]:
break
j += 1
count += 1
i += 1
print(count)
六、标识符:
1、标识符的命名规则:
①由字母、数字、下滑线组成,且数字不能作为开头;
②长度任意,不能和关键字重名;
③区分大小写
2、查找关键字
import keyword:
print(keyword.kwlist) #['False', 'None', 'True', 'and', 'as', 'assert', 'async', 'await', 'break', 'class', 'continue', 'def', 'del', 'elif', 'else', 'except', 'finally', 'for', 'from', 'global', 'if', 'import', 'in', 'is', 'lambda', 'nonlocal', 'not', 'or', 'pass', 'raise', 'return', 'try', 'while', 'with', 'yield']
3、常量约定用全大写命名:
TAX = 0.98
print(TAX)
七、逻辑运算符:
1、逻辑运算符优先等级:not > and > or
a = 0
b = 1
c = 2
d = 3
e = a and b and c and d #a = 0 为false,返回false
print(e) #0 a = 0为false,返回false
a = 1
b = 2
c = 3
d = 4
e = a and b and c and d
print(e) #全部成立,返回最后一个数
a = 1
b = 2
c = 3
d = 4
e = a or b or c or d #a = 1为True,成立,返回a
print(e) #1
a = 1
b = 2
c = 0
d = a and c or b #先判断a and c,再判断or
print(d) #2
a = 1
b = 2
c = 0
d = b or a and c #先判断a and c,再判断or
print(d) #2
八、比较运算符:
1、简单比较:
a = 1
b = 2
print(a == 1) #True
print(a == 2) #False
print(a >= 1) #True
print(b <= 2) #True
2、连续比较:
a = input("请输入一个字符")
if "a" <= a <= "z" or "A" <= a <= "Z":
print("是字母")
else:
print("不是字母")
九、数据类型转换
1、int(x),把x转化为整型,float(x)把x转化为浮点型
a = True
print(int(a)) #1
b = False
print(int(b)) #0
c = 1.11
print(int(c)) #1
2、str( x ),把x转化为字符串
a = 1
b = str(a)
c = b * 2
print(c,type(c)) #11 <class 'str'>
d = str(True)
print(d,type(d)) #True <class 'str'>
3、ord(x),把x转化成ascii码值,chr(x),把x转化成字符
a = "A"
a = chr(ord(a)+4)
print(a) #E
4、eval():用来计算在字符串中的有效Python表达式,并返回一个对象
a = "[1,2,3,4,5]"
b = eval(a)
print(b,type(b)) #[1, 2, 3, 4, 5] <class 'list'>
a = "(1,2,3,4,5)"
b = eval(a)
print(b,type(b)) #(1, 2, 3, 4, 5) <class 'tuple'>
5、list( x ),将序列 x 转化成列表,tuple( x ),将序列 x 转化成元组:
a = "12345"
b = list(a)
c = tuple(a)
print(b,type(b)) #['1', '2', '3', '4', '5'] <class 'list'>
print(c,type(c)) #('1', '2', '3', '4', '5') <class 'tuple'>
十、作业:
类型转换题
1.计算 True+100的值
print(True + 100) #101
2.将3.14 转换成 整数和布尔值
print(int(3.14)) #3
print(bool(3.14)) #True
3.将十进制20分别使用二进制、八进制、十六进制进行转换
print(bin(20)) #0b10100
print(oct(20)) #0o24
print(hex(20)) #0x14
4.将字符串‘123’转换为int类型的数值,赋值给num,并查看num数据类型
num = int('123')
print(type(num)) #<class 'int'>
5.计算2的10次方
print(2 ** 10) #1024
6.计算出101除以3的余数
print(101 % 3) #2
7.将 65 转换成 A
char = chr(65)
print(char) #A
8.计算 字符串 “10/2” 的值
num = eval('10/2')
print(num) #5.0
9.超市买苹果
收银员向机器输入苹果的单价,输入客户购买的数量,显示总价格
price = input('请输入单价:')
weight = input('请输入重量:')
price = float(price)
weight = float(weight)
total_money = price * weight
print(total_money)
10. 数字逆序输出
a = int(input("请输入一个整数:"))
b = 0
while a > 0:
b = b * 10 + a % 10
a = a // 10
print(b)
11.逻辑运算符
not and or