Python数据分析实战2.1-基本指令、基本数据类型【python】

1.最简单,最常用:print 用于输出字符串

关于单引号(’),和双引号("")在使用中的不同,请查看文章:https://blog.csdn.net/woainishifu/article/details/76105667

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

2 通配符的使用:%s,%d

%s代表字符串,%d代表数字

**代码块**
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 基本数据结构

Python的序列中的每个元素都有自己的编号。Python中有6种内建的序列,其中列表和元组是最常见的类型。其他包括字符串、Unicode字符串、buffer对象和xrange对象。下面重点介绍下列表元组字符串

  1. 列表:列表是可变的,这是他区分于元组和字符串的重要方面,一句话概括,列表可以修改,而元组和字符串不能。
**代码块**
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]

常用操作list的方法:

追加元素:list.append(var)

  • 插入元素:list.insert(index,var)

  • 返回最后一个元素并删除:list.pop(var)

  • 删除第一次出现的该元素:list.remove(var)

  • 元素在列表中出现的个数:list.count(var)

  • 合并list到L上:list.extend(list)

  • 正序排序:list.sort()

  • 倒序排序: list.reverse()

  • 返回var在列表中出现的次数:list.count(var)

    2.元组:元组与列表一样,也是一种序列,唯一不同的是元组不能被修改(字符串其实也有这种特点)。

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

3.字符串

#字符串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.字典

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

5.数据类型转换

#用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'>
发布了36 篇原创文章 · 获赞 17 · 访问量 6274

猜你喜欢

转载自blog.csdn.net/qq_39248307/article/details/105309053
今日推荐