您的位置:首页 > 编程语言 > Python开发

python学习记录1

2017-06-08 15:53 148 查看
记:先mark一下,希望自己可以坚持的学习python下去,大学毕业一直在从事运维的工作,快5年了,也不知道自己学了些啥东西,感觉挺自卑的,几乎还是啥都不懂,或者不深入,所以就立帖,开始通过博客这种古老的方式来记录学习的过程和遇到的问题,先从python的学习开始吧。

欢迎大家推荐好的书籍资料,或者多多指点我这个菜鸟,先谢过。

参考的学习资料:a byte of python3

2017/6/8学习的内容:

1、常量、数(整数、浮点数、复数)、字符串、单引号(指定字符串)、双引号(和单引号一样)、三引号(多行字符串,也可以引用单引号和双引号)

2、转义:字符串中包含单引号/双引号等,需要用转义,如字符串:’what‘s your name‘,需要这样表示字符串:’what’\s your name‘。双引号的转义字符为:\\;

           如果要指定两行及以上的字符串,第一种方法:用三引号;第二种方法:用"\n"的转义字符。

           “\”接在字符串末尾表示下一行字符串是上一行的继续

3、format()方法:一个字符串能使用确定的格式,可以用format()方法来替代这些格式(有变动的地方),参数要和format方法参数一致

如:for i in range(1,5):
age=i
name='zxm'
print('{0} is {1} years old'.format(name,age))

输出结果:

zxm is 1 years old

zxm is 2 years old

zxm is 3 years old

zxm is 4 years old

4、标识符的命名:第一个字符必须是大小写的字母或下划线,由大小写字母、下划线、数字组成;对大小写敏感

5、缩进:python的缩进特别重要,每一个层次的语句必须是相同的缩进,通过python的编辑器(我暂时用的idle)一般会自动缩进

控制流:

1、if语句:基本格式:if-elif-...-else

2、while语句:while true: 语句  else:语句

3、for循环:格式: for i in range(1,5):语句   else(可省略):语句

4、break语句:终止循环语句,跳出循环

5、continue语句:跳过当前循环的剩余语句,继续下一轮循环

函数:

函数就是一个程序段,创立后可被调用,我们自身也在调用很多系统内建的函数,如:len(),range()等

函数的定义由关键字:“def” 来定义,如:def sayHello():

函数参数:参数在函数定义的括号内指定,由逗号分割,函数中的参数名称为形参,而提供给函数调用的值称为实参

局部变量:当你在函数内定义内声明变量的时候,它们与函数外具有相同名称的其他变量没有任何关系

全局变量:定义全局变量就必须使用global语句

非局部语句:非局部变量使用nonlocal语句,一般使用介于两种作用域之间

如:

ef func_outer():
x =2
print('x is',x)
def func_inner():
nonlocal x

                x=5
        func_inner()
        print('Changed local x to',x)
func_outer()

结果:
x is 2
Changed local x to 5

2017/6/9:

默认参数值:可以在定义函数的时候声明形参,并初始参数值,但是要注意的是:必须先声明有默认值的形参后才能声明没有默认值的形参
正确:def func(a,b=6),错误:def func(a=5,b)
关键参数:如果定义的函数有多个参数,而只想指定一部分,则可通过命名来为这些参数赋值,这样就不用担心参数的顺序了
如:
>>> def func(a,b=8,c=9):
print(a,b,c)

>>> func(3,7)

(3, 7, 9)

>>> func(25,c=24)

(25, 8, 24)

>>> func(c=50,a=20)

(20, 8, 50)

return语句:从一个函数返回即跳出函数
docStrings:文档字符串工具,从函数回复文档字符串
如:
>>> def printMax(x,y):
'''Prints the maximum of two numbers

The two values must be integers.'''
x=int(x)
y=int(y)
if x>y:
print(x)
else:
print(y)

>>> print(3,5)

(3, 5)

>>> print(printMax.__doc__)
Prints the maximum of two numbers

The two values must be integers.

模块:可重复使用和调用,模块的编写方法多样,最简单的就是创建以.py为扩展名的文件,包含函数和变量;模块可以从另一个程序导入来使用其函数功能

格式:import sys

from...import...语句:如果你想要直接输入argv变量到你的程序中(避免每次使用时调用),那么可以使用“from sys import argv”语句,如果想要sys这个模块的所有名字,则可以使用语句“from sys import *”

创建自己的模块

如:模块(mymodule_demo.py)

def sayhi():
print('Hi,this is my mode.')
__version__='0.1'

import mymodule

mymodule.sayhi()

print('Version',module.__version__)

或:

from mymodule import sayhi,__version__

sayhi()

print('Version',__version__)

dir()函数:列出模块定义的标识符,如:

import sys

dir(sys)

结果:

['__displayhook__', '__doc__', '__excepthook__', '__interactivehook__', '__loader__', '__name__', '__package__', '__spec__', '__stderr__', '__stdin__', '__stdout__', '_clear_type_cache', '_current_frames', '_debugmallocstats', '_getframe', '_git', '_home',
'_xoptions', 'abiflags', 'api_version', 'argv', 'base_exec_prefix', 'base_prefix', 'builtin_module_names', 'byteorder', 'call_tracing', 'callstats', 'copyright', 'displayhook', 'dont_write_bytecode', 'exc_info', 'excepthook', 'exec_prefix', 'executable', 'exit',
'flags', 'float_info', 'float_repr_style', 'get_asyncgen_hooks', 'get_coroutine_wrapper', 'getallocatedblocks', 'getcheckinterval', 'getdefaultencoding', 'getdlopenflags', 'getfiles
8b4c
ystemencodeerrors', 'getfilesystemencoding', 'getprofile', 'getrecursionlimit',
'getrefcount', 'getsizeof', 'getswitchinterval', 'gettrace', 'hash_info', 'hexversion', 'implementation', 'int_info', 'intern', 'is_finalizing', 'last_traceback', 'last_type', 'last_value', 'maxsize', 'maxunicode', 'meta_path', 'modules', 'path', 'path_hooks',
'path_importer_cache', 'platform', 'prefix', 'set_asyncgen_hooks', 'set_coroutine_wrapper', 'setcheckinterval', 'setdlopenflags', 'setprofile', 'setrecursionlimit', 'setswitchinterval', 'settrace', 'stderr', 'stdin', 'stdout', 'thread_info', 'version', 'version_info',
'warnoptions']

遇到的问题:

1、>>> print ('this is idle',name)

('this is idle', 50)

为什么输出的结果是:('this is idle', 50),而不是:this is idle 50呢?

解决:这个问题的原因没找到,python2.7是这样的问题,python3.6就OK

PS:语法或格式错误的时候注意是否中英文字符的切换
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: