Learning Python—Day1

Contents of this section

1. Why learn Python

2. Python installation

3. Introductory study and case study

1. Why learn Python

1. Introduction to Python

The founder of Python was Guido van Rossum. During Christmas 1989, Guido Van Rossum decided to develop a new scripting language to pass the time.

2. Python is good at areas

(1) WEB development: Many excellent web frameworks and large websites are developed in Python, such as: Django framework, Douban, Youtube, etc.

(2) Financial field: quantitative trading, financial analysis

(3) Scientific computing, artificial intelligence:   typical libraries NumPy, SciPy, Matplotlib, Enthought libraries, pandas

(4) GUI graphics development: wxPython PyQT Kivy

(5) Operation and maintenance automation: OpenStack SaltStack Ansible Tencent Blue Whale

(6) Cloud computing: the most popular language for cloud computing, typically OpenStack

3. What kind of language is Python

Python is a dynamically interpreted and strongly typed language.

4. Python advantages and disadvantages

advantage:

(1) Positioning, elegant, clear and simple. That is: simple and easy to understand

(2) High development efficiency and very powerful third-party libraries

(3) High-level language - when you write programs in Python, you don't need to think about low-level details such as how to manage the memory used by your program

(4) Portability - Due to its open source nature, Python has been ported to many platforms. If care is taken to avoid using system-dependent features, all Python programs can run without modification on almost all platforms on the market

(5) Scalability - if you need a key piece of code to run faster or you want some algorithms not to be disclosed, you can write some programs in C/C++, and then use them in Python

(6) Embeddability - you can embed Python into C/C++ programs to provide scripting capabilities to your program users

shortcoming:

(1) Slow: slower than C, slower than JAVA

(2) The code cannot be encrypted: because it is an interpreted language

(3) Threads cannot utilize multiple CPUs

5. Python interpreter

When we write Python code, what we get is a text file with the extension .pythat . To run the code, the Python interpreter is required to execute the .pyfile.

Interpreters: CPython, IPython, PyPy, Jython, IronPython

The most widely used is CPython. If you want to interact with java and .net platforms, the best way is not to use Jython or IronPython, but to communicate through network calls to ensure the independence of each program.

 

PS: Python has so much to learn, there is no reason not to learn it.

 

2. Python installation

Take Windows 10 64-bit installation as an example:

1. Download the installation package corresponding to the system version from the official Python website: https://www.python.org/downloads/

2. Installation: You can modify the installation directory by yourself. If you do not want to configure the environment variables yourself, choose to add to the environment variables during installation. (Automatically added by me directly)

3. Introductory study and case study

The compiler used is PyCharm Professional Edition: first create a new project, and then create a new Python file in the project

1. Starter applet Hello Word

print("Hello Word!")

Right-click to run this program, and you can see that Hello Word is printed!

2. Variables

There is no need to specify variable types in Python

Rules for variable definition:

    • Variable names can only be any combination of letters, numbers or underscores
    • The first character of a variable name cannot be a number
    • The following keywords cannot be declared as variable names
      ['and', 'as', 'assert', 'break', 'class', 'continue', 'def', 'del', 'elif', 'else', ' except', 'exec', 'finally', 'for', 'from', 'global', 'if', 'import', 'in', 'is', 'lambda', 'not', 'or' , 'pass', 'print', 'raise', 'return', 'try', 'while', 'with', 'yield']

Variable assignment and printing:

name = "car"
print("My name is",name)

Notes

  Single-line gaze: # commented content

  Multi-line comment: """ Commented content """ (three single quotes, or three double quotes)

3. User input  

username = input("username:")
password = input("password:")

4.if...else expression

 

import getpass

_username = "sue"
_password = "abc123"
username = input("username:")
password = input( " password: " )
 # password = getpass.getpass("password:") #The cipher text is not easy to use in Pycharm, you should use Python

if _username==username and _password == password:
    print("Welcome user {name} login ...".format(name=username))
else:
    print("Invalid username or password!")

 

5.for loop

 

i loops 10 times: range(10) is 0,1,2,3,4,5,6,7,8,9

for i in range(10):
    print("loop:",i)

Guess age, print more than 3 times, try too many times or continue

age_of_oldboy = 56
for i in range(3):
    guess_age =int(input("guess age:"))

    if guess_age == age_of_oldboy:
        print("yes,you got it. ")
        break
    elif guess_age > age_of_oldboy:
        print("think smaller...")
    else:
        print("think bigger!")
else:
    print("you have tried too many times..fuck off")

6.while

 

age_of_oldboy = 56
count = 0
while count < 3:
    guess_age =int(input("guess age:"))

    if guess_age == age_of_oldboy:
        print("yes,you got it. ")
        break
    elif guess_age > age_of_oldboy:
        print("think smaller...")
    else:
        print("think bigger!")
    count+=1
    if count ==3:
        countine_confirm = input("do you want to keep guessing..?")
        if countine_confirm != 'n':
            count = 0

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325010811&siteId=291194637