一、print
print ("test") #直接打印内容
name = "zhangsan"
age = 28
'''
此为注释,可单引号,也可双引号,
'''
print (name) #打印变量内容
print (type(name)) #打印变量的类型
print (type(age))
print (("hello ")+(name)+(" age is ")+str(age)) #打印文字+变量内容,age是数值,所以在转换为字符串
print ("name is %s,age is %d" %(name,age))
结果:
test
zhangsan
<class 'str'>
<class 'int'>
hello zhangsan age is 28
name is zhangsan,age is 28
[Finished in 0.1s]
二、input
>>> input
<built-in function input>
>>> fdfdf
Traceback (most recent call last):
File "<pyshell#3>", line 1, in <module>
fdfdf
NameError: name 'fdfdf' is not defined
>>> input
<built-in function input>
>>> "tttt"
'tttt'
实际应用很少,如果输入字符,要加引号,否则会报错。并且只能在IDE里使用,不能使用sublime工具
错误:NameError: name 'raw_input' is not defined
原因出在raw_input ,python3.0版本后用input替换了raw_input
三、if
a = 2
b = 3
if a>b:
print ("the max number is" +a)
else:
print ("max is ",+a)
结果:
max is 2
[Finished in 0.4s]
str1='hello'
str2='hello zhangsan'
if str1 in str2:
print ("str1 in str2")
else:
print("str1 is not in str2")
结果:
str1 in str2
[Finished in 0.9s]
tt = "true"
if tt:
print ("is true")
else:
print ("false")
结果:is true
四、for
'''
循环1到9,第三个参数是步长
'''
for i in range(1,10):
print (i)
print ()
for i in range(1,13,3):
print (i)
print ()
fruits = ['banana','apple','mango']
for fruit in fruits:
print (fruit)
结果:
t
e
s
t
a
a
b
b
1
2
3
4
5
6
7
8
9
1
4
7
10
banana
apple
mango
五、数组
>>> lists = ['0','1','2','A','3']
>>> lists
['0', '1', '2', 'A', '3']
>>> lists[2]
'2'
在后面追加数据
>>> lists.append("dd")
>>> lists
['0', '1', '2', 'A', '3', 'dd']
六、字典
要在IDE下使用
>>> dic={"key":"value","name":"zhangsan","age":"12"}
>>> dic.keys()
dict_keys(['key', 'name', 'age'])
>>> dic.values()
dict_values(['value', 'zhangsan', '12'])
七、模组
#coding=utf-8
#import com.demo.test
#def
from selenium import webdriver
from time import sleep,ctime
print ("start")
print (ctime())
sleep(2)
print (ctime())
结果:
start
Fri Jan 25 22:51:55 2019
Fri Jan 25 22:51:57 2019
[Finished in 2.4s]