Python3 basic syntax

installation##

Use docker to open a new machine as an experimental environment

docker run -it centos /bin/bash

yum -y install python3 

Edit the first python file
vi hello.py
print('hello world'):wq #Save and exit
python3 hello.py #Run hello.py

There are six standard data types in Python 3:

Number:

Python 3 supports int, float, bool, complex (plural).

String:

The string str in Python is enclosed in single quotes ('') or double quotes (" "), and special characters are escaped with backslashes ().

If you don't want to escape the backslash, you can add an r in front of the string to indicate the original string:

In addition, the backslash can be used as a line continuation character, indicating that the next line is a continuation of the previous line. You can also use """...""" or'''...''' to span multiple lines.

Strings can be concatenated using the + operator string, or repeated using the * operator:

There are two indexing methods for strings in Python. The first is from left to right, increasing from 0 in order; the second is from right to left, decreasing in order from -1.

Note that there is no separate character type, a character is a string of length 1.

You can also slice a string to obtain a substring. Separate the two indexes with a colon in the form of a variable [head subscript: tail subscript].

The intercepted range is closed before opening, and both indexes can be omitted:

List#

List (list) is the most frequently used data type in Python.

The list is a list of elements written between square brackets and separated by commas. The types of elements in the list can be different:

Like strings, lists can also be indexed and sliced. After the list is sliced, a new list containing the required elements is returned. I won’t go into details here.

Lists also support concatenation operations, using the + operator:

List has many built-in methods, such as append(), pop(), etc., which will be discussed later.

note:

Tuple#

Tuples are similar to lists, except that the elements of tuples cannot be modified. Tuples are written in parentheses, and the elements are separated by commas.

The element types in the tuple can also be different:

String, list and tuple all belong to sequence (sequence).

note:

Collection:

A set is a set of unordered and non-repeating elements.

The basic function is to conduct membership testing and eliminate duplicate elements.

You can use braces or set() to create a set collection. Note: To create an empty collection, you must use set() instead of {}, because {} is used to create an empty dictionary.

Dictionary:

Dictionary (dictionary) is another very useful built-in data type in Python.

A dictionary is a mapping type, which is an unordered collection of key:value pairs.

Keywords must use immutable types, which means that lists and tuples containing mutable types cannot be keywords.

In the same dictionary, the keywords must also be different from each other.

The dictionary type also has some built-in functions, such as clear(), keys(), values(), etc.

note:

Recommended Posts

Python basic syntax (1)
Python3 basic syntax
Python basic syntax generator
Basic syntax of Python
Python basic syntax iteration
Python basic syntax list production
Python basic summary
Python basic operators
Python basic syntax and number types
Python basic drawing tutorial (1)
Python function basic learning
Basics of Python syntax
python_ crawler basic learning
Python basic data types
Basic knowledge of Python (1)
Python basic data types
Python basic knowledge question bank
Python basic drawing tutorial (two)
Python introductory notes [basic grammar (on)]
Python entry notes [basic grammar (below)]
Python file operation basic process analysis
Python crawler basic knowledge points finishing
Real zero basic Python development web
Python multithreading
Python CookBook
Python FAQ
Python3 dictionary
Python3 module
python (you-get)
Python string
Python basics
Python descriptor
Python basics 2
Python exec
Python notes
Python3 tuple
CentOS + Python3.6+
Python advanced (1)
Python decorator
Python IO
Python multithreading
Python toolchain
Python basic actual combat-guess the age game
Python3 list
Python multitasking-coroutine
Python overview
python introduction
Python analytic
Python basics
07. Python3 functions
Python basics 3
Python multitasking-threads
Python functions
python sys.stdout
python operator
Python entry-3
Centos 7.5 python3.6
Python string
python queue Queue
Python basics 4
Python basics 5