變量的類型
在 python 中定義變量是不需要指定類型(在其他很多高級語言中都需要)
數(shù)據(jù)類型可以分為數(shù)字型、非數(shù)字型
- 數(shù)字型:整型(int)、浮點型(float)、 布爾型(bool): 真 True 、假 False,總之非 0 即真
- 非數(shù)字型:字符串、 列表、 元組、 字典
使用 type 函數(shù)可以查看一個變量的類型 在使用交互式終端時,也可以知道每個變量的準確類型
變量示例一
需求:
定義變量保存小明的個人信息
姓名:圖圖
年齡:18
性別:是女生
身高:1.6米
體重:52斤
不同類型變量之間的計算
數(shù)字變量之間可以直接計算
在 python 中,兩個數(shù)字型變量是可以直接進行算術(shù)運算的
如果變量是 bool 型,在計算時:
True 對應(yīng)的數(shù)字是 1
Fals 對應(yīng)的數(shù)字是 0
字符串變量之間使用 + 拼接字符串
在 python 中,字符串之間可以使用 + 拼接生成新的字符串,字符串變量可以和整數(shù)使用 * 重復(fù)拼接相同的字符串
注意:字符串變量和字符串之間不能進行其他計算
變量示例二
[kiosk@foundation24 Desktop]$ ipython
Python
2.7.5 (
default, Aug
2 2016,
04:
20:
16)
Type "copyright",
"credits" or "license" for more information.IPython
3.2.1
? -> Introduction
and overview
of IPython
's features.
%quickref -> Quick reference.
help -> Python
's own help system.
object? -> Details about
'object',
use 'object??'
for extra details.
In [
1]: name =
'tutu'
In [
2]: name
Out[
2]:
'tutu'
In [
3]:
type(name)
Out[
3]: str
In [
4]: age =
18In [
5]:
type(age)
Out[
5]: int
In [
6]: gender = True
In [
7]:
type(gender)
Out[
7]: bool
In [
8]: height =
1.75In [
9]:
type(height)
Out[
9]: float
In [
10]:
2 **
32
Out[
10]:
4294967296In [
11]:
type(
2 **
32)
Out[
11]: int
In [
12]:
2 **
64
Out[
12]:
18446744073709551616L
In [
13]:
type(
2 **
64)
Out[
13]: long
In [
14]: i =
10In [
15]: f =
10.5In [
16]: b = True
In [
17]: i + f
Out[
17]:
20.5In [
18]: f - b
Out[
18]:
9.5In [
19]: i * f
Out[
19]:
105.0In [
20]: first_name =
'lily'
In [
21]: first_name =
'tutu'
In [
22]: last_name =
'hu'
In [
23]: first_name
Out[
23]:
'tutu'
In [
24]: last_name
Out[
24]:
'hu'
In [
25]: first_name + last_name
Out[
25]:
'tutuhu'
In [
26]: first_name *
10
Out[
26]:
'tutututututututututututututututututututu'
In [
27]: last_name *
5
Out[
27]:
'huhuhuhuhu'
In [
28]: (first_name + last_name) *
5
Out[
28]:
'tutuhututuhututuhututuhututuhu'
In [
29]: first_name =
'tom'
In [
30]: first_name +
10
TypeError Traceback (most recent call last)
<ipython-input-
31-
0198723420d2>
in <module>()
TypeError: cannot concatenate
'str'
and 'int' objects
In [
31]:
Do you really want
to exit ([y]/n)? y
[kiosk@foundation24 Desktop]$
變量的輸入
例如:去銀行取錢,在 ATM 上輸入密碼 在 python 中,如果要獲取用戶在鍵盤上的輸入信息,需要使用
raw_input 函數(shù) 關(guān)于函數(shù): 一個提前準備好的功能(別人或者自己寫的代碼),可以直接使用,而不用關(guān)心細節(jié) raw_input
函數(shù)實現(xiàn)鍵盤輸入 在 python 中可以使用 raw_input 函數(shù)從鍵盤等待用戶的輸入 用戶輸入的任何內(nèi)容 python
都認為是一個字符串
[root@foundation50 kiosk]# ipython
Python
2.7.5 (
default, Oct
11 2015,
17:
47:
16)
Type "copyright",
"credits" or "license" for more information.IPython
3.2.1
? -> Introduction
and overview
of IPython
's features.
%quickref -> Quick reference.
help -> Python
's own help system.
object? -> Details about
'object',
use 'object??'
for extra details.
In [
1]: raw_input()
123
Out[
1]: '
123'
In [
2]: raw_input('請輸入銀行卡密碼:')
請輸入銀行卡密碼:
123
Out[
2]: '
123'
In [
3]: passwd = raw_input('請輸入銀行卡密碼:')
請輸入銀行卡密碼:
123In [
4]: passwd
Out[
4]: '
123'
In [
5]:
type(passwd)
Out[
5]: str
In [
6]: age = raw_input('請輸入您的年齡:')
請輸入您的年齡:
18In [
7]: age
Out[
7]: '
18'
In [
8]:
type(age)
Out[
8]: str
In [
9]: int('
123')
Out[
9]:
123In [
10]:
type(int('
123'))
Out[
10]: int
In [
11]: float('
12.3')
Out[
11]:
12.3In [
12]:
type(float('
12.3'))
Out[
12]: float
In [
13]: age = int(raw_input('請輸入您的年齡:'))
請輸入您的年齡:
18In [
14]: age
Out[
14]:
18In [
15]:
type(age)
Out[
15]: int
In [
16]:
exit
[root@foundation50 kiosk]#
變量示例四:
需求:
收銀員輸入橘子的價格,單位:元/斤(橘子價格不再固定)
收銀員輸入用戶購買橘子的重量,單位:斤
計算并且輸出付款金額版本一
版本二
變量的格式化輸出
在 python 中可以使用 print 函數(shù)將信息輸出到控制臺,如果希望輸出文字信息的同時,一起輸出數(shù)據(jù),就需要使用到格式化操作符
%:被稱為格式化操作符,專門用于處理字符串中的格式
包含%的字符串,被稱為格式化字符串
格式化字符串含義
| %s | 字符串 |
| %d | 符號十進制整數(shù),%06d 表示輸出的整數(shù)顯示位數(shù)字,不足的地方使用0 補全 |
| %f | 浮點數(shù),%.02f 表示小數(shù)點后只顯示兩位 |
| %% | 輸出% |
語法格式:
- print '格式化操作符' % 變量
- print '格式化操作符' % (變量1,變量2...)
變量示例五:
1.定義字符串變量 name,輸出:我的名字叫圖圖,請多多關(guān)照
2.定義整數(shù)變量 student_nu,輸出: 我的學號是 000001
3.定義小數(shù) price,weight,money 輸出:水果的單價是price,購買水果的重量是weight,總價是money
4.定義一個小數(shù) scale,輸出:數(shù)據(jù)比例是 10.00%
總結(jié)
以上是生活随笔為你收集整理的python——变量的类型、不同类型变量的计算、变量的输入以及格式化输出的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。