c c++,java,javacript到python你要懂得的

       

     

目录

问题背景

1.打印变量

2.弱类型

3.代码块上下对齐/缩进上下匹配

4.元组tuple,这个也是python中独用的语法

5. Dictionaries字典,对象键值对表示/可枚举属性

6.操作符重载

7.对象自动垃圾回收



问题背景

​ 随着人工智能,机器学习及深度学习的兴起,学Python的人越来越多。我最近用了不到一年的时间在Coursera学了机器学习和深度学习两个专业八门课程,拿到了相应的证书,其中的编程语言就是Python. 对于一个在学校学过c/c++,主要用java,javascript编程语言做互联网,企业应用之类的项目的程序员,如何能快速掌握Python来编写机器学习程序呢?以我的经验,总的来说,python的语法和javascript最相似,如果你已经掌握好了一门高级编程语言,你还应该掌握或特别注意以下编程语言的语法知识,让你快速过渡到python编程高手。

1.打印变量

为了较验你的程序是否运行正确,你往往要打印变量的值到控制台,这是最方便的debug方式

javascript的例子

var x = 100;

console.log(x);//print number x in console

x = { name: "xxx", sex: "male" };

console.log(x);//print object x in console

python的例子

x = 100;
print(x)  # print number x in console
x = {"name": "xxx", "sex": "male"}
print(x)  # print object x in console


 

2.弱类型

   变量弱类型,那么它就能接受不同类型的值给它赋值

Javascript弱类型的例子:

var x = null; //initialize x to null value

x = 1;//assign a number to variable x

x = "string value other than number";//assign a string to variable x

console.log(x)

Python弱类型的例子:

x = None  # initialize x to None value
x = 1  # assign a number to variable x
x = "string value other than number"  # assign a string to variable x
print(x)

3.代码块上下对齐/缩进上下匹配

python要求同一块代码要上下对齐,这是其它编程语言所不具备的语法

male = True
adult = True
# below code start from column 1
if male is True:
   # below code start from column 6
   if adult is True:
       print('adult male')
   # below code start from column 6 is same with its if operator
   else:
       print('others')
# below code start from column 1 is same with its if operator
else:
   print('others')

4.元组tuple,这个也是python中独用的语法

变量可以由多个变量组成,多个变量之间通过逗号分开

例子1,一个变量包含两个变量x,y

# define a variable z which is composed of variable x and y, separated by comma
x = 1
y = "a string"
z = x, y
print(z)

例子2 一个函数返回元组变量,由两个变量组成。直接取出这两个变量到x和y,或者赋值组一个变量z

# define a function which return a tuple
def return_tuple():
   x = 1
   y = "a string"
   return x, y
x, y = return_tuple()
print(x)
print(y)
z = return_tuple()
print(z)

5. Dictionaries字典,对象键值对表示/可枚举属性

   一个对象的零对或多对属性名和相关值的逗号分隔列表,用大括号({})括起来。

javascript的例子:

// define an object with two properties

x = { 'property1': 'x1', 'property2': 'x2' };

console.log(x);

// add one property numbered 3

x['property3'] = 'x3';

console.log(x);

// modify value in property1

x['property3'] = 'x3'

console.log(x);

// remove property1 from object x

delete x['property1']

console.log(x)

python的例子:

# define an object with two properties
x = {'property1': 'x1', 'property2': 'x2'}
print(x)
# add one property numbered 3
x['property3'] = 'x3'
print(x)
# modify value in property1
x['property1'] = 'modified x1'
print(x)
# remove property1 from object x
del x['property1']
print(x)

6.操作符重载

C++的例子:

// C++ Program to demonstrate Operator Overloading
#include <iostream>
using namespace std;

class class Point:
{
private:
   int x, y;

public:
   Point(int x = 0, int y = 0)
   {
      this.x = x;
      this.y = y;
   }
   // operator overloading for operator plus
   // This is automatically called when '+' is used between two Point objects
   Point operator+(Point const& another)
   {
      Point res;
      res.x = x + another.x;
      res.y = y + another.y;
      return res;
   }
   void print() { cout << "point at position (" << this.x<<","<<this.y << ")" << endl; }
};

int main()
{
   Point p1(1, 2), p2(2, 3);
   Point p3 = p1 + p2;
   p3.print();// output: point at position  (3,5)
}

Python的例子:

# Python Program to demonstrate Operator Overloading
class Point:
   def __init__(self, x=0, y=0):
       self.x = x
       self.y = y

   def __str__(self):
       return "point at position ({0},{1})".format(self.x, self.y)

   # operator overloading for operator plus
   # This is automatically called when '+' is used between two Point objects
   def __add__(self, another):
       x = self.x + another.x
       y = self.y + another.y
       return Point(x, y)


p1 = Point(1, 2)
p2 = Point(2, 3)

# plus two points then output its result
print(p1 + p2)

# output point at position  (3,5)

7.对象自动垃圾回收

C/c++不会自动对象做垃圾回收处理要编码通过free函数对对象进行垃圾回收。

其它语言包括python有垃圾回收机制,不用的对象,它们的系统会自动清理对象,回收内存。如果你学习Python,就不需要特别关注如何写代码去清理内存了。

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

JetHsu1

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值