通过这个帖子来记录我学习及理解Python的过程
1 介绍
1.1 输出 hello world
print("Hello, world! My name is yourName")
从这里就开始了我的第一行Python代码
1.2 在代码中写注释
# This is the comment for the comments.py file
print("Hello!") # this comment is for the second line 这一行是注释
print("# this is not a comment") #但是"#"写到输出里就会输出出来而不是注释了
# add new comment here
2 变量
2.1 变量的定义
a = b = 2
# This is called a "chained assignment". It assigns the value 2 to variables "a" and "b".
#同时定义两个变量
print("a = " + str(a))
# We'll explain the expression str(a) later in the course. For now it is used to convert the variable "a" to a string.
print("b = " + str(b))
greetings = "greetings"
print("greetings = " + str(greetings))
greetings = "WTF"
print("greetings = " + str(greetings))
2.2 未定义变量
variable = 1
print(sstd)
#NameError: name 'sstd' is not defined
#变量未定义
2.3 变量类型
number = 9
print(type(number)) # print type of variable "number"
float_number = 9.0
print(type(float_number))
#Out Put
#<class 'int'>
#<class 'float'>
2.4 类型转换
number = 9
print(type(number)) # print type of variable "number"
float_number = 9.0
print(float_number)
print(int(float_number))
#将float型变量转换为int型
2.5 运算符
number = 9.0 # float number
result = number /2
#除法
remainder = number%2
#求余数
print("result = " + str(result))
print("remainder = " + str(remainder))
#Out Put
#result = 4.5
#remainder = 1.0
2.6 自运算
number = 9.0
print("number = " + str(number))
number -= 2
print("number = " + str(number))
number += 5
print("number = " + str(number))
#Out Put
#number = 9.0
#number = 7.0
#number = 12.0
2.7 布尔型操作
two = 2
three = 3
is_equal = two == three
print(is_equal)
#Out Put
#False
2.8 比较运算符
one = 1
two = 2
three = 3
print(one < two < three) # This chained comparison means that the (one < two) and (two < three) comparisons are performed at the same time.
is_greater = three > two
print(is_greater)
#True one<two & two<three 同时成立
#True thre>two 成立
证明上述结论的另一个例子
one = 1
two = 3
three = 2
print(one < two < three) # This chained comparison means that the (one < two) and (two < three) comparisons are performed at the same time.
is_greater = three > two
print(is_greater)
#Out Put
#False
#False
下一章从string的日常使用开始