In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-25 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly explains "what are the basic sentences of Python". The content of the explanation is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn what are the basic sentences of Python.
1. Print
The print () function is one of the most commonly used built-in functions in Python, and its function is to output objects as text. The most common usage is shown below:
Print ("Hello Python!") print ("Hello", "Python!")
Output:
Hello Python!Hello Python! II. Input
The function input () is just the opposite of print (), which allows the program to pause and wait for the user to enter some text. After getting the user input, Python stores it in a variable for your convenience.
For example, the following program asks the user to enter some text and then presents it to the user:
Text = input ("Please enter a text:") print (text)
Output:
The function input () takes a parameter: a prompt to be displayed to the user to let the user know what to do. In this example, when Python runs line 1, the user will see the prompt: Please enter a text:. The program waits for user input and continues to run after the user presses enter. The input is stored in the variable text, and the next print (text) presents the input to the user.
III. If
The if statement is the basis for judging the statement. The basic form of the if statement is as follows:
If case1: block1elif case2: block2else: block3
If the compound statement is a single statement, you can write the previous statement on the same line. Its basic form is as follows:
If case1:block1elif case2:block2elif case3:block3else:block4
Note: this principle also applies to for loop and while loop statements.
In addition, if the structure of the judgment condition is simple, you can also use the conditional expression, that is, the ternary operator. The basic form is as follows:
# if condition is true, return X, otherwise YX if condition else Y
Let's take a simple example to demonstrate the ternary operator:
# output the larger of the two numbers, aMagne b = 6pc8c = an if a > b else bprint (c)
Running result:
IV. For
The for statement is generated for iteration, and it is the basic statement in the loop statement. The basic forms of for are as follows:
For iter_var in iterable: repeat_block
For each loop, the iteration variable (iter_var) is set to the current element of the iterable object (iterable), which is provided for use by the repeat_block statement block. Where iterable can be a sequence, iterator, or other object that supports iteration.
Here is an example to demonstrate three different iterations:
1. Iterate over the elements in nameList directly.
NameList = ["Zhang San", "Li Si", "Wang er pockmarked"] for name in nameList:print (name)
Running result:
Zhang San Li Si Wang er Ma Zi
2. Through nameList element subscript iteration, len is used when generating range objects.
NameList = ["Zhang San", "Li Si", "Wang er pockmarked"] for i in range (0, len (nameList)): print (nameList [I])
Running result:
Zhang San Li Si Wang er Ma Zi
3. Generate an iterator using enumerate, and access the list subscript and element values in the for loop.
NameList = ["Zhang San", "Li Si", "Wang er Ma Zi"] for I, name in enumerate (nameList): print (I, name)
Running result:
Zhang San 1 Li Si 2 Wang er pockmarked five, while
The while statement is also the basic statement in the loop statement. The basic form of the while statement is as follows:
While condition: repeat_block
The block repeat_block in the while loop executes in the loop until the condition value is false.
After while and foe statements, there can also be else statements. The basic form of the while-else statement is as follows:
While condition: repeat_blockelse: once_block
Note: if the break statement within the loop terminates the loop, it does not enter the else statement block.
VI. Import
Use import or from in Python. Import to import the corresponding module. Here are several common forms:
Import the entire module module into: import module
Import a function from a module: from module import fuction
Import multiple functions from a module: from module import func1, func2, func3
Import all functions in a module: from module import *
7. Pass
The pass statement can be easily used as a placeholder. For example, where you need a statement block, you can use pass to occupy a place before you have a good idea of what to write. This is useful in development and program debugging.
For example, when we write a judgment statement, we can write something like this before we can figure out what each condition should execute:
If case1:passelif case2:passelse:pass
When determining what to do under each case condition, replace the pass with a program block.
Thank you for your reading, the above is the content of "what are the basic sentences of Python". After the study of this article, I believe you have a deeper understanding of what the basic sentences of Python have, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.