python基础

1、交互式解释器

在linux终端下,直接输入python,返回以下结果

[root@localhost ~]# python

Python 2.6.6 (r266:84292, Jan 22 2014, 09:42:36) 

[GCC 4.4.7 20120313 (Red Hat 4.4.7-4)] on linux2

Type "help", "copyright", "credits" or "license" for more information.

>>> print ('hello world')

hello world

>>> 

2、算法

编写代码的过程,使计算机能够理解的语言执行操作的方法叫算法

3、数字与表达式

>>> 2+2

4

>>> 1231231+3123123

4354354

>>> 

>>> 1/2     #这个是在2.*版本上得到的结果,在3.0将得到修正,会得到0.5

0

>>> 

>>> from __future__ import division #如果要在2.0得到0.5的结果可以导入该模块

>>> 1/2

0.5

>>> 

>>> 1.0/2.0

0.5

>>> 

>>> 1/2.0

0.5

>>> 

>>> 1.0/2

0.5

以上的基本上跟计算器的结果一致,那么python其实还有整除运算符(//)

>>> 

>>> 1.0//2

0.0

>>> 1//2.0

0.0

>>> 

我们看到结果为0.0

>>> 

>>> 

>>> 10/3

3

>>> 10%3   #取余,这里得1主要是因为10整除3等于3,看上一个结果就知道

1

>>> 9/3

3

>>> 9%3

0

>>> 

>>> 2**3

8

>>> 3**3

27

>>> (-2)**3

-8

>>> (-3)**2

9

>>> 

上面为幂运算,负数要加括号

4、长整数

python可以处理很大的整数

>>> 100000000000000000000000000000

100000000000000000000000000000L

注意:2.2版本以前会报以下错误

>>> 100000000000000000000000000000

OverflowError:integer literal too large

普通整数不能大于2147483674(也不能小于-2147483674),如果需要更大的数,可以使用长整数,长整数与普通整数的书写方式一样,只不过会在结尾多一个L(理论上小写的l也可以,不过它看起很像1,所以建议用大写L)

5、十六进制与八进制

>>> 

>>> 0xAF

175

>>> 010

8

>>> 

十六进制与八进制的首位都是零

6、变量

变量(variable)

可以这么理解,变量其实就是代表或引用某值的名字,举例来说,如果希望用x代表5,只需要执行以下语句即可

>>> x = 5

>>> print (x)

5

>>> 

这样相当于将变量x邦定到了值或者对象上5上面,在变量被赋值之后,就可以在表达式中使用变量

5

>>> x * 2

10

>>> x ** 2

25

>>> 

注意:变量名不能以数字或者特殊字符开头,例如,800var,#123123等

7、语句

到目前为止,其实已经介绍了两类语句:print语句和赋值语句

>>> print (x * 2)

10

>>> x * 2

10

>>> 

注意,这里是pyhton 3.0,2.0的python其实可以不加括号,因为在python3中,print是函数了

8,获取用户输入

python 2.0可以这么写

x = raw_input('please input something: ')

x = input('input something: ')

不过在3.0里,raw_input已经被去掉了,所有的输入只能用input

x = input('input something: ')

x = int(input('input a number: ')   #输入的需要是数据类型时需要这么操作

9、函数

先看下面的操作

>>> 2 ** 2

4

>>> pow(2,2)

4

>>> 2 ** 3

8

>>> pow(2,3)

8

>>> 

使之得到相同结果的pow就是函数,叫幂的运算符

10、模块

可以把模块想象成导入到Python的插件,使用import来导入模块,使用dir()可以查看到模块里包函的函数,如下测试

C:UsersCX>python3
Python 3.5.1 (v3.5.1:37a07cee5969, Dec 6 2015, 01:54:25) [MSC v.1900 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>>
>>>
>>> import sys,os
>>>
>>> dir(sys)
['__displayhook__', '__doc__', '__excepthook__', '__interactivehook__', '__loader__', '__name__', '__package__', '__spec__', '__stderr__', '__stdin__', '__stdout__', '_clear_type_cache', '_current_frames', '_debugmallocstats', '_getframe', '_home', '_mercurial', '_xoptions', 'api_version', 'argv', 'base_exec_prefix', 'base_prefix', 'builtin_module_names', 'byteorder', 'call_tracing', 'callstats', 'copyright', 'displayhook', 'dllhandle', 'dont_write_bytecode', 'exc_info', 'excepthook', 'exec_prefix', 'executable', 'exit', 'flags', 'float_info', 'float_repr_style', 'get_coroutine_wrapper', 'getallocatedblocks', 'getcheckinterval', 'getdefaultencoding', 'getfilesystemencoding', 'getprofile', 'getrecursionlimit', 'getrefcount', 'getsizeof', 'getswitchinterval', 'gettrace', 'getwindowsversion', 'hash_info', 'hexversion', 'implementation', 'int_info', 'intern', 'is_finalizing', 'maxsize', 'maxunicode', 'meta_path', 'modules', 'path', 'path_hooks', 'path_importer_cache', 'platform', 'prefix', 'ps1', 'ps2', 'set_coroutine_wrapper', 'setcheckinterval', 'setprofile', 'setrecursionlimit', 'setswitchinterval', 'settrace', 'stderr', 'stdin', 'stdout', 'thread_info', 'version', 'version_info', 'warnoptions', 'winver']
>>> dir(os)
['F_OK', 'MutableMapping', 'O_APPEND', 'O_BINARY', 'O_CREAT', 'O_EXCL', 'O_NOINHERIT', 'O_RANDOM', 'O_RDONLY', 'O_RDWR', 'O_SEQUENTIAL', 'O_SHORT_LIVED', 'O_TEMPORARY', 'O_TEXT', 'O_TRUNC', 'O_WRONLY', 'P_DETACH', 'P_NOWAIT', 'P_NOWAITO', 'P_OVERLAY', 'P_WAIT', 'R_OK', 'SEEK_CUR', 'SEEK_END', 'SEEK_SET', 'TMP_MAX', 'W_OK', 'X_OK', '_Environ', '__all__', '__builtins__', '__cached__', '__doc__', '__file__', '__loader__', '__name__', '__package__', '__spec__', '_execvpe', '_exists', '_exit', '_get_exports_list', '_putenv', '_unsetenv', '_wrap_close', 'abort', 'access', 'altsep', 'chdir', 'chmod', 'close', 'closerange', 'cpu_count', 'curdir', 'defpath', 'device_encoding', 'devnull', 'dup', 'dup2', 'environ', 'errno', 'error', 'execl', 'execle', 'execlp', 'execlpe', 'execv', 'execve', 'execvp', 'execvpe', 'extsep', 'fdopen', 'fsdecode', 'fsencode', 'fstat', 'fsync', 'ftruncate', 'get_exec_path', 'get_handle_inheritable', 'get_inheritable', 'get_terminal_size', 'getcwd', 'getcwdb', 'getenv', 'getlogin', 'getpid', 'getppid', 'isatty', 'kill', 'linesep', 'link', 'listdir', 'lseek', 'lstat', 'makedirs', 'mkdir', 'name', 'open', 'pardir', 'path', 'pathsep', 'pipe', 'popen', 'putenv', 'read', 'readlink', 'remove', 'removedirs', 'rename', 'renames', 'replace', 'rmdir', 'scandir', 'sep', 'set_handle_inheritable', 'set_inheritable', 'spawnl', 'spawnle', 'spawnv', 'spawnve', 'st', 'startfile', 'stat', 'stat_float_times', 'stat_result', 'statvfs_result', 'strerror', 'supports_bytes_environ', 'supports_dir_fd', 'supports_effective_ids', 'supports_fd', 'supports_follow_symlinks', 'symlink', 'sys', 'system', 'terminal_size', 'times', 'times_result', 'truncate', 'umask', 'uname_result', 'unlink', 'urandom', 'utime', 'waitpid', 'walk', 'write']
>>>

11、保存并执行程序

原文地址:https://www.cnblogs.com/zcx-python/p/5521163.html