Python
一.基础输出/变量/变量类型
from pip._vendor.distlib.compat import raw_input
a=”ghghggjgwz”
b=”10”
print(a)
print(b)
print(int(b))
print(“gjhghjh”)
print((int)(5/2)) #2
print(5/2)
二.基础字符串操作
c=”Null-Byte”
print(c.upper())
print(c.lower())
print(c.split(“-“))
三.基本的用户输入
d=input()#根据用户的输入变换返回值的类型
print(d)
e=raw_input(“Give me your input”)
print(e)
1 | s=input().split() |
四.条件控制语句
if(c.lower()==”null-byte”):
print(“yes”)
elif(c.lower()==”baidu”):
print(“wzj”)
else:
print(“no”)
五.列表型变量 列表方法 列表索引
test=[“ggh”,23,”78”]
print(test[1])
#在列表尾部添加元素
test.append(“7878”)
print(test[3])
#在列表尾部添加列表
num=list((1,2,3,4,5,”34”))
test.extend(num)
print(test)
#在列表指定位置添加元素
test.insert(0,0)
print(test[0])
#删除列表中首次出现的元素
test.remove(2)
六.函数,for,while
for i in range(len(test)):
print(test[i])
七.错误与异常
num=raw_input(“input:”)
try:
if(int(num)+1>int(num)):
print(“Y”)
else:
print(“N”)
except ValueError:
print(“Error”)
八.创建函数
def greeting1():
name=raw_input(“Input your your name: “)
print(“your name is “+name)
greeting1()
def greeting2(name,age):
greeting=”Your name is “+name+” and age “+age
return greeting
print(greeting2(“wzj”,”21”))
九.元组与字典
yuanzu=(“wzj”,”ld”)#不可修改
#字典是一系列成对的关键字与值
yuanzu=(“wzj”,”ld”)
print(yuanzu)
print(yuanzu[1])
print(yuanzu[0])
dictionary={“w”:”我家住在撮镇”,”z”:”温正江”}
print(dictionary)
print(dictionary.keys())
print(dictionary.values())
print(dictionary.items())
十.逻辑运算符和成员运算符
and or not
in和not in
十.列表进阶-切片,列表推导式
test=[“my”,”name”,”is”,”wzj”,”and”,”l”,”like”,”women”]
sentence1=test[0 : -1: 2]
print(sentence1)
相关百度未解决问题:
1.命令行程序运行期间,如果用户想终止程序,一般都会采用Ctrl-C快捷键,这个快捷键会引发python程序抛出KeyboardInterrupt异常。我们可以捕获这个异常,在用户按下Ctrl-C的时候,进行一些清理工作。
从python自带的异常对象来看,与退出程序有关的异常,都继承自BaseException。KeyboardInterrupt异常也在其中。因此,我们要捕获这个异常,就要以如下方式写python代码:
try:
except BaseException as e:
if isinstance(e, KeyboardInterrupt):
这段代码在except中使用isinstance函数来判断具体是哪一个异常发生了,这种写法可以区分具体的异常,进而分别处理。
或者,直接在except语句中对接KeyboardInterrupt异常:
try:
*****
except KeyboardInterrupt as e:
*****
注意,except Exception将无法捕获KeyboardInterrupt异常。
2.python中的isspace函数;