Python - Get Started

Introduction

Python is an easy to learn, powerful programming language. It has efficient high-level data structures and a simple but effective approach to object-oriented programming.

Python’s elegant syntax and dynamic typing, together with its interpreted nature, make it an ideal language for scripting and rapid application development in many areas on most platforms.

The Python interpreter and the extensive standard library are freely available in source or binary form for all major platforms from the Python Web site, and may be freely distributed. The same site also contains distributions of and pointers to many free third party Python modules, programs and tools, and additional documentation.

The Python interpreter is easily extended with new functions and data types implemented in C or C++ (or other languages callable from C). Python is also suitable as an extension language for customizable applications.

Preparation

Python Installation

Install Python on windows

You can visit https://www.python.org/ to download the latest python version which is suitable for your OS.

Note: DO check Add Python 3.6 to path when installing Python otherwise you should add python.exe to Path manually.

 

IDEs

Here we introduce several IDEs for writing, testing and debugging your python code. 

  • PyCharm

  • Spyder 
  • Rodeo
  • Atom
  • Jupyter Notebook

More details can be found at https://www.datacamp.com/community/tutorials/data-science-python-ide. You can find the features of each IDE and choose the suitable IDE.

 

Basic Syntax

 Python is easy to use if you have the experience with other programming languages. 

 Here we introduce some basic syntaxes of python which are different from other programming languages.

Lines and Indentation

 

Python provides no braces to indicate blocks of code for class and function definitions or flow control. Blocks of code are denoted by line indentation, which is rigidly enforced.

The number of spaces in the indentation is variable, but all statements within the block must be indented the same amount. For example:

if True:

   print "True"

else:

   print "False"

 

Multi-Line Statements

Statements in Python typically end with a new line. Python does, however, allow the use of the line continuation character (\) to denote that the line should continue. For example −

total = item_one + \

        item_two + \

        item_three

Statements contained within the [], {}, or () brackets do not need to use the line continuation character. For example −

days = ['Monday''Tuesday''Wednesday',

        'Thursday''Friday']

 

Comments in Python

A hash sign (#) that is not inside a string literal begins a comment. All characters after the # and up to the end of the physical line are part of the comment and the Python interpreter ignores them.

 

### Python 中进程与线程的概念 #### 进程 (Process) 进程是指操作系统结构中的基本单元,拥有独立的地址空间。每个进程都有自己私有的数据段、堆栈区和全局变量等资源,在创建子进程时会复制父进程的数据副本[^3]。 - **优点** - 更高的稳定性:由于各进程间相互隔离,一个进程崩溃不会影响其他进程。 - **缺点** - 创建销毁成本高:因为涉及内存分配等问题,所以开销较大。 - 数据共享困难:不同进程之间传递消息较为复杂。 ```python from multiprocessing import Process, Queue def f(q): q.put([42, None, 'hello']) if __name__ == '__main__': queue = Queue() p = Process(target=f, args=(queue,)) p.start() print(queue.get()) # prints "[42, None, 'hello']" p.join() ``` #### 线程 (Thread) 线程是进程中可调度运行的一个实体;同一进程下的多个线程共享该进程的所有资源,包括代码段、数据段等,但每个线程有自己的调用栈[^1]。 - **优点** - 启动速度快:相比于进程来说更轻量级。 - 资源消耗少:不需要像启动新进程那样占用大量系统资源。 - **缺点** - 容易受到GIL(Global Interpreter Lock)的影响,在CPU密集型任务上表现不佳。 - 如果某个线程发生异常,则可能会影响到整个程序的安全性和可靠性。 ```python import threading class MyThread(threading.Thread): def run(self): print(f"{threading.current_thread().getName()} started") threads = [] for i in range(5): t = MyThread() threads.append(t) t.start() for thread in threads: thread.join() print("All threads finished execution.") ``` ### 应用场景分析 对于I/O 密集型操作(如文件读写、网络请求),推荐使用多线程来处理,这样可以在等待期间让出 CPU 给其他线程继续工作,从而提高整体性能。 而对于计算密集型的任务(比如大规模数据分析或图像渲染),则更适合采用多进程方案,以充分利用多核处理器的优势并绕过 GIL 的限制。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值