jas0nhuang

猴子學Python - Business Computing in Python (week 3)

  1. 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.
  2. 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.
  3. 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 “+”.
  4. assignment

    • use “=”
    • self assignment: a += 2, a -= 2, a/= 2
    • cascade assignment: a=b=c=d=…..=x
  5. conditionals

    • if, else, elif
      if condition:
      statements
    • “:” and “indentation” are obligatory
  6. boolean

    • True or False
    • often used with comparison operator
  7. 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 “”” )

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)