P2 Start
P2 Start
Literals
Identifier
Keywords, Reserved Words
statements
廖文宏 Python Programming 1
PE1 Module 2
Data Types, Variables, Basic Input-Output Operations,
Basic Operators
How to write and run the first Python program;
Python literals;
add-on modules
user defined
print("\n")
Text/String: str 字串
Boolean: bool
Mapping: dict
the ones with a fractional part (e.g. 5.0, 1.6) have type float.
In addition to int and float, Python supports other types of
numbers, such as Decimal and Fraction.
Python also has built-in support for complex numbers, and
uses the j or J suffix to indicate the imaginary part (e.g.
3+5j).
texts"""
anything you put inside the quotes will be taken literally,
not as code, but as data.
Python strings cannot be changed — they are immutable
There is no separate character type
a character is simply a string of size one
廖文宏 Python Programming 15
2.2.1.10 Boolean
These two Boolean values have strict denotations in Python:
True (not 0)
False (0)
To take these symbols as they are,
including case-sensitivity.
0, 0.0, empty string、empty list, empty tuple、empty
container, None 都視為 False
e.g. 3 ** 2
e.g. (a * h ) / 2
a, h, 2 為 operand
* / 為 operator
fractional result)
+ 加
% remainder
** exponent
- 減
* 乘
/ 兩數相除取商
// 整數除法
% 兩數相除取餘數
** 次方
廖文宏 Python Programming 18
2.3.1.7 Operator Precedence
operator precedence
https://docs.python.org/3/reference/expressions.html
e.g. 8 + 12 / 4 =?
operator associativity/binding
left-to-right
right-to-left
e.g. 2 ** 2 ** 3 = ?
hello = "welcome"
An assignment statement to a variable modified the contents
of the memory location associated to the variable
dynamic data type
binding at runtime
a = "Hello World"
str(number)
operator
e.g. 'un' + 'ium' gives 'unium'
ensure that both its arguments are strings.
replication operator *
Strings can be replicated(repeated) with the * operator