Python之路,Day2 - Python基础-变量

本节内容

  1. Python 2 or 3?
  2. 安装
  3. Hello World程序
  4. 变量

一、Python 2 or 3?

In summary : Python 2.x is legacy, Python 3.x is the present and future of the language

Python 3.0 was released in 2008. The final 2.x version 2.7 release came out in mid-2010, with a statement of

extended support for this end-of-life release. The 2.x branch will see no new major releases after that. 3.x is

under active development and has already seen over five years of stable releases, including version 3.3 in 2012,

3.4 in 2014, and 3.5 in 2015. This means that all recent standard library improvements, for example, are only

available by default in Python 3.x.

Guido van Rossum (the original creator of the Python language) decided to clean up Python 2.x properly, with less regard for backwards compatibility than is the case for new releases in the 2.x range. The most drastic improvement is the better Unicode support (with all text strings being Unicode by default) as well as saner bytes/Unicode separation.

Besides, several aspects of the core language (such as print and exec being statements, integers using floor division) have been adjusted to be easier for newcomers to learn and to be more consistent with the rest of the language, and old cruft has been removed (for example, all classes are now new-style, "range()" returns a memory efficient iterable, not a list as in 2.x). 

py2与3的详细区别

PRINT IS A FUNCTION

The statement has been replaced with a print() function, with keyword arguments to replace most of the special syntax of the old statement (PEP 3105). Examples: 

1
2
3
4
5
6
7
Old:  print  "The answer is" 2 * 2  New:  print ( "The answer is" 2 * 2 )
Old:  print  x,  # Trailing comma suppresses newline New: print(x, end=" ") # Appends a space instead of a newline
Old:  print  # Prints a newline
New:  print ()  # You must call the function!
Old:  print  >>sys.stderr,  "fatal error"  New:  print ( "fatal error" file = sys.stderr)
Old:  print  (x, y)  # prints repr((x, y))
New:  print ((x, y))  # Not the same as print(x, y)!

You can also customize the separator between items, e.g.: 

1
print ( "There are <" 2 * * 32 "> possibilities!" , sep = "")

ALL IS UNICODE NOW

从此不再为讨厌的字符编码而烦恼

还可以这样玩: (A,*REST,B)=RANGE(5)

1
2
3
4
<strong>>>> a, * rest,b  =  range ( 5 )
>>> a,rest,b
( 0 , [ 1 2 3 ],  4 )
< / strong>

某些库改名了

Old Name

New Name

_winreg

winreg

ConfigParser

configparser

copy_reg

copyreg

Queue

queue

SocketServer

socketserver

markupbase

_markupbase

repr

reprlib

test.test_support

test.support

  

还有谁不支持PYTHON3?

One popular module that don't yet support Python 3 is Twisted (for networking and other applications). Most

actively maintained libraries have people working on 3.x support. For some libraries, it's more of a priority than

others: Twisted, for example, is mostly focused on production servers, where supporting older versions of

Python is important, let alone supporting a new version that includes major changes to the language. (Twisted is

a prime example of a major package where porting to 3.x is far from trivial 

二、Python安装

windows

1
2
3
4
5
6
7
1 、下载安装包
     https: / / www.python.org / downloads /
2 、安装
     默认安装路径:C:\python27
3 、配置环境变量
     【右键计算机】 - - 》【属性】 - - 》【高级系统设置】 - - 》【高级】 - - 》【环境变量】 - - 》【在第二个内容框中找到 变量名为Path 的一行,双击】  - - > 【Python安装目录追加到变值值中,用 ; 分割】
     如:原来的值;C:\python27,切记前面有分号

linux、Mac

1
2
3
无需安装,原装Python环境
  
ps:如果自带 2.6 ,请更新至 2.7

三、Hello World程序

在linux 下创建一个文件叫hello.py,并输入

1
print ( "Hello World!" )

然后执行命令:python hello.py ,输出

1
2
3
localhost:~ jieli$ vim hello.py
localhost:~ jieli$ python hello.py
Hello World!

指定解释器

上一步中执行 python hello.py 时,明确的指出 hello.py 脚本由 python 解释器来执行。

如果想要类似于执行shell脚本一样执行python脚本,例: ./hello.py ,那么就需要在 hello.py 文件的头部指定解释器,如下:

1
2
3
#!/usr/bin/env python
  
print  "hello,world"

如此一来,执行: ./hello.py 即可。

ps:执行前需给予 hello.py 执行权限,chmod 755 hello.py

在交互器中执行 

除了把程序写在文件里,还可以直接调用python自带的交互器运行代码, 

1
2
3
4
5
6
localhost:~ jieli$ python
Python  2.7 . 10  (default,  Oct  23  2015 18 : 05 : 06 )
[GCC  4.2 . 1  Compatible Apple LLVM  7.0 . 0  (clang - 700.0 . 59.5 )] on darwin
Type  "help" "copyright" "credits"  or  "license"  for  more information.
>>>  print ( "Hello World!" )
Hello World!

四、变量\字符编码  

Variables are used to store information to be referenced and manipulated in a computer program. They also provide a way of labeling data with a descriptive name, so our programs can be understood more clearly by the reader and ourselves. It is helpful to think of variables as containers that hold information. Their sole purpose is to label and store data in memory. This data can then be used throughout your program.

声明变量

1
2
3
#_*_coding:utf-8_*_
 
name  =  "Alex Li"

上述代码声明了一个变量,变量名为: name,变量name的值为:"Alex Li" 

变量定义的规则:

    • 变量名只能是 字母、数字或下划线的任意组合
    • 变量名的第一个字符不能是数字
    • 以下关键字不能声明为变量名
      ['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']
变量的赋值
1
2
3
4
5
6
7
8
name  =  "Alex Li"
 
name2  =  name
print (name,name2)
 
name  =  "Jack"
 
print ( "What is the value of name2 now?" )

 


猜你喜欢

转载自blog.csdn.net/sj349781478/article/details/80940003
今日推荐