1,Python中的输入与输出
****输入****
python2中:raw_input()接受字符串的数据
name = raw_input("input name:")
print name
input只能接受数值类型的数据
info = input("input:")
print info
python3中:无raw_input,只有input,用input接受的数据为字符串类型
****输出****
python2: print "要打印的字符串"
python3: print("要打印的字符串")
%s字符串 %数值 %o八进制数 %x十六进制数 %f浮点数 %%显示百分数
2,Python中的数据类型
python中支持的数值类型: int,long,float, complex
a = 13
print(type(a))#查看数据类型
num1=28
num1=1L #转换数值类型为长整数型
print type(num1)
注: 数值类型可互相转换;
python中没有1L;
科学技术法(e)的数属于浮点数;
复杂数中:
num1 = 2+3j
print type(num1)
print num1.real #打印实部
print num1.imag #打印虚部
print num1.conjugate() #打印共轭复数
3,分支语句
if 条件表达式:
满足表达式执行的内容
elif 条件表达式
满足条件的内容
else:
不满足表达式执行的内容
a=4
b=3
if a>b:
print(a)
else:
print(b)
print(a if a>b else b) #三目运算
4,循环语句
****while条件表达式****
count = 0
while count < 3:
print("hello world")
count += 1
****while死循环****
# while True:
# print('hello')
# while 0<1:
# print("hello")
# while 1:
# print("hello")
****for循环****
for i in range(5):
print(i)
else:
print("结束执行....")
# range(end): 0~end-1
# range(start, end): start~end-1# range(start, end, step): start~end-1, 步长为step
# range(1,10,2) # 1, 1+2, 1+2+2
****跳出循环****
# break(结束此循环)
# continue(跳出本次循环并继续)
注:#break 只能结束一个循环,嵌套时需要多次跳出
#getpass模块可以使输入不显示
5,变量名的命名规则
# 变量名可以由字母,数字或者下划线;
# 变量名只能以字母或者下划线组成;
# 变量名不能是python的关键字: if, else,while, for, break,continue,pass
6,字符串
****字符串常用转义符号****
# \n:换行# \t: 一个tab键
# \': '
# \": "
print('"hello"')
print("he's")
print("\"hello w\'s python\"")
****字符串的特性****
# 索引: 0,1,2,3,4, 索引值是从0开始的;
s='hello'
print(s[0])
print(s[4])
print(s[-1]) # 拿出字符串的最后一个子符;
# 切片
# 切片时规则为s[start:end:step],从start开始,到end-1结束, 步长为step;
print(s[0:4:2])
print(s[:]) # 显示所有子符
print(s[:3]) # 显示前3个子符
print(s[::-1]) # 对于字符串倒序输出;
print(s[1:]) # 除了第一个子符之外, 其他全部显示;
# 重复
print(s*10)
# 连接
print("hello "+"world")
# 成员操作符 s = "hello", in, not in
print('he' in s)
print('a' in s)
print('he' not in s)
****字符串的开头和结尾****
s = "hello.python"
print(s.endswith('thon'))#打印s是否以thon结尾
print(s.startswith('hel')#打印s是否以hel开头
****字符串的类型判断****
#"str".isdigit (是否全为数字)
#"str".isupper (是否全为大写字母)
#"str".isalpha (是否全为字母)
#"str".islower (是否全为小写字母)
#"str".islanum (是否全为数字和字母)
#"str".istitle (是否首字母为大写,其余全为小写)
****字符串的搜索与替换****
s = "hello world hello"
print(s.find("hello"))
print(s.find("world"))# find找到子串,并返回最小的索引值;
print(s.rfind("hello"))# find找到子串,并返回最大的索引值;
print(s.replace("hello", "westos"))# 替换字符串中所有的“hello”为"westos"
****删除不必要的空格****
str1="hello xiao peng you"
print(str1.strip())#删除两边多余的空格
print(str1.lstrip())#删除左边多与的空格
print(str1.rstrip())#删除右边多余