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
True
、False
Two values , OrTrue
, OrFalse
.
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 lowercaseA
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
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 |
is 、 is not |
Identity operator |
in 、 not in |
member operator |
not 、 or 、and |
Logical operators |
= 、+= 、-= 、*= 、/= 、%= 、//= 、**= 、&= 、` |
= 、 ^=、 >>=、 <<=` |
- The order of operators in the above table is roughly from high to low
- The identity operator is understood as
yes
orNo
- The member operator is understood as
stay
perhapsbe 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
Namelya=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
summary
- Understand the role of variables , Naming rules , And how it is used
- Learned the function
input()
、type()
、 And the use of various functions of conversion types - 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.
https://en.pythonmana.com/2022/01/202201301044015818.html
The sidebar is recommended
- Python Network Programming -- create a simple UPD socket to realize mutual communication between two processes
- leetcode 110. Balanced Binary Tree(python)
- Django uses Django celery beat to dynamically add scheduled tasks
- The bear child said "you haven't seen Altman" and hurriedly studied it in Python. Unexpectedly
- Optimization iteration of nearest neighbor interpolation and bilinear interpolation algorithm for Python OpenCV image
- Bilinear interpolation algorithm for Python OpenCV image, the most detailed algorithm description in the whole network
- Use of Python partial()
- Python game development, pyGame module, python implementation of angry birds
- leetcode 1104. Path In Zigzag Labelled Binary Tree(python)
- Save time and effort. 10 lines of Python code automatically clean up duplicate files in the computer
guess what you like
-
Learn python, know more meat, and be a "meat expert" in the technical circle. One article is enough
-
[Python data structure series] "stack (sequential stack and chain stack)" -- Explanation of knowledge points + code implementation
-
Datetime module of Python time series
-
Python encrypts and decrypts des to solve the problem of inconsistency with Java results
-
Chapter 1: introduction to Python programming-4 Hello World
-
Summary of Python technical points
-
11.5K Star! An open source Python static type checking Library
-
Chapter 2: Fundamentals of python-1 grammar
-
[Python daily homework] day4: write a function to count the number of occurrences of each number in the incoming list and return the corresponding dictionary.
-
Python uses turtle to express white
Random recommended
- Some people say Python does not support function overloading?
- "Python instance" was shocked and realized the dirty words and advertisement detection of the chat system with Python
- Introduction to Python - CONDA common commands
- Python actual combat | just "4 steps" to get started with web crawler (with benefits)
- Don't know what to eat every day? Python to tell you! Generate recipes and don't worry about what to eat every day!
- Are people who like drinking tea successful? I use Python to make a tea guide! Do you like it?
- I took 100g pictures offline overnight with Python just to prevent the website from disappearing
- Binary operation of Python OpenCV image re learning and image smoothing (convolution processing)
- Analysis of Python event mechanism
- Iterator of Python basic language
- Base64 encryption and decryption in Python
- Chapter 2: Fundamentals of python-2 variable
- Python garbage collection summary
- Python game development, pyGame module, python takes you to realize a magic tower game from scratch (1)
- Python draws a spinning windmill with turtle
- Deep understanding of Python features
- A website full of temptations for Python crawler writers, "lovely picture network", look at the name of this website
- Python opencv Canny edge detection knowledge supplement
- Complex learning of Python opencv Sobel operator, ScHARR operator and Laplacian operator
- Python: faker extension package
- Python code reading (Part 44): find the location of qualified elements
- Elegant implementation of Django model field encryption
- 40 Python entry applet
- Pandas comprehensive application
- Chapter 2: Fundamentals of python-3 character string
- Python pyplot draws a parallel histogram, and the x-axis value is displayed in the center of the two histograms
- [Python crawler] detailed explanation of selenium from introduction to actual combat [1]
- Curl to Python self use version
- Python visualization - 3D drawing solutions pyecharts, Matplotlib, openpyxl
- Use python, opencv's meanshift and CAMSHIFT algorithms to find and track objects in video
- Using python, opencv obtains and changes pixels, modifies image channels, and trims ROI
- [Python data collection] university ranking data collection
- [Python data collection] stock information collection
- Python game development, pyGame module, python takes you to realize a magic tower game from scratch (2)
- Python solves the problem of suspending execution after clicking the mouse in CMD window (fast editing mode is prohibited)
- [Python from introduction to mastery] (II) how to run Python? What are the good development tools (pycharm)
- Python type hints from introduction to practice
- Python notes (IX): basic operation of dictionary
- Python notes (8): basic operations of collections
- Python notes (VII): definition and use of tuples