Basic python language day01

1, Basic syntax of python Foundation

1. Notes

Definition: annotation is the part of the code that does not participate in compilation (Interpretation) and execution (the existence of annotation does not affect the function of the program);

1) The role of comments in Code:

1> Annotate and explain the code (making it easier for programmers to read the code)

2> Comments make the program function disappear

2) Category of notes:

1> Single line comment (ctr + /) - precede the comment content#

2> Multiline comment - put the comment content in '' or ""

Note Content 

2. Lines and indents

1) python statement writing specification

1> In Python, a statement occupies one line. Unlike other languages, semicolons can not be written after a line of statements


2> If you want to display multiple statements on one line, you must use semicolons between statements. If you do not add semicolons, an error will be reported

print('hello')  print('world!')     -------  Will report SyntaxError syntax error
print('hello'); print('python~')

3> Indentation (space or tab) cannot be added at the beginning of a statement in Python

print('hello world!')         ----------  The syntax is correct and the output is correct
	print('hello world!')     --------  Will report IndentationError Indent error

3. Identifiers and keywords

1) Identifier

1> Definition: identifier is the naming requirement when programmers name variables, functions or classes in the program

2> Naming rules:

1. It is composed of letters, numbers and underscores

2. The number cannot start

3> Form: variable name = Data

Correct naming

abc = 100
_abc = 200
abc123 = 300
__ = 400

Wrong naming

a*b = 100     ------------- Will report SyntaxError: syntax error
123abc = 200  ------------- Will report SyntaxError: syntax error

2) Keywords

1> Definition: in python, identifiers with special functions or special meanings are keywords. Keywords can only be used in a specific way in specific situations, and cannot be used to name variables, functions and classes

2> Common keywords


4. Common data and data types

1) Digital data

1> Definition: the data representing the size of a numerical value is digital data. When used in a program, it is written directly in the same way as in mathematics, such as 1002000, - 10, 3e4

print(3e4)      -------- 30000

2> Type:

a. Integer (int): - the type corresponding to all integers

b. Floating point type: - the type corresponding to all decimals

c. complex: - a+bj

2) Text data

1> Definition: the data used to represent text information is text data. When using text data in a program, the text content needs to be placed between single quotation marks or double quotation marks

2> Type (str) - string

3) Boolean data

1> Definition: there are only two values: true and False. True is equivalent to yes in life, which means true, positive, False is equivalent to no in life, which means False and negative

2> Type (bool) - Boolean

4) Null - None indicates no

5) Other common types: list, tuple, dictionary, collection, iterator, generator, function, custom type

5. Get data type

---------Get the specified data type ----------- type (data)

Case display

print(type(100))    ---------<class 'int'>-------

2, Input and output of python Foundation

1. Output (print) function ----- print

1> Print one data at a time

print('python')             Output results: python

2> Print multiple data at a time: Print (data 1, data 2, data 3...) ---------- the data types of data 1, 2, 3... Can be different

print('math', 100, 12.3)     Output results: math  100  12.3

3> Set parameter end

– print (data, end = enter) --- when printing data, first display all the data on the console and automatically print the end value after printing. End is enter by default

print('~'*20,'Set parameters end','~'*20)
print(100, end=' ')

4> Set sep parameters

- Print (data 1, data 2, data 3,..., sep = '') - by default, when a print prints multiple data, the data are separated by spaces, that is, sep = ''

print(11, 12, 13, sep=' + ')

2. Input function - input

------Form: input (input prompt)------

Note that no matter what data is input from the console through input, the resulting data type is a string

Supplementary type conversion: type name (data)

name = input('Please enter user name:')
age = input('Please enter age:')
int(age)----String age Cast to int type-----

3, python based variables

1. Define variables

1> Definition: variables are used to save data. You can use the data saved in variables by using variables

2> Form: define = create

Basic grammar

Variable name = data
    Variable name - Programmers name themselves
           Requirements: 1.Is an identifier     2.Not a keyword
           Specification: 1.See the name to know (see the variable name to roughly know what data is stored in the variable)
                2.All letters are lowercase and multiple words are separated by underscores
                3.The function name, class name and module name of the system are not used
     =    -  Assignment symbol is a fixed writing method
     data  -  Any resulting expression, such as a specific type of data, a variable that has been assigned,                                      Operation expression
a = 100
c = a
print(a, c)

2. Use variables

Using variables is to use the data stored in variables. Variables can be used to do whatever data can do

name = 'Tingtingzi'
print(name)         ------- Tingtingzi---------

Tags: Python

Posted on Tue, 14 Sep 2021 19:38:18 -0400 by aufkes