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 learn Python from scratch

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

Share

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

This article mainly explains "how to learn Python from scratch". Interested friends may wish to have a look at it. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn the tutorial on how to learn Python from scratch.

Subject

Today's topic is to use Python to achieve a guessing game, the so-called guessing game is to let us set a number in the mind, and then let the computer to guess. Every time the computer guesses the number, we will tell the computer whether the guessed number is high or low, and so on, until the computer guesses it.

To complete this task, you need to understand the basic input and output of Python and the writing of loops, as well as dichotomy. The final result is something like this:

Knowledge point

First of all, we need to set a range for the number guessed by the computer, otherwise the program obviously won't work. This range should be entered by us, so we need to use the basic input and output of Python here.

Input

The input of Python is relatively simple and is implemented through input.

X = input ()

The result is an entire line of content on the screen, unlike other languages, which are segmented according to spaces, and are all read in the Python.

We can also add some prompts to the input, which can be passed into input as a string.

X = input ('Please enter a number')

Output

The output of Python is slightly more complex, via the print function. But there are several ways to print print, which we will introduce one by one.

The first is direct output. Python can output either one quantity or multiple quantities. If you want to output multiple quantities, you can use commas to separate them, and the comma-separated quantities will be separated by spaces when you output. These variables that are print may not be of string type, and Python will automatically call their _ _ repr__ method to convert them into strings.

Print (3) print ('hello',' world')

The second way is through string concatenation. Strings can be concatenated together by + in Python, so we can also concatenate multiple variables together and output them in print.

Print ('hello' + 'world')

Note here, however, that only strings can be concatenated by +, and an error will be raised if it is a variable of other types.

The third way is to use format, which I use more often. The advantage of this method is that print is relatively clear, and you can output some results in more complex formats.

Print ('{} like {}, but i prefer {} '.format (' xiaoming', 'basketball',' football'))

In other words, Python will fill the variables in format into the preceding {} in order.

The final approach is a variant of format, which only supports Python 3.6 and later. It's clearer than format, and the example above can be written like this using a format variant:

He = 'xiaoming' sport = [' basketball', 'football'] print (f' {he} like {sport [0]}, but i prefer {sport [1]}')

The if- select statement does not have the use of switch in Python, only if-else. It goes something like this:

If rule1 and rule2 or rule3: pass elif rule4: pass else: pass

Write the condition we want to judge after the if. And indicates the and operation of the condition, or indicates or operation, and not indicates the non-operation. The priority of and is higher than that of or. If you can't remember clearly, you can define the operation order through parentheses.

Neither elif nor else is required and is defined according to the actual situation.

While- cycle

This time we also need to use the while loop, and the meaning of the while loop is simple, that is, the while keyword is followed by a condition that will be executed when the condition is true until it is no longer true.

Write it down as follows:

While rule1: pass

Let's not talk about the break and continue keywords until we use them later.

Basically, all the knowledge points used are here, so you might as well write it yourself. It doesn't matter if you can't do it or the bug you encounter can't be adjusted. Reply to python1 in the official account background, and I'll send you the code I wrote.

At this point, I believe you have a deeper understanding of "the method course of learning Python from scratch". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

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