首页 > 编程语言 >Python学习第二天

Python学习第二天

时间:2023-09-03 14:31:50浏览次数:44  
标签:name Python py 学习 python 第二天 print hello

一、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: 

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.: 

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

ALL IS UNICODE NOW

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

 

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

<strong>>>> a,*rest,b = range(5)

>>> a,rest,b

(0, [123], 4)

</strong>

某些库改名了

 

Python学习第二天_变量名

  还有谁不支持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、下载安装包

https://www.python.org/downloads/

2、安装

默认安装路径:C:\python27

3、配置环境变量

【右键计算机】--》【属性】--》【高级系统设置】--》【高级】--》【环境变量】--》【在第二个内容框中找到 变量名为Path 的一行,双击】 --> 【Python安装目录追加到变值值中,用 ; 分割】

如:原来的值;C:\python27,切记前面有分号

linux、Mac

无需安装,原装Python环境

 

ps:如果自带2.6,请更新至2.7

三、Hello World程序

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

1

print("Hello World!")

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

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自带的交互器运行代码, 

localhost:~ jieli$ python

Python 2.7.10 (default, Oct 23 201518: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.

声明变量

#_*_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']

变量的赋值

name = "Alex Li"

 

name2 = name

print(name,name2)

 

name = "Jack"

 

print("What is the value of name2 now?")

标签:name,Python,py,学习,python,第二天,print,hello
From: https://blog.51cto.com/u_15130867/7340487

相关文章

  • Python:使用Resend发送邮件
    官网:https://resend.com/很简单,只需调用api接口,即可发送邮件需要提前准备好参数api_key从Resend申请的keyto_email接收邮件的邮箱地址importrequestsheaders={'Authorization':'Bearer<api_key>','Content-Type':'application/json',}json_d......
  • python操作sqlite
    importjsonimportsqlite3importpandasaspdclassSqliteTool:def__init__(self,db_path):self.db_path=db_pathself.conn=sqlite3.connect(self.db_path)self.conn.row_factory=sqlite3.Rowself.cursor=self.con......
  • 科普:人工智能与机器学习的关系
    大家好,我是炼数之道,一个在人工智能之路上学习和摸索的算法工程师。今天的文章在前期推文的基础上,继续用通俗的话来介绍人工智能领域的基本概念。前期文章回顾:《科普:什么是机器学习》、《科普:什么是深度学习?什么是人工智能?》 那么,人工智能和机器学习之间的关系是什么呢?下图很好......
  • Python安装
    Python3编译安装1.安装编译相关工具yum-ygroupinstall"Developmenttools"yum-yinstallzlib-develbzip2-developenssl-develncurses-develsqlite-develreadline-develtk-develgdbm-develdb4-devellibpcap-develxz-develyuminstalllibffi-devel-y2.下载安......
  • python+selenium自动化测试
    自动化测试工具selenium使用指南python+selenium环境安装:直接pipinstallselenium 安装webdriver打开/关闭浏览器:importtimefromseleniumimportwebdriverbrowser=webdriver.Edge()browser.get("http://www.baidu.com/")time.sleep(5)browser.get("https://ma......
  • python学习
    python学习正则表达式的使用正则表达式以下是替换指定文件夹下文本中的内容对图片形式的pdf提取目录,可以用以下程序叠加多个正则表达式来去除重复项。importosimportredefreplace_timestamp(directory):#遍历目录下的所有文件和文件夹forroot,dirs,fil......
  • openGauss学习笔记-59 openGauss 数据库管理-相关概念介绍
    openGauss学习笔记-59openGauss数据库管理-相关概念介绍59.1数据库数据库用于管理各类数据对象,与其他数据库隔离。创建数据对象时可以指定对应的表空间,如果不指定相应的表空间,相关的对象会默认保存在PG_DEFAULT空间中。数据库管理的对象可分布在多个表空间上。59.2表空间在......
  • 【Python】90%开发者未注意到的Python特性
    1.引言如果你在日常工作中经常使用Python进行编码,那么毫无疑问你会非常喜欢这个简单的Python功能。闲话少说,我们直接开始吧!2.举个栗子例如,要求大家打印列表中元素的值。我想大部分人可以立即使用for循环来执行此操作。li=[10,20,30,40,50]foriinli:print(i)结......
  • 浅析常用的Python Web的几大框架
    在各种语言平台中,python涌现的web框架恐怕是最多的,是一个百花齐放的世界,各种micro-framework、framework不可胜数;猜想原因应该是在python中构造框架十分简单,使得轮子不断被发明。所 以在Python社区总有关于Python框架孰优孰劣的话题。下面就给大家介绍一下python的几大框架: Djan......
  • Python练习:嵌套列表解析,讲3*4的矩阵转换成4*3的矩阵
      1#嵌套列表解析,讲3*4的矩阵转换成4*3的矩阵23matrix=[[1,2,3,4],4[5,6,7,8],5[9,10,11,12]]678forrowinmatrix:9print("遍历每一行:",row)101112print("\n")1314s=[[row[i]forrow......