First understanding of Python

Life is too short to use Python

    1. Understanding of Python

    Python is an interpreted computer software design language. It was developed by Guido in 1989 to pass the boring Christmas. Let’s worship. If you want to see more Python stories, please go directly to Baidu. Python is easy to read and extensible. It can be used for scientific computing, which is easier to learn than MATLAB, and the most important thing is that it is free and open source. Today is the first day to start learning Python, set yourself a goal and stick to it.

    2. Python installation and getting started

    Python has two versions, that is, Python 2 and Python 3. Detailed version information is available on the official website -- Python official website.

    Let me talk about the biggest difference between Python 2 and Python 3, that is, the versions are incompatible. Python 3 has refactored Python 2 a lot, so there are many differences between 2 and 3, but they have no essential difference. It is recommended to learn the new version. Then there are different places to compare.

    This is the difference between Python 2 and Python 3 on the official website, keep it, and study it later --------> The difference between Python 2 and Python 3

    Install:

    1. Download Python, it is recommended to install both versions ------------> Python installation and download

    2. Installation steps ---------------> Python installation steps

    3. After the installation is complete and the environment variables are modified, you can open the command line window and enter python directly to enter the Python interactive window, and the Python version information will also be displayed.

        Note: Since there are two versions installed, how do I specify which version to launch? We can start the corresponding version with the py -2 and py -3 commands.

    getting Started:

      1. Types of Python

           There are mainly jpython, cpython, ironpython, javascriptpython, pypy, and we generally use cpython, different types of python will use the corresponding computer language to compile when calling the underlying code, and then hand it over to C for mechanical code compilation, cpython is It is directly handed over to C for compilation, so the speed will be faster than other types of Python, and then there is pypy, pypy is PythonPython, which is characterized by the slow running for the first time, but it is particularly fast when running again later.

      2. Python file name

                The suffix name of the Python file does not matter much, no matter what the suffix is, it can be executed by Python, but if a python file is imported by other python files, the file must be suffixed with .py, otherwise an error will be reported.

      3. Encoding format (the difference between py2 and py3)

                Python2 does not directly support Chinese encoding (add # -*-coding:utf-8-*- to solve)

                Python3 directly supports Chinese

      4. Variable name

                Letters, numbers, and underscores (_), note a few points: numbers cannot start, keywords and built-in functions cannot be used

      5. Operators

                (1). Arithmetic operations

                        

operator describe example
+ add - add two objects a + b outputs the result 30
- Subtract - get a negative number or subtract one number from another a - b output result -10
* multiply - multiply two numbers or return a string repeated several times a * b output result 200
/ divide - x divided by y b / a output result 2
% modulo - returns the remainder of the division b % a output result is 0
** power - returns x raised to the y power a**b is 10 to the 20th power, and the output result is 100000000000000000000
// Divide - Returns the integer part of the quotient 9//2 outputs result 4 , 9.0//2.0 outputs result 4.0

                (2). Comparison operation

operator describe example
== equals - compares objects for equality (a == b) returns False.
!= not equal - compares whether two objects are not equal (a != b) returns true.
<> not equal - compares whether two objects are not equal (a <> b) returns true. This operator is similar to != .
> Greater than - Returns whether x is greater than y (a > b) returns False.
< less than - Returns whether x is less than y. All comparison operators return 1 for true and 0 for false. This is equivalent to the special variables True and False, respectively. (a < b) returns true.
>= Greater than or equal to - Returns whether x is greater than or equal to y. (a >= b) returns False.
<= Less than or equal to - Returns whether x is less than or equal to y. (a <= b) returns true.

                (3). Assignment operation

operator describe example
= simple assignment operator c = a + b assigns the result of the operation of a + b to c
+= addition assignment operator c += a is equivalent to c = c + a
-= Subtraction assignment operator c -= a is equivalent to c = c - a
*= multiplication assignment operator c *= a is equivalent to c = c * a
/= division assignment operator c /= a is equivalent to c = c / a
%= Modulo assignment operator c %= a is equivalent to c = c % a
**= exponentiation assignment operator c **= a is equivalent to c = c ** a
//= Integer division assignment operator c //= a is equivalent to c = c // a

                (4). Logical operations

operator logical expression describe example
and x and y boolean "and" - x and y returns False if x is False, otherwise it returns the computed value of y. (a and b) returns 20.
or x or y boolean "or" - if x is non-zero, it returns the value of x, otherwise it returns the computed value of y. (a or b) returns 10.
not not x boolean "not" - returns False if x is True. It returns True if x is False. not(a and b) returns False

                (5).成员运算

运算符 描述 实例
is is 是判断两个标识符是不是引用自一个对象 x is y, 类似 id(x) == id(y) , 如果引用的是同一个对象则返回 True,否则返回 False
is not is not 是判断两个标识符是不是引用自不同对象 x is not y , 类似 id(a) != id(b)。如果引用的不是同一个对象则返回结果 True,否则返回 False。

                 (6).位运算


运算符 描述 实例
& 按位与运算符:参与运算的两个值,如果两个相应位都为1,则该位的结果为1,否则为0 (a & b) 输出结果 12 ,二进制解释: 0000 1100
| 按位或运算符:只要对应的二个二进位有一个为1时,结果位就为1。 (a | b) 输出结果 61 ,二进制解释: 0011 1101
^ 按位异或运算符:当两对应的二进位相异时,结果为1 (a ^ b) 输出结果 49 ,二进制解释: 0011 0001
~ 按位取反运算符:对数据的每个二进制位取反,即把1变为0,把0变为1 。~x 类似于 -x-1 (~a ) 输出结果 -61 ,二进制解释: 1100 0011,在一个有符号二进制数的补码形式。
<< 左移动运算符:运算数的各二进位全部左移若干位,由 << 右边的数字指定了移动的位数,高位丢弃,低位补0。 a << 2 输出结果 240 ,二进制解释: 1111 0000
>> Right shift operator: shifts all the binary bits of the operand on the left of ">>" by a number of bits, and the number on the right of >> specifies the number of bits to move a >> 2 output result 15, binary interpretation: 0000 1111
                    

        6. Conditional Statements

                
if condition:
    print("hello")
else :
    print("error")
if condition 1:
    print("hello")
elif condition 2 :
    print("hi~")
    .
    .
    .
else :
    print("error")

        7. Loop Statement

count = 0
while count<5 :
     count = count +1
     print(count)
count = 0
while count<5 :
     count = count +1
     print(count)
else :
      print("error")

        

        8.break和continue

              break: Break out and end the loop.

              continue: Jump out of the current loop and execute the next loop.


Preview content

        1. Basic data types

                (1).Number----int

                (2). String --str

                (3). Boolean ---- boolean

                (4).List----list

                (5). Tuple----tuple

                (6). Dictionary----dict

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324492125&siteId=291194637