Python Basics 1

Source: Internet
Author: User

The difference between Python2 and 3:

1. Character encoding

In python2.7

# _*_coding:utf-8_*_ Print ' Hello '

python3.5 doesn't need to be bothered by annoying character encodings.

Print (' hello ')

2.print

Need to add () in python3.5

3. Renaming some libraries

Old name New name

_winreg winreg

Configparser Configparser

Copy_reg Copyreg

Queue queue

Socketserver Socketserver

Markupbase _markupbase

Repr Reprlib

Test.test_support Test.support

Second, Python installs Linux installed Python3.5wget Https://www.python.org/ftp/python/3.5.2/Python-3.5.2.tgztar XF python-3.5.0.tgz
CD Python-3.5.0
./configure--prefix=/usr/local--enable-shared
Make
Make install
Ln–s/usr/local/bin/python3/usr/bin/python3 (soft link) Windows
1234567 1、下载安装包    https://www.python.org/downloads/2、安装    默认安装路径:C:\python273、配置环境变量    【右键计算机】--》【属性】--》【高级系统设置】--》【高级】--》【环境变量】--》【在第二个内容框中找到 变量名为Path 的一行,双击】 --> 【Python安装目录追加到变值值中,用 ; 分割】    如:原来的值;C:\python27,切记前面有分号
Third, Hello World procedure

Create a file under Linux called hello.py, and enter

#!/usr/bin/env python

Print ("Hello World")

So, execute:. /hello.py Can.

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

Comments:

When the line stares: # is annotated content

Multiline Comment: "" "Annotated Content" ""

Iv. user Input

#!/usr/bin/env python

#_*_coding:utf-8_*_   #name = raw_input("What is your name?") #only on python 2.x name  =  input ( "What is your name?" )#3.5 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: #!/usr/bin/env python # -*- coding: utf-8 -*-    import  getpass    # 将用户输入的内容赋值给 name 变量 pwd  =  getpass.getpass( "请输入密码:" )    # 打印输入的内容 print (pwd)Note: Can only be used on LinuxFive, module

(1) Sys

  import  sys print (sys.argv)     #输出   $ python test.py helo world   [ ‘test.py‘ ‘helo‘ ‘world‘ ]   #把执行脚本时传递的参数获取到了

(2) OS

Import OS

  os.system("df -h"#调用系统命令

(3) Tab Completion module

1#!/usr/bin/env python2#python startup file3ImportSYS4ImportReadLine5ImportRlcompleter6Importatexit7ImportOS8#Tab Completion9 Readline.parse_and_bind ('Tab:complete')10#History FileHistfile = Os.path.join (os.environ['HOME'],'. Pythonhistory')12Try:13readline.read_history_file (histfile)14exceptIOError:15Pass16Atexit.register (Readline.write_history_file, Histfile)17delOS, Histfile, ReadLine, Rlcompleter
View Code


 

Python Basics 1

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.