Lecture 2: Python Binary and Character Encoding & Identifier Reserved Words & Variables & Data Types and Conversion & Comments


1. Binary and character encoding

#Unicode几乎包含了全世界的字符
print(chr(0b100111001011000)) 
print(ord('乘')) 

Insert picture description here

2. Identifiers and reserved words

import keyword #不能用以下名字命名(保留字)
print(keyword.kwlist)

#标识符:
# 规则:字母、数字、下划线
# 不能以数字开头、不能是保留字、不严格区分大小写

3. The definition and use of variables


#变量是内存中一个带标签的盒子
name = 'XCsss98'
print(name)
print('标识 :',id(name))
print('类型 :',type(name))
print('值  :',name

Insert picture description here

4. Commonly used data types

4.1 Integer type (int)

Binary: 0b
Octal: 0o
Hexadecimal: 0x

print('十进制',110)
print('二进制',0b10110)
print('八进制',0o172)
print('十六进制',0x1EBD)

Insert picture description here

4.2 Floating point numbers are similar (float)

#float 计算不太精确,用decimal解决

from decimal import Decimal

a = 1.11
b = 2.22
print(a+b)

4.3 Boolean type (bool)

f1 = True
f2 = False
print(f1+1) #2
print(f2+1) #1

4.4 String type (str)

str = '哈哈哈哈哈'
str = "哈哈哈哈哈"
str = """好好
    好好
    """

5. Type conversion

5.1 str() function and int() function


name = 'XCsss98'
age = 22
print('我是'+name+',今年'+str(age)+'岁')

a = 10
b = 1.2
c = False
print(str(c),type(str(c))) #其他同理
print(int(c),type(int(c)))#非数字串不能转化

Insert picture description here

5.2 float() function

Same as above

6. Notes

#Single-line comment
``'Multi-line
comment'''

Guess you like

Origin blog.csdn.net/buxiangquaa/article/details/113871250
Recommended