Python与C++的区别(基础)

15 篇文章 0 订阅
注释:
C++: template: 单行注释:
			   // 需要注释的内容
			   多行注释:
			   /* 需要注释的内容 */
	 code: // int cza;
		   /*
		   int csb;
		   int csc
		   */
Python: template: # 需要注释的内容
		code: # pza = 10
			  # pzb = 0

定义:
C++: template: 数据类型 名称及初始化等;
	 code: int ca = 10,cc,cd,ce,cg = 10,cf = 0;
	       double cb[3] = {1.09,983.98237,2.0923};
Python: 无


初始化:
1.变量
C++: template: 数据类型 变量名 = 数据;
	 code: int c1 = 10,c2 = 0;
Python: template: 变量名 = 数据
		code: p1 = 10
			  p2 = 0
			  p3 = 5
			  p4 = 0
2.C++叫数组,Python叫列表
C++: template: 数据类型 数组名[元素个数] = {可以有数据;也可以没数据,全部为0};
	 code: int ca1[5] = {};
		   double ca2[5] = {1.1,1.2,1.3,1.4,1.5};
Python: template: 列表名 = [可以有数据,也可以没数据]
		code: pl1 = []
			  pl2 = [1,2,0.23,238]
3.字典
Python: template: 字典名 = {键1:值1,键2:值2,键n:值n}
		code: pd1 = {"1":1,"1":2,"1":3,"1":4,"1":5}


输入:
C++: template: cin >> 需要输入的内容1 >> 需要输入的内容2;
	 code: cin >> cc;
		   cin >> cd >> ce;
Python: template: 需要输入的内容(一般为变量) = input(可加提示语)
		code: pa = int(input())
			  pb = input("你的名字叫什么? ")
			  pc = int(input())
			  pd = int(input())


输出:
C++: template: cout << 需要输出的内容1 << 需要输出的内容1;
	 code: cout << "你好" << endl;
		   cout << "Hello!" << "你好";
		   cout << "你好";
Python: template: print(需要输出的内容)
		code: print("你好")


if:
1.单分支 略
2.双分支 略
3.多分支
C++: template: if (条件表达式1)
			  {
			      语句1;
			  }
			  else if(条件表达式2)
			  {
			      语句2;
			  }
			  else if(条件表达式n)
			  {
				  语句n;
			  }
			  else
			  {
			      语句n-1;
			  }
	code: if (0 == cc)
		  {
			  cout << "0 = cc" << endl;
		  }
		  else if (0 == cd)
		  {
			  cout << "0 = cd" << endl;
		  }
		  else if (0 == ce)
		  {
			  cout << "0 = ce" << endl;
		  }
		  else
		  {
			  cout << "0 != cc,0 != cd,0 != ce" << endl;
		  }
Python: template: if 条件表达式1:
				      语句1
				  elif 条件表达式2:
				      语句2
				  elif 条件表达式n:
				      语句n
				  else:
					  语句n-1
		code: if pa == 0:
			      print("pa = 0")
			  elif pc == 0:
				  print("pc = 0")
			  elif pd == 0:
				  print("pd = 0")
			  else:
			      print("pa != 0,pc != 0,pd != 0")


for循环(初级):
C++: template: for (初值;条件;增量)
			   {
			       循环体;
			   }
	 code: for (int ci1 = 0;ci1 <= 10;ci1++)
		   {
		       cout << ci1 << " ";
		   }
Python: template: for 变量 in range(循环次数):
				      循环体
		code: for pi1 in range(10):
			      print(pi1)
				  print("  ")


while循环:
C++: template: while(循环条件)
			   {
			       循环体;
			   }
	 code: while(cg != 0 && cf == 0)
		   {
		       cout << cg << " ";
			   cg--;
		   }
Python: template: while 布尔型数据:
				      循环体
		code: while True:
			      print(1)
				  p4 += 1
				  if p4 >= 5:
				      break

本文为纯代码。

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值