python常用数据类型与输入输出

###数据类型

1.数值类型

1)整形
```
>>> aInt = 13
>>> print(aInt)
13
>>> print(type(aInt))
<type 'int'>

```

2)(长整形)

```

** python2: 有长整形
>>> aLong = 125653274468735986958609585
>>> print(type(aLong))
<type 'long'>
>>> bLong = 1L
>>> print(type(bLong))
<type 'long'>


**python3中: 没有长整形
>>> aInt=1
>>> type(aInt)
<class 'int'>
>>> aLong = 8727398274987398555567985798567777777777985769843
>>> type(aLong)
<class 'int'>
>>> bLong = 1L    
  File "<stdin>", line 1
    bLong = 1L
             ^
SyntaxError: invalid syntax

```
3)浮点型

```
>>> aFloat = 1.2
>>> type(aFloat)
<type 'float'>
>>> aFloat = 12e10
>>> aFloat
120000000000.0
>>> type(aFloat)
<type 'float'>
>>> aFloat=12e-10
>>> aFloat
1.2e-09
>>> type(aFloat)
<type 'float'>

```

4)复数类型

```
>>> aComplex = 2j+3(这里我们用2i+3时发现会报错,因为一般复数类型多用于精密的领域,而j又有一定的含义,所以用j)
>>> type(aComplex)
<type 'complex'>


***查看帮助: 可以使用什么方法, 实现什么功能?
>>> help(aComplex)

>>> dir(aComplex)    (这里help无法获得帮助时,也可以通过dir获得帮助)
['__abs__', '__add__', '__class__', '__coerce__', '__delattr__', '__div__', '__divmod__', '__doc__', '__eq__', '__float__', '__floordiv__', '__format__', '__ge__', '__getattribute__', '__getnewargs__', '__gt__', '__hash__', '__init__', '__int__', '__le__', '__long__', '__lt__', '__mod__', '__mul__', '__ne__', '__neg__', '__new__', '__nonzero__', '__pos__', '__pow__', '__radd__', '__rdiv__', '__rdivmod__', '__reduce__', '__reduce_ex__', '__repr__', '__rfloordiv__', '__rmod__', '__rmul__', '__rpow__', '__rsub__', '__rtruediv__', '__setattr__', '__sizeof__', '__str__', '__sub__', '__subclasshook__', '__truediv__', 'conjugate', 'imag', 'real']


***案例:
>>> aComplex.conjugate()    (求共轭复数)
(3-2j)
>>> aComplex.imag   (求虚数的虚部)
2.0
>>> aComplex.real     (求虚数的实部)
3.0
```

2.字符串数据类型

字符串的定义:
•第一种方式:
str1 = 'our company is westos'
•第二种方式:
str2 = "our company is westos"
•第三种方式:
str3 = """our company is westos"""

转义符号
一个反斜线加一个单一字符可以表示一个特殊字符,通常是不可打印的字符
\n: 代表换行符       \": 代表双引号本身
\t: 代表tab符           \': 代表单引号本身

```
>>> aString = "hello"
>>> type(aString)
<type 'str'>

>>> dir(aString)
['__add__', '__class__', '__contains__', '__delattr__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__getnewargs__', '__getslice__', '__gt__', '__hash__', '__init__', '__le__', '__len__', '__lt__', '__mod__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__rmod__', '__rmul__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '_formatter_field_name_split', '_formatter_parser', 'capitalize', 'center', 'count', 'decode', 'encode', 'endswith', 'expandtabs', 'find', 'format', 'index', 'isalnum', 'isalpha', 'isdigit', 'islower', 'isspace', 'istitle', 'isupper', 'join', 'ljust', 'lower', 'lstrip', 'partition', 'replace', 'rfind', 'rindex', 'rjust', 'rpartition', 'rsplit', 'rstrip', 'split', 'splitlines', 'startswith', 'strip', 'swapcase', 'title', 'translate', 'upper', 'zfill']
>>> help(aString.center)(这里我们不但能看字符串的用法,还可以看里面用法的用法)

>>> aString.center(40)
'                 hello                  '
>>> aString.center(40, '*')
'*****************hello******************'
>>> print("学生管理系统".center(50, '-'))
----------------学生管理系统----------------
>>> print("学生管理系统".center(50, '*'))
****************学生管理系统****************

```

3.布尔数据类型

bool: 只有两个值(True, False)

```
>>> bool(a)
True
>>> bool(0)
False
>>> bool(67)
True
>>> bool(67.768)
True


>>> name = "westos"
>>> bool(name)
True
>>> name = ""
>>> bool(name)
False

```

4.数据类型的转换

- 在python中, 所有的数据类型都可以作为内置函数,用来转换数据类型;
```
>>> str(1)
'1'
>>> int(2e-10)
0
>>> complex(2)
(2+0j)
```


5. 如何删除内存中的变量?

```
>>> aFloat
1.2e-09
>>> del aFloat
>>> aFloat
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'aFloat' is not defined
```

###输入

# 程序: 输入(键盘)------代码(java/python)-------输出(显示屏)

*** python2:
- input:(只接受数值类型)
```
>>> help(input)

>>> input()
1
1
>>> num = input()
1
>>> num
1
>>> num = input("请输入密码:")
请输入密码:1234567
>>> import getpass       (隐藏密码)
>>> num = getpass.getpass("请输入密码:")
请输入密码:
>>> print(num)
12345678


>>> num = input("请输入密码:")
请输入密码:westos123

```
- raw_input(接收字符串类型)

```
>>> name = raw_input("请输入用户名:")
请输入用户名:westos

# 如果接收的值要进行数值比较时, 一定要转化为同种类型比较;
>>> age = raw_input("请输入年龄:")
请输入年龄:19
>>> type(age)
<type 'str'>
>>> age >19
True
>>> int(age) >19
False

```

*** python3
- input: 接收的为字符串数据类型, 没有raw_input
```
>>> num = input()
12
>>> name = input()
westos
>>> type(num)
<class 'str'>
>>> type(name)
<class 'str'>
```

###输出

```
# %s:代表字符串, %d: 整形, %f: 浮点型
>>> print("%s的年龄为%s" %(name, age))
westos的年龄为19


# .2f: 保留小数点后两位
>>> money = 7800.7812345660
>>> print("%s本月的公资为%f" %(name, money))
westos本月的公资为7800.781235
>>> print("%s本月的工资为%.2f" %(name, money))
westos本月的工资为7800.78

#.3d: 整形总占位数, 不够的前面补0
>>> sid = 1
>>> print("%s的学号为130%d" %(name, sid))
westos的学号为1301
>>> print("%s的学号为130%.3d" %(name, sid))
westos的学号为130001
>>> sid = 10
>>> print("%s的学号为130%.3d" %(name, sid))
westos的学号为130010

猜你喜欢

转载自blog.csdn.net/forever_wen/article/details/81503032