Introduction to Python + small routine

Time:2022-5-4

PythonIs one of the most popular programming languages in the world

PythonIt has occupied the first place in the ranking of various languages for many consecutive years. It has always been equal to R language in the field of data analysis, but in recent years, python has become more and more popularPythonAnalysis tool libraries have sprung up, and many open source data analysis libraries are welcomed by data analysts.

From data collection, cleaning, processing, analysis, report, data mining, machine learning,PythonHave provided mature solutions, it can be said that they have learnedPython, it can be used in the field of data analysis. In fact, some commonly used Python libraries, such as numpy / pandas / mattolib, have become daily necessary tools for data analysts of major Internet enterprises.

PythonIt is widely used in various fields, including data analysis, server development, operation and maintenance development, artificial intelligence development, and even children’s programming.

There are currently two versions available:

1. Python 2

2. Python 3

There are only slight differences between the two versions. The former has an early technology market, high popularity and active community, butPython officialIt will not be maintained since January 1, 2020. Therefore, it is only a matter of time to replace Python 3. Fortunately, there is little difference. It is recommended that developers use Python 3.

In terms of development environment, there are two main types:

1. Command line interaction mode:

Introduction to Python + small routine

2. Integrated development environment(PyCharm):

Introduction to Python + small routine

The two methods have their own advantages and disadvantages. In short, the interaction mode is simple and powerful, but it is difficult to operate. If you want to eat chicken, you need to remember it, and multi line coding is complex; IDE environment, intuitive commands, easy to use, requires additional software or plug-in support, where notspotwhere?

There is no difference between good and bad coding methods“Without permission”From this, perhaps the functions that can be realized by a few characters in the command-line mode can be highlighted in the IDE for a long time, and perhaps a large number of difficult commands can be written in advance by clicking the shortcut command menu in the IDE. For example, VIM, Lua, Scala, or spark shell, HBase shell, zkcli SH, such as eclipse, ieda, visual studio, vs Code and Xcode, interaction mode is the key of technologyInside, IDE is the of technologyface, take it inside and out, and you’ll be a big man.

In short, the interaction mode is zero distance contact with the technical core. The IDE mode is to beat cattle across the mountain and add sharp tools. Novices use the IDE first and study the interaction mode after they have rich experience. They will find endless mysteries.

Access to the topic

Variable declaration

x, y = 123,456
print(x, y)

123 456

#Fast exchange
x, y = y, x
print(x, y)

456 123

Output article

print("Hello World")

Hello World

print("Hello" , "World")

Hello World

print("Hello" + "World")

HelloWorld

print("My age is", 18)

My age is 18

#ERROR
print("My age is " + 18)
Traceback (most recent call last):
  File "F:/lagou/TestPython/helloworld.py", line 1, in <module>
    print("My age is " + 18)
TypeError: must be str, not int

print(“My age is ” + str(18))

My age is 18

Data type

String type

sentence = "This's a very long long long \
long long sentence............"
print(sentence)

This’s a very long long long long long sentence…………

paragraph = """This is line 1,
this is line 2,
this is line 3.
The End.
print(paragraph)
"""

*
This is line 1,
this is line 2,
this is line 3.
The End.
*

Null type none

temp = None 
print(temp)

None

Boolean type conversion

For numeric types, all non-zero values are converted to true, and only zero values are converted to false
#The following values are true
print(  bool(1)         )
print(  bool(-1)        )
print(  bool(255)       )
print(  bool(0.0000001) )
print(  bool(-99.99)    )
#The following value is false 
print(  bool(0)         )
print(  bool(0.0)       )
For any non empty string, conversion to a Boolean value is true
#This is an empty string and the conversion result is false
print(  bool("")    )
#The conversion result is true
print(  bool("abc") )
#This is a string containing only one space, and the conversion result is true
print(  bool(" ")   )

*
False
True
True
*

Converting null values to Boolean values is always false
print(  bool(None)  )

False

Other types — > strings
Str (true) # result is' true ' 
Str (false) # result is' false ' 
Str (none) # result is' none '
Str (123) # result is' 123 '
String — > number
Int ("100") # result is 100
Int (3.14) # result is 3
Float (100) # result is 100.0
Round
#Keep three decimal places. Since the fourth digit is 5, the result is 3.142 
round(3.1415926, 3)

Operator article

Division and integral Division
Print (4 / 2) # result is 2.0
Print (5 / 2) # result is 2.5 
Print (5 // 2) # division result is 2
String use operator
print("apple " + "apple " + "apple ")

apple apple apple

print("apple " * 5)

apple apple apple apple apple

Assignment Operators
x = 2
y = 3
x **= 3
print(x)

8

Logical operator and or not

Conditional judgment

The condition is sandwiched between a keyword and a colon (:)
Indent determines the scope
Elif is equivalent to else if of Java
score = 100 
if score >= 60: 
    if score < 70: 
        Print ("your test result is qualified") 
    elif score < 90: 
        Print ("your test score is good") 
    else:
        Print ("your test score is excellent") 
else:
    Print ("your test result failed")
Concise compound condition
age = 22
if 18 < age < 60:
    Print ("you're not a child anymore, it's time to go to work")
Pass is the keyword of python, which means to do nothing
result = None
if result:
    pass 
else:
    Print ("nothing gained")

The 0 value, none, and empty string are all false when converted to a Boolean value

Circular chapter

Wihle cycle
lap = 0
while lap < 10:
    lap += 1
    Print ("I finished the first" + str (lap + 1) + "lap")

*
Nothing
I finished the second lap
I finished the third lap
I finished the fourth lap
I finished the fifth lap
I finished the sixth lap
I finished the seventh lap
I finished the eighth lap
I finished the ninth lap
I finished lap 10
I finished lap 11
*

For loop
seq = "hello"
for s in seq:
    print(s)

*
h
e
l
l
o
*

for i in range(5):
    print(i)

*
0
1
2
3
4
*

loop nesting
#Specifies the width and height of the rectangle
width, height = 10, 5
#Because it starts printing from top to bottom, traverse the height first
for i in range(height):
    for j in range(width):
        print("*", end="")
    print()
**********
**********
**********
**********
**********
for i in range(5):
    for j in range(i + 1):
        print("*", end="")
    print()
*
*
**
***
****
*****
for i in range(1, 10):
    for j in range(1, i + 1):
        print("%s*%s=%s" % (j, i, i * j), end=" ")
    print()
1*1=1 
1*2=2 2*2=4 
1*3=3 2*3=6 3*3=9 
1*4=4 2*4=8 3*4=12 4*4=16 
1*5=5 2*5=10 3*5=15 4*5=20 5*5=25 
1*6=6 2*6=12 3*6=18 4*6=24 5*6=30 6*6=36 
1*7=7 2*7=14 3*7=21 4*7=28 5*7=35 6*7=42 7*7=49 
1*8=8 2*8=16 3*8=24 4*8=32 5*8=40 6*8=48 7*8=56 8*8=64 
1*9=9 2*9=18 3*9=27 4*9=36 5*9=45 6*9=54 7*9=63 8*9=72 9*9=81 

The End