Python lexical structure
- 2021-12-11 18:23:09
- OfStack
1. Variables and Types
A variable is a carrier for storing data, that is, a container. Variables in a computer are actually existing data or a block of memory space in which data is stored, and the values of variables can be read and modified, which is the basis of all computers and controls. Computers include various data types, such as text, video, audio and so on.
1.1 Types commonly used in Python
Integer type:Python
Can handle integers of any size, and support binary, octal, 106-ary notation.
Floating point type: Floating point number is also commonly known as decimal.
String type: A string is arbitrary text enclosed in single or double quotation marks, wrapped in a single quotation mark ''or double quotation mark'', and can be written in multiple lines (starting with three single or three double quotation marks and ending with three single or three double quotation marks).
Boolean: Boolean values are only
True
,
False
Two values, either
True
, or is it
False
.
1.2 Variable commands
You define 1 name for each variable,
Python
Command rules for variables
Variable names are composed of letters (generalized
Unicode
Characters, excluding special characters), numbers and underscores, and numbers cannot start.
Case sensitive (uppercase a and lowercase A are two different variables).
Don't conflict with keywords (words with special meanings, which will be discussed later) and system reserved words (such as the names of functions, modules, etc.).
1.3 Use of variables
If you define a variable and assign a value to the variable, you can call it directly in the function, as follows:
# Integer type
a = 123
# Floating point type
b = 1.23
# String type
c = "1 Bowl week "
# Boolean type
d = True
print(a, b, c, d)
# 123 1.23 1 Bowl week True
2. Detect the type and transformation of variables
2.1 Detecting Variable Types
The type () function is used to detect the type of change, and the code is as follows:
# Integer type
a = 123
# Floating point type
b = 1.23
# String type
c = "1 Bowl week "
# Boolean type
d = True
print(type(a), type(b), type(c), type(d))
# <class 'int'> <class 'float'> <class 'str'> <class 'bool'>
2.2 Type Conversion
You can use the
Python
To convert variable types,
The details are as follows:
int():
Converts a numeric value or string to an integer, and you can specify the binary system.
float():
Converts a string to a floating-point number.
True
0
Converts the specified object to a string, and you can specify the encoding.
chr():
Converts an integer to a string (1 character) corresponding to the encoding.
ord():
Converts a string (1 character) to the corresponding encoding (integer).
The sample code looks like this:
num = 10
str = str(num)
bool = bool(str)
print(type(str)) # <class 'str'>
print(type(bool)) # <class 'bool'>
3. Operators
运算符 | 描述 |
---|---|
[] 、[:] | 下标,切片 |
** | 指数 |
~ 、+、 - | 按位取反, 正负号 |
* 、/、 %、 // | 乘,除,模,整除 |
+、 - | 加,减 |
>> 、<< | 右移,左移 |
& | 按位与 |
^、 ` | ` |
<=、 < 、>、 >= | 小于等于,小于,大于,大于等于 |
== 、!= | 等于,不等于 |
is、 is not | 身份运算符 |
in、 not in | 成员运算符 |
not、 or 、and | 逻辑运算符 |
=、+=、-=、*= 、/= 、%= 、//=、**=、&=、` | = 、^=、>>=、<<=` |
and
That is, they are all true, and the result is true. If one of them is false, it is false;
or
If one of them is true, it is true. If the left side is true, the right side will not be implemented (short circuit principle);
not
Is reversed.
The assignment operator assigns the right value to the left variable
Of the compound assignment operator
a+=b
Is
a=a+b
, others are similar
4. Use of input function and placeholder
Use
input()
Function to get keyboard input (string).
Placeholder, as its name implies, is a symbol of standing position inserted in the output. Among them
%d
Is a placeholder for integers
%f
Is a placeholder for decimals
%s
Is a string placeholder
%%
Represents a percent sign (because the percent sign represents a placeholder, the percent sign in a placeholder string must be written as%%)
aa = input(" Please enter a string: ")
bb = int(input(" Please enter an integer value: "))
cc = float(input(" Please enter a floating-point value: "))
print(" This is the string entered as: %s" % aa)
print(" This is the integer entered as: %d" % bb)
print(" This is the floating point number entered as: %f" % cc)
5. Summary
Understand the role of variables, naming rules, and how to use them
Learn about the function
input()
,
type()
And how to use various functions of conversion types
Learned
Python
In the basic use of the operator, the assignment operator of the lowest priority, the priority is not a thorough understanding of the words can be through () to increase its priority.