python3基础语法(2)

1. python3基本数据类型

常见数据类型:Number、String、bool,List(列表)、Tuple(元组)、Set、Dictionary

python3六大标准数据类型

  • 不可变数据(3):Number、String、Tuple
  • 可变数据(3):List、Set、Dictionary

在Python 3里,只有一种整数类型 int,表示为长整型,没有 python2 中的 Long。

内置的 type() 函数可以用来查询变量所指的对象类型。

num1 = 11
num2 = 1.1
num3 = 1+1j
booltest = True
str = 'string'
tupletest = (1,2)
listtest = [1,2,3,2]
settest = set([1,1,1,2,2])
dic = {'one':1, 'two':2}
print(type(num1), type(num2), type(num3), type(booltest))
print(type(str), type(tupletest), type(listtest), type(settest), type(dic))

此外还可以用 isinstance 来判断:

listtest = [1,2,3,2]
print(isinstance(listtest,list))#打印True

type与isinstance区别:

  • type()不会认为子类是一种父类类型。
  • isinstance()会认为子类是一种父类类型。

2. 数据类型转换

Python 数据类型转换可以分为两种:

  • 隐式类型转换 - 自动完成
  • 显式类型转换 - 需要使用类型函数来转换

内置转换函数

常用:int()、float()、str()、tuple()、list()、set()、dict()

其他:complex()、eval()、chr()、ord()、hex()、oct()、repr()、

frozenset()

num1 = 11
num2 = 1.1
num3 = int(num2)
num4 = float(num1)
num5 = complex(num2)
str_ = str(num1)
print(num3, num4,type(num3),type(num4))
print(str_, type(str_))
print(num5, type(num5))

  • eval():用来计算在字符串中的有效Python表达式,并返回一个对象;不是有效表达式报错
  • chr():将一个整数转为字符
  • ord():将一个字符转化为它的整数值
  • hex():将一个整数转换为一个十六进制字符串
  • oct():将一个整数转换为一个八进制字符串
  • repr():将对象 x 转换为表达式字符串
  • frozenset():转换为不可变集合
str1 = '1+2*2'
ans = eval(str1)
print(ans, type(ans))
num1 = 98
stra = 'a'
test1 = chr(num1)
test2 = ord(stra)
print(test1, type(test1), test2, type(test2))
test3 = hex(num1)
test4 = oct(num1)
print(test3, type(test3), test4, type(test4))
ans2 = repr(str1)
print(str1, ans2, type(ans2))
print(eval(ans2), type(eval(ans2)))

3. 运算符

  • 算术运算符

+ - * /(结果是float)//(结果是int)%(取模)**(幂)

  • 比较运算符

>    >=    <    <=    ==    !=

  • 赋值运算符

=    +=    -=    *=    /=    //=    %=    **=     :=(海象运算符,这个运算符的主要目的是在表达式中同时进行赋值和返回赋值的值,Python3.8 版本新增运算符

  • 逻辑运算符

and  or  not

  • 位运算符

按位运算符是把数字看作二进制来进行计算的。

&    |    ~    ^(异或)    <<(左移)    >>(右移)

a = 60  # 60 = 0011 1100
b = 13  # 13 = 0000 1101
c = a & b  # 12 = 0000 1100
print("1 - c 的值为:", c)
c = a | b  # 61 = 0011 1101
print("2 - c 的值为:", c)
c = ~a  # -61 = 1100 0011
print("3 - c 的值为:", c)
c = a ^ b  # 49 = 0011 0001
print("4 - c 的值为:", c)
c = a << 2  # 240 = 1111 0000
print("5 - c 的值为:", c)
c = a >> 2  # 15 = 0000 1111
print("6 - c 的值为:", c)
  • 成员运算符

in    not in

a = 11
ch = 'a'
testlist = [1,2,3,4]
strtest = "abcde"
if a in testlist:
    print('a存在于listtest中')
else:
    print('a不存在于listtest中')
if ch not in strtest:
    print('ch不存在于strtest中')
else:
    print('ch存在于strtest中')

  • 身份运算符

is    is not

身份运算符用于比较两个对象的存储单元

id()函数用于获取对象内存地址

a = 20
b = 20
print(id(a), id(b))
print(a==b, a is b, id(a)==id(b), id(a) is id(b))

4. 条件控制

if

a = 10
if a==10:
    print('a等于10')

if...else

a = 10
if a==10:
    print('a等于10')
else:
    print('a不等于10')

if...elif...elif...[else]

a = 90
if a<60:
    print('不及格')
elif a<80:
    print('合格')
elif a<90:
    print('良好')
else:
    print('优秀')

match...case

语法:

match subject:
    case <pattern_1>:
        <action_1>
    case <pattern_2>:
        <action_2>
    case <pattern_3>:
        <action_3>
    case _:
        <action_wildcard>

case _: 类似于 C 和 Java 中的 default:,当其他 case 都无法匹配时,匹配这条,保证永远会匹配成功。

state = 400
match state:
    case 400:
        print('400')
    case 404:
        print('404')
    case 500:
        print('500')

5. 循环控制

while循环

a = 1
while a < 10:
    print(a)
    a += 2

while循环使用else语句

count = 0
while count < 5:
   print (count, " 小于 5")
   count = count + 1
else:
   print (count, " 大于或等于 5")

for语句

sites = ["Baidu", "Google","Runoob","Taobao"]
for site in sites:
    print(site)

for...else

for x in range(6):
  print(x)
else:
  print("Finally finished!")

break、continue、pass语句

break直接跳出循环

continue满足条件跳过一次后面语句,条件是否退出需要继续判断

pass空语句,保持结构的完整性

n = 5
while n > 0:
    n -= 1
    if n == 2:
        break
    print(n)
print('循环结束。')#打印出4 3 循环结束

m = 5
while n > 0:
    m -= 1
    if m == 2:
        continue
    print(m)
print('循环结束。')#打印4 3 1 0 循环结束

while True:
    pass  # 等待键盘中断 (Ctrl+C)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值