Key python notes-basics, python notes-Basics
This article aims to refine the basics: A list of knowledge points in the basics
1. print and print "..."
Print: print ('Hello World! ')
Command Line Mode: Run Python and enter the command in the command line and execute it.
Program mode: Write and run a Python program.
Note: Create a folder in Linux, touch test. py, and run python test. py in vim.
You can change the above program to a script to run it directly. You need to specify the interpreter for the script language :#! /Usr/bin/env python,
After editing, change the File Permission chmod 755 hello. py and execute./hello. py to run the script program.
2. Basic Data Types
When introducing basic types, we should first understand the concept of "variables". Let's take a look at several examples.
A = 20 # int integer
A = 0.3 # float floating point number
A = True # True Value (True/False)
A = 'hello, python' # string. Double quotation marks can also be used for strings.
Example:
>>> A = 1.3
>>> Print (a, type ())
"A" can be understood as a virtual memory area of a computer, which is identified as "a", and a variable in python can be understood as a pointer variable in C language.
Variables do not need to be declared or deleted. They can be recycled directly. function type (): queries the data type. You only need to know its usage.
In the basics, you only need to keep in mind integer, floating point number, true value, and string. Next, you need to practice data type conversion, learn some mathematical functions, and master the operations such as String concatenation, indexing, and search.
3. Introduction to operators
Mathematics:
>>> Print 1 + 9 # Addition
>>> Print 1.3-4 # Subtraction
>>> Print 3*5 # Multiplication
>>> Print 4.5/1.5 # Division
>>> Print 3 ** 2 # multiplication party
>>> Print 10% 3 # Calculate the remainder
Judgment:
Returns True/False if it is True or False.
>>> Print 5 = 6 #=, equal
>>> Print 8.0! = 8.0 #! =, Not equal
>>> Print 3 <3, 3 <= 3 # <, less than; <=, less than or equal
>>> Print 4> 5, 4> = 0 #>, greater than; >=, greater than or equal
>>> Print 5 in [1, 3, 5] #5 is an element of list [1, 3, 5 ].
(There are also is, is not, and so on, not in-depth)
Logic:
Operation between True and False
>>> Print True and True, True and False # and, "and", both are True.
>>> Print True or False # or, "or" operation. If one of them is True, it is True.
>>> Print not True # not, non operation, Inverse Operation
It can be used with the previous part to do some exercises, such:
>>> Print 5 = 6 or 3> = 3
4. sequence: the most basic data structure
Sequence: sequence is the most basic data structure in python. The list and metadata data types are the most commonly used sequences,
Python has six built-in sequences. Apart from the two types mentioned earlier, there are also strings, Unicode strings, buffer objects, and the last xrange object, these types are not commonly used.
This is a bit difficult to understand. It's easy to understand. You need to know the following:
Sequence is a set of ordered elements.
(Strictly speaking, it is a collection of objects, but we have not introduced the concept of "object", so we will talk about elements for the moment)
A sequence can contain one or more elements, or contain no elements.
The basic data types we mentioned earlier can all be used as sequence elements. The element can also be another sequence and other objects we will introduce later.
There are two sequences: tuple (value table; also translated as tuples) and list (table)
>>> S1 = (2, 1.3, 'love', 5.6, 9, 12, False) # s1 is a tuple
>>> S2 = [True, 5, 'smile '] # s2 is a list
>>> Print (s1, type (s1 ))
>>> Print (s2, type (s2 ))
The main difference between tuple and list is that, once created, each element of tuple cannot be changed, and each element of list can be changed.
Elements of a sequence as another sequence
>>> S3 = [1, [3, 4, 5]
Empty Sequence
>>> S4 = []