Python编程与Java编程
在过去的几年里,我一直在编写Java,现在我已经开始用python编写了(另外) 问题是,当我看我的Python代码时,它看起来像是有人试图将Java代码转换成Python格式,结果很糟糕,因为——好吧,Python不是Java 关于如何摆脱这种“用Python编写Java”模式的任何提示Python编程与Java编程,java,python,Java,Python,在过去的几年里,我一直在编写Java,现在我已经开始用python编写了(另外) 问题是,当我看我的Python代码时,它看起来像是有人试图将Java代码转换成Python格式,结果很糟糕,因为——好吧,Python不是Java 关于如何摆脱这种“用Python编写Java”模式的任何提示 谢谢 学习一些其他语言。它将帮助您区分算法(处理结构,语言之间保持不变)和语言的局部语法特征。然后,您可以为“Foo”和“Bar”语言的任意组合“在Bar中编写Foo” < p>这是有用的,如果您想了解如何以
谢谢 学习一些其他语言。它将帮助您区分算法(处理结构,语言之间保持不变)和语言的局部语法特征。然后,您可以为“Foo”和“Bar”语言的任意组合“在Bar中编写Foo” < p>这是有用的,如果您想了解如何以更python或正确的方式对Python进行编码: 您可以将代码张贴以查看是否有人可以向您展示更多的python方式。 您可以考虑将自己沉浸在Python范式中。最好的方法是首先了解它们是什么,然后通过阅读一些文献和查看一些代码示例来探索最佳实践。我推荐马克·卢茨;非常适合初学者和高级用户 如果您使用Python编程,并且未能充分利用所有内置的、开发人员友好的Python语法,那么您将对自己造成极大的不公
正如我的法语老师过去常说的,“法语不仅仅是有不同单词的英语。”你可以从阅读开始。它将让您了解Python代码应该如何编写,前提是您对该语言的理解足以理解它所说的内容。:-) 如果您想看到一些相当地道的Python,它可以做一些不平凡的事情,尽管有更新的样式提示,可能是更好的样式提示来源。如果您想了解更多的要点,请查看以下内容。Python与C/Java类语言的一些主要区别是:
当然,还有其他获取他人代码的方法。您还可以下载并浏览任何优秀的开源Python项目的代码。我建议,书籍也是一种很好的资源。它有很多有用的代码示例和非常详细的解释。肯定不是万能的,但我认为您应该尝试一些Python代码。显然,没有人应该编写“golfed”代码IRL,但找到最简洁的方式来表达某种东西确实会迫使您利用该语言的内置功能。如果您是Python新手,并且来自Java(或C#,或其他类似的静态类型OO语言),PJ Eby和Ryan Tomayko的这些经典文章是必要的阅读:
- (PJE)
- (PJE)
- (PJE)
- (托梅科)
- (托梅科)
另外,请查看我不久前发布的类似帖子。吃蟒蛇、睡蟒蛇和喝蟒蛇。这是唯一的方法……。尝试找到您非常了解的算法,并了解它们是如何在python标准库中实现的 坚持……) 1)Python支持 面向对象编程;但事实确实如此 可以编写一个Python程序而不需要 利用OO概念 1) Java只支持面向对象 编程 2) Python被设计为解释性地使用。 Python语句可以在 翻译提示 (>>>) ,并将被执行 马上。(实施会带来一些好处 使用自动编译成字节码 (.pyc文件) 2) 用Java编写的程序必须显式 编译成字节码(.class文件), 虽然IDE可以在一个特定的环境中自动完成这项工作 对用户透明的方式。Java有 不支持直接执行语句- 尽管有像Dr.Java这样的工具 支持这一点 3) Python是动态类型化的: •通过分配变量来引入变量 它的价值。例如:
someVariable = 42
someVariable = 'Hello, world'
someVariable=42
•已分配值为的变量
一个给定的类型可能会在以后更新
int someVariable;
int someVariable = 42;
Statements in Java always end with a
semicolon (;)
// This is a comment
A new statement starts here
/* This is also a comment */
/* And this is also a comment, which is
long enough to require several lines
to say it. */
'This is a string'
"This is also a string" # Equivalent
'c' # A string
"c" # An equivalent string
and, or and not. Example:
not(x > 0 and y > 0) or z > 0
"This is a String"
'c' // A character, but not a String
Java uses the following operators for
constructing compound boolean expressions:
&&, ||, ! and ^ (meaning exclusive or)
Example:
! (x > 0 && y > 0) || z > 0 ^ w > 0
for p in pixels:
something
for (variable in collection) Example:
for (p in pixels)
something;
for i in range(1, 10)
something
(i takes on values 1, 2, 3, 4, 5, 6, 7, 8, 9)
for i in range(1, 10, 2)
something
(i takes on values 1, 3, 5, 7, 9)
for (int i = 1; i < 10; i ++)
something;
(i takes on values 1, 2, 3, 4, 5, 6, 7, 8, 9)
for (int i = 1; i < 10; i += 2)
something;
(i takes on values 1, 3, 5, 7, 9)
if x < 0:
something
if x < 0:
something
else:
something different
if x < 0:
something
elif x > 0:
something different
else:
yet another thing
if (x < 0)
something;
if (x < 0)
something;
else
something different;
if (x < 0)
something;
else if (x > 0)
something different;
else
yet another thing;
if x < 0:
do something
do another thing regardless of the value of x
if x < 0:
do something
do something else
do yet a third thing
do another thing regardless of the value of x
if (x < 0)
do something;
do another thing regardless of the value of x;
if (x < 0)
do something; // Bad style-don't do this!
do another thing regardless of the value of x;
if (x < 0)
{
do something;
do something else;
do yet a third thing;
}
do another thing regardless of the value of x;
if (x < 0)
{
do something;
}
do another thing regardless of the value of x;