就是照着网上的代码敲一敲。
练练手,熟悉一下。
变量类型:
counter = 100 #赋值整型变量
miles = 1000.0 #浮点型
name = "lifei" #字符串
print (counter)
print (miles)
print (name)
print ("3.0这么有意思呢?!~不加括号还不让打印了!!?~")
结果:
SyntaxError: Missing parentheses in call to 'print'. Did you mean print(counter)
?
f:\workSpaceForPython>python python变量类型.py
100
1000.0
lifei
f:\workSpaceForPython>python python变量类型.py
100
1000.0
lifei
3.0这么有意思呢?!~不加括号还不让打印了!!?~
f:\workSpaceForPython>
多个变量赋值
a = b = c = 1 print (a) print (b) print (c) print ("======================") a , b , c = 1, 2, "john" print (a) print (b) print (c)
结果:
f:\workSpaceForPython>python python多个变量赋值.py 1 1 1 ====================== 1 2 john
python数字
""" 标准数据类型 python 有五个标准的数据类型: Numbers (数字) String (字符串儿) List (列表) Tuple (元组) Dictrionary (字典) """ var1 = 1 var2 = 10 print (var1) print (var2) del var1,var2
结果:
f:\workSpaceForPython>python pythonnumber.py
1
10
f:\workSpaceForPython>python pythonnumber.py
File "pythonnumber.py", line 17
del var1[,var2]
^
SyntaxError: invalid syntax
f:\workSpaceForPython>python pythonnumber.py
1
10
想说的是:
在这个页面下http://www.runoob.com/python/python-variable-types.html
给出的代码片段是有问题。删除语句并不会写成嵌套那么复杂。。。
就那个 嵌套的语法,有机会的话,可能要深入研究一下。。。
python 字符串
s = 'abcdef'
s = s[1:5]
print (s)
str = "Hello World!"
print (str)
print (str[0])
print (str[2:6])
print (str[2:])
print (str * 2)
print (str+"TEST")
结果:
f:\workSpaceForPython>python python字符串.py
bcde
Hello World!
H
llo
llo World!
Hello World!Hello World!
Hello World!TEST
python列表
list = ['lifei',786,2.23,'Letben',70.2] tinylist = [123,'john'] print (list) #['lifei',786,2.23,'Letben',70.2] print (list[0]) #lifei print (list[1:3])#[786,2.23] print (list[2:])#[2.23,'Letben',70.2] print (tinylist * 2)#[123,'john',123,'john'] print (list + tinylist)#['lifei',786,2.23,'Letben',70.2,123,'john']
结果:
f:\workSpaceForPython>python python列表.py
['lifei', 786, 2.23, 'Letben', 70.2]
lifei
[786, 2.23]
[2.23, 'Letben', 70.2]
[123, 'john', 123, 'john']
['lifei', 786, 2.23, 'Letben', 70.2, 123, 'john']