Python通过缩进来表示作用范围,可采用Tab或Space来进行缩进,1 Tab = 4 Spaces。
x = 1 if x == 1: print("x = 1")Python是面向对象的编程语言,其中的每个变量都是对象,在使用之前无需对变量的类型进行定义。
1)Numbers
- 在此学习过程中主要提及
a) Integer 整数 定义如下
myint = 7 print(myint)b) Floating point number 浮点数 两种定义方式如下
myfloat = 7.0 print(myfloat) myfloat = float(7)] print(myfloat)2) Strings
- 可用单引号或双引号来对字符串进行定义
str1 = 'Hello' print(str1) str2 = "Hello" print(str2)-其中单引号与双引号的区别在于,当你使用双引号来定义时不用担心字符串中是否包含省略号,而在单引号的情况下可以会终止进程。(尚未发现此情况)
-简单的运算符号在纯数字或纯字符串中得以运行
one = 1 two = 2 three = one + two print(three) hello = "hello" world = "world" helloworld = hello + " " + world print(helloworld)-但在数字和字符串混合的情况下不能运行
# This will not work! one = 1 two = 2 hello = "hello" print(one + two + hello)-可以实现同步打印
a,b = 3,4 print(a,b)-简单定义列表,可使用方法append :list.append(value)在列表后方添加值
//数字列表的情况 mylist = [] mylist.append(1) mylist.append(2) mylist.append(3) print(mylist[0]) # prints 1 print(mylist[1]) # prints 2 print(mylist[2]) # prints 3 # prints out 1,2,3 for x in mylist: print(x) //字符串列表的情况 strings = [] strings.append("hello") strings.append("world") replacement = "here" strings[1] = replacement print(strings)
