python数据类型
整型数据:
- int (包含boolean)
- float
- complex
首先学numerics and String
int: unlimited length
float: 实现用double in C,可查看sys.float_info;
complex:real(实部) and imaginary(虚部) 用 z.real and z.imag来取两部分
import sys
a = 3
b = 4
c = 5.66
d = 8.0
e = complex(c,d)
f = complex(float(a),float(b))
print("a is type",type(a))
print("d is type",type(d))
print("e is type",type(e))
print(a + b)
print(d / c)
print(b / a)
print(b // a)
print(e)
print(e + f)
print(sys.float_info)
输出:
a is type <class 'int'>
d is type <class 'float'>
e is type <class 'complex'>
7
1.4134275618374559
1.3333333333333333
1
(5.66+8j)
(8.66+12j)
sys.float_info(max=1.7976931348623157e+308, max_exp=1024, max_10_exp=308, min=2.2250738585072014e-308, min_exp=-1021, min_10_exp=-307, dig=15, mant_dig=53, epsilon=2.220446049250313e-16, radix=2, rounds=1)
以上是整型数据类型:
String类型和变量:
-既可以用单引号
-也可以用双引号
字面常量和变量
变量的命名规则:
第一个字符必须是字母或者下划线开头
#This is the demo
#这是注释
print("Hello world")
print('Hello World')
print('''hello
apple
xiaomi
huawei
''')
age = 3 #This is 变量
name = "zhangsan"
print("{0} was {1} years old".format(name,age))
print(name + " was " + str(age) + " years old")
#
#这是简单的多行注释
#
#缩进
#必须顶格写
输出:
Hello world
Hello World
hello
apple
xiaomi
huawei
zhangsan was 3 years old
zhangsan was 3 years old
对列表的处理:
#List列表
print("你好")
#换行符
print("what is your name?\nTom")
#创建一个list
number_list = [1,5,3,4,7]
print("number_list:"+str(number_list))
#创建一个String类型的list
string_list = ["abc","bbc","python"]
mixed_list = ["python","java",3,12]
print("string_list"+str(string_list))
print("mixed_list:"+str(mixed_list))
#访问list中的元素
#list中 用index(0123)来访问元素(element)
number_list2 = [1,7,3,5,9]
second_num = number_list2[1]
third_num = number_list2[2]
fourth_num = number_list2[3]
print("second_num: {0} third_num: {1} fourth_num: {2}".format(second_num,third_num,fourth_num))
#更新列表元素:通过index来改变元素element
number_list[1] = 30
print("number_list 更新之后:" + str())
print("----------")
print("number_list:"+str(number_list2))
#删除元素
del number_list2[1]
print("number_list:"+str(number_list2))
print("----------")
number_list3 = [1,5,3,4,7,4,8]
输出:
你好
what is your name?
Tom
number_list:[1, 5, 3, 4, 7]
string_list['abc', 'bbc', 'python']
mixed_list:['python', 'java', 3, 12]
second_num: 7 third_num: 3 fourth_num: 5
number_list 更新之后:
----------
number_list:[1, 7, 3, 5, 9]
number_list:[1, 3, 5, 9]
----------
一些简单的脚本:
#Python脚本语言
print(len([1,2,3]))#长度
print([1,2,3]+[4,5,6])#合流
print(['HELLO'] * 4)#输出多个
print(3 in [1,2,3])#是否在里面
#列表的获取
number_list3 = [1,5,3,4,7,4,8]
print(number_list3[1])
print(number_list3[-2])
print(number_list3[1:])
输出:
3
[1, 2, 3, 4, 5, 6]
['HELLO', 'HELLO', 'HELLO', 'HELLO']
True
5
4
[5, 3, 4, 7, 4, 8]