Python Foundation Day1

Source: Internet
Author: User
Tags python script

Python Introduction

1, Python is an explanatory language, and the syntax is beautiful, clear, simple, is an excellent and widely used language

2, Python main application areas: ① Cloud Computing (typical application OpenStack) ②web development (such as YouTube, watercress ... Typical framework Django) ③ Scientific Operations, artificial intelligence (typical library numpy, SciPy) ④ system operations (operation and maintenance personnel must language)

⑤ Finance: Quantitative trading, financial analysis, in the field of financial engineering, Python is not only used, but also used the most, and the importance of increasing year by year. Reason: As a dynamic language Python, the language structure is clear and simple, the library is rich, mature and stable, scientific computing and statistical analysis are very good, production efficiency is much higher than c,c++,java, especially good at strategy backtesting ⑥ graphics gui:pyqt, WxPython, TkInter

3. Python's pros and Cons:

Advantages: ① Positioning "elegant", "clear", "simple". Easy to get started, can write very complex programs

② development efficiency is very high, Python has a very powerful third-party library, the Python official library has comprehensive support, can be developed on the basis of the base library, greatly reducing the development cycle.

③ high-level language, ④ portability, easy to run on different platforms

⑤ Extensibility: Requires the function code to run faster or some algorithms do not need to be exposed, you can use Python to invoke it after writing in C + +

⑥ embeddable: Python can be embedded in a/C + + program to provide scripting capabilities to your program users

Cons: ① slow: Python is slower than Java, but most of the time you can meet your program's speed requirements, unless you want to write a search engine that requires very high program speed.

The ② code cannot be encrypted: Because Python is an explanatory language, its source is stored in plaintext.

③ threads cannot take advantage of multi-CPU problems, which is one of the most common drawbacks of Python, the Gil is the global interpreter lock (interpreter lock), which is a tool used by the computer programming language interpreter to synchronize threads, so that only one thread executes at any moment, The python thread is the native thread of the operating system. On Linux for Pthread, on Windows for win thread, the execution of threads is fully dispatched by the operating system. A Python interpreter process has a main thread and the execution thread for multiple user programs. Multi-threaded parallel execution is prohibited even on multicore CPU platforms due to the existence of the Gil.

4. Python Interpreter

Python has a lot of interpreters, such as CPython, IPython, PyPy, Jython, IronPython, but the most widely used or CPython, If you want to interact with the Java or. NET platform, the best approach is not jpython and IronPython, but rather network-based interaction to ensure the independence of the program

5. Python version:

Python2. X and Python3. X, the difference is that the wording of the statement is inconsistent, 2.7 will be supported by 2020, will be replaced by 3.4+

Python installation

Windows

1. Download the installation package    HTTPS://WWW.PYTHON.ORG/DOWNLOADS/2, install the    default installation path: C:\python273, configure    the environment variable "right-click Computer"-"Properties"-"Advanced system Settings" --"Advanced"-"Environment variable"-"in the Second content box find a row of the variable named path, double-click the"-"Python installation directory appended to the variable value, use; split"    such as: the original value; C:\python27, remember that there's a semicolon in front

Linux, Mac

No installation, original Python environment  PS: If you bring your own 2.6, please update to 2.7

Python Basic Learning

1. Create files such as hello.py, and enter

Print ("helloWorld")

2. Then enter the command: Python hello.py, output Hello World

When executing Python hello.py in the previous step, the hello.py script is executed by the Python interpreter

If you want to execute a python script similar to executing a shell script, you need to hello.py the header of the file to specify the interpreter, as follows:

#!/usr/bin/env pythonprint ("Hello Wprld")

PS: Need to give hello.py execute permission before execution, chmod 755 hello.py

Executing in the interactive device

C:\users\chendezhi>pythonpython 3.6.4 (V3.6.4:d48eceb, Dec, 06:04:45) [MSC v.1900 + bit (Intel)] on Win32type ' Help ', ' copyright ', ' credits ' or ' license ' for more information.>>> print (' Hello World ') Hello World

variable, character encoding

declaring variables

Name = "Chendz"

Rules for variable definitions:

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 ', ' F ' Rom ', ' Global ', ' if ', ' import ', ' in ', ' was ', ' lambda ', ' not ', ' or ', ' pass ', ' print ', ' raise ', ' return ', ' try ', ' while ', ' WI Th ', ' yield ']

Assigning values to variables

Name = "Chendz" name2 = Nameprint (name,name2) name = "Jack"

Character encoding

ASCII: A maximum of 8 can be used to represent a byte, that is, 2**8=255,ASCII code can only represent a maximum of 255 characters

Chinese Code: >>gb2312 (1980) contains 7,445 characters, including 6,763 Kanji and 682 other characters.

>>GBK (1995) contains 21,886 symbols

>>GB18030 (2000) included 27484 Kanji

From ascii->gb18030, these coding methods are backwards compatible.

Unicode (Uniform Code) is a character encoding that is used on a computer. 16 Bits, minimum 2 bytes

UTF-8: Is the compression and optimization of Unicode encoding

When the Python interpreter loads the code in the. py file, the content is encoded (the default ASCII),

In the 2. X, if it is the following code, it will be error-guaranteed, ASCII code can not be expressed in Chinese

#!/usr/bin/env python  print "Hello, World"

The Python interpreter should be told what code to use to execute

#!/usr/bin/env python#-*-coding:utf-8-*-  print "Hello, World"

Comments

Single-line comment: Using #

Multiline Comment: Use "" "" "" "

User input

#name = Raw_input ("What's your Name?") #only on Python 2.xname = input ("What's your name?") Print ("Hello" + name)

When entering a password, if you want to be invisible, you need to take advantage of the Getpass method in the Getpass module, namely:

Import Getpass  # Assigns user input to the name variable pwd = getpass.getpass ("Please enter password:")  # Print the input contents of the printer (PWD)

Python basic syntax

An expression if: Else

Name = input ("Please enter user name")
PWD = Int (input ("Please enter password"))

If name = = "Chendz" and pwd = = 123:
Print ("Login Successful")
elif name = = "or pwd = =" ":
Print ("User name and password cannot be an empty string")
Else
Print ("User name or password error")

An expression for

For I in Range (0,10,2):    print ("loop", i)

An expression while

Age = 18count = 3while count > 0:    guess_age = int (input (' Guess Age: '))    if guess_age = = Age:        print ("OK") 
   
    break    elif guess_age >age:        print ("Think smaller ...")    else:        print ("Think bigger ...")    Count-=1else:    Print ("You have tried too many times.")
   

  

  

 

Python Foundation Day1

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.