“Offer comes, ask friends to take it! I am participating in the 2022 Spring Recruit Punch card campaign. Click here for more details.”

Introduction of Python

Any programmer should learn Python, because it works damn well in certain scenarios.

Python is a programming language, simple but fun, that can do a lot of mind-boggling things with minimal scripting.

For example, put together a table for HR. Like writing a chatbot for your girlfriend. Like monitoring your crush’s circle of friends. Like writing automated scripts for the operation sister… Anyway, this is a language that lets you get out of your shell, and I don’t think anyone has ever defined Python that way.

What, you’re a girl? After that study to the boyfriend to change the BUG, in the technology to suppress him, when you fix a BUG for him, home forever you have the final say.

Python environment installation and development tool selection

Version selection and download

There’s a website for Python. If you search for Python at some point, the first thing you’ll see is all sorts of stuff, but chances are you won’t understand it at first.

If you’re running Windows7, choose Python3.6, Python3.7, or Python3.8. If you’re running Windows10, choose Python3.9. Python3.10 will do.

Do not ask which version, which version is the same to you, in principle, the lower the version is better, why? Because when you encounter a BUG, there is a lot of information on the Internet, the new version is available to older programmers, you encounter strange bugs, you can’t find information.

Don’t ask python2.x if you need to learn it. From the time you read this, you won’t.

Don’t ask if you can use Linux, Centos, Ubuntu, Windows is enough. What? You have a Mac, of course, but I don’t have a Mac, so I’m not going to talk about macs.

Python software download, although the website can also, but huawei open source mirror stations speed is really touching, version also good search, download url here: repo.huaweicloud.com/python/, also…

Find a version you like, click on it, and it will look something like this.

There is a bit of knowledge, there is English basis may not be able to download the correct, this is the difference between programming experience and no programming experience.

Before downloading, you need to determine the number of bits on your computer. Today’s computers are generally 64-bit, so choose Python-3.8.9-amd64. exe to download. Does it matter? Does it matter? Does it matter? Forget it. It’s not easy for you to remember.

Python installation

After downloading the file, there is an EXE program, double-click to install, select the default installation on the line. Here I have taken a screenshot of the Python3.7 installation interface to demonstrate why this version was chosen. Can I just say it’s because I happen to have this version on my desktop for some reason? Again, what if it doesn’t match the version you chose? No problem, or the same problem, the current stage of the installation of the environment is the focus, the differences between versions we do not use now.Have a doubt again, no matter what you think of now is very important, but need not consider, install the environment, can run is king.

Click on it and expect it to return no error. If it does, go to the search engine or ask the community. If your computer is Windows 10 or above, chances are it won’t be wrong.

Next, go to the Start menu on your computer and dig through Python, as I have shown below.

Ok, next we use Python3.6 demo. (are you mentally yelling at me, liar, didn’t you just install Python3.7? Come here ~)

Ok, now that you’ve seen the image, don’t click on anything, close the menu, and go back to the desktop as if nothing happened.

As a programmer, direct use of the mouse click, no feeling, pressing the shortcut keys to stimulate.

Press Ctrl+R on the keyboard, wake up the run window, print CMD, call out the console, only black can wake up our programming awareness.

In the above window, enterpython, and then enter Python interactive mode.Next type"hello world"So, by now, your Python environment is pretty much configured.The only thing to notice herehello world, before and after the double quotation marks, and is in English mode input, how about you try Chinese double quotation marks?

Continue typing exit(), noting the parentheses, to exit the Python environment.

In order to facilitate subsequent coding, you need to continue to enter in the current windowpip -V, the capital of theV, and then the following content appears, indicatingpipSuccessful installation, this is called package extension tool, the core function isUsing someone else’s code.

Anaconda installation

Now, once you’ve installed Python, you can actually write code, you can use notepad on your computer, but notepad is really hard to write, so you’ve got integrated development environments, and Anaconda is one of the great ones, and when you install this thing, it makes learning Python a lot faster, So you need to download and install one.

Search Anaconda in the search engine and find the mirror site of Tsinghua University, which is fast to download.After opening the corresponding site, select the latest version of the software to download.Since we downloaded the latest version of Anaconda, Python3.9 will be installed by default during the installation. Surprise? We are using the latest new version of the Python environment again. (note that this version is not available for Windows7)

After installation, the corresponding program is found in the start menu, and the following interface appears after running. Everything is successful.In the follow-up learning process, mainly aroundjupyterAction, so click under the iconLaunchTry running it for the first time and the program will open your default browser and openYour documentDirectory that can be modifiedCertainly some people do not like the code file in C disk, so there is a need to modify, change the method is also very simple, inYour document (C:\Users\Administrator)find\.jupyterFolder, which contains the files shown below.

Open the file with notepad or install a notepad++, navigate to line 393, change it to the following format, save, and exit.

c.NotebookApp.notebook_dir = 'F:/78tech'
Copy the code

Note that a new folder needs to be created on drive FjupyterYou can open the directory.Don’t panic if you can’t find the above file. Open itAnaconda Powershell PromptAnd then executejupyter notebook --generate-configJust generate the configuration file.

I met a Python

Find the New button on the right, pull down Python3, and click to create a New file where you can write Python code. About jupyter use, you can search engines, you can also follow the series of blogs, a little bit of learning.

Enter in the text boxprint("hello world")“, then click Run, and you have written your first Python code.The above code already contains a lot of programming knowledge, including no fewer than five Python knowledge points in a single line, which will be explained later.

It all starts with strings

First up, a Python string. A string is a programming term that translates to a string of characters.

In Python, strings are represented by quotation marks, such as double quotation marks “, single quotation marks “, and the odd triple quotation mark “””, corresponding to triple single quotation marks “”.

So the following code is declaring a string.

name = eraser
name = 'Eraser'
name = """ eraser """
name = Eraser
Copy the code

At this point, you should try typing in the code above to get a feel for programming. After a few lines of writing, if you can come up with the following conclusions, it’s a good proof.

  • The quotes come in pairs, one before and one after
  • Single quotation marks correspond to single quotation marks, double quotation marks to double quotation marks, and triple quotation marks to triple quotation marks
  • These quotation marks are entered in English mode. Chinese quotation marks cannot be used

Strings will be with us throughout our programming lives, in almost every piece of code, so we’ll cover them at the beginning.

Let’s take an example from the code above, name = “eraser”, where name is a variable, variable, and also a programming noun, = is an assignment operation, and the code reads from right to left, just like the old script, so name = “eraser” means, Assign a string eraser to the variable name.

This is where programming gets tricky, and you’re going to come across a lot of terms in the programming world, and that’s what programming languages are.

If you feel familiar with the above three nouns, chances are you will have the following questions. Don’t worry, let’s explain slowly.

  1. What is a variable?
  2. What does assignment mean?
  3. =Student: Isn’t it equal in math?

With these questions, you’re on your way. The hardest part of getting started with programming is these new concepts.

What is a variable? You’ve seen variables in grade one, for example, an equation of one variable, declaring an x.

x + 1 = 2So let's figure out what x isCopy the code

The x above is a variable, and when it comes to programming, it means something similar, something that can be changed, something that can be used to store some value, something that can represent a particular value in a particular place, these are all concepts of variables.

What does assignment mean? Assignment is an action, it’s a command, it’s telling the computer to bind this thing to that thing, do you think that’s what assignment means?

Why isn’t = equal? That’s a good question, because this is called an assignment in Python, so two equals equals equals equals, don’t ask why, just make sure you don’t make a mistake when you write code.

Strings in Python

So far, you’ve only touched on the Python concept of strings. Is ‘1’ a string? The answer must be, because 1 is wrapped in single quotes.

Since a string is a string of characters, you can think of it as a string of sugar gourd. You can do anything to the sugar gourd, you can do anything to the string, such as eat the first, eat the last, eat the middle 2, insert a hawthorn in the head, insert a hawthorn in the tail, count the number of hawthorn…

These correspond to Python strings, which are various programming syntax, programming concepts, and let’s translate them together.

I have a string (tanghulu) and I’m going to eat the first one

tang_hu_lu = "They say ice sugar gourd acid." # declare string
print(tang_hu_lu) Print the output string
print(tang_hu_lu[0]) Print the first character of the string
Copy the code

Note that the code is written in JUPyter, and then hit Run to achieve the effect, the shortcut key is Shift + Enter

Tang_hu_lu you already know that tang_hu_lu is a variable, but Python variables have naming conventions. 1_a, for example, is incorrect. The proposal goes searching, need to tell you only a principle, have meaning as far as possible namely, even if you use pinyin.

# indicates a comment. Anything written after # will not be executed by Python software. Tang_hu_lu [0] : [0] : [0] : [0] : [0] : [0] : [0] : [0] : [0] : [0] : [0] : [0] : [0]

Do you know binary? Binary is denoted by 0, 1, so count from 0.

We already know the first character. What about the last character

tang_hu_lu = "They say ice sugar gourd acid." # declare string
print(tang_hu_lu) Print the output string
print(tang_hu_lu[-1]) Print the last character of the string
print(tang_hu_lu[7]) Print the last character of the string
Copy the code

Note that the number in parentheses [-1] is the last one in reverse order. Of course, you can follow the number and count to 7 is the eighth hawthorn. Here try to change 7 to 8 and see what error is reported.

I have to throw in a new noun here,The indexThe numbers in brackets areThe index valueSee, it’s a new concept. I can’t help it. It’s programming.

You can get both the first and the last string. What if you eat the middle one

tang_hu_lu = "They say ice sugar gourd acid." # declare string
print(tang_hu_lu) Print the output string
print(tang_hu_lu[2:4]) # Output rock sugar
print(tang_hu_lu[3:5]) # Output sugar gourd
Copy the code

Note that the parentheses have been expanded to two numbers with a colon in the middle. Be sure to compare this to the original string.

- They say rock sugar gourd acid. - 0, 1, 2, 3, 4, 5, 6, 7Copy the code

Watch the positions

  • [then]The outputRock sugar, corresponding to the characters in index 2 and index 3 respectively;
  • [8]The outputSugar assembles, corresponding to the characters in index 3 and index 4 respectively;

So [0:6] do you know what the output is? Congratulations, you’ve mastered Python string slicing 90% of the time. Imagine another new concept, slicing!

String extension method

String in addition to slice usage, you can also use a new concept, method, called objects in Python has a saying, called everything has its object (the concept of the object in this study, we now know that the way it is), there is now a string, for example, the content is “said ice-sugar gourd, acid”.

Look at the code

my_str = "They say ice sugar gourd acid."
a = my_str.find("Sugar")
print(a) The output is 3
Copy the code

Find (” sugar “), where.find() is a string object method whose purpose is to find the index value of a small string named “sugar” within a large string.

Similarly, what other string object methods do you need to know?

my_str = "AaBbCcDd"
new_my_str = my_str.title()
print(new_my_str) # Aabbccdd
Copy the code

The.title() method capitalizes the first letter of the string

my_str = "AaBbCcDd"
new_my_str = my_str.upper()
print(new_my_str) # AABBCCDD
Copy the code

The.upper() method means to uppercase all strings, all lowercase is.lower()

String with left and right Spaces removed.

my_str = " AaBbCcDd "
new_my_str = my_str.strip()
print(new_my_str)
Copy the code

In addition to these, the method of string objects have a lot of, but I don’t need to remember, you need to search some related blog to read, search keywords for the Python string method, and then after a few blog, end up with, “oh ~ string objects with these methods, can caps, can remove the blank space, you can retrieve the string, You can count the number of occurrences of characters and determine the content of characters in a string…”

Built-in function

Now, we need to count the number of sugar gourd, that is, the number of characters in the string, first look at the code, as follows:

my_str = " AaBbCcDd "
len_my_str = len(my_str)
print(len_my_str) # 10
Copy the code

Len () in the code above is a built-in Function in Python, which stands for statistical length. Notice the difference between the built-in function and the method you just learned. Len () has no previous object to call. Symbols.

In the statistical process, you’ll also notice that “AaBbCcDd” is exactly 10 characters with Spaces, so you get the basic idea that Spaces are characters.

We’ll look at more built-in functions in the future, and then we’ll look at two more that will help you program more efficiently.

The first is help(), which is used to view the help manual for programming objects, such as the following code

my_str = " AaBbCcDd "
help(my_str )
Copy the code

But the code is not available injupyter, the following results are obtainedI can just type it in herehelp(), enter the manual mode, in the query.Enter in the text boxstr, you can get the related content in the help manual, which is to get the related methods of the string object.

The third built-in function to learn is dir(), which directly prints all the methods supported by an object.

my_str = " AaBbCcDd "
print(dir(my_str))
Copy the code

jupyterThe use of the process, if presentIn [*]“Indicates that the command is not executed, and you need to click on the menu barKernelThen restart the service.

String formatting

In Python, there are three ways to format a string: the % placeholder format, the format() method, and the f-string method. We’ll focus on the second and third ways to format a string, such as the following code:

my_str = "Xiao Ai, the weather today"
answer_str = "The weather today is: {}".format("Fine")
print(answer_str)
Copy the code

A brace {} appears in the second string and is then padded with the format() function. Similarly, braces can appear in multiple forms and have names, as in the following code:

# Multiple braces, the format() method requires multiple values to fill
my_str = "Xiao Ai, the weather today"
answer_str = "The weather this morning is: {}, the weather this afternoon is: {}".format("Fine"."Cloudy")
print(answer_str)
# there are placeholders in braces
my_str = "Xiao Ai, the weather today"
answer_str = "The weather this morning is {shangwu} and the weather this afternoon is {Xiawu}".format(shangwu="Fine",xiawu="Cloudy")
print(answer_str)
Copy the code

When using placeholders, be careful to be consistent, otherwise you will make mistakes.

The above code actually uses the function related support, these as we learn, will gradually unfold, the most effective way to learn, is to copy the code again.

conclusion

The first day of learning is done here, focusing on the Python environment installation, Anaconda installation, and Python strings related content, after mastering Python strings, the subsequent learning will be a little easier, I believe that you have joined 78 technical people can do it.

It’s not enough to just learn concepts for homework programming. The important thing is to integrate these concepts and solve practical problems. So look, can you solve the following problems?

  1. When reading some data, it is found that the string is read with Spaces around it, for example" a short text "Need to get rid of.
  2. When printing text, you need to print the user’s name and age to a fixed location in the template, for exampleZhang SAN.28Output format:Read zhang SAN, age 28.
  3. The login module has a verification code, which is case-sensitive when explicit, but does not require the user to enter it according to case, that is, displayaAbB, but the user inputaabbIt can also be verified.
  4. Read a text and find it"The"I wrote it wrong. I need to change it to"Have to", using the string object method. (This function is not covered in this article and depends on the search engine implementation.)

Support blogger time

Today is day 277/365 of continuous writing. You can follow me, like me, comment on me, favorites me.