Python basic syntax

Python basic syntax

The basic syntax only points out the parts that I am not familiar with

input Output

The same print function prints multiple data

print(1,3,4,5)


1 3 4 5

Comma separated will be separated by spaces to print

print print multiple lines of data

print("""aaaa
bbbbbb
cccccc
"""
)


aaaa
bbbbbb
cccccc

Will automatically print out a newline

Data types and variables

Basic data types supported by Python

  1. Integer
  2. Floating point number. In addition to standard floating-point number writing, there is also scientific notation that can be used, such as 1e10
  3. String

Note: When escaping a string, only one/is needed to escape/, not\\\, the output result is/

  1. bool

String (str)

In a computer, the computer memory is encoded in unicode

In Python 3.x, strings are encoded in unicode

ord function

Use the ord function to view the encoding value. Note that it can only be a single character

print(ord("A"))
# 65
print(ord(" "))
# 20013

chr function

Reverse analysis.

print(chr(65))
# A

Bytes

Since the computer memory can only be encoded in unicode, it is necessary to convert the string to bytes as much as possible. The representation of bytes starts with b, which is similar to x = b"ABC". Pay attention to the difference between "ABC" and b"ABC"

encode function

Convert a string to a sequence of bytes

decode function

Convert the bytes sequence into a string. If a small part of the string cannot be decoded, you can use error=ignore to ignore

print(b"\xe4\xb8\xad\xff".decode(encoding="UTF8", errors="ignore"))

Python beginning comment

#!/usr/bin/env python3
# -*- coding: utf-8 -*-

The first line is only applicable to Unix-like systems and tells the location of the Python interpreter.

The second line tells the Python interpreter how to decode.

Character formatting

The first formatting method

Python s character formatting is similar to the C language

print("  %s,   %d  " % ("lightingsui"12))

Use% to separate characters and parameters. If there is only one parameter, you can omit the parentheses, that is

print("  %s" % "lightingsui")

Common placeholders

Placeholder description
%d Integer
%f Floating point
%s String
%x Hexadecimal integer

If you don't want to remember these placeholders, you can just remember %s, it can format everything.

Of course, you can also specify to output 0 in front or display a few digits after the decimal point when outputting

print("%02d------%.2f" % (10.1345))
# 01------0.13

If you want to output% in a string, you can use %% for output

print("%s---%%" % "sdas")
# sdas---%

If there is no placeholder specified in it, it can be output directly without escaping

The second formatting method

Another way to format a string is to use a string format()method, which replaces the placeholders {0}, {1} in the string with the passed parameters in turn , but this method is much more troublesome than %:

>>> 'Hello, {0},   {1:.1f}%'.format(' '17.125)
'Hello,  ,   17.1%'