python3学习基础

python3学习基础

简介
Python是Guido van Rossum在1989年圣诞节期间所开发的一种新的脚本解析语言。

设计思想
Python的设计哲学是优雅,明确,简单。
1.利用缩进规则来规范代码,表示语句块的开始和退出。
2.代码中出现许多语法糖,用最少的代码做最多的事情。
3.支持面向对象和面向过程,同时支持C,C++进行扩展。

基础一:关键字
False class finally is return
None continue for lambda try
True def from nonlocal while
and del global not with
as elif if or yield
assert else import pass break
except in raise
注:
False/True:布尔类型的值,假/真。
class:定义类的关键字。
try/except/else/finally:用于捕获错误。
is:用与判断。
return:返回值
None:特殊类型,不存在。
continue:跳过当前循环,进行下一轮循环。
for/in:用于循环一个序列或可迭代对象。
lambda:匿名函数。
def:定义函数。
from/import:导入模块。
nonlocal:用来在函数或其他作用域中使用外层(非全局)变量。
while:用于循环。
and/not/or:逻辑运算与/非/或
del:删除操作,删除对象属性,list也是一个对象。
global:用来在函数或其他局部作用域中使用全局变量。但是如果不修改全局变量也可以不使用global关键字。
with/as:用于打开关闭文件等file-like object对象,主要用法是实现一个类__enter__()和__exit__()方法。
if/elif:判断语句。
yield:构造生成器,使用next()返回一个值。
assert:断言,检查函数是否正确。
pass:跳过语句,意味着什么都不做。
break:终止循环语句
raise:抛出异常

基础二:数据类型
1.字符串
a.使用单引号''
str1 = 'this is a test!'
print str1 #'this is a test!
使用双引号""
str2 = "this is a test!"
print str2 #'this is a test!'
使用三引号''' '''
str3 = '''this is a test!'''
print str3 #"this is a test!"
注:
单引号与双引号一样,使用单引号或双引号时要转义
三引号不需要转义符号,里面可以使用单引号或双引号

2.数字类型Num
a.int类型
x = 1
print x #1
b.float类型
x = 1.2345
print x #1.2345
c.complex类型
x = complex(1, 2)
y = 1 + 2j
print x #(1+2j)
print y #(1+2j)

3.bool类型
print bool(0) #False
print bool(1) #True
print bool(-1) #True
print bool([]) #False
print bool(()) #False
print bool({}) #False
print bool('') #False
print bool(None) #False
注:存在大于零的数或元素大于零的组合数据都为True

4组合数据
a.list列表
L = [1, 2, 'hello']
print L #[1, 2, 'hello']
注:
可以修改类型
类似数组,但是可以存放不同类型的数据,包括list数据
可以通过下标进行操作
b.dict字典
d = {'name': 'huangzijian', 'score': '60'}
print d #{'name': 'huangzijian', 'score': '60'}

可修改类型
可以通过关键字查找数据

c.Tuple元组
t1 = (1, 'hello', 2, 'hi')
t2 = (1,)
print t1 #(1, 'hello', 2, 'hi')
print t2 #(1,)

单个元素后面要加逗号区分是否为元组
不可修改类型

d.集合set
s1 = {1, 2, hello}
s2 = set(d) #为字典,仅保留key
print s1 #{1, 2, hello}
print s2 #{'name'}
注:
set集合可以通过list,dict,tuple等转化

基础三:帮助函数
a.dir函数式可以查看对象的属性,使用方法很简单,举str类型为例,在Python命令窗口输入 dir(str) 即可查看str的属性
['__add__', '__class__', '__contains__', '__delattr__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__getnewargs__', '__gt__', '__hash__', '__init__', '__iter__', '__le__', '__len__', '__lt__', '__mod__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__rmod__', '__rmul__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', 'capitalize', 'casefold', 'center', 'count', 'encode', 'endswith', 'expandtabs', 'find', 'format', 'format_map', 'index', 'isalnum', 'isalpha', 'isdecimal', 'isdigit', 'isidentifier', 'islower', 'isnumeric', 'isprintable', 'isspace', 'istitle', 'isupper', 'join', 'ljust', 'lower', 'lstrip', 'maketrans', 'partition', 'replace', 'rfind', 'rindex', 'rjust', 'rpartition', 'rsplit', 'rstrip', 'split', 'splitlines', 'startswith', 'strip', 'swapcase', 'title', 'translate', 'upper', 'zfill']

b.查看对象的某个属性还可以用help函数,使用方法为help(str.split)
Help on method_descriptor:

split(...)
S.split(sep=None, maxsplit=-1) -> list of strings

Return a list of the words in S, using sep as the
delimiter string. If maxsplit is given, at most maxsplit
splits are done. If sep is not specified or is None, any
whitespace string is a separator and empty strings are
removed from the result.

 

转载于:https://www.cnblogs.com/MGT614469730/articles/5677721.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值