今天开始将进行Python基本语法练习,因大部分语法跟很多程序语言相似,故这个部分将主要以笔记方式注记重点,做为未来的备忘录。
补充说明:部分范例程序来自於W3Schools。
if 2>1:
print("2>1")
print("First Part")
else:
print("Else Part")
#This is a comment
"""
Comment 1
Comment 2
Comment 3
"""
x = 4
y = "Test"
print(type(x))
print(type(y))
x = 4
print(type(x))
x = "Test"
print(type(x))
x = str(1)
print(type(x))
x = int(1)
print(type(x))
x = float(1)
print(type(x))
x, y, z = "A", "B", "C"
x = y = z = "A"
testSet = ["A","B","C"]
x, y, z = testSet
Text Type:str
Numeric Types:int, float, complex
x = 1 # 数值
y = "j" # 文字
z = 1j # 复合型态
print(type(x))
print(type(y))
print(type(z))
x = ["A","B","C"] # list
y = ("A","B","C") # tuple
z1 = range(1, 7) # range
z2 = range(7) # range
employee = {"id": 1, "name": "Alice"}
print(employee)
print(employee["name"])
x = {"A","B","C"} # set
x = frozenset({"A","B","C"}) # frozenset
x = True
y = False
使用bool()可以将各种型态的变数转换为bool型态。
判断为False的情境:
需要使用Random模组
import random
print(random.randrange(1,10))
因重点较多故另外整理出来。
x = """Line 1.
Line 2.
Line 3.
"""
y = '''Line 4.
Line 5.
Line 6.
'''
print(x)
print(y)
a = "Test"
print(len(a))
x = "Hello World"
print("World" in x)
x = "Hello World"
print(x[2:5])
print(x[:5])
print(x[2:])
print(x[-5:-2])
x = " Hello World !"
print(x.lower()) # 转换为小写
print(x.upper()) # 转换为大写
print(x.strip()) # 移除头尾的空白
print(x.replace("l", "L")) # 取代
print(x.split(" ")) # 字串分隔
x = "Hello "
y = "World"
z = x + y
print(z)
quantity = 3
itemno = 567
price = 49.95
myorder = "I want {} pieces of item {} for {} dollars."
print(myorder.format(quantity, itemno, price))
可以使用index来指定欲取代的变数。
quantity = 3
itemno = 567
price = 49.95
myorder = "I want to pay {2} dollars for {0} pieces of item {1}."
print(myorder.format(quantity, itemno, price))
Arduino W5100 是一块含有网路及EEPROM功能的扩充版. 笔者在之前的文章中曾提过可以...
为了让元素的边界格线看得更清楚, 这边统一对<body>之外的所有元素加上outline...
我们来看看Executor介面的内容: package java.util.concurrent; ...
『请问, 有流程图可以看吗?』每次我看别人的系统时, 都会想这样问 身为一个需要透过视觉来辅助理解的...
上午: 网站设计与网页工程技术 # 连接资料库 import sqlite3 import nump...