• Python基础B(数据类型----交互)


    数据类型

    数字类型

    一、整型(int)

    age = 18 % age=int(18)
    
    print(id(age))
    print(type(age))
    print(age)
    

    4530100848
    <class 'int'>
    18

    可用于四则运算、整除取整、逻辑判断等

    二、浮点型(float)

    salary = 2.1  # salary=float(2.1)
    
    print(id(salary))
    print(type(salary))
    print(salary)
    
    

    4569240656
    <class 'float'>
    2.1

    可用于四则运算、整除取整、逻辑判断等

    字符串类型

    name1 = 'nick'
    name2 = 'egon'
    print(id(name1))
    print(type(name2))
    print(name2)
    

    4418849624
    <class 'str'>
    nick

    字符串可进行+、*和逻辑比较

    #拼接操作
    
    msg2 = "my name is 'nick'"
    msg3 = 'my name is "nick"'
    
    print(msg2 + msg3)
    
    #乘法操作
    
    name = 'nick'
    print(name * 10)
    
    # 比较大小
    
    msg1 = 'hello'
    msg2 = 'z'
    msg3 = 'hx' 
    
    print(msg1 > msg2)
    print(msg1 > msg3)
    print('Z' > 'A')
    print('Z' > 'a')
    

    my name is 'nick'my name is "nick"

    nick nick nick nick nick nick nick nick nick nick

    False

    False

    True

    True

    列表类型

    # 定义
    hobby = 'read'
    hobby_list = [hobby, 'run', 'girl']
    print(id(hobby_list))
    print(type(hobby_list))
    print(hobby_list)
    
    # 使用
    hobby_list = ['read', 'run', 'girl'] 
    print(hobby_list[1])
    
    hobby_list = ['read', 'run', ['girl_name', 18, 'shanghai']]
    
    print(hobby_list[2][1])
    

    4558605960
    <class 'list'>
    ['read', 'run', 'girl']

    run

    18

    字典类型

    #字典套列表
    user_info = {'name':'nick', 'gender':'male', 'age': 19, 'company_info':['oldboy', 'shanghai',50]}
    print(user_info['name'])
    print(user_info['company_info'][0])
    
    #字典套字典
    user_info = {'name':'nick', 'gender':'male', 'age':19, 'company_info':{'c_nmae':'oldboy', 'c_addr': 'shanghai', 'c_num_of_employee':50}}
    
    print(user_info['name'])
    print(user_info['company_info']['c_name'])
    
    #取出students中第一个学生的姓名
    students = {
    	{'name': 'nick', 'age': 19},
    	{'name': 'egon', 'age': 18}
    	
    }
    print(students[0]['name'])
    

    nick

    oldboy

    nick

    oldboy

    nick

    布尔类型

    print(bool(0))
    print(bool('nick'))
    print(bool(1 > 2))
    print(bool(1 == 1))
    # 除了0、None、空、False,其余所有布尔值均为True
    print(bool(None))
    print(bool(''))
    print(bool([]))
    print(bool({}))
    print(bool(False))
    

    False

    True

    False

    True

    False

    False

    False

    False

    False

    解压缩

    name_list = ['nick', 'egon', 'jason',]
    x, y, z = name_list
    print(f'x:{x}, y:{y}, z:{z}')
    
    #告诉计算机不需要y了
    x, _, z = name_list
    
    #只输出z,之前的全部不输出
    *_, z = name_list
    
    #字典类型解压缩
    info = {'name': 'nick', 'age': 10}
    x,y = info
    print(x,y)
    

    x:nick, y:egon, z:jason

    name age

    与用户进行交互

    print('_' * 20)
    
    input("请输入你的姓名:")
    
    print('*'*100)
    
    
    name = input('请输入你的姓名:')
    pwd = input('请输入你的密码:')
    
    print(type(name))
    print(type(pwd))
    

    nick


    请输入你的姓名:nick

    请输入你的密码:123

    <class ‘str’>

    <class ‘str’>

  • 相关阅读:
    149. Max Points on a Line(js)
    148. Sort List(js)
    147. Insertion Sort List(js)
    146. LRU Cache(js)
    145. Binary Tree Postorder Traversal(js)
    144. Binary Tree Preorder Traversal(js)
    143. Reorder List(js)
    142. Linked List Cycle II(js)
    141. Linked List Cycle(js)
    140. Word Break II(js)
  • 原文地址:https://www.cnblogs.com/michealjy/p/11270773.html
Copyright © 2020-2023  润新知