程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Examples of usage (), [], and {} of several parentheses in Python

編輯:Python

stay Python There are three most common kinds of parentheses in languages , Namely : parentheses ()、 brackets []、 Curly braces {}; Its function is also different , They are used to represent different Python Basic built-in data types . Because the blogger is java Birth , So I will give some examples Java The analogy .python Unlike java You need to force the definition of types when defining , So parentheses are just a few basic data types .

Python Parentheses in ():tuple

representative tuple Tuple data type , Yuanzu is an immutable sequence . It's easy to create , Most of the time it is enclosed in parentheses . This is similar to java In the array , The content is immutable after definition

 

Python Brackets in []:list

representative list List data type , A list is a variable sequence . The creation method is simple and special . This is similar to java Medium list aggregate , After definition, the content is variable , There are append() Method

Python Curly brackets in {}:dict

representative dict Dictionary data type , The dictionary is Python The only built-in mapping type in . There is no special order for the values in the dictionary , But it's all stored in a specific key (key) Next . Keys can be numbers 、 Strings, even tuples . The type is dict

This is similar to java Medium Map aggregate , After definition, the content is variable

a = ("test1","test2","test3")
b = (1,2,3)
c = ["test1","test2","test3"]
c.append("test4")
list("ABC")
list(["ABC"])
dic = {'name':'Alex','sex"':'boy'}
dic['age'] = 10
dic.get('name')

By the way java Several ways to define arrays

// Mode one : Statement 、 Allocate space 、 Assign a value and grasp it a Equate to b
int[] a = {1,2,3,4};
// Statement 、 Allocate space 、 Assign a value and grasp it
int [] b = new int[]{1,2,3,4};
// Mode two : Statement 、 Allocate space
int [] c = new int[4];
// assignment
for (int i=0;i<c.length;i++) c[i] = 10*i;
// Mode three : Statement
int[] d ;
// Allocate space
d = new int[4];
// assignment
for (int i=0;i<d.length;i++) d[i] = 10*i;
// Statement
int[] e ;
// Allocate space 、 assignment
e = new int[]{1,2,3,4};
System.out.println(Arrays.toString(a));
System.out.println(Arrays.toString(b));
System.out.println(Arrays.toString(c));
System.out.println(Arrays.toString(d));
System.out.println(Arrays.toString(e));

List aggregate 、Map aggregate

//list aggregate
List<String> list = new ArrayList<String>();
list.add("a");
//Map aggregate
Map<String,String> map = new HashMap<String,String>();
map.put("key","value");

 


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved