跳到主要內容

Python Study (3) - Numeric Type

We will talk about in-depth of python data type. Numeric type is basic types of Python language.

Data Type Classic
We can classify these type to follow as =>

  (1) Integer and floating-point objects : 1234, 1.23, 14e-10
       sign : e or E  is exponent. Python 3.x only has integer (normal and long have
       been merged) but Python 2.x , it still are  two integer types and normal.
  (2) Complex number objects : 3+4j, j
  (3) Decimal Obejct : Decimal(‘2.1456’)
  (4) Fraction : Fraction(-8,5)
  (5) Sets  : set(“apple”)
  (6) Boolean Object :Ture or False, Bool , 1
  (7) Numeric module(build-in libraries) : random, math
  (8) Third-party extension : vector, plotting


Operation function

Operator Example
express operators +, -, *, /, >>, **, &,
>>> 1+1
2
Built-in mathematical functions pow, abs, round ….
module random, math,


Expression operators

Operators Description
or Logical OR => x or y
and Logical AND => x and y
x<y, x<=y, x>y, x>=y  
x==y, x!=y Logical equal , not equal
x | y Bitwise OR
x ^ y Bitwise XOR
x <<y , x >>y Shift x left or right by y bits
+, -, *, /, %  
// floor
~x Bitwise Not
x[i], x[i:j] retrive sub slicing
[] list
{} dictionary
() tuple
x in y, y not in x membership
foor  


Integer and float
In Python 2.0, Integer is represented by “L” , but it is none in python 3.0.

*Python 2.0
>>>3 ** 200
265613988875874769338781322035779626829233452653394495974574961739092490901302182994384699044001L

*Python 3.0
>>>3 ** 200
265613988875874769338781322035779626829233452653394495974574961739092490901302182994384699044001

Complex Numbers
Complex Numbers divide into real number and imaginary,  J or j suffix to the imaginary part.

>>> (1+1j) * 1j
(-1+1j)
 
>>> 1+1j*3
(1+3j)
>>> j * j
-1
          
Fraction Type
   (1) 1/2 is represented to Fraction( 1 , 2 )
   (2) accurate , Ex: 0.1 + 0.1 + 0.1 -0.3 and Fraction(1, 10) + Fraction(1, 10)  + Fraction(1, 10)
                                - Fraction(3, 10)
   
>>> from fractions import Fraction
>>> Fraction(1,10) + Fraction(1,10)
Fraction(1, 5)

>>> Fraction(2,5) - Fraction(1,5)
Fraction(1, 5)

>>> Fraction(2,5) * Fraction(1,5)
Fraction(2, 25)

>>> Fraction("0.25")
Fraction(1, 4)

>>> Fraction("0.3") + Fraction("0.1")
Fraction(2, 5)

>>> 0.1 + 0.1 + 0.1 -0.3  # The result should be 0
5.551115123125783e-17

>>> Fraction(1, 10) + Fraction(1, 10) + Fraction(1, 10) - Fraction(3, 10)
Fraction(0, 1)

>>>from decimal import Decimal
>>> Decimal('0.1') + Decimal('0.1') + Decimal('0.1') - Decimal('0.3')
Decimal('0.0')

>>> 1/3
0.3333333333333333

          
Sets
(1) It is  not unordered and non-duplicated a of collection items.
(2) It is used to  “ {}”  to represent element of the sets 

>>> food = {'apples', 'banana'}
>>> food
set(['banana', 'apples'])

>>> type(food)
<type 'set'>
 

set  operator
x = set("apple") ,  y = set("banana")

Operators Description Example

-

different >>> x – y
set(['p', 'e', 'l'])

| or Union

Union >>> x | y
set(['a', 'p', 'b', 'e', 'l', 'n'])
>>> x.union(y)
set(['a', 'p', 'b', 'e', 'l', 'n'])

&

Intersection

>>> x &  y
set(['a'])

^

Symmetric different(XOR)

>>> x ^ y
set(['b', 'e', 'l', 'n', 'p'])

< , >

superset, subset

>>> x > set(“a”)
True
>>> x > set(“b”)
Flase

in

include >>> “a” in x
True

add(Object)

insert into one to the set

>>> x.add("apple")
>>> x
set(['a', 'p', 'e', 'l', 'apple'])

update(Obejct)

update set  the set

>>> x.update("a","b")
>>> x
set(['a', 'p', 'b', 'e', 'l'])

remove(Objcet)

remove >>> x.remove('a')
>>> x
set(['p', 'e', 'l'])


Note : {} is still a dictionary in all Python.

>>> type({})   # don’t {} and space element
<class 'dict'>

>>> s = set()
>>> type(s)
<class, ‘set’>

Advanced  

>>> { x*4  for x in 'apple'}
set(['aaaa', 'llll', 'pppp', 'eeee'])


Boolean
  
  (1)  Boolean  is the same as 1 , Ex:  True = 1

>>> type(Ture)
<class, ‘bool’
>

留言

這個網誌中的熱門文章

JavaBean 和POJO

前言 今天介紹JavaBean和POJO的不同,這兩個名詞在JAVA文章常常被拿來使用以及討論。在JDK1.1時候釋出才有的一個標準架構,很多時候常常被搞混,所以我們特別開闢一章來加以討論。POJO規範在企業級應用已經廣大的被使用的規範。 解釋 POJO : 全名為Plain-old-Java-object,只需要繼承Object就可以,沒有特定規定,只要建立的類別有setter/getter方法都可以稱為POJO JavaBean: JavaBean通常用來封裝多個物件成為單獨物件使用,規範比較嚴格,規則如下 規則 說明 1 需要實作序列(Serializable/Externalizable) 2 不能有參數的建構子( no-arg constructor) 3 需要有公用setter/getter 4 屬性必須要私人(private) 5 屬於特定POJO規則 比較 所有的JavaBean都為POJO,但是所有的POJO不一定為JavaBean 都可以當作重複元件 都必須序列化 特性都為可用性、易用性和持久化使用 - 應用 由圖我們可以知道POJO在應用程式中,主要用來存取資料庫資料達到持久化的目的,並提供給商業邏輯流程處理使用。這種POJO的架構提供程式人員開發時的可以很有規則將資料封裝並加以使用。 範例1. JavaBean(以員工為實例) JavaBean建立員工物件,可以發現Employee物件建構子沒有任何參數,屬性為私有化並setter/getter的命名方式。 //實作序列化 public class Employee implements java.io.Serializable{ private int id; private String name; //無參數建構子 public Employee(){} //以下實作setter/getter public void setId(int id){this.id=id;} public int getId(){return id;} public void setName(String ...

GSON基礎教學

GSON 前言 JSON是很常見的資料交換格式,在JAVA領域常用處理JSON的函式庫:GSON、FastXML和JSON-B,本章節會以GSON為主,學習目標如下 JSON格式說明 GSON 套件函式 GSON: 物件轉換JSON字串 GSON: JSON字串轉換物件 JSON 格式說明 JSON全名為JavaScript Object Notation,它是一種輕量級的資料交換格式,會大為流行的理由,主要是他比傳統用xml更輕巧且容易處理, JSON表達方式物件會用大括弧{},陣列則是用中括號[]。 用JSON字串來表達Employee的物件內容,由JSON字串可以知道物件name、age、sex和salary屬性。 JSON表示員工資料方式: {“name”:”Jack Bryant”, “age”:18, “sex”:”M”,”salary”:3500.00} JSON陣列表示方式: 跟我們使用JAVA的陣列方式類似,內容值可以是數字’、文字、布林、陣列、物件、null等等。 範例: 字串: [“紅”、”橙”、”黃”、”綠”、”青”、”藍”、”紫”} 布林: [true, true, false, false, true, true] GSON 套件函式 Gson為google所發布的函式庫,主要將物件與json字串之間的轉換時方便使用。當我們將JAVA物件轉換成JSON字串稱為 序列化 ,JSON字串轉換至JAVA物件稱為 反序列化 。 GSON: 物件轉換JSON字串 有了JSON基本概念後,我們進入本章重點,首先我們需要建立員工類別(Employee),定義如下 物件 屬性 員工類別 Employee name 名字 age 年紀 sex 性別 salary 薪水 /** * name:員工類別 */ public class Employee implements Serializable { //constructor public Employee(String name, double salary){ this.name = name; this.sala...

PHP與Python搭配

今天介紹如何利用php網頁呼叫目錄下的python程式工作或是資料交換,本人整理的方法有兩種 使用system()、exec()、shell_exec()呼叫程式 (1) string system ( string return_var ] ) 參考網址 官網解釋system()用來執行外部命令,返回為印出的結果,passthru()跟system()類似但是它不會返回結果。 範例1. 利用system執行ls指定並顯示在網頁上,無法使用變數保留ls的結果 檔案名稱: psystem.php $jsondata= system("ls -al", $result); 結果: (2) exec() : string exec ( string output [, int &$return_var ]] ) 參考網址 範例2. 利用exec執行python程式並可以回傳json格式給前端網頁做處理並顯示。我們ptopy.php就是可以看到callpy()為執行py的函式,它執行完pyEx01.py會將結果給$jsondata變數,做後面json解析。 檔案名稱: ptopy.php function callpy() { $jsondata= exec("/usr/bin/python pyEx01.py"); return $jsondata ; } $jsondata= callpy(); echo $jsondata ; echo " " ; $obj = json_decode($jsondata) ; echo "name:".$obj-> { 'name'} .',' ; echo "id:".$obj-> { 'id'} ; 檔案名稱: pyEx01.py import sys ...