zoukankan      html  css  js  c++  java
  • Python 2.7 学习笔记 基本知识

    python是一种解释型的、面向对象的、带有动态语义的高级程序设计语言。本文介绍下python的基本知识。

    一、安装

    各种操作系统有自己的安装方法,linux系统一般都自带了python的环境。这里不再介绍。

    二、检查

    安装好python后,可在命令行下执行如下命令检查当前环境下python的版本:  python  --version

    xxx@ubuntu:~$ python --version
    Python 2.7.6

    三、编写和运行python程序

    运行python程序,通常有两种方式,一是在交互式命令行程序下边输入代码边执行(往往用于学习和调试);
    二是将代码保存在文件中,执行文件,这是实际生产环境下的应用场景。

    交互式命令:

    xxx@ubuntu:~$ python
    Python 2.7.6 (default, Mar 22 2014, 22:59:56) 
    [GCC 4.8.2] on linux2
    Type "help", "copyright", "credits" or "license" for more information.
    >>> print 'hello'
    hello
    >>> 

    上面例子演示了,先在命令行提示符下输入 python命令,会显示基本信息后,出来一个交互界面,其中 >>>为提示符。
    上面代码在提示符下输入了 print命令,执行后又回到提示符。

    如果想要退出交互式界面,可输入 exit() 命令即可。

    通过代码文件:

    1、编写一个python文件,如test.py,内容如下

    xxx@ubuntu:~$ more test.py
    print 'hello'
    x=12
    print x

    2、执行该文件

    xxx@ubuntu:~$ python test.py
    hello
    12

    四、代码缩进格式要求

    和其它语言不同,python的语句块不是用  {} 或begin、end等标记括起来的。它是靠缩进来自动管理的。

    每一个级别的代码行在同一个缩进内。缩进要求是4个空格(或tab键)。举例:

    xxx@ubuntu:~$ more test.py
    a=1
    if a:
        print True
        print a
    else:
        print False
        print a
    print "end"
    xxx@ubuntu:~$ python test.py
    True
    1
    end
    xxx@ubuntu:~$ 

    五、注释

    在python中,用 #进行注释,所有 #右边的一切都会被忽略。

    python不支持  /* */ 这样的多行注释。

  • 相关阅读:
    AutoMapper在ABP框架
    Github for Windows使用介绍
    Net中的反应式编程
    webstorm创建nodejs + express + jade 的web 项目
    Nancy 框架
    Quartz.NET 任务调度框架
    从电商秒杀与抢购谈Web系统大规模并发
    SVN中tag branch trunk用法详解
    Hsql中In没有1000的限制
    Gradle sourceCompatibility has no effect to subprojects(转)
  • 原文地址:https://www.cnblogs.com/51kata/p/5331711.html
Copyright © 2011-2022 走看看