2024年3月8日 星期五

[Python] Remove elements from a list

.
[Python] Remove elements from a list
.

(1) Using the del keyword
(2) Using the list.remove(x) method
(3) Using the list.pop([x]) method

... ... ...
(1) Using the del keyword:
# Sample Code 1
list1 = [0, 1, 2, 3, 4, 5, 6]
del list1[0]
print(list1)
# [1, 2, 3, 4, 5, 6]

del list1[-1]
print(list1)
# [1, 2, 3, 4, 5]

del list1[:2]
print(list1)
# [3, 4, 5]


(2) Using the list.remove(x) method:
# Sample Code 2
list1 = [0, 1, 2, 3, 4, 5, 6]
list1.remove(2)
print(list1)
# [0, 1, 3, 4, 5, 6]


(3) Using the list.pop([x]) method:
# Sample Code 3
list1 = [0, 1, 2, 3, 4, 5, 6]
list1.pop()
print(list1)
# [0, 1, 2, 3, 4, 5]
list_removed = list1.pop(4)
print(list1)
# [0, 1, 2, 3, 5]


// End

[Python] Concatenate Lists / Merge Lists in Python

.
[Python] Concatenate Lists / Merge Lists in Python
.

(1) Concatenating Lists with "+" Operator
(2) Utilizing the Extend() Method
(3) Applying the Itertools.chain() Function
(4) Native Method for List Concatenation (list.append())
(5) List Comprehension to concatenate lists

... ... ...

2024年1月1日 星期一

[Python] Python String format 字串格式化 : (1) 舊式字串格式化 %-formatting (2) 新式字串格式化 str.format() (3) 字串插值 f-string (Formatted String Literal) (4) Template Strings

.


Python 在處理字串時, 由於版本的演變, 有太多種方法, 所以很容易搞混. 筆記整理一下.
.


(1) 舊式字串格式化 %-formatting:

string interpolation

初代 format string, 這是類似 C語言 printf 語法, 使用 % 格式, 
例如: %s (字串), %d (十進位整數), %f (浮點數), 將 tuple 中的一組變量依照指定字串格式輸出.

# Example 1.1:
text = 'World'
print('Hello %s' % text)
# Hello world


# Example 1.2:
name = "John"
age = 23
print('%s is %d years old.' % (name, age))
# John is 23 years old.


# Example 1.3:
print('%x' % 11)  # 轉成十六進位
# b

(2) 新式字串格式化 str.format()

Python 2.6 (發布於 2008 年), 開始有新式字串格式化 str.format(), 透過{} 和 format 來代替 % 運算符號.

# Example 2.1:
text = 'World'
print('Hello {0}'.format(text))
# Hello world
print('Hello {}'.format(text))
# Hello world


# Example 2.2:
name = "John"
age = 23
print('{0} is {1} years old.'.format(name, age))
# John is 23 years old.
print('{} is {} years old.'.format(name, age))
# John is 23 years old.


# Example 2.3:
print('{:x}'.format(11))  # 轉成十六進位
# b


# Example 2.4:
print('{:.2f}'.format(3.1416))  # 保留小數點後兩位
# 3.14


# Example 2.5:
print('{:+.2f}'.format(3.1416))  # 帶符號保留小數點後兩位
# +3.14


細節可以參考 Ref3: Python format 格式化函数


(3) 字串插值 f-string (Formatted String Literal)

Python 3.6 (發布於 2016 年) 新增 f-string, 解決變量不易閱讀以及變量超長的問題.

# Example 3.1:
text = 'World'
print(f'Hello {text}')
# Hello world



# Example 3.2:
name = "John"
age = 23
print(f'{name} is {age} years old.')
# John is 23 years old.


# Example 3.3:
print(f'{11:x}')  # 轉成十六進位
# b


# Example 3.4:
print(f'{3.1416:.2f}')  # 保留小數點後兩位
# 3.14
a = 3.1416
print(f'{a:.2f}')  # 保留小數點後兩位
# 3.14


# Example 3.5:
print(f'{3.1416:+.2f}')  # 帶符號保留小數點後兩位
# +3.14


細節可以參考 Ref4: 制霸 Python f-string 各種格式使用方法


(4) Template Strings:

# Example 4.1:
from string import Template
name = 'Bob'
t = Template('Hey, $name!')
t.substitute(name=name)
# 'Hey, Bob!'


--- --- ---

同場加映 : Python String 字串 join() 語法


# Example 5.1:
chars = ['P', 'y', 't', 'h', 'o', 'n']
text = "".join(chars)
print(text)  # 顯示:Python


# Example 5.2:
chars = ['P', 'y', 't', 'h', 'o', 'n']
text = "##".join(chars)
print(text)  # 顯示:P##y##t##h##o##n


# Example 5.3:
words = ["Python", "is", "awesome"]
text = " ".join(words)
print(text)  # 顯示:Python is awesome



--- --- ---

Ref2: 如何使用 Python 進行字串格式化

Ref3: Python format 格式化函数

Ref4: 制霸 Python f-string 各種格式使用方法

Ref5: Python String Formatting Best Practices

// End.

2023年12月13日 星期三

Angular 超級入門教學 000

...
Angular 超級入門教學 000
...

1. https://angular.io/   or   https://angular.tw/ 

2. Install Node.js JavaScript runtime:
from https://nodejs.org/en
to D:\DevTool\nodejs\

3. Install Visual Studio Code:
from https://code.visualstudio.com/

4. Install the Angular CLI:
from https://angular.io/guide/setup-local
CMD> npm install -g @angular/cli

5. Create a workspace and initial application:
CMD> cd D:\DevTool\Angular
CMD> ng new HelloWorld
CMD> cd D:\DevTool\Angular\HelloWorld
CMD> npm start
> hello-world@0.0.0 start
> ng serve
Local:   http://localhost:4200/

後記1:
CMD> npm start
對應到 package.json




// End

2023年12月12日 星期二

.燕俠語錄 20231212: 吾日三省吾身: 我是誰? 我在哪? 我在這邊做什麼? --- 墨者燕俠(劉建春)

曾子曰:「吾日三省吾身,為人謀而不忠乎?與朋友交而不信乎?傳不習乎?」

2023年12月9日 星期六

IT 數位轉型 (IT Digital Transformation)

.


.
這幾年的 IT 數位轉型 (IT Digital Transformation) 持續發燒與演進.

1. 由傳統的 Physical Machine -> Virtual Server (VM) 發展到熱門的 K8S (Kubernetes) Container. + CI/CD Pipeline.

2. Serverless 雲端服務 (Cloud Service Models) 的演進:
   地端(On-Promise) to Cloud Service.
   IaaS (Infrastrcture as a Service) 基礎設施即服務: e.g. Google GCP, AWS EC2, Azure VM
   PaaS (Platform as a Service) 平台即服務: e.g. Pivotal Cloud Foundry (PCF), Red Hat OpenShift (OKD)
   SaaS (Software as a Service) 軟體即服務: e.g. Salesforce, Netflix, Spotify

3. DevOps and DevSecOps 的興起:
    Under a DevOps model, development and operations teams are no longer “siloed.” Sometimes, these two teams are merged into a single team where the engineers work across the entire application lifecycle, from development and test to deployment to operations, and develop a range of skills not limited to a single function.
    在 DevOps 模型之下,開發與營運團隊不再「孤軍奮戰。」 有時,這兩個團隊會合併成為一個團隊,讓工程師負責整個應用程式生命週期中的工作,包含從開發和測試、部署以及營運,並發展出許多不限於單一功能的技能。

=> 學學 Azure GitOps, Gitlab, Github, Docker, Harbor, Helm, K8S (Kubernetes), CI/CD (Continuous Integration / Continuous Delivery or Continuous Deployment), ymal, AI, ML, AI-Ops, AI-ChatBot ...

4. 網站可靠性工程 SRE (Site Reliability Engineering):
    Site reliability engineering (SRE) is the practice of using software tools to automate IT infrastructure tasks such as system management and application monitoring. Organizations use SRE to ensure their software applications remain reliable amidst frequent updates from development teams. SRE especially improves the reliability of scalable software systems because managing a large system using software is more sustainable than manually managing hundreds of machines. 
    網站可靠性工程(SRE)是使用軟件工具自動化 IT 基礎架構任務(例如系統管理和應用程序監控)的實踐。組織使用 SRE 確保其軟體應用程式在開發團隊的頻繁更新中保持可靠性。SRE 特別提高了可擴展軟件系統的可靠性,因為使用軟件管理大型系統比手動管理數百台機器更具可持續性。
=> + Resilience, + Observability



5. 軟體開發 (Software development) PM, SA, SD, QA -> Coding, Developer, Programming
    每天下班跟假日都要來一點 HackerRank, LeetCode 練練功.
    後端(Back-end), 前端(Front-end), 全端(Full-stack).
    Pythonic!
    JavaScript, TypeScript, MEAN (MongoDB, Express.js, Angular, Node.js), MERN (MongoDB, Express.js, React, Node.js)
    Golang,
    


6. Globalization (English):
    每天下班跟假日都要 聽多一點英文, 多說一點英文一點英文, 一點英文.


// End