Origins of the Python language

It’s a cliche, but sometimes there’s an unexpected benefit to going back to your roots, and it’s all in your personal savvy.

The author of Python, Guido von Rossum, was indeed Dutch. In 1982, Guido received a master’s degree in mathematics and computer science from the University of Amsterdam. But although he was a mathematician, he enjoyed computers more. In his words, despite his qualifications in mathematics and computer science, he always tended to do computer work and was keen to do anything related to programming.

Python Learning Exchange: Click here

At that time, he was exposed to and used languages such as Pascal, C, and Fortran. The basic design principle of these languages is to make machines run faster. The core of all compilers is to optimize the program so that it runs. To increase efficiency, languages also force programmers to think like computers in order to write programs that are more machine-friendly. It was a time when programmers wanted to wring every inch of computer power from their hands.

This way of thinking, however, troubles Guido. Guido knows how to write a feature in C, but the whole process takes a lot of time. His other option is the shell. However, the essence of the shell is to invoke commands. It’s not really a language. For example, shells have no numeric data types, and addition operations are complicated. In short, the shell does not fully mobilize the functions of the computer.

Guido hopes to have a language, this language can be like C language, can fully call the functional interface of the computer, but also like shell, can be easily programmed. Guido began writing a compiler/interpreter for Python in 1989 to pass the Christmas holidays. Python comes from Guido’s beloved Monty Python’s Flying Circus. He hopes the new language, called Python, will fulfill his vision of a fully functional, easy-to-learn, extensible language between C and shell. As a language design enthusiast, Guido has made (unsuccessful) attempts at designing languages. This time, too, it was pure hacking.

In 1991, the first Python compiler (and interpreter) was created. It is implemented in C and can call the C library (.so files). Since its birth, Python has had classes, functions, exceptions, core data types including lists and dictionaries, and an extended system based on modules.

The data type

! [](https://pic2.zhimg.com/80/v2-6ee36ff8493b995d9d9a1342f3cef0f5_720w.jpg)

Number data type

Int integer (positive integer 0 negative integer)

A float is a decimal number

Bool Boolean (True True False False)

Insert a complex type (which I haven’t used in over two years of writing code)

# 1. Print (type(complexvar)) print(id(complexvar)) Res = complex(14,2) print(res)=> (14,2)Copy the code

Container data type

STR string

"" "" escape characters: \(1) convert a meaningful character into a meaningless character (2) Convert a meaningless character into a meaningful character \n or \r\n: Meaning \t: means "an indent" meaning \r: means to put all the characters after \r to the beginning of the lineCopy the code

Yuan string

Meta strings can invalidate escape characters

! [](https://pic3.zhimg.com/80/v2-d75791990a29f1fcbda6b74c3832c46a_720w.jpg)

String formatting

“%d %f %s” syntax: “string” % (actual value) %d placeholder D for integer data, %nd for N positions

! [](https://pic4.zhimg.com/80/v2-a15c9338e6369b26de727716e5e7f27b_720w.png)

Result: XXX bought 3 inflatable dolls

%f placeholder f indicates that floating point data is reserved with 6 decimal points by default. If f is preceded by a value, the corresponding decimal point is reserved according to the value

! [](https://pic4.zhimg.com/80/v2-fc35d69649c72a393a332acf5b537673_720w.png)

Result: Today Chinese cabbage 2.35 yuan a catty

! [](https://pic4.zhimg.com/80/v2-7157bd6c1de81dfe1e49589a4f408a0b_720w.jpg)

Result: today Chinese cabbage 2.3 yuan a catty

The %s placeholder represents a string

! [](https://pic1.zhimg.com/80/v2-6727a3b27db84cb1ec97e5856b7716d0_720w.jpg)

List phenotype ([])

Features: Data can be retrieved and modified in order

! [](https://pic1.zhimg.com/80/v2-c8789208a357f59fafeb4e83086ffbf4_720w.jpg)

List modification

! [](https://pic4.zhimg.com/80/v2-7a85078ba720c7d9163837bd1e469ee7_720w.png)

Tuple type (())

Characteristics: Data that can be retrieved but cannot be modified, in an orderly order

! [](https://pic4.zhimg.com/80/v2-97894b858c8bc3e1c93a102155c0cb73_720w.jpg)

Get data from a tuple: same value as list (forward subscript, reverse subscript)

Set set type ({})

The setvar = {} data type displays a dict dictionary

Features: Cannot be obtained, nor can be modified, unordered, automatically remove duplicate data

Dict dictionary ({” aaa “:” BBB “,})

To the left of the colon are the keys and to the right are the values, separated by commas

Features: The underlying hash algorithm is used to store the data as hashes, and the stored data is retrieved from the dictionary by key-value pairs. You can change it, just by replacing the key with the value that you want to change. In general, when you name dictionary keys, it's recommended to use strings, strings that are named by variables.Copy the code

! [](https://pic1.zhimg.com/80/v2-02bfb039bc48c64a3ac6fa9536714588_720w.jpg)

supplement

Function to get data type: type() Function to get variable address: id()

arithmetic

! [](https://pic4.zhimg.com/80/v2-e7ecc0230881d795a846d5c50e0496d3_720w.jpg)

However, with so many arithmetic operators, I suggest you look through them first to get an idea. You can save this picture first, and then look it up when you need it.

Let’s say the same thing again — computation priority: The Python world’s computation priority is the same as our usual computation priority.

! [](https://pic1.zhimg.com/80/v2-645abab2ee3c979484977d196a1dca38_720w.jpg)

String splicing

One great thing about Python that I really like is its string concatenation. Someone once said that programming is all about manipulating strings, and I think he’s right. Despite all the bells and whistles, it’s all about manipulating strings.

Anyway, I’ve had enough of the string manipulation in C/C++.

String concatenation in Python is simple. It uses the string concatenation symbol [+] to concatenate the variables to be concatenated.

! [](https://pic2.zhimg.com/80/v2-1d5701281232c84dd62a77560fe41755_720w.jpg)

But, since it’s string concatenation, the limitation is pretty obvious, you have to concatenate strings.

What if the pieces I have to put together are uneven? How to do? Don’t worry

Cast casting

There are three types of functions that convert data types: STR (), int(), and float().

! [](https://pic1.zhimg.com/80/v2-d3ea661582bcebd9e7a8e92e16f890e8_720w.jpg)

str()

The STR () function converts data to its string type, whether int or float, by placing it in parentheses. This data can be transformed into a string. Isn’t that easy? We can convert integer type [153] into string type [153] by just one step of STR (number), successfully completing data concatenation.

int()

The method of converting data to an integer type is also simple: the int() function. It is used the same way as STR (), just putting what you want to convert in parentheses, like this: int(what to convert). Int () casts only string data that meets the integer specification. Int () casts only string data that meets the integer specification. Although it is only one sentence, it actually has three meanings:

First, integer strings such as '6' and '1' can be cast by int(). Second, literal forms, such as Chinese, Martian, or punctuation, cannot be cast by int(). Finally, decimal strings cannot be cast using the int() function due to Python syntax rules.Copy the code

The int() function cannot be used, though, for floating-point strings. But floating-point numbers can be cast by int().

float()

The first use of the float() function is to put the data to be converted in parentheses, like this: float(data). Second, the float() function also converts integers and strings to floating-point types. But at the same time, if the data inside the parentheses is a string, it must be a number.Copy the code

So, after the previous STR () and int() exercises, is the float() function a little clearer?

To summarize

! [](https://pic3.zhimg.com/80/v2-2891bb05ced683df72eaad6ce48d3a9a_720w.jpg)

Standard input output

Okay, so some of you might be wondering, why don’t you say I/O? Don’t worry!

The print () function

Print (520) print(520) single quotes in parentheses. Print (' Let's play ') with double quotation marks in parentheses. Print (" Let's play ") double and single quotation marks inside parentheses. Print ("Let's play") print("Let's play")Copy the code

Print can print various types of data. See print() earlier in this article and print() later in this article.

The input () function

First, let’s take a look at how the input() function is used:

Input (' Please enter the name of the house you wish to attend in the following four boxes: gryffindor, Slytherin, Ravenclaw, Hufflepuff ')Copy the code

The input() function is the input function. In the example above, it would require you to enter the name of the house you would like to go to: in the following four boxes: gryffindor; Slytherin; Ravenclaw; Hufflepuff. So, when you write a question inside the function’s parentheses, the input() function displays the question as it is on the screen and waits for your answer to the question in the terminal area.

But why do we type the answer at the terminal? How about not typing? In fact, we can think of the input() function as a door between the real world and the code world. When a question is passed to us from the code world and we don’t answer it, the input() gate stays open, waiting for you to send an answer.

Pay attention to the point

For input(), the input value (the collected answer) will always be forcibly converted to string regardless of what answer we enter, whether you enter the integer 1234 or the string “Invisibility cloak is the magic I most want to have.”

Choice = int(input(‘ Please enter your choice: ‘))

Control statements

Conditional control statement

If judgment

! [](https://pic4.zhimg.com/80/v2-7a0cec33a940c49cb87ebd01735df1cf_720w.jpg)
! [](https://pic1.zhimg.com/80/v2-eb1df43c0efed072534e0e4e05a411fc_720w.jpg)

One detail you might notice here is that there are several empty Spaces after the colon: and before the next line in the conditional code, but why? First of all, in computer language, the technical term for space is indentation. For example, when we write an essay, we should leave two Spaces blank. This is called the first line indentation. icon

With Python, colons and indentation are syntax. It helps Python distinguish between layers of code and understand the logic and sequence of conditional execution. [Note: indent is four Spaces] Here do not use TAB, on the matter of four Spaces, young people so lazy why, after forming a habit of many places are restricted.

! [](https://pic1.zhimg.com/80/v2-284d82f591e4c291216722302c9096b0_720w.jpg)

If… else…

Most of the time, we can not put all our eggs in one basket, we should be prepared to do two things: if the conditions are not met, we will do. Python helpfully lets us borrow if… The else… If… is not satisfied,…

! [](https://pic1.zhimg.com/80/v2-d2201fbbb94fabacd04a9109f6d6a2e0_720w.jpg)

In the if… Else conditional statements, if and else are grouped together to form two separate code blocks. Represents a mutually exclusive relationship between a condition and another condition — if the if condition is not satisfied, the else condition is executed.

The if… elif… the else

For three or more conditions, we use Python’s multidirectional judgment command: if… Elif… The else… .

When more than three conditions are judged, elIF can be used for all intermediate conditions.

! [](https://pic1.zhimg.com/80/v2-284d82f591e4c291216722302c9096b0_720w.jpg)

Elif is not followed by else

If the nested

How do we write this rule in Python and evaluate it when there are ifs underneath?

The answer is nested conditions.

! [](https://pic4.zhimg.com/80/v2-604d14c27ab6c27f800352ddd8e2f01b_720w.jpg)

For…… in circulation

The Python for loop can iterate over any sequence of items, such as a list or a string.

The syntax for the for loop is as follows:

for iterating_var in sequence:
   statements(s)
Copy the code

! [](https://pic2.zhimg.com/80/v2-abc9c2855481fdd1753ca207bab3fc35_720w.jpg)
Print (' current letter :', letter) fruits = ['banana', 'apple', 'mango'] for fruit in fruits: Print (' current fruit :', fruit) print ("Good bye!" )Copy the code

As you can see, there is no pre-assignment for iterating_var in the template.

The range () function

Using the range(a,b) function, you can generate a sequence of integers. Such as:

For I in range(13,17): print(I)Copy the code

Results: 13, 14, 15, 16

The range() function has another use, which we’ll try directly:

For I in range(0,10,3): print(I)Copy the code

This is a slicing method, and the third parameter is called “step size”, which is a number of intervals. The result of this code execution is: 0, 3, 6, 9

Loop through the else statement

In Python, for… The for statement is the same as the normal statement. The else statement is executed when the loop completes normally, while… Same thing with else.

For num in range(10,20): # iterate over the number between 10 and 20 for I in range(2,num): Print ('%d = %d * %d' % (num, I,j)) else: print (num, 'is a prime number ')Copy the code

The while loop

The while loop is similar to the for loop, except that the count variable is initialized:

Print (a) (a +1) print(a) (a +1) print(a) (a +1) print(a) (a +1) print(a) (a +1) print(aCopy the code

! [](https://pic2.zhimg.com/80/v2-594d148f469804baaae50bdb88cbe601_720w.jpg)

Obviously, the while loop has two main points: 1. Release conditions; 2. Process.

As with the for loop, colons and indentation of internal code are essential.

other

break

Let’s look at the break statement first. Break means “to break” and is used to end a loop, usually written as if… Break. It’s written like this:

# break statement with for loop for... in... :... if ... Break # break statement with while loop while... (Condition):... if ... : breakCopy the code

Here, if… Break means to end the loop prematurely if one of the conditions is met. Remember, this can only be used inside the loop.

continue

Continue means’ to go on ‘. This clause is also used inside the loop. When a condition is satisfied, the continue statement is fired, which skips the rest of the code and goes straight back to the beginning of the loop.

# continue statement with for loop for... in... :... if ... : continue ... # continue statement with while loop while... (Condition):... if ... : continue ...Copy the code

! [](https://pic4.zhimg.com/80/v2-5e3a3293fa31df8df3e03ac09f299403_720w.jpg)

pass

The pass statement is very simple. It means “skip” in English.

Compare the two cycles

The biggest difference between a for loop and a while loop is whether the workload of the loop is determined. A for loop is like an empty room, and you don’t leave until you’ve done all the work. But a while loop is like a checkpoint pass, working until the checkpoint is not met

Practice small projects

Next, I’d like to talk to you about how a project is usually completed. More specifically, how do programmers think and solve problems?

I think one of the most important skills is problem solving. Problem disassembly refers to the disassembly of a matter or a problem into multiple steps or layers to gradually implement and solve the problem until the final effect is achieved.

! [](https://pic2.zhimg.com/v2-de524dab5e8e3c668e9e695d5c6f2fe1_b.jpg)

The suggestion collects, otherwise paddling paddling can not find to.

I created a Python learning q&A group, interested friends can know about it

There are a lot of dry goods (zero basis and advanced classical actual combat) to share with you, and there are eight years of development senior architecture beauty Python lecturer to give you free lessons, to share with you the most complete domestic high-end practical Python learning process system

The portal of the straight group:portal

I’m going to learn Python secretly and surprise everyone (Day 1)