Light Plan 1

Today is the last day of July, August strive!

Three characteristic variables

A print Dian

print(x)
print(id(x))
print(type(x))

Analyzing two variable values ​​are equal Dian

True False

Wed and determines whether the same variable id

x = 11
y = x
z = 11
print(x == y) # True
print(x is y) # True
print(x is z)# True,整数池的原因

x = 257
z = 257
print(x is z)  # False

Id variable equal, equal to a certain value, pointing to the same memory address; value equal to the variable, id not necessarily equal.

Fancy assignment

Dian a chain assignment

a = b = c = d = 10
print(f'a:{a}, b:{b}, c:{c}, d:{d}')

Two Dian Cross assignment

x = 100
y = 200
temp = x
x = y
y = temp
print(f'x:{x}')
print(f'y:{y}') # x:200,y:100

x, y = y, x
print(f'x:{x}')
print(f'y:{y}') # x:100,y:200

Note

Dian a Comment

Note the code division multi-line and single-line comments, single-line comment with #, three multi-line comments can be single / double quotes, tris quoted comment can be wrapped.

Two principles Dian add a comment

  1. Not all add comments, or just need to find it difficult to understand the important part to add a comment to their own

  2. Comments can either Chinese or English, but do not use Pinyin

    Basic data types

    What is a data type Dian

    First, let's review what variables are: Variable used to record the state of the world. The purpose is to create a computer want to make computers and people can identify things in the world, since the computer can recognize things in the world. So we have to think about how to let the computer know me?
    We do not speak computer how I know, we talk about how do you know me? If you, would you be by my name, age, height, sex and hobbies know me. So imagine the computer can know me like this. So if your computer variables to record these my state, my age 19 can go to record, but my name can use the number to record it? My hobby can go digital records? If you say that when you age, 18.5 years old you would use to describe your age? One can imagine that you will not do this.
    The above statement by a large segment, in fact, can understand the data type refers to different types of variable values, the name may be a data type, age may be a data type, but may be interested in another data type.

    Two Dian how to classify data types

    Variable is used to reflect the status and state of change, there is no doubt you should use a different type of de-identified data for different states.

    Wed and different data types

    Digital type
    string type
    list type
    dictionary type
    Boolean type

    Digital Type

  3. Digital Type

    1. Plastic (int)

      1. Role: represents the person's age, all kinds of numbers, level

      2. definition:

        age = 18  # age = int(18)
        print(age)
        print(id(age))
        print(type(age))
        
        18
        1921150976
        <class 'int'>
        
      3. Usage: addition, subtraction, logical judgment (greater than, less than)

    2. Float (float)

      1. Role: represents the height, weight, payroll

      2. definition:

        salary = 2.1  # salary=float(2.1)
        print(id(salary))
        print(type(salary))
        print(salary)
        
        1611865330144
        <class 'float'>
        2.1
      3. Usage: addition, subtraction, logical judgment (greater than, less than)

  4. String type

    1. Role: represents the name, hobby

    2. definition:

      name1 = 'nick'
      name2 = "egon"
      print(id(name1))
      print(type(name1))
      print(name1)
      
      2346984016336
      <class 'str'>
      nick
      
      
      name3 = """nick
      egon"""
      print(name3)
      
      nick
      egon
    3. Usage: string only +, * and the logical comparison

      1. Note: If there are marks within a string, the string inside the quotation marks and wrapping string can have the same
      2. Note: multiplication strings are only multiplied by the numbers.
      3. Note: string comparison size, comparing the ASCII code
      4. Note: comparison string is the order of the letters.
  5. List

    1. Role: store multiple values.

    2. definition:

      Any type of value are separated by a comma within [].

      hobby = 'read'
      hobby_list = [hobby, 'run', 'girl']
      print(id(hobby_list))
      print(type(hobby_list))  
      print(hobby_list)
      
      2050766486152
      <class 'list'>
      ['read', 'run', 'girl']
    3. how to use:

      1. Deposit is not an end, is taking aim, we introduce the method list index value, bearing in mind the index number from zero.

      2. hobby_list = ['read', 'run', 'girl']
        # 索引序号      0       1      2
        # 取出第二个爱好
        print(hobby_list[1])
        
        run
        hobby_list = ['read', 'run', ['girl_name', 18, 'shanghai']]
        # 取出girl的年龄
        print(hobby_list[2][1])
        
        18
  6. dictionary

    1. Action: a plurality of values ​​used to access, in accordance with key: value of the stored-value mode, may not take the time to go to the index value by the key, key has a function of descriptive value. Store a variety of types of data and more data when you can use a dictionary.

    2. Is defined: {} in the plurality of elements separated by commas, each element is key: value format, the format in which the value is an arbitrary data type, since the key has a function descriptive, it is usually a string key type .

      ser_info = {'name': 'nick', 'gender': 'male', 'age': 19,  # 字典套列表
                   'company_info': ['oldboy', 'shanghai', 50]}
      print(id(user_info))
      print(type(user_info))
      print(user_info)
      
      2479795990672
      <class 'dict'>
      {'name': 'nick', 'gender': 'male', 'age': 19, 'company_info': ['oldboy', 'shanghai', 50]}
    3. Usage: dictionary index value no longer depends on the way, but rather on the key, to obtain the value corresponding to the key value through the [key].

      # 字典套列表
      user_info = {'name': 'nick', 'gender': 'male', 'age': 19,
                   'company_info': ['oldboy', 'shanghai', 50]}
      print(user_info['name'])
      print(user_info['company_info'][0])
      
      nick
      oldboy
      # 字典套字典
      user_info = {'name': 'nick', 'gender': 'male', 'age': 19, 'company_info': {
          'c_name': 'oldboy', 'c_addr': 'shanghai', 'c_num_of_employee': 50}}
      
      print(user_info['name'])
      print(user_info['company_info']['c_name'])
      
      nick
      oldboy
  7. Boolean

    1. Effect: Conditions for determination result

    2. Definitions: True, False usually not directly quoted, required logic operation result obtained.

    3. Instructions:

      print(type(True))
      print(True)
      
      <class 'bool'>
      True
      
      
      print(bool(0))
      print(bool('nick'))
      print(bool(1 > 2))
      print(bool(1 == 1))
      False
      True
      False
      True
      
      
      注意:Python中所有数据类型的值自带布尔值。如此多的数据类型中只需要记住只有0、None、空、False的布尔值为False,其余的为True。
      print(bool(0))
      print(bool(None))
      print(bool(''))
      print(bool([]))
      print(bool({}))
      print(bool(False))
      False
      False
      False
      False
      False
      False

unzip

A decompression Dian

Decompression can be understood: the supermarket package is put together more merchandise, decompression is actually unpack multiple disposable goods out.

name_list = {'nick','egon','jason'}
x,y,z = name_list

Sometimes we decompress values ​​may be we do not want, you can use an underscore, omnipotent underlined.

name_list = ['nick','egon','jason','tank']
x,y,z,a = name_list
x,_,z,_ = name_list

But also have a more Sao operation, can be felt but not explained.

name_list = {'shiqi','zz','zhangyi','jinx','2019'}
x,y,*_,z = name_list

Dictionary is also possible, but dictionaries decompression is key.

info = {'name':'shiqizz','age':17}x,y = infoprint(x,y)

Python interaction with the user

Why a Dian interaction?

Let's review what is the significance of computers invention, invention of the computer to computer slavery, the liberation of labor. Suppose we now write an ATM system replaces teller, if we want to withdraw money from this ATM, the ATM is not so that we will be asked to enter your name and password? We are not required to enter the withdrawal amount we need? This is not to be understood as an interaction. Let us now understand how to achieve the next Python is interactive.

Two Dian how to interact?

input()

int(inpt())

str(888)

Regardless of the value of our input is to receive the value of numeric types, strings, lists, input is of type string.

Three ways formatted output

Dian a placeholder

% S (for all data types)

Such as:

name = "十七zz"
height = 180
weight = 140
print('my name is %s,my height is %s,my weight is %s' %(name,height,weight))

my name is 十七zz,my height is 180,my weight is 140

Two Dian format format

name = "十七zz"
height = 180
weight = 140
print('my name is {name},my height is {height},my weight is {weight}'. format(name=name, height=height, weight=weight))

my name is 十七zz,my height is 180,my weight is 140

Wed and f -String format

Compared placeholder way, python3.6 version adds f-String formatted way, relatively simple to understand, this is the way I use the most, it is recommended to use this way.

name = "十七zz"
height = 180
weight = 140
print(f'my name is {name}, my height is {height},my weight is {weight}')
print(f'{name*2}')

my name is 十七zz,my height is 180,my weight is 140

Seventeen seventeen zz zz

salary = 3.1415926
print(f'{salary:.3f}')

3.142

Basic operators

An arithmetic operators Dian

a = 10 , b = 20.

029- basic operators - Arithmetic Operators

Two Dian comparison operators

029- basic operators - Comparison Operators

l1 = [1, 'a', 3]
l2 = [3]
print(l1 < l2)  # False,列表比较从左向右比较,不同数据类型无法比较。

True

Wed and assignment operator

029- basic operators - Assignment operators

Four logical operators Dian

029- basic operators - Logical Operators

Five Dian identity operator

029- basic operators - Operators identity

== s and the difference between: is configured to determine whether two variables refer to the same objects (whether in the same memory space), a reference value for determining == variables are equal.

Six Dian operator precedence Python

High priority brackets on the line.

029- basic operators operator precedence -python

determining if the flow control

A grammar Dian

if the judge is doing it? In fact, if the judge is to make a judgment in the simulation of people. If that is so what, so what if. For ATM system, you need to determine the correctness of your account password.

1.1 if

if expressed if the establishment of the code will be set up to do.

1.2 if...else

if ... else indicate if the establishment of the code will be set up to do, else set up will not do.

1.3 if...elif...else

if ... elif ... else represent if conditions do 1 set up, elif condition 2 set up to do, elif condition 3 set up to do, elif ... else to do.

If two nested Dian

# if的嵌套
cls = 'human'
gender = 'female'
age = 18
is_success = False

if cls == 'human' and gender == 'female' and age > 16 and age < 22:
    print('开始表白')
    if is_success:
        print('那我们一起走吧...')
    else:
        print('我逗你玩呢')
else:
    print('阿姨好')

Guess you like

Origin www.cnblogs.com/shiqizz/p/11279126.html