dayⅢ、基本数据类型+运算符

一、基本数据类型

1、数字类型

# 数字类型可以进行数学运算以及比较运算,但仅限于数字类型之间进行

(1)整型int

# 作用:记录年龄、等级、号码等状态
# 定义与使用
# age = 999
# level = 10
# qq = 383838338

# res=age * 1
# print(age != 10)

(2)浮点型float

# 作用:记录薪资、身高、体重等状态
# 定义与使用
# salary=3.3
# print(salary,type(salary))

# print(3.3 + 1)
# print(3.3 >= 1)


# print("abc" > 1) # 不能混用

2、字符串str

# 作用:记录一些描述性的状态,比如名字、性别、消息等等
# 定义与使用:在双引号内或者单引号内或三引号内(''''''、"""""")定义的一组字符称之为字符串
msg="一行白鹭上青天"
msg1 = '一行白鹭上青天'
msg2 = '行一白鹭上青天'
msg3 = '行 1&*一白鹭上青天'
msg4 = '行'
print(msg4,type(msg4))

msg5="""
aaa
bbbb
cccc
"""
print(type(msg5))


引号的嵌套
msg="my name is 'egon'"
print(msg)


gender="male"
gender=1
gender=0

# 了解:字符串类型也可以进行数学运算以及比较运算,但仅限于字符串类型之间进行
# 数学运算:字符串只能做+、*
print("abc"+" "+"def") # 字符串的相加是一个拼接操作,效率极低
print("abc"*10)
print("#"*50)
print('hello')
print("#"*50)

3、列表list

# 索引对应值,索引从0开始
# 作用:按顺序存放多个值,并且能够按顺序取出来单独的一个值
# 定义与使用:在[]内用逗号分隔开多个任意类型的值
# hobbies = "play music read"

#           0       1      2
# egon_hobbies = ["play","music","read"]
# print(egon_hobbies[0])
# print(egon_hobbies[1])
# print(egon_hobbies[2])
# print(egon_hobbies[-1])

#    0       1       2                3
# l = [111, 333.3333, "abc", [444444, 5555, 66666,["aaa","bbbb"]]]
# print(l[2])
# print(l[3][0]) # [444444, 5555, 66666][0]
# print(l[3][-1]) # [444444, 5555, 66666][0]

# print(l[3][3][0])

# 练习题1:存放一个人信息(名字、年龄、多个爱好)
# egon_info = ["egon", 18, ["play", "movie"]]
# 练习题2:存放多个人信息(名字、年龄、多个爱好)
# info = [
#     ["egon1", 18, ["play", "movie"]],
#     ["egon2", 19, ["read"]],
# ]
# print(info[1][2][0])

# names=["egon","张三","李四"]
# salaries=[1.1,2.2,3.3]

#        0   1   2
# info=["egon",18,"male",19,10,11,12]
# print(info[-1])

4、字典

# 作用:存放多个值,用key对应值,key通常为字符串类型
# 定义与使用:在{}内用逗号分隔开多个key:value,其中value可以是任意类型
# info={"name":"egon","age":18,"gender":"male","hobbies":["play","music"]}
# print(info["name"])
# print(info["age"])

# print(info["hobbies"][1])

# 练习题:存放多个人信息(名字、年龄、多个爱好)
# info = [
#     {
#         "name": "egon1",
#         "age": 18,
#         "level": 10,
#         "hobbies": ["play", "movie"]
#
#     },
#     {
#         "name": "egon2",
#         "age": 19,
#         "level": 11,
#         "hobbies": ["read"]
#     },
# ]
#
# print(info[1]["level"])

5、布尔类型

# 布尔类型只有两个值True和False
# 可以自己定义布尔值
# tag=True
# print(tag,type(tag))

# 通过是通过比较运算得到布尔值的
print(10 > 3)
print("egon" == "egon")

# 布尔值用于条件

二、输入输出

# 1、python3中input功能会把用户输入的任意内容都存成字符串类型
# 接收用户的输入
inp_name=input("请输入您的账号名>>>: ") # "egon123*"
print(inp_name,type(inp_name))

# 注意1:
inp_age=input("请输入您的年龄>>>: ") # "18"
inp_age=int(inp_age) # int会把纯数字的字符串转换成int型
print(inp_age+1) # "18"+1

# 注意2:
inp_pwd=input("请输入您的密码>>>: ")  # "123"
print(inp_pwd == 123) # "123" == 123
print(inp_pwd == "123") # "123" == "123"

# 注意3:无论输啥,都存成字符串
x=input(">>>: ") # "[1,2,3]"
print(x,type(x))

# 了解(**):
# python2中raw_input功能与python3中的input功能一模一样

# python2中input功能


# 格式化输出
print('login successfull')
name=input('your name: ')
age=input('your age: ')
print("my name is %s my age is %s" %(name,age))


print("my name is %s" % "egon")
print("my name is %s my age is %s" % ("egon", 18))
print("my name is %s my age is %s" % ("egon", [1,2,3]))

三、基本运算符

1、算数运算符

# 算数运算符通常用于数字类型之间进行,但是+号和*比较特殊
# +和*可以用于其他类,但是仅限于同种类型直之间进行
# print(10 / 3)
# print(10 // 3)
# print(10 % 3) # 取余数,取模
# print(10 ** 2)


# 了解知识(**)
# 特殊:+ *
# l1=[1,2,3]
# l2=[4,5,6]
# print(l1 + l2)
# print(l1*3)

2、比较运算符

# 比较运算符通常用于数字之间进行
# print(10 == 10)
# print(10 == "10")
# print(10 != "10") # True

# 了解(*)
# print("abz" > "abcdef") # True

# l1=[1,'abc',18]
# l2=["a",'abc',18]
# l1 > l2 # 报错,对应位置的值必须是同种类型才可以

# l1=[1,'abc',18,111,222,333]
# l2=[2,'abc',18]
# print(l2 > l1)

3、赋值运算符

age = 18
age += 1  # age = age + 1
print(age)
#
# age *= 100 # age = age * 100
# print(age)
```python
## 4、逻辑运算符
```python
# not: 把布尔值结果取反
# print(not 10 > 3)
# print(not "egon" == "egon")
# print(not True)
# print(not False)

# and:用来链接左右两个条件
# 左右两个条件同时为True,最终结果才为True
# print(10 > 3 and 3 == 3)
# print(10 > 33 and 3 == 3)
# print(False and True)

# or:用来链接左右两个条件
# 左右两个条件但凡有一个结果为True,最终结果就为True
# print(False or True)
# print(True or True)
# print(False or False)
# print(10 > 33 or False)


# 短路行为=》偷懒行为
# 10 < 3 and 1 == 1
# False or 1 == 1
# True or 1 == 1


# 优先级:not > and > or
# 推荐用()区分优先级
# (True and 1 == 1) or (not "egon" == "egon") or ("aaa" == "aaa" and 1 <= 3)

# res = True and 1 == 1 or not "egon" == "egon" or "aaa" == "aaa" and 1 <= 3
# (True and 1 == 1) or not "egon" == "egon" or ("aaa" == "aaa" and 1 <= 3)
# print(res)


res = True and 1 != 1 or not "egon" == "egon" or "aaa" == "aaa" and 11 <= 3
#res=(True and 1 != 1) or not "egon" == "egon" or ("aaa" == "aaa" and 11 <= 3)
print(res)
上一篇:suprocess模块


下一篇:3/12