Python data analysis combat 2.1-basic instructions, basic data types [python]

1. The simplest and most commonly used: print is used to output strings

For the difference between single quotes (') and double quotes (""), please check the article: https://blog.csdn.net/woainishifu/article/details/76105667

**代码块**
print("Hello,word!")
print("Hello","word!")               # 如果想输出多个字符串,中间使用逗号(,)隔开
**输出结果**
Hello,word!
Hello word!                              #同时发现,输出结果中,每个字符串用一个空格隔开

2 Use of wildcards:% s,% d

% s stands for string,% d stands for number

**代码块**
print"My name is %s" %"jack"print("I am %d years old" %18)      #不加双引号
print("My name is %s,I am %d years old" %("jack",18))      
**输出结果**
My name is jack
I am 18 years old
My name is jack,I am 18 years old

3 Getting help

**代码块**
input numpy as np
help (np.mean)
**运行结果**
Help on function mean in module numpy:
mean(a, axis=None, dtype=None, out=None, keepdims=<no value>)    #a代表必传值因为没有默认值,后面加等会可以不用传值
    Compute the arithmetic mean along the specified axis.
    Returns the average of the array elements.  The average is taken over
    the flattened array by default, otherwise over the specified axis.
    `float64` intermediate and return values are used for integer inputs.

4 Basic data structure

Each element in Python's sequence has its own number. There are 6 built-in sequences in Python, of which lists and tuples are the most common types. Others include strings, Unicode strings, buffer objects, and xrange objects. The following focuses on the following lists , tuples, and strings .

  1. List: The list is variable, which is an important aspect that distinguishes it from tuples and strings. In one sentence, the list can be modified, but the tuples and strings cannot.
**代码块**
sample_list=["a",1,("b","c")]    #变量sample_list有三个元素,分别是字符串、数字、两个字符串组成的第三个元素
print sample_list
**运行结果**
["a",1,("b","c")]


**代码块**
sample_list.append(0) # 尾部插入某个值
print sample_list
**运行结果**
["a",1,("b","c")0]


**代码块**
del sample_list(1) #删除某个值,注意从0开始数
print sample_list
**运行结果**
['a', ('a', 'b'), 0]


**代码块**
sample_list[0,0]=["sample value"]   #头部插入某个值
print sample_list
**运行结果**
["sample value",'a'1, ('a', 'b'), 0]

len(sample_list) #计算长度


**代码块**
sample_list2=sample_list  #使指针指向相同,对L操作即对L2操作。函数参数就是这样传递的
sample_list3=sample_list[:] #复制
sample_list[1]=test  #将数组的第二个元素进行替换
print sample_list2,sample_list3
**运行结果**
['sample value', 'test', ('a', 'b'), 0] ['sample value', 'a', ('a', 'b'), 0]

Common methods of operating list:

Append elements: list.append (var)

  • Insert element: list.insert (index, var)

  • Return to the last element and delete: list.pop (var)

  • Remove the element that appears for the first time: list.remove (var)

  • The number of elements in the list: list.count (var)

  • Merge list into L: list.extend (list)

  • Positive order sorting: list.sort ()

  • Sort in reverse order: list.reverse ()

  • Return the number of times var appears in the list: list.count (var)

    2. Tuples: Tuples are a sequence like lists. The only difference is that tuples cannot be modified (strings also have this feature).

#元组tuple,内容不可更改
**代码块**
x=(1,2,3)
print(x,type(x))
**运行结果**
{'a': 100, 'b': 'hello'} <class 'dict'>

3. String

#字符串string,用引号包含,单引号和双引号都可以,三引号代表多行输入
**代码块**
x1="hello word"
x2='hello word'
x3="""
a
b
c
"""
print(x1)
print(x2)
print(x3)
print(type(x1))
**输出结果**
hello word
hello word

a
b
c

<class 'str'>

4. Dictionary

#Dict字典,用{}标识,由索引key和它对应的值value组成,无序对象
**代码块**
x = {'a':100,'b':"hello"}
print(x,type(x))
**输出结果**
{'a': 100, 'b': 'hello'} <class 'dict'>

5. Data type conversion

#用int float str 进行数据类型转换,dict tuple list后面讲
**代码块**
var1 = 10
print(var1,type(var1))

var2 = float(var1)
print(var2,type(var2))

var3 = str(var1)
print(var3,type(var3))
**输出结果**
10 <class 'int'>
10.0 <class 'float'>
10 <class 'str'>
Published 36 original articles · praised 17 · visits 6274

Guess you like

Origin blog.csdn.net/qq_39248307/article/details/105309053