zoukankan      html  css  js  c++  java
  • Python学习系列(二)(基础知识)

    Python基础语法

           Python学习系列(一)(基础入门)

          对于任何一门语言的学习,学语法是最枯燥无味的,但又不得不学,基础概念较繁琐,本文将不多涉及概念解释,用例子进行相关解析,适当与C语言对比,避免陷入语法的苦海。我认为初学者学习语法的目标是学会使用即可,关于对概念的深入理解,剖析,没有一定的知识积累是很难做到的。

          学习Python,基本语法不是特别难,有了C的基本知识,理解比较容易。本文的主要内容是Python基础语法,学完后,能熟练使用就好。(开发环境依然是Python2.7,简单使用)

    一,基本知识
    1,不需要预先定义数据类型(此说法值得商榷,姑且这么说吧),这是与其他语言的最大不同(如C,C++,C#,Delphi等)
    1 >>> x=12
    2 >>> y=13
    3 >>> z=x+y
    4 >>> print z
    5 25

    注意:尽管变量不需要预先定义,但是要使用的时候,必须赋值,否则报错:

    1 >>> le
    2 Traceback (most recent call last):
    3   File "<pyshell#8>", line 1, in <module>
    4     le
    5 NameError: name 'le' is not defined

    2,查看变量的类型函数type():

    1 >>> type(x)
    2 <type 'int'>
    3,查看变量的内存地址函数id():
     1 >>> x=12
     2 >>> y=13
     3 >>> z=x+y
     4 >>> m=12
     5 >>> print 'id(x)=',id(x)
     6 id(x)= 30687684
     7 >>> print 'id(m)=',id(m)
     8 id(m)= 30687684
     9 >>> print 'id(z)=',id(z)
    10 id(z)= 30687528
    11 >>> x=1.30
    12 >>> print 'id(x)=',id(x)
    13 id(x)= 43407128

    从上述结果可以发现:变量的指向变,地址不变,换句话说,整数12的地址值始终不变,变化的是变量的指向(如x的地址变化);

    4,输出函数print()
    1 >>> x='day'
    2 >>> y=13.4
    3 >>> print x,type(x)
    4 day <type 'str'>
    5 >>> print y,type(y)
    6 13.4 <type 'float'>
    逗号运算符():可以实现连接字符串和数字型数据。
    1 >>> print 'x=',12
    2 x= 12

    格式化控制符:%f浮点数%s字符串;%d双精度浮点数(这和C的输出是一致的)。

    1 >>> x=12
    2 >>> y=13.0004
    3 >>> z='Python'
    4 >>> print "output %d %f %s"%(x,y,s)
    5 output 12 13.000400 Python

    5,输入函数raw_input():

    1 >>> raw_input("input an int:")
    2 input an int:12
    3 '12'
    注意:raw_input()输入的均是字符型。
    6,查看帮助函数help():
    1 >>> help(id)
    2 Help on built-in function id in module __builtin__:
    3  
    4 id(...)
    5     id(object) -> integer
    6    
    7     Return the identity of an object. This is guaranteed to be unique among
    8     simultaneously existing objects. (Hint: it's the object's memory address.)
    9  
    注意:Python的注释,#:仅支持单行注释;另外,Python编程具有严格的缩进格式。
     
    二、函数
    1,函数定义及其调用:
     1 #define function:add (函数说明)
     2 def add(x,y):  #函数头部,注意冒号,形参x,y
     3     z=x+y           #函数体
     4     return z        #返回值
     5 #define main function
     6 def main():
     7     a=12
     8     b=13
     9     c=add(a,b)   #函数调用,实参a,b
    10     print c
    11 main()             #无参函数调用
    12 print 'End1!'

    注意:这部分与C的存在的异同在于:

    1,形参与实参的用法,无参函数,有参函数,默认参数等规则一致。
    如def add(x,y=2),调用可以是add(3)也可以是add(3,4),add(y=34,x)
    2,C的形参需要指定数据类型,而Python不需要。
    3,Python的返回值允许有多个。如:
     1 def test(n1,n2):
     2     print n1,
     3     print n2
     4     n=n1+n2
     5     m=n1*n2
     6     p=n1-n2
     7     e=n1**n2
     8     return n,m,p,e
     9 print 'Entry programme1'
    10 sum,multi,plus,powl=test(2,10)   #这个是C语言所没有的赋值方式
    11 print 'sum=',sum
    12 print 'multi=',multi
    13 print 'plus=',plus
    14 print 'powl=',powl
    15 re=test(2,10)
    16 print re                                #数据类型为:'tuple'
    17 print re[0],re[1],re[2],re[3]
    18 print 'End1!
    '

    运行结果:

     1 Entry programme
     2 2 10
     3 sum= 12
     4 multi= 20
     5 plus= -8
     6 powl= 1024
     7 2 10
     8 (12, 20, -8, 1024)
     9 12 20 -8 1024
    10 End!

    2,局部变量:

     1 def f1():
     2     x=12     #局部变量
     3     print x
     4 def f2():
     5     y=13      #局部变量
     6     print y
     7 def f3():
     8     print x       #错误:没有定义变量x,这与“不需要预先定义数据类型”不矛盾
     9     print y
    10 def main():
    11     f1()
    12     f2()
    13     #f3()#变量报错  
    14 main()
    15 print 'End2!'
    3,修改全局变量的值:
     1 def modifyGlobal():
     2     global x              #全局变量定义
     3     print 'write x =-1'
     4     x=-1
     5 def main():
     6 # printLocalx()
     7 # printLocaly()
     8 # readGlobal()
     9     modifyGlobal()
    10  
    11 x=200
    12 #y=100
    13 print 'before modified global x=',
    14 print x
    15 main()
    16 print 'after modified global x=',
    17 print x
    运行结果:
    1 >>>
    2 before modified global x= 200
    3 write x =-1
    4 after modified global x= -1

    三、表达式与分支语句

    1,表达式:
          是由数字运算符,数字分组符号括号自由变量约束变量等以能求得数值的有意义排列方法所得的组合。表示通常有操作数和操作符两部分组成。
          分类:算术表达式;关系表达式,逻辑表达式(and/or/not)
    2,if分支语句:
    1)形式一:(if <condition>:)
    1 >>> sex="male"
    2 >>> if sex=='male':
    3  print 'Man!'
    4 #此处有两次回车键
    5 Man!
    6 >>> 

    2)形式二:(if <condition>: else (if <condition>:))

    1 sex=raw_input('Please input your sex:')
    2 if sex=='m' or sex=='male':
    3  print 'Man!'
    4 else:
    5     print 'Woman!'

    运行结果:

    1 >>>
    2 Please input your sex:male
    3 Man!

    3)形式三:(if <condition>: elif <condition>: else ))(这是Python有而C没有的形式)

     1 count=int(raw_input('Please input your score:'))
     2 if count>=90:
     3    print'优秀!'
     4 elif count>=80:
     5     print '优良!'
     6 elif count>=70:
     7     print '合格!'
     8 elif count>=60:
     9     print '及格!'
    10 else:
    11     print '不及格!'

    运行结果:

    1 >>>
    2 Please input your score:90
    3 优秀!

    注意:Python没有switch语句。

     
    四、循环语句
           背景:在程序设计的时候,经常会遇到一些语句被不断的重复执行,这样的代码极长又低效,很不直观,那么应该考虑用循环体来实现。
     1,while语句:与C在表达上有区别,c有while与do……while形式;Python下:while与while……else……形式
     1)while形式下:
    1 i=1
    2 while i<5:
    3     print 'Welcome you!'
    4     i=i+1

    2)while……else……形式下:

    1 i=1
    2 while i<5:
    3     print 'Welcome you!'
    4     i=i+1
    5 else:
    6     print "While over!"  #循环正常结束

    注意:如果while非正常状态结束(即不按循环条件结束),则else语句不执行。如下:

    1 i=1
    2 while i<5:
    3     print 'Welcome you!'
    4     i=i+1
    5     if i==2:
    6         print 'While……'
    7         break
    8 else:
    9     print "While over!"

    运行结果:

    1 >>>
    2 Welcome you!
    3 While……
    补充:
    continue语句:在while循环体中出现时,本次循环continue之下的语句不被执行,直接进入下一次循环。
     1 i=1
     2 while i<=5:
     3     if i==2 or i==4:
     4         print 'While……continue'
     5         i=i+1
     6         continue
     7     print 'Welcome you!'
     8     i=i+1
     9 else:
    10     print "While over!"

    运行结果:

    1 >>>
    2 Welcome you!
    3 While……continue
    4 Welcome you!
    5 While……continue
    6 Welcome you!
    7 While over!

    2,for语句:(见后续文章)

    五,小结

          本文介绍了Python的变量,输入输出函数,表达式,基本语句(分支和循环)等知识的相关使用,通过练习,应该对Python有一个初步的认识。

  • 相关阅读:
    Unity3D性能优化之美术资源制件规范
    Unity3D屏幕自适应
    Unity3D性能优化之内存科普篇
    面向对象设计和特性
    Uinty3D性能优化之贴图科普篇
    Learn OpenGL 概念(一)
    假如博客园也有年度总结报告……
    2019年修图汇总
    Python 小案例实战 —— 简易银行存取款查询系统
    Win10 + Anaconda + Tensorflow-cpu + Pycharm安装教程
  • 原文地址:https://www.cnblogs.com/zhangbc/p/3766513.html
Copyright © 2011-2022 走看看