1、输入
city=raw_input("write down the name of city:") #字符串输入使用raw_input
num = input("输入:") #数字输入使用input
2、输出
firstName = 'HUI'
secondName = 'CAO'
name = secondName + firstName
print(name)
3、乘法,可用于字符串
words = 'words'*3
print(words)
控制台输出:
wordswordswords
4、Python注释方法
#注释
'''
注释
'''
5、字符串的截取
name='My Name is Mike'
print(name[0])
print(name[-3]) #从右往左数,e为-1,k为-2,i为-3
print(name[11:14]) #不包括编号14
print(name[11:15]) #不包括编号15
print(name[5:]) #输出从编号5开始往右的字符,包括编号5
print(name[:5]) #输出编号为5左侧的字符
控制台输出:
M
i
Mik
Mike
me is Mike
My Na
6、不等号
<> 和!=都是不等号
7、创建列表,以及列表的append方法
#coding:utf-8
album = []
album_list = ['Black Star','David Bowie',25,True]
album_list.append('caohui')
print(album_list)
print(album_list[0],album_list[-1]) #打印第一个和最后一个元素
8、成员运算符 in 和 not in、身份运算符 is 和 not is
name = 'xiaoming'
album_list = ['Black Star','David Bowie',25,True]
print('David Bowie' in album_list)
print(name is 'xiaoming')
print('xiaoming' is name)
控制台输出:
True
True
True
9、条件语句
age=24
if age<=18:print('未成年人')
elif age>18 and age<=30:print('青年人')
elif age>30 and age<=60:print('中年人')
else:print('老年人')
控制台输出:
青年人
10、循环语句
for循环
for every_letter in 'Hello':
print (every_letter)
控制台输出:
H
e
l
l
o
for num in range(1,11): #range(1,6)不包括11
print(num)
控制台输出
1
2
3
4
5
while循环
#coding:utf-8
print('测试1:')
num = 0
while num<5:
print('repeat this line!')
num = num +1
print('测试2')
num = 0
while True:
print('repaet this line!')
num = num +1
if(num>5):
break
控制台输出:
测试1:
repeat this line!
repeat this line!
repeat this line!
repeat this line!
repeat this line!
测试2
repaet this line!
repaet this line!
repaet this line!
repaet this line!
repaet this line!
repaet this line!
注意:
1、Python是区分大小写的
2、在文件开头加一行 #coding:utf-8 则支持写入中文,包括中文注释也需要
3、Python的单引号和双引号没有实质区别