python面试之语言特性(1)

Python和Java语言的区别

1.语言类型的不同

Java是一种静态类型语言,更适合作为一个底层的实现语言。
python是一种动态类型语言,更适合作为一种‘胶水’语言。

2.变量使用的不同

java中所有变量必须先声明才能使用,它是面向对象的,你还需要执行一个编译过程来编译代码,然后才可以运行它。例如以java中八种基本数据类型为例的代码:

java

byte a = 100
short s = 1000
int a = 100000
long a = 100000L
float f1 = 234.5f
double d1 = 123.4
boolean one = true
char letter = 'A';

python

python中不需要声明变量。你也可以混合使用面向对象和命令式编程,且你可以直接运行你的代码。例如以python中五种标准数据类型为例的代码:

var1 = 1
s = 'abcdef'
list = [ 'runoob', 786 , 2.23, 'john', 70.2 ]
tuple = ( 'runoob', 786 , 2.23, 'john', 70.2 )
tinydict = {'name': 'john','code':6734, 'dept': 'sales'}

3.运行速度

Java快,python 慢。可以用java和python执行一段同样的逻辑代码看谁的速度快。本人经过测试,百分之90以上都是java执行的时间短。

Java

public static void main(String[] args) {
    long timeStart = System.currentTimeMillis() / 1000;
    for (int i = 0 ;i < 1000000;i++){
        System.out.println("java");
    }
    long endStart = System.currentTimeMillis() / 1000;
    long diffTime = endStart - timeStart;
    System.out.println(diffTime);
}

python

import time
if __name__ == '__main__':
    timeStart = time.time()
    for i in range(0,1000000):
        print('python')
    timeEnd = time.time()
    diffTime = timeEnd - timeStart
    print(diffTime)

4.HELLO WORLD

请看如下代码:

java

public static void main(String[] args) {
    System.out.println("hello world");
}

python

print('hello world')

5.字符串操作的不同

请看如下代码:

java

public static void main(String[] args) {
    String strTest = "hello world";
    for(String str : strTest.split(" ")){
        System.out.println(str);
    }
}
结果:
hello
world

python

if __name__ == '__main__':
    str = 'hello world'
    print(str.split(' '))
    
结果:
['hello', 'world']

6.control flow(控制流)

请看如下代码:

java

public static void main(String[] args) {
   int condition = 10;
   
    //if
    if(condition > 10){
        System.out.println(" > 10");
    } else {
        System.out.println(" <= 10");
    }
    
    //while
    while(condition > 1){
        System.out.println(condition);
        condition--;
    }
    
    //switch
    switch (condition){
        case 1:
            System.out.println(" is 1");
            break;
        case 2:
            System.out.println(" is 2");
            break;
    }
    
    //for
    for(int i = 0; i< 10; i++){
        System.out.println(" i ");
    }
}

python

def switch_test(condition):
    if condition > 1:
        print(' >1 ')
    else:
        print(' <=1 ')

if __name__ == '__main__':
   condition = 10

   # if
   if condition > 10:
       print('>10')
   else:
       print('<=10')

   # while
   while condition > 1:
       print(condition)
       condition = condition - 1

   # python中没有switch case语句,可以这样实现
   switch_test(condition)
   
   # for
   for i in range(0,10):
       print(i)

7.类和继承关系

请看如下代码:

java

父类:
public class Animal {

    private String name;

    public Animal(String name) {
        this.name = name;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}

子类:
public class Dog extends Animal{

    public Dog(String name) {
        super(name);
    }

    public void saySomething(){
        System.out.println(this.getName());
    }

}

测试类:
public class Test {
    
    public static void main(String[] args){
        Animal animal = new Dog("我是狗狗");
        ((Dog) animal).saySomething();
    }

}

python

父类:
class Animal():

   def __init__(self,name):
       self.name = name

   def saySomething(self):
       print(self.name)

子类:
class Dog(Animal):
    def saySomething(self):
        print(self.name)

测试类:
if __name__ == '__main__':
   dog = Dog('我是狗狗')
   dog.saySomething()

8.IO流

请看如下代码:

java

import java.io.*;

public class Test {

    public static void main(String[] args) throws IOException {
        File dir = new File(".");
        InputStream bufferedInputStream = Test.class.getResourceAsStream("/java.txt");
        BufferedReader in = new BufferedReader(new InputStreamReader(bufferedInputStream));
        String aLine = null;
        while((aLine = in.readLine()) != null){
            System.out.println(aLine);
        }
        in.close();
    }

}

python

if __name__ == '__main__':
    with open('python.txt', "r") as f:  #设置文件对象,只读模式
        print(f.read())

对比发现,python的代码量少很多,很方便。

9.List

请看如下代码:

java

import java.util.ArrayList;

public class Test {

    public static void main(String[] args)  {
        ArrayList<String> arrayList = new ArrayList<String>();
        //添加元素
        arrayList.add("1");
        System.out.println(arrayList);
    }

}

python

aList = []
#添加元素
aList.append('1')
print(aList)

10.NULL

请看如下代码:

java

Object obj = null;

python

obj = None

11.元组

请看如下代码:

java

java中没有元组

python

tup = ('Google', 'Runoob', 1997, 2000)

12.set集合(无序的不重复元素序列)

请看如下代码:

java:

Set<String> set = new TreeSet<String>();       
set.add("f"); 
System.out.println(set);

python

thisset = set(("1", "2", "3"))
thisset.add("4")
print(thisset)

初出茅庐,承蒙各位大佬关照。欢迎纠正错误,谢谢🙏

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值