# 0810 Data Types and Variables Benson Chiu @ FDCS 8th x 7th <!-- Put the link to this slide here so people can follow --> slide: https://hackmd.io/@benson-elementary-cpp/ryAWn8q1F --- ## Four Basic Data Types - int : $3, -2, -900$ - float: $4.88 , 5.0, 900.345$ - bool: $1 - True , 0-False$ - str: "abc 123", "I am happy" 'Haha' > Python will recognize the type **automatically** --- ```python= a = 5 print(type(a)) #int a = 5.0 print(type(a)) #float a = '5' print(type(a)) #str ``` --- ### bool ```python= flag = (1 > 2) print(flag) #False flag = (1+1 == 2) print(flag) #True ``` --- ### str - Usually covered by " " or ' ' - If you triple " " " or ' ' ', inside the string may contains ' or " ```python= print("""靜'夜'思 "李白" 床前明月光 疑是地上霜 舉頭望明月 低頭思故鄉 """) ``` ![](https://i.imgur.com/SvNULG9.png) This is the end of the first video --- ### Type Casting - new_type (value) ```python= print(int(5.0)) print(str(5)) print(float(5)) print(bool(5)) print(str(12345)) ``` --- ### Variables naming rules - The first character **should not be numbers** - e.g. 1happy, 2sad, 3angry -> these are wrong ! - You can take a shot at the jupyter notebook --- - The variable should never be identical to **keywords** (a.k.a. reserved words) - e.g. False, None, True, and, def, class .... - Tips: Just put it on the Jupyter! --- - Legal elements - A~Z; a~z; 1~9; _ --- ### POP QUIZ Choose the valid variables: - [ ] 1piece - [ ] happy_benson - [ ] file-name - [ ] imBenson - [ ] fileName --- ### Variables mechanism Every value represents **a box** Every variable name represents **a name tag** ```python= age = 12 print(type(age)) #string age = '兒童' print(type(age)) #str ``` > If you have learned C or C++, you may probably feel strange about it :poop: --- ```python= age = 18 old = 18 ``` > This means two tags are attatched to one single box --- ### Comment ```python= # Comment for single line ''' Comment for multiple lines ''' ``` --- ### Pause and exercise I will show you the answer in the Demo session > Create intergers a and b, then swap them
{"metaMigratedAt":"2023-06-16T06:14:14.470Z","metaMigratedFrom":"YAML","title":"0810 Data Types and Variables","breaks":true,"description":"View the slide with \"Slide Mode\".","slideOptions":"{\"spotlight\":{\"enabled\":true}}","contributors":"[{\"id\":\"29625303-a0ab-4215-b54d-35c95f11006b\",\"add\":4302,\"del\":1996}]"}
    183 views
   Owned this note