Python_ Basic data types

Source: Internet
Author: User

  • What is data?

    The data is the value of the variable, such as age=18,18 is the data we save.

  • Why is there more than one type of data?
    Variables are used to reflect/hold state and state changes, there is no doubt that the different States should be used to identify different types of data;
  • 3 numbers
    int integral type
    Definition: age=10 #age =int (10)
    For identification: Age, grade, XXX number, QQ number, number

    float Float Type
    Definition: salary=3.1 #salary =float (3.1)
    Used to identify: salary, height, weight,
    To view quantity types:

    #type (age) to view data types
    4. String
    In Python, the quoted character is the string type, and Python does not have a character type.
    Definition: name= ' Egon ' #name =str (' Egon ')
    Used to identify: descriptive content, such as name, gender, nationality, ethnicity
    What is the difference between #那单引号, double quotes, and multiple quotes? Let me tell you aloud that there is no difference between single and double quotes, only the following situation you need to consider a single pair of mates
    msg = "My name is Qyy, I ' m years old!"
    #多引号什么作用呢? The function is that multiple lines of string must be in multiple quotes
    msg = "'
    Before the window was bright moonlight,
    Staring at the ground frost,
    Jutou look at the moon,
    Think of your hometown.
    ‘‘‘
    Print (msg)

    #数字可以进行加减乘除等运算, where are the strings? Let me tell you aloud, too, can I? What? Yes, but only the "add" and "multiply" operations can be performed.

    >> name= ' Qyy '
    >> age= ' 18 '
    >> Name+age #相加其实就是简单拼接
    ' Egon18 '
    >> name*3
    ' Qyyqyyqyy '

    #注意1: The efficiency of string addition is not high
    The string 1+ string 3, and does not add the string 2 on the basis of string 1, but applies a completely new memory space to the string 1 and the string 3, the equivalent string 1 with the space of the string 3 is copied once,

    #注意2: String only, cannot string plus other types

    string concatenation (can only be done between strings, and can only be added or multiplied)

    5. List
    #在 [] separated by commas, you can store n any type of value
    Definition: students=[' A ', ' B ', ' C ',] #students =list ([' A ', ' B ', ' B ',])
    Used to identify: a situation in which multiple values are stored, such as a person having multiple hobbies
    #存放多个学生的信息: Name, age, hobby

    >> students_info=[[' A ', 18,[' play ',]],[' B ', 18,[' C ', ' sleep ']
    >> Students_info[0][2][0] #取出第一个学生的第一个爱好
    ' Play '

    6. Dictionaries
    Why do you use a dictionary?
    To store a person's information: name, gender, age, is obviously a number of values, since it is a number of values, we can completely based on the list of just learning to store, as follows

    >> info=[' qyy ', ' male ', 18]
    The purpose of defining the list is not only to save, but also to consider the value, if I want to take out this person's age, can use
    >> Info[2]
    18
    But this is based on the knowledge that we already know the age in the 3rd place, we know that index 2 corresponds to age
    That
    #name, sex, age
    info=[' qyy ', ' male ', 18]
    And this is purely an assumption, and there is no real sense that the third place is the age, so we need to find a way, that can store multiple arbitrary types of values, but also can be hard to specify the value of the mapping relationship type, such as Key=value, which is used in the dictionary

    The #在 {} is separated by commas and can hold multiple key:value values, and value can be any type
    Definition: info={' name ': ' Egon ', ' age ': +, ' sex ': 18} #info =dict ({' name ': ' Egon ', ' age ': *, ' sex ': + ') '
    Used to identify: store multiple values, each value has a unique key, can be more convenient and efficient to take the value
    students=[
    {' name ': ' GCL ', ' age ': 248, ' hobbies ': [' play ', ' Sleep ']}, # 0 The first Student
    {' name ': ' Qyy ', ' age ': +, ' hobbies ': [' read ', ' Sleep ']}, # 1 second student
    {' name ': ' Cyh ', ' age ': ' Hobbies ': [' music ', ' read ', ' Sleep ']}, # 2 the 3rd Student
    ]
    Print (students[1][' hobbies '][1]) #取第二个学生的第二个爱好

    7. Boolean
    #布尔值, a true one false
    #计算机俗称电脑, that is, when we write a program to let the computer run, it should be to let computers infinitely close to the human brain, or what the human brain can do, what the computer should be able to do, the main role of the human brain is data operation and logical operation, where the Boolean type of the simulation of the logic of human operation, that is, when a condition Mark with true, false if not established

    >> a=3
    >> b=5
    >>
    >> a > B #不成立就是False, i.e. fake
    False
    >>
    >> a < b #成立就是True, which is true
    True

    #所有数据类型都自带布尔值
    1, none,0, empty (empty string, empty list, empty dictionary, etc.) three cases with Boolean value false
    2. The rest are true

      1. mutable type Immutable Type:
        #1. mutable type: In the case where the ID is constant, value can be changed, which is called a mutable type, such as a list, a dictionary

        #2. Immutable type: Once the value changes, the ID also changes, which is called the immutable type (ID variable, which means that a new memory space is created)

    Python_ Basic data types

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.