zoukankan      html  css  js  c++  java
  • Python学习笔记——基本语法

    1.程序输出——print语句

    >>> myString = 'Hello World!'
    >>> print myString
    Hello World!
    >>> myString
    'Hello World!'
    
    >>> print type(u"你")    #都能输出汉字,但是输出的类型不一样
    <type 'unicode'>
    >>> print type("你")
    <type 'str'>
    

     注意:在仅用变量名的时候,输出的字符串是用单引号括起来的

    2.程序输入——raw_input()内建函数

    >>> user = raw_input('Enter your name:')
    Enter your name:root
    >>> print 'Your name is:',user
    Your name is: root
    

    3.注释——使用#

    4.操作符——

    <1>两种除法——单斜杠/用于传统的除法,双斜杠//用作浮点除法(对结果进行四舍五入)

    <2>乘方操作符——双星号**

    <3>标准比较操作符——< <= > >= == != <>

    <4>逻辑操作符——and or not

    5.变量和赋值——Python是动态类型语言,不需要预先声明变量的类型

      Python不支持C语言中的自增1和自减1操作符

    6.数字——

    <1>有符号整型 长整型 布尔值

    <2>浮点值

    <3>复数

    7.字符串——

    >>> myString = "Hello"
    >>> myString[0]
    'H'
    >>> myString[2:4]
    'll'
    >>> myString[1:]
    'ello'
    >>> myString[:4]
    'Hell'
    >>> myString[-1]
    'o'
    >>> myString * 2
    'HelloHello'
    >>> myString + myString
    'HelloHello'
    
    >>> myString = '''python
    ... is cool'''
    >>> myString
    'python
    is cool'
    >>> print myString
    python
    is cool
    

    8.if语句

    >>> if x>1:
    ...     x -= 1
    ...     print '#%d' % (x)
    ... 
    #2
    

    9.while循环

    >>> x = 0
    >>> while x<3:
    ...     print x
    ...     x += 1
    ... 
    0
    1
    2
    >>> 
    

    10.for循环和range()内建函数

    >>> for x in ['A','B','C','D']:
    ...     print x
    ... 
    A
    B
    C
    D
    
    >>> for x in ['A','B','C','D']:
    ...     print x,
    ... 
    A B C D
    

    使用range()内建函数

    >>> for x in range(3):  #循环从0到2
    ...     print x,
    ... 
    0 1 2
    >>> 
    
    >>> myString = 'ABCD'  #循环字符串中的每一个字母
    >>> for x in myString:
    ...     print x,
    ... 
    A B C D
    >>> 
    

    使用len()内建函数

    >>> myString
    'ABCD'
    >>> for i in range(len(myString)):
    ...     print myString[i],i
    ... 
    A 0
    B 1
    C 2
    D 3
    

    11.列表解析

    >>> square = [x ** 2 for x in range(4)]
    >>> for i in square:
    ...     print i
    ... 
    0
    1
    4
    9
    
    >>> square = [x ** 2 for x in range(4) if not x % 2]    #如果整除2
    >>> for i in square:
    ...     print i
    ... 
    0
    4
    

    12.文件和内建函数open()、file()

    >>> filename = raw_input('Enter file name:')
    Enter file name:/home/common/software/hexo/source/_posts/book_2016.md
    >>> fobj = open(filename,'r')
    >>> for eachLine in fobj:
    ...     print eachLine,
    ...
    
    
    >>> fobj.close()
    

    13.函数

    >>> def addMe2Me(x):
    ...     return (x+x)
    ... 
    >>> addMe2Me(2.5)
    5.0
    

    14.类

    >>> class myClass(object):
    ...     def _init_(self,nm='123'):
    ...             self.name = nm
    ...             print 'Constructor'
    ...     def showname(self):
    ...             print 'My name is',self.name
    ... 
    >>> newClass = myClass()
    >>> newClass._init_()
    Constructor
    >>> newClass.showname()
    My name is 123
    

    15.模块

    >>> import sys
    >>> sys.stdout.write('Hello Word!
    ')
    Hello Word!
    >>> sys.platform
    'linux2'
    >>> sys.version
    '2.7.6 (default, Jun 22 2015, 18:00:18) 
    [GCC 4.8.2]'
    

    16.基本规则和特殊字符

    <1># —— 注释

    <2> —— 行分隔符

    <3> —— 继续上一行

    <4>; —— 将两个语句连接到一行中

    <5>: —— 将代码块的头和体分开

    17.Python对象

      <1>标准类型

          数字——Integer整型 Boolean布尔型 Long integer 长整型 Floating point number 浮点型 Complex number复数型

          字符串——String

          列表——List []

          元组——Tuple ()

          字典——Dictionary

      <2>其他内建类型

          类型——type(1)——><type,'int'> 得到特定对象的类型信息

          Null对象(None)——不支持任何运算,也没有任何内建方法

          文件

          集合/固定集合

          函数/方法

          模块

          类

    18.标准类型操作符

      <1>对象值的比较 < > <= >= == !=

      <2>对象身份比较 is  is not  比较是否指向同一个对象 注意:Python中整型对象和字符串对象是不可变对象

    19.字符串和操作符

    <1>格式化操作符(%)

      P115页  

    <2>字符串模板——字符串Template对象

    >>> from string import Template
    >>> s = Template("This is ${name} ${num}")
    >>> print s.substitute(name="Python",num=3)
    This is Python 3
    

      

  • 相关阅读:
    mysql 历史版本下载
    mysql 5.7 版本 You must reset your password using ALTER USER statement before executing this statement报错处理
    5.7 zip 版本的安装 以及遇到的坑
    mysql 5.6zip版本的卸载与5.7 zip 版本的安装
    mysql数据库的备份与还原
    本地Navicat连接docker里的mysql
    docker修改数据库密码
    docker 在push镜像到本地registry出现的500 Internal Server Error
    linux 没有界面内容显示不全解决办法
    json与map互相转换
  • 原文地址:https://www.cnblogs.com/tonglin0325/p/5700938.html
Copyright © 2011-2022 走看看