Python同Java同Js语言语法区别之三字符串字典列表赋值与逻辑运算

前言

作为一枚测开工程师,需要的技能是全栈的,项目中,经常需要切换语言进行开发,语言用多了,难免会经常用混淆,故趁有空整理下这系列笔记“Python同Java同Js语言语法区别‘,希望对大家有用。

Python

赋值

不管是字典还是列表、字典都可以直接赋值

#!/usr/bin/python
list1=["testxiaosheng"]
str1="testxiaosheng666"
dict1={"testxiaosheng":666}

逻辑运算

在python 中使用的是 and or 进行逻辑判断

if list1 and "testxiaosheng" in list1:
	print("list1有匹配值")

Java

赋值

java中除了字符串可以直接赋值,ArrayList数组(固定数组 可以)与HashMap都不可直接赋值,需要初始化个类型对象

        String s2 = "abandon";
        int[] ns = { 68, 79, 91, 85, 62 }; //固定数组,不能添加
        List<String> l1 =new ArrayList();
        l1.add("testxiaosheng");
        Map<String,String> m1= new HashMap<String,String>();
        m1.put("testxiaosheng","666");

逻辑运算

java中使用 &&(对应python and) 与 || (对应python or) 来进行逻辑运算

        List<String> l1 =new ArrayList();
        l1.add("testxiaosheng");
        if (l1 !=null && l1.contains("testxiaosheng")){
            System.out.println("存在");
        }else {
            System.out.println("不存在");
        }

Js

赋值

js 也只可以直接赋值

var str1 = "testxiaosheng";
let arr=[22,33] ;
var object1 ={"test":666};

逻辑运算

js 同样使用&&(对应python and) 与 || (对应python or) 来进行逻辑运算

// let arr=null;
// let arr 
let arr=[22,33] ;
if (arr!==null && arr!==undefined && arr.indexOf(22)!==-1   ){
// if (arr!==null && arr!==undefined && arr.includes(22)   ){ // 更简洁
   console.log("数组不为空")
}else {
   console.log("数组为空")
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值