C++、java、python的一些区别

1.变量类型

变量赋值命名不同

Python 中的变量赋值不需要类型声明

counter = 100 # 赋值整型变量

miles = 1000.0 # 浮点型

name = "John" # 字符串

C++中变量必须有效的声明,变量可以在声明的时候被初始化(指定一个初始值)

int d = 3, f = 5; // 定义并初始化 d 和 f

char x = ‘x‘; // 变量 x 的值为 ‘x‘

在Java语言中,所有的变量在使用前必须声明

int a, b, c;         // 声明三个int型整数:a、 b、c

int d = 3, e = 4, f = 5; // 声明三个整数并赋予初值

String s = "runoob"; // 声明并初始化字符串 s

double pi = 3.14159; // 声明了双精度浮点型变量 pi

char x = ‘x‘;        // 声明变量 x 的值是字符 ‘x‘。

2.运算符

算数运算符:

Python算术运算符

**  : 幂 - 返回x的y次幂

a**b   //为10的20次方

输出结果 :

100000000000000000000

//   : 取整除 - 返回商的整数部分(向下取整

>>> 9//2

4

>>> -9//2

-5

C++算数运算符:

++ : 自增运算符,整数值增加 1

1++ 得 2

--:自减运算符,整数值减少1

1-- 得 0

Java算数运算符:

++ : 自增运算符,整数值增加 1

1++ 得 2

++1 得2

--:自减运算符,整数值减少1

--1 得 0

1—得0

前缀自增自减法(++a,--a): 先进行自增或者自减运算,再进行表达式运算。

后缀自增自减法(a++,a--): 先进行表达式运算,再进行自增或者自减运算 实例:

public class selfAddMinus

{

public static void main(String[] args){

int a = 5;//定义一个变量;

int b = 5;

int x = 2*++a;

int y = 2*b++;

System.out.println("自增运算符前缀运算后a="+a+",x="+x); System.out.println("自增运算符后缀运算后b="+b+",y="+y);

}

}

自增运算符前缀运算后a=6,x=12

自增运算符后缀运算后b=6,y=10

3.循环

while循环

Python:

while 判断条件:

执行语句……

count = 0

while (count < 3):

print ‘The count is:‘, count

count = count + 1

print "end"

The count is: 0

The count is: 1

The count is: 2

End

C++:

C++ 中 while 循环的语法:

while(condition)

{

statement(s);

}

statement(s) 可以是一个单独的语句,也可以是几个语句组成的代码块。condition 可以是任意的表达式,当为任意非零值时都为真。当条件为真时执行循环

#include <iostream>

using namespace std;

int main ()

{

// 局部变量声明

int a = 1;

// while 循环执行

while( a < 4 )

{

cout << "a 的值:" << a << endl;

a++;

}

return 0;

}

a 的值: 1

a 的值: 2

a 的值: 3

java:

while( 布尔表达式 )

{

//循环内容

}

只要布尔表达式为 true,循环就会一直执行下去

public class Test {

public static void main(String args[]) {

int x = 1;

while( x < 3 )

{

System.out.print("value of x : " + x ); x++;

System.out.print("\n");

}

}

}

value of x : 1

value of x : 2

For循环

Python语法:

for循环的语法格式如下:

for iterating_var in sequence:

statements(s)

for letter in ‘Python‘:

print ‘当前字母 :‘, letter

当前字母 : P

当前字母 : y

当前字母 : t

当前字母 : h

当前字母 : o

当前字母 : n

C++ 中 for 循环的语法:

for ( init; condition; increment )

{

statement(s);

}

#include <iostream>

using namespace std;

int main () { // for 循环执行

for( int a = 1; a < 3; a = a + 1 )

{

cout << "a 的值:" << a << endl;

}

return 0;

}

a 的值: 1

a 的值: 2

Java:

for(初始化; 布尔表达式; 更新)

{

//代码语句

}

public class Test

{

public static void main(String args[])

{

for(int x = 1; x < 3; x = x+1)

{

System.out.print("X的值 : " + x );

System.out.print("\n");

}

}

}

X的值 :1

X的值 :2

4.判断语句

Python 编程中 if 语句用于控制程序的执行,基本形式为:

if 判断条件:

执行语句……

else:

执行语句……

flag = False name = ‘luren‘

if name == ‘python‘: # 判断变量否为‘python‘

flag = True # 条件成立时设置标志为真

print ‘welcome boss‘ # 并输出欢迎信息

else:

print name # 条件不成立时输出变量名称

luren            # 输出结果

C++:

一个 if 语句 后可跟一个可选的 else 语句,else 语句在布尔表达式为假时执行。

if(boolean_expression)

{

// 如果布尔表达式为真将执行的语句

}

else

{

// 如果布尔表达式为假将执行的语句

}

#include <iostream>

using namespace std;

int main ()

{

// 局部变量声明

int a = 100;

// 检查布尔条件

if( a < 20 )

{

// 如果条件为真,则输出下面的语句

cout << "a 小于 20" << endl;

}

else

{

// 如果条件为假,则输出下面的语句

cout << "a 大于 20" << endl;

}

cout << "a 的值是 " << a << endl;

return 0;

}

结果:

a 大于 20

a 的值是 100

Java:

一个 if 语句包含一个布尔表达式和一条或多条语句。

if(布尔表达式)

{

//如果布尔表达式为true将执行的语句

}

public class Test {

public static void main(String args[]){

int x = 1;

if( x < 20 ){

System.out.print("结果小于20");

}

}

}

结果:结果小于20

5.数组

Python 元组:

tup1 = (‘physics‘, ‘chemistry‘, 1997, 2000)

tup2 = (1, 2, 3, 4, 5, 6, 7 )

print "tup1[0]: ", tup1[0]

print "tup2[1:5]: ", tup2[1:5]

以上实例输出结果:

tup1[0]:  physics

tup2[1:5]:  (2, 3, 4, 5)

C++数组:

double balance[] = {1000.0, 2.0, 3.4, 7.0, 50.0};

#include <iostream>

using namespace std;

#include <iomanip>

using std::setw;

int main () {

int n[ 10 ]; // n 是一个包含 10 个整数的数组 // 初始化数组元素

for ( int i = 0; i < 10; i++ )

{

n[ i ] = i + 100; // 设置元素 i 为 i + 100

}

cout << "Element" << setw( 13 ) << "Value" << endl;

// 输出数组中每个元素的值

for ( int j = 0; j < 10; j++ )

{

cout << setw( 7 )<< j << setw( 13 ) << n[ j ] << endl;

}

return 0;

}

Element        Value

0          100

1          101

2          102

3          103

4          104

5          105

6          106

7          107

8          108

9          109

Java数组:

public class TestArray

{

public static void main(String[] args) {

// 数组大小 int size = 10;

// 定义数组 double[] myList = new double[size];

myList[0] = 5.6;

myList[1] = 4.5;

myList[2] = 3.3;

myList[3] = 13.2;

myList[4] = 4.0;

myList[5] = 34.33;

myList[6] = 34.0;

myList[7] = 45.45;

myList[8] = 99.993;

myList[9] = 11123;

// 计算所有元素的总和

double total = 0;

for (int i = 0; i < size; i++)

{ total += myList[i];

}

System.out.println("总和为: " + total);

}

}

以上实例输出结果为:

总和为: 11367.373

6.基础语法

Python是交互式编程不需要创建脚本文件,是通过 Python 解释器的交互模式进来编写代码

>>> print "Hello, Python!"

Hello, Python!

C++ 程序可以定义为对象的集合,这些对象通过调用彼此的方法进行交互。

#include <iostream>

using namespace std;

// main() 是程序开始执行的地方

int main() {

cout << "Hello World"; // 输出 Hello World

return 0;

}

一个Java程序可以认为是一系列对象的集合,而这些对象通过调用彼此的方法来协同工作。

public class HelloWorld

{

/* 第一个Java程序 * 它将打印字符串 Hello World */

public static void main(String []args) {

System.out.println("Hello World"); // 打印 Hello World

}

}

7.字符串

Python 字符串

var1 = ‘Hello World!‘

var2 = "Python Runoob"

C++ 字符串

char greeting[6] = {‘H‘, ‘e‘, ‘l‘, ‘l‘, ‘o‘, ‘\0‘};

或者

char greeting[] = "Hello";

Java字符串:

String greeting = "helloword";

8.函数

Python函数代码块以 def 关键词开头,后接函数标识符名称和圆括号()。

def printme( str ):

"打印传入的字符串到标准显示设备上"

print str

return

C++:

// 函数返回两个数中较大的那个数

int max(int num1, int num2)

{

// 局部变量声明 i

nt result;

if (num1 > num2)

result = num1;

else

result = num2;

return result;

}

Java:

/** 返回两个整型变量数据的较大值 */

public static int max(int num1, int num2)

{ int result;

if (num1 > num2)

result = num1;

else

result = num2;

return result;

}

9.日期时间

Python 提供了一个 time 和 calendar 模块可以用于格式化日期和时间

import time; # 引入time模块

ticks = time.time()

print "当前时间戳为:", ticks

当前时间戳为: 1459994552.51

C++ 继承了 C 语言用于日期和时间操作的结构和函数

为了使用日期和时间相关的函数和结构,需要在 C++ 程序中引用 <ctime> 头文件。

#include <iostream>

#include <ctime>

using namespace std;

int main( ) {

// 基于当前系统的当前日期/时间

time_t now = time(0);

// 把 now 转换为字符串形式

char* dt = ctime(&now);

cout << "本地日期和时间:" << dt << endl;

// 把 now 转换为 tm 结构

tm *gmtm = gmtime(&now); dt = asctime(gmtm);

cout << "UTC 日期和时间:"<< dt << endl;

}

本地日期和时间:Sat Jan  8 20:07:41 2011

UTC 日期和时间:Sun Jan  9 03:07:41 2011

Java 日期时间:

import java.util.Date;

public class DateDemo {

public static void main(String args[]) {

// 初始化 Date 对象

Date date = new Date();

// 使用 toString() 函数显示日期时间

System.out.println(date.toString());

}

}

结果:Mon May 04 09:51:52 CDT 2013

10读写文件

Python File(文件) 方法

Python open() 方法用于打开一个文件,并返回文件对象,在对文件进行处理过程都需要使用到这个函数,如果该文件无法被打开,会抛出 OSError。

open() 函数常用形式是接收两个参数:文件名(file)和模式(mode)。

open(file, mode=‘r‘, buffering=-1, encoding=None, errors=None, newline=None, closefd=True, opener=None)

参数说明:

file: 必需,文件路径(相对或者绝对路径)。

mode: 可选,文件打开模式

buffering: 设置缓冲

encoding: 一般使用utf8

errors: 报错级别

newline: 区分换行符

closefd: 传入的file参数类型

opener:

C++ 文件

在从文件读取信息或者向文件写入信息之前,必须先打开文件。ofstream 和 fstream 对象都可以用来打开文件进行写操作,如果要打开文件进行读操作,则使用 ifstream 对象。

open() 函数是 fstream、ifstream 和 ofstream 对象的一个成员

void open(const char *filename, ios::openmode mode);

Java:

FileInputStream该流用于从文件读取数据,它的对象可以用关键字 new 来创建。

InputStream f = new FileInputStream("C:/java/hello");

或者

File f = new File("C:/java/hello"); InputStream out = new FileInputStream(f);

原文地址:https://www.cnblogs.com/liulala2017/p/10694912.html

时间: 2024-08-03 14:54:40

C++、java、python的一些区别的相关文章

java和python真的有区别吗?

Java和Python的特性区别! 这篇文章整合了java语言的特性和python语言的特性,希望可以帮到想要了解或初学编程的你! Python特性 Python是简单易学的:Python是一种代表简单主义思想的语言,它使你能够专注于解决问题而不是去搞明白语言本身.Python极其容易上手,因为Python有极其简单的说明文档. Python是速度快的:Python 的底层是用 C 语言写的,很多标准库和第三方库也都是用 C 写的,运行速度非常快. Python是免费.开源的:Python是FL

[LeetCode] 011. Container With Most Water (Medium) (C++/Java/Python)

索引:[LeetCode] Leetcode 题解索引 (C++/Java/Python/Sql) Github: https://github.com/illuz/leetcode 011.Container_With_Most_Water (Medium) 链接: 题目:https://oj.leetcode.com/problems/container-with-most-water/ 代码(github):https://github.com/illuz/leetcode 题意: 给一些

[LeetCode] 012. Integer to Roman (Medium) (C++/Java/Python)

索引:[LeetCode] Leetcode 题解索引 (C++/Java/Python/Sql) Github: https://github.com/illuz/leetcode 012.Integer_to_Roman (Medium) 链接: 题目:https://oj.leetcode.com/problems/integer-to-roman/ 代码(github):https://github.com/illuz/leetcode 题意: 把十进制转为罗马数. 分析: 模拟即可.

[LeetCode] 013. Roman to Integer (Easy) (C++/Java/Python)

索引:[LeetCode] Leetcode 题解索引 (C++/Java/Python/Sql) Github: https://github.com/illuz/leetcode 013.Roman_to_Integer (Easy) 链接: 题目:https://oj.leetcode.com/problems/roman-to-integer/ 代码(github):https://github.com/illuz/leetcode 题意: 把罗马数转为十进制. 分析: 跟 012. I

java.lang.ClassNotFoundException与java.lang.NoClassDefFoundError的区别

java.lang.ClassNotFoundException与java.lang.NoClassDefFoundError的区别 以前一直没有注意过这个问题,前两天机缘巧合上网查了一下,然后自己测试验证了一下.虽然网上说法很多,但是关于NoClassDefFoundError并没有给出一个样例,所以一直无法理解,索性自己验证了一下,收获还不少. ClassNotFoundException ClassNotFoundException这个错误,比较常见也好理解. 原因:就是找不到指定的cla

[LeetCode] 004. Median of Two Sorted Arrays (Hard) (C++/Java/Python)

索引:[LeetCode] Leetcode 题解索引 (C++/Java/Python/Sql) Github: https://github.com/illuz/leetcode 004.Median_of_Two_Sorted_Arrays (Hard) 链接: 题目:https://oj.leetcode.com/problems/Median-of-Two-Sorted-Arrays/ 代码(github):https://github.com/illuz/leetcode 题意: 求

[LeetCode] 005. Longest Palindromic Substring (Medium) (C++/Java/Python)

索引:[LeetCode] Leetcode 题解索引 (C++/Java/Python/Sql) Github: https://github.com/illuz/leetcode 005.Longest_Palindromic_Substring (Medium) 链接: 题目:https://oj.leetcode.com/problems/Longest-Palindromic-Substring/ 代码(github):https://github.com/illuz/leetcode

[LeetCode] 006. ZigZag Conversion (Easy) (C++/Java/Python)

索引:[LeetCode] Leetcode 题解索引 (C++/Java/Python/Sql) Github: https://github.com/illuz/leetcode 006.ZigZag_Conversion (Easy) 链接: 题目:https://oj.leetcode.com/problems/zigzag-conversion/ 代码(github):https://github.com/illuz/leetcode 题意: 把一个字符串按横写的折线排列. 分析: 直

原码,反码,补码详解及 Java中&gt;&gt;和&gt;&gt;&gt;的区别

前两天分析 HashMap 的 hash 算法的时候,遇见了 >> 和 >>> 这两个符号,当时查了下资料,在脑子中过了一下.今天又碰到了,没想到竟然忘了  0-0........ 我这记忆力哎,不说了.只好做个笔记,提醒自己,遇到啥不会的最好记下来,好记性不如烂博客啊~ 既然涉及到位运算,我们追本索源,就先从最基础的原码,补码和反码学起.搜了一下这方面的资料,发现一篇专门介绍这方面的文章,写的很是透彻,便直接引用过来了,原文地址是:http://www.cnblogs.co

java 和javaw 的区别——&lt;转&gt;

java 和javaw 的区别 javaw.exe用法和java.exe 相同 javaw的程序不在java console 上面显示任何东西,如果在开发程序,就用java,这样可以看到错误提示, 如果是运行完成了的程序,就用javaw, 可以提高一点速度 两个应用程序都能运行你的程序,并非常相似,但是有一个重要的区别,java通过控制台运行,javaw则不是. 控制台几乎是为纯文本编成的,例如如果你用javaw运行所有你打印的状态不会被打印出来.你打印在程序中的有用 信息,错误信息也是一样.