猴子學Python - Business Computing in Python (week 3)
integer vs floating-point
- type auto- assigned in python
- concept of floating-point numbers
The “binary point” my “float” to make the mapping flexible. - int(), float()
- type is important for the computer to understand the binary value. Same binary can be different meanings in different types.
string
- ASCII code, only one byte(-128 to 127)
- chr() check the ASCII code of the given number.
- len() to check the string’s length.
- (+)
- Other encod ing standards for CH or JP …etc.
casting
- type conversion
- print() uses “,” to seperate elements(tokens). It will auto-add a space in between.
To remove the white space we can just concatnate two strings useing “+”.
assignment
- use “=”
- self assignment: a += 2, a -= 2, a/= 2
- cascade assignment: a=b=c=d=…..=x
conditionals
- if, else, elif
if condition:
statements - “:” and “indentation” are obligatory
- if, else, elif
boolean
- True or False
- often used with comparison operator
formatting
- general guidelines:
- Add proper spaces: wite spaces around operator, white space after each comma, empty lines to separate groups of codes.
- Variable declaration: understandable names.
- Write comments. (Use # and “”” )
- general guidelines:
Exercise:
money transfer confirmation
account1 = int(input("Account 1 balance:"))
account2 = int(input("Account 2 balance:"))
transfer = int(input("Transfer:"))
if transfer <= account1:
account1 -= transfer
account2 += transfer
else:
transfer = account1
account1 -= transfer
account2 += transfer
print(account1, account2)