current position:Home>Python notes (II): lexical structure

Python notes (II): lexical structure

2022-01-30 10:44:04 A bowl week

Little knowledge , Great challenge ! This article is participating in “ A programmer must have a little knowledge ” Creative activities .

Hello everyone , I am a A bowl week , One doesn't want to be drunk ( Internal volume ) The front end of the . If you are lucky enough to get your favor , I'm very lucky ~

Variables and types

Variable is a carrier for storing data , That is, a container . The variables in a computer are the actual data or a piece of memory space in which data is stored , The values of variables can be read and modified , This is the basis of all computers and controls . Computers include a wide variety of data types , For example, text 、 video 、 Audio and so on. .

Python Common types

  • integer :Python Can handle integer of any size , And support binary 、 octal 、 Hexadecimal notation .
  • floating-point : Floating point numbers are commonly referred to as decimals .
  • String type : A string is any text enclosed in single or double quotation marks , single quote '' Or double quotes "" String of package , And it can be written in multiple lines ( Start with three single quotes or three double quotes , Three single quotes or three double quotes ).
  • Boolean type : Boolean is only TrueFalse Two values , Or True, Or False.

Variable command

Define a name for each variable ,Python Command rules for variables

  • Variable names are made up of letters ( The generalized Unicode character , Excluding special characters )、 Numbers and underscores make up , The number can't start .
  • Case sensitive ( uppercase a And lowercase A It's two different variables ).
  • Don't follow keywords ( Words with special meanings , I'll talk about it later ) And system reserved words ( Such as function 、 Name of module, etc ) Conflict .

Use of variables

If you define a variable and assign a value to it , You can call... Directly in the function , The following code :

#  Integer type 
a = 123
#  floating-point 
b = 1.23
#  String type 
c = " A bowl week "
#  Boolean type 
d = True

print(a, b, c, d)
# 123 1.23  A bowl week  True
 Copy code 

Detect the type and conversion of variables

Detect variable types

adopt type() Function to detect the change type , The code is as follows :

#  Integer type 
a = 123
#  floating-point 
b = 1.23
#  String type 
c = " A bowl week "
#  Boolean type 
d = True

print(type(a), type(b), type(c), type(d))
# <class 'int'> <class 'float'> <class 'str'> <class 'bool'>
 Copy code 

Type conversion

have access to Python The built-in functions in convert variable types , The details are as follows :

  • int(): Convert a numeric value or string to an integer , You can specify base .
  • float(): Convert a string to a floating point number .
  • str(): Converts the specified object to string form , You can specify the code .
  • chr(): Convert the integer to the string corresponding to the encoding ( A character ).
  • ord(): The string ( A character ) Convert to the corresponding code ( Integers ).

The sample code is as follows :

num = 10

str = str(num)

bool = bool(str)

print(type(str))  # <class 'str'>

print(type(bool))  # <class 'bool'>
 Copy code 


Operator describe
[][:] Subscript , section
** Index
~+- According to the not , The sign
*/%// ride , except , model , to be divisible by
+- Add , reduce
>><< Move right , Move left
& Bitwise AND
^、 ` `
<=<>>= Less than or equal to , Less than , Greater than , Greater than or equal to
==!= be equal to , It's not equal to
isis not Identity operator
innot in member operator
notorand Logical operators
=+=-=*=/=%=//=**=&=、` =^=>>=<<=`
  • The order of operators in the above table is roughly from high to low
  • The identity operator is understood as yes or No
  • The member operator is understood as stay perhaps be not in
  • Logical operators connect Boolean types ,and It's all true , The result is true , One of them is false , Is false ;or One of them is true , It is true , If the left is true , The right will not execute ( Short circuit principle );not Is to take the opposite .
  • The assignment operator assigns the value on the right to the variable on the left
  • Of the compound assignment operator a+=b Namely a=a+b, Other similar

input Use of functions and placeholders

Use input() Function to get keyboard input ( character string ).

Place holder , As the name suggests, it is the symbol of the station inserted in the output . among

  • %d Is a placeholder for integers
  • %f Is a placeholder for decimals
  • %s It's a string placeholder
  • %% It's a percent sign ( Because the percent sign represents a placeholder , Therefore, the percentage sign in the string with placeholder must be written as %%
aa = input(" Please enter the string :")
bb = int(input(" Please enter an integer value :"))
cc = float(input(" Please enter a floating point number :"))

print(" This is the input string for :%s" % aa)
print(" This is the integer entered as :%d" % bb)
print(" This is the floating point number of the input is :%f" % cc)
 Copy code 


  1. Understand the role of variables , Naming rules , And how it is used
  2. Learned the function input()type()、 And the use of various functions of conversion types
  3. Learned Python Basic usage of operators in , The assignment operator has the lowest priority , If you don't have a thorough understanding of priorities, you can use () To increase its priority .

copyright notice
author[A bowl week],Please bring the original link to reprint, thank you.

Random recommended