Link: Python
Ch.1 Python basics
Variable names
- no hyphens (e.g. new-balance)
- cannot begin with number
Concatenate
- Use + operator can only add numbers or two strins
- Use
str(), int(), float()
to change data type
Ch.2 Flow control
Flow control statement
if, else and elif
name = 'Carol'
age = 3000
if name == 'Alice':
print('Hi, Alice.')
elif age < 12:
print('You are not Alice, kiddo.')
else:
print('You are neither Alice nor a little kid.')
while, break and continue
break
: end the loop earlycontinue
: return to the start of the loop
while True:
print('Who are you?')
name = input()
➊ if name != 'Joe':
➋ continue
print('Hello, Joe. What is the password? (It is a fish.)')
➌ password = input()
if password == 'swordfish':
➍ break
➎ print('Access granted.')
Truthy and falsey values
When used in conditions, 0, 0.0 and ” (empty string) are considered False
.
for and range()
break and continue
can be used infor
loops# Add 0 to 100 ➊ total = 0 ➋ for num in range(101): ➌ total = total + num ➍ print(total)
Note that it’s range(101)
because range(start, end)
does not include the end. Why? Because it’s actually mean “start and stop”. Think it as if it’s a stop sign!
sys.exit()
function
sys.exit()
: stop the program