Python入門(二)數據處理:數值和字符串

瞭解python文件結構,
保存成one.py
另外pyc, pyo文件
pyc編譯執行的文件
pyo編譯優化後的文件

掌握python變量和常量
推薦第一門語言爲python
變量:c語言是強類型

變量的命名
有字母、數字
數字不能開頭
first= 100
print(first)
id(first)

變量的類型取決於數據
name = 'milo'
print(name)
id(name)

寫個四則運算器

from __future__ import division
import sys
running = True
while running:
try:
t = int(raw_input())
p = int(raw_input())
except EOFError:
break

print('operator + result\n', t+p)
print('operator - result\n', t-p)
print('operator * result\n', t*p)
print('operator / result\n', t/p)

[code="java"]
[/code]
算數運算符(加減乘除)
5/2 = 2
5.0/2 = 2.5
5//2 = 2 整除

賦值運算符
a = 3
b = 5
c = a+b

關係運算符(結果爲布爾值)
x = 5
x >0 and x<10

NameError: name 'true' is not define
變量沒有定義

數據類型
數字、字符串、列表、元組和字典、布爾
a = 100
b = 'hello'
c = '100' 字符串
id(a)
id(c)
a == c result: False

查看數據類型
type(a)
<type 'int'>

q = 10000000000000
type(q)
<type 'long'> 長整形

l = 10L (長整形)
type(l)
<type 'long'>

f = 1.1
type(f)
<type 'float'>

say = "let's go"
print(say)
雙引號和單引號,沒有太大區別
使用轉義"\"來處理單雙引號問題
"\n" 換行
"\t" 縮進

docstring使用
s = """ tom:"let's go"
>>> s = """ tom:"let's go"
... jerry:"ok"
... """
>>> print s

序列
>>>s2 = 'abcdefg'
>>>print s2[0]
'a'
>>>s2[2]
'c'
>>>s2[-1] 倒數第一個
'g'

字符串小技巧
>>> x = input()
10
>>> y = input()
20
>>> z = x+y
>>> z
30

練習題
對輸入的2個數字加減乘除運算

>>> x = input()
10
>>> y = input()
20
>>> z = x+y
>>> z
30
>>> z = x -y
>>> z
-10
>>> z = x *y
>>> z
200
>>> z = x / y
>>> z
0
發佈了29 篇原創文章 · 獲贊 0 · 訪問量 2萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章