Python 0 foundation - no failing at the end of the term

Keywords: Python

πŸ’– Said 0 foundation, is 0 foundation, the Chinese do not cheat the Chinese
🀞Qiumingshan code people's home page🀞
πŸŽ‰ Welcome to pay attention πŸ”Ž give the thumbs-up πŸ‘ Collection ⭐ Leave a message πŸ“
πŸ™ The author's level is very limited. If you find an error, you must inform the author in time
🀞 The selected content comes from rookie tutorial + self understanding 🀞

preface

Bloggers have limited ability and help, but they still don't want to see students fail. After all, what the teacher said is really in danger. The purpose of writing this blog is also very simple. It is based on the premise of making up for failing. Codeman's scholarship has been lost. I don't want some people to lose the award because of python. That's the same sentence. If you don't want money, You can not participate in any activity, so I feel like I lost it. Comprehensive test? Cattle and horses? It may not be suitable for me! I didn't say much. I left the train. Due to space reasons, I planned to write it in 2-3 articles, but I never failed. Therefore, I don't suggest that you want to improve from my point of view

1, Introduction to python (back)

To put it simply, if you often take exams, just remember them. After all, they won't let you start again 🀣

  • Python is an interpreted language: this means that there is no compilation in the development process. Similar to PHP and Perl languages.
  • Python is an interactive language: this means that you can execute code directly after a python prompt > > >.
  • Python is an object-oriented language: this means that Python supports object-oriented style or code encapsulated in object programming technology.
  • Python is a language for beginners: Python is a great language for junior programmers. It supports a wide range of application development, from simple word processing to Web browsers to games.

It has the following characteristics

1. Easy to learn: Python has relatively few keywords, simple structure, and a clearly defined syntax, which is easier to learn.
2. Easy to read: Python code definition is clearer.
3. Easy maintenance: Python's success is that its source code is quite easy to maintain.
4. A wide range of standard libraries: one of the biggest advantages of Python is its rich libraries, cross platform, and good compatibility with UNIX, Windows and Macintosh.
5. Interactive mode: with the support of interactive mode, you can input the language of executing code and obtaining results from the terminal, and interactive testing and debugging code fragments.
6. Portability: Based on its open source features, Python has been ported (that is, made to work) to many platforms.
7, extensible: if you need a fast running key code, or you want to write some algorithms that do not want to be open, you can use C or C++ to complete that part of the program and then call it from your Python program.
8. Database: Python provides interfaces to all major commercial databases.
9.GUI Programming: Python supports GUI, which can be created and ported to many system calls.
10. Embeddable: you can embed Python into C/C + + programs, so that users of your programs can get the ability of "scripting".

Disadvantages:
The running speed is slow. If you have speed requirements, rewrite the key parts with C + +.

2, Basic grammar

1. Format

print("Hello World") # note

if True:
    # indent
    print("True")
else:
    print("False")

# Multiline statement '\'
"total = item_one + \
        item_two + \
        item_tree"

Number type

# int integer
# bool Boolean
# Float float
# Complex complex, 1+2j

"String"

Note: there is no single character in python, for example, char in c + +, and 'a' is also a string in python

string
Single quotation marks and double quotation marks are used exactly the same in python
You can specify a multiline string using three quotation marks' 'or' '
Escape character
The backslash can be used to escape or not escape\

print("this is a line with \\n")
print(r'\n')

String connection mode

Connect strings literally, "this" "is" "string" is automatically replaced by "this is string"
Strings can be concatenated with the + operator and overloaded with the * operator
There are 2 indexing methods, starting from left to right, 0, right to left, and - 1
The string cannot be changed
There is no separate character type. A character is a string
Interception syntax: variable [header subscript: tail subscript: step size]

paragraph = "This is a paragraph," \
           "Can consist of multiple lines"
print(paragraph)
print(paragraph[0:-1]) # Output from first to penultimate
print(paragraph[0]) # first
print(paragraph[2:5]) # Third to fifth
print(paragraph[1:5:2]) # The second to the fifth, two at a time
print(paragraph*2) # 2 times
print(paragraph + 'Hello')
print("\n") # Blank line segmentation

Like the figure below

Code specification

Like other high-level languages, python can have multiple statements in one line. In Python, we use * * semicolon; * * to implement it

Code group: a group of statements indented the same is called a code group
For example, compound statements such as if while def class

The most basic usage of print output

The first thing worth noting is the print in python. The output of a statement is a line feed by default. To make it not perform line feed, you need to add end = "" at the end. Let's see this operation from the code below

print("l",end="")
print("y)

library

python is simple and convenient. A large part of the reason is that there are too many libraries, so we don't need to know the library and call the library

Because there are too many, I list one or two, and the others learn according to their needs
Two ways

import imports the entire library
from... import... import the required part of the library

import sys
print("----------")
print('Command line parameters')
for i in sys.argv:
    print(i)
print('\npython The path is:',sys.path)

from sys import argv,path #Import specific members
print('------------')
print('path:',path)
#Because the path member has been imported, do not add sys.path to the reference here

Basic data type

0 basis, equal sign, =, variable name on the left and value on the right

conunter = 100  #Integer variable
miles = 1000.0 #Floating point variable
name = "runoob" #character string

Assignment of multiple variables

a = b = c = 1
a,b,c = 1,2,"runoob"

Standard data type (difficulty)

Put an outline first
Number
String (string)
List
Tuple (tuple)
Set
Dictionary

It can be divided into two categories

Immutable: number, string, tuple
Variable: list, dictionary, set

So how to determine the type of data? python provides two methods, the type() function and the isinstance function
Difference: don't remember, it involves the knowledge of the object

Type does not consider a subclass to be a parent type
isinstance considers a subclass to be a parent type

a, b, c, d = 20,5.5,True,4+3j
print(type(a),type(b),type(c),type(d))

a = 111
print(isinstance(a,int))


There are the following numerical calculation methods

Addition, subtraction, multiplication, division / (floating point number), division / (integer), remainder%, power

Note the following:

  1. Python can assign values to multiple variables at the same time, such as a, b = 1, 2.
  2. A variable can be assigned to different types of objects.
  3. The division of a number contains two operators: / returns a floating-point number and / / returns an integer.
  4. In mixed computation, Python converts integers to floating-point numbers.
#Both real and imaginary parts of complex numbers are floating-point
#string 0 is the starting value and - 1 is the starting position at the end
#Unlike the C string,
# Python string cannot be changed. Assigning a value to an index position, such as word [0] ='m ', will cause an error.

list

Actually, it's more like a superset of arrays in c + +,

The list can complete the data structure implementation of most data collection classes. The types of elements in the list can be different. It can even contain a list called list nesting

Syntax rule: [data1, data2, data3]
The index is consistent with the string. After interception, a new list containing the required elements is returned

example

list = ['abcd',789,2.23,'runoob',7.02]
tinylise = [123,'runnoob']

print(list)
print(list[0])
print(list[1:3])
print(list[2:]) # Outputs all elements starting with the third element
print(tinylise * 2)
print(list + tinylise)

Summarize the following:

1. List is written between square brackets, and elements are separated by commas.
2. Like strings, list s can be indexed and sliced.
3. List s can be spliced using the + operator.
4. The elements in the List can be changed.

tuple

Lists can be modified, but tuples cannot modify elements
Syntax: tuple = ()

tuple = ( 'abcd', 786 , 2.23, 'runoob', 70.2  )
tinytuple = (123, 'runoob')

print (tuple)             # Output full tuple
print (tuple[0])          # The first element of the output tuple
print (tuple[1:3])        # The output starts from the second element to the third element
print (tuple[2:])         # Outputs all elements starting with the third element
print (tinytuple * 2)     # Output tuple
print (tuple + tinytuple) # Join tuple

Use an example to illustrate that modifying tuple elements is illegal

tuple = (1,2,3)
tuple[0] = 11

1. Like strings, elements of tuples cannot be modified.
2. Tuples can also be indexed and sliced in the same way.
3. Note the special syntax rules for constructing tuples containing 0 or 1 elements.
4. Tuples can also be spliced using the + operator.

aggregate

A set is composed of one or several large and small entities with different shapes. The things or objects constituting the set are called elements or members.
The basic function is to test membership and delete duplicate elements.
You can create a collection using braces {} or the set() function,
Note: to create an empty collection, you must use set() instead of {}, because {} is used to create an empty collection

sites = {'Google','taobao','qq','weixin','taobao'}
print(sites) # Output set, duplicate elements will be automatically removed

#Member test
if 'taobao' in sites :
    print("stay")
else :
    print("be not in")


Sets are computable

a = set('abracadabra')
b = set('alacazam')
print(a)
print(a - b)     # Difference sets of a and b
print(a | b)     # Union of a and b
print(a & b)     # Intersection of a and b
print(a ^ b)     # Elements in a and b that do not exist at the same time

Dictionaries

A list is an ordered collection of objects, and a dictionary is an unordered collection of objects. The difference between the two is that the elements in the dictionary are accessed by keys, not by offsets.
A dictionary is a mapping type. The dictionary is identified by {}. It is an unordered set of keys: values.
Keys must be of immutable type.
Keys must be unique in the same dictionary.

dict ={}
dict['one'] = "1-ye"
dict['two'] = "2-no"
print(dict['one']) #The output key is the value of one
print(dict[2]) #The output key is a value of 2
print(dict) #Output complete dictionary
print(dict.keys()) #Output all keys
print(dict.values()) #Output all values

1. A dictionary is a mapping type whose elements are key value pairs.
2. Dictionary keywords must be immutable and cannot be duplicated.
3. Create an empty dictionary using {}. n

last

Unknowingly, it has been 5k words. It is said that the 0 foundation will not fail and can't be written. Otherwise, some people will say that they can't understand. The road of computer is very difficult, but business is to do difficult and valuable things. Bye. We'll see it in a few days and ask for a three company. Your three company is the driving force for me to update the next article!

Posted by mark_c on Sat, 20 Nov 2021 05:47:40 -0800