python基础知识---简单语法

1.if语句

>> a = 10
>> b = 20
>> if(a>b):
print("a max!")
else:
print("b max!")

b max!

>> student = "zhangxiaoyu"
>> if (student == "zhanxgiaoyu"):
print("YES")
else:
print("NO")

NO

>> if (student != "zhanxgiaoyu"):
print("YES")
else:
print("NO")

YES
【in 和 not in 表示包含的关系】

>> hi = "hello python"
>> if ("h" in hi):
print("yes")
else:
print("no")

yes

>> if (" " in hi):
print("yes")
else:
print("no")

yes

>> if ("i" in hi):
print("yes")
else:
print("no")

no

【if语句可以进行布尔类型判断】

>> a = True
>> if a:
print(" a is True")
else:
print("a is no True")

a is True

2.for语句

>> names = ["xiaoyu","xiaowen","huahua","qiongge"]
>> for name in names:
print(name)

xiaoyu
xiaowen
huahua
qiongge

>> for name in names:
print(name)
if(name == "huahua"):
print("yes")
break

xiaoyu
xiaowen
huahua
yes

>> for name in names:
print(name)
if(name == "huahua"):
print("yes")
continue

xiaoyu
xiaowen
huahua
yes
qiongge

range()函数:默认从零开始循环,也可以设置起始位置和步长

>> for i in range(10):
print(i)

01
2
3
4
5
6
7
8
9

>> for i in range(1,20,5):
print(i)

1
6
11
16
3.数组与字典
(1)数组

>> lists = [1,2,3,4,5,6,'a']
>> lists[0]
1
>> lists[6]
'a'
>> lists[6] = 'n'
>> lists[6]
'n'
>> lists.append("v")
>> lists
[1, 2, 3, 4, 5, 6, 'n', 'v']
(2)字典
>> ret = {"usenames":"xiaoyu","password":"12345"}
>> ret.keys()
dict_keys(['password', 'usenames'])
>> ret.values()
dict_values(['12345', 'xiaoyu'])
>> ret.items()
dict_items([('password', '12345'), ('usenames', 'xiaoyu')])
4.函数、类和方法
(1)函数
def关键字来定义函数。
>> def add(a,b):
print(a+b)

>> add(1,2)
3
>> sum = add(1,2)
3
>> def add(a,b):
return (a+b)
>> add(1,9)
10

若在调用add()函数时不想传参,可设置默认参数。

>> def add(a = 1,b = 9):
return (a+b)
>> add()
10
>> add(10,20)
30
(2)类和方法
class A(object):
def add(self,a,b):
return (a+b)

count = A()
print(count.add(3,9))

class A():
def init(self,a,b): #初始化
self.a = int(a)
self.b = int(b)

def add(self):
return self.a+self.b

count = A(0,0)
print(count.add())
class A(): #B继承A
def add(self, a, b):
return(a+b)

class B(A):
def sub(self,a,b):
return(a-b)

count = B()
print(count.add(10,90))
print(count.sub(10-9,9))
5.模组(类库、模块)
(1)在time模块下面有一个ctime()方法用于获取当前时间
import time
print(time.ctime())

from time import ctime #只会用到tiem下的ctime()方法
print(ctime())

import time
print(time.ctime())
print(time.sleep(2))
print("休眠两秒")
print(time.sleep(2))
print(time.ctime())

from time import #“”用于表示模块下面的所有方法
print(ctime())
print("休眠3秒")
print(sleep(3))
print(ctime())
5.异常
(1)
try:
open("abc.txt",'r')
except FileNotFoundError: #试图打开一个不存在的文件与目录
print("异常了!")
(2)
try:
print(aa)
except NameError: #使用一个还未赋值对象的变量
print("这是一个name异常")
(3)
try:
open("abc.txt",'r')
print(aa)
except Exception(BaseException) as msg:
print("这是一个异常")
print(msg)
[Errno 2] No such file or directory: 'abc.txt'
在python中所有的异常类都继承Exception,所以可以使用它来接收所有类型的异常。从python2.5版本之后,所有的异常都有了新的基类BaseException。Exception同样继承自BaseException。
(4)异常与if语句配合
try:
aa = "python"
print(aa)
except Exception as msg:
print("有异常存在")
else:
print("输出正常,没有异常")
try:
aa = "python"
print(aa)
except Exception as meg:
print(msg)
finally:
print("不管是否有异常存在,都会执行")

猜你喜欢

转载自blog.51cto.com/10810429/2104766