Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to understand the lexical structure of Python

2025-01-20 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article introduces the relevant knowledge of "how to understand the lexical structure of Python". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

1. Variables and types

A variable is a carrier for storing data, that is, a container. The variable in the computer is the actual data or a piece of memory space in which the data is stored in memory, and the value of the variable can be read and modified, which is the basis of all computers and controls. Computers include a variety of data types, such as text, video, audio, and so on.

1.1 types commonly used in Python

Integers: Python can handle integers of any size and supports binary, octal, and hexadecimal representations.

Floating-point: floating-point numbers are commonly known as decimals.

String type: a string is any text enclosed in single or double quotes, wrapped in single or double quotes, and can be written in multiple lines (beginning with three single or three double quotation marks, ending with three single or three double quotes).

Boolean: there are only two Boolean values, True and False, either True or False.

1.2 variable command

For each variable, you want to define a name for it, the command rules for the Python variable

Variable names consist of letters (generalized Unicode characters, excluding special characters), numbers, and underscores, and numbers cannot begin.

Case sensitive (uppercase an and lowercase An are two different variables).

Do not 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 it, you can call it directly in the function, as shown in the following code:

# Integer type a = 12x floating point type b = 1.2mm string type c = "one bowl week" # Boolean d = Trueprint (a, b, c, d) # 123 1.23 one bowl week True2, type of detection variable and conversion 2.1 detection variable type

Using the type () function to detect the change in type, the code is as follows:

# Integer a = 12 floating point b = 1.2 million string c = "one bowl week" # Boolean d = Trueprint (type (a), type (b), type (c), type (d)) # 2.2 Type conversion

You can convert variable types using functions built into Python

The details are as follows:

Int (): converts a numeric value or string to an integer, which can be specified as a base.

Float (): converts a string to a floating point number.

Str (): converts the specified object to a string, and you can specify the encoding.

Chr (): converts an integer to a string (one character) corresponding to the encoding.

Ord (): converts a string (one character) to the corresponding encoding (integer).

The sample code is as follows:

Num = 10str = str (num) bool = bool (str) print (type (str)) # print (type (bool)) # 3, operator

The order of the operators in the above table is roughly sorted from high to low

The identity operator is understood as yes or no

The member operator is understood to be in or out of

Logical operators connect Boolean types, and is all true, the result is true, and one of them is false, it is false; or is one of them is true, it is true, if the left is true, the right side will not be implemented (short-circuit principle); not is reversed.

Assignment operator assigns the value on the right to the variable on the left

The aplomb of the compound assignment operator is a=a+b, and the others are similar

3.2 use of input functions and placeholders

Use the input () function to get the keyboard input (string).

Placeholders, as the name implies, are symbols that stand in the output. Among them

D is a placeholder for an integer

% f is a placeholder for decimals

S is a string placeholder

%% represents the% sign (because the% sign represents a placeholder, the% sign must be written as%% in a placeholder string)

Aa = input ("Please enter string:") bb = int (input ("Please enter integer value:") cc = float (input ("Please enter floating point value:") print ("this is the input string:% s"% aa) print ("this is the input integer:% d"% bb) print ("this is the input floating point:% f"% cc) "how to understand Python lexical structure" ends here Thank you for your reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report